-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathhelper.go
More file actions
144 lines (137 loc) · 4.8 KB
/
Copy pathhelper.go
File metadata and controls
144 lines (137 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/sdslabs/gasper/lib/database"
"github.com/sdslabs/gasper/lib/docker"
"github.com/sdslabs/gasper/lib/utils"
"github.com/sdslabs/gasper/services/appmaker"
"github.com/sdslabs/gasper/types"
"google.golang.org/grpc"
)
func startGrpcServer(server *grpc.Server, port int) error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
msg := fmt.Sprintf("Port %d is invalid or already in use", port)
utils.Log("Main-Helper-4", msg, utils.ErrorTAG)
os.Exit(1)
}
return server.Serve(lis)
}
func buildHTTPServer(handler http.Handler, port int) *http.Server {
if !utils.IsValidPort(port) {
msg := fmt.Sprintf("Port %d is invalid or already in use", port)
utils.Log("Main-Helper-5", msg, utils.ErrorTAG)
os.Exit(1)
}
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
ReadTimeout: 5 * time.Second,
WriteTimeout: 30 * time.Second,
}
return server
}
func setupDatabaseContainer(serviceName string) {
var containers []string
var err error
if serviceName == types.MongoDBGasper || serviceName == types.RedisGasper {
containers, err = docker.ListContainers()
if err != nil {
utils.LogError("Main-Helper-5.5", fmt.Errorf("Error Fetching List of Running Containers: %v", err))
return
}
} else {
containers = appmaker.FetchAllApplicationNames()
}
if !utils.Contains(containers, serviceName) {
utils.LogInfo("Main-Helper-6", "No %s instance found in host. Building the instance.", strings.Title(serviceName))
containerID, err := database.SetupDBInstance(serviceName)
if err != nil {
utils.Log("Main-Helper-7", fmt.Sprintf("There was a problem deploying %s service.", strings.Title(serviceName)), utils.ErrorTAG)
utils.LogError("Main-Helper-8", err)
} else {
utils.LogInfo("Main-Helper-9", "%s Container has been deployed with ID:\t%s", strings.Title(serviceName), containerID)
}
} else {
containerStatus, err := docker.InspectContainerState(serviceName)
if err != nil {
utils.Log("Main-Helper-10", "Error in fetching container state. Deleting container and deploying again.", utils.ErrorTAG)
utils.LogError("Main-Helper-11", err)
err := docker.DeleteContainer(serviceName)
if err != nil {
utils.LogError("Main-Helper-12", err)
}
containerID, err := database.SetupDBInstance(serviceName)
if err != nil {
utils.Log("Main-Helper-13", fmt.Sprintf("There was a problem deploying %s service even after restart.",
strings.Title(serviceName)), utils.ErrorTAG)
utils.LogError("Main-Helper-14", err)
} else {
utils.LogInfo("Main-Helper-15", "Container has been deployed with ID:\t%s", containerID)
}
}
if !containerStatus.Running {
if err := docker.StartContainer(serviceName); err != nil {
utils.LogError("Main-Helper-16", err)
}
}
}
// Setting up general logging for MySQL
if serviceName == types.MySQL {
mysqlConfig := `
[mysqld]
general_log = 1
general_log_file = /var/log/mysql/general.log
`
_, err := docker.ExecProcess(serviceName, []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/my.cnf", mysqlConfig)})
if err != nil {
utils.LogError("Main-Helper-17", err)
}
err = docker.ContainerRestart(serviceName)
if err != nil {
utils.LogError("Main-Helper-18", err)
}
}
if serviceName == types.PostgreSQL {
postgresConfig := `
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql_log.log'
log_statement = 'all'
log_duration = on
log_min_duration_statement = 0
`
_, err := docker.ExecProcess(serviceName, []string{"sh", "-c", fmt.Sprintf("echo %s >> /var/lib/postgresql/data/postgresql.conf", postgresConfig)})
if err != nil {
utils.LogError("Main-Helper-19", err)
}
err = docker.ContainerRestart(serviceName)
if err != nil {
utils.LogError("Main-Helper-20", err)
}
}
if serviceName == types.MongoDB {
mongoConfig := `
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongodb.log
verbosity: 1 `
// command := []string{"sh", "-c", "echo '\nsystemLog:' >> /etc/mongod.conf;echo ' destination: file' >> /etc/mongod.conf;echo ' logAppend: true' >> /etc/mongod.conf;echo ' path: /var/log/mongodb/mongodb.log' >> /etc/mongod.conf;echo ' verbosity: 1' >> /etc/mongod.conf;"}
command := []string{"sh", "-c", fmt.Sprintf("echo '%s' >> /etc/mongod.conf", mongoConfig)}
output, err := docker.ExecProcess(serviceName, command)
if err != nil {
utils.LogError("Main-Helper-21 ", fmt.Errorf("Failed to update mongod.conf: %v, output: %s", err, output))
}
command = []string{"sh", "-c", "mongod --config /etc/mongod.conf --replSet rs0"}
output, err = docker.ExecProcess(serviceName, command)
if err != nil {
utils.LogError("Main-Helper-22 ", fmt.Errorf("Failed to update mongod.conf: %v, output: %s", err, output))
}
}
}