-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
176 lines (141 loc) · 4.36 KB
/
container.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Deployments
*/
package main
import (
"bytes"
"fmt"
"time"
"github.com/rqlite/gorqlite"
)
const ContainerJobTypeDelete = "container_del"
const ContainerJobStatusPlanned = "planned"
const ContainerJobStatusFinished = "finished"
type ContainerJob struct {
Id string
Status string
EnvironmentId string
MachineId string
JobType string
}
func addContainerJob(job ContainerJob) ContainerJob {
id, err := NanoId(7)
if err != nil {
fmt.Println("Cannot generate new NanoId for DeploymentJob:", err)
return job
}
job.Id = id
_, err = connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "INSERT INTO ContainerJob( Id, Status, EnvironmentId, MachineId, JobType) VALUES(?, ?, ?, ?, ?)",
Arguments: []interface{}{job.Id, job.Status, job.EnvironmentId, job.MachineId, job.JobType},
},
},
)
if err != nil {
fmt.Printf(" Cannot write to ContainerJob table: %s\n", err.Error())
}
fmt.Printf("New ContainerJob is scheduled for EnvironmentId %s\n", job.EnvironmentId)
return job
}
func getContainerJobsByMachineIdAndStatusAndJobType(machineId string, status string, jobType string) []ContainerJob {
rows, err := connection.QueryOneParameterized(
gorqlite.ParameterizedStatement{
Query: "SELECT Id, Status, EnvironmentId, MachineId, JobType from ContainerJob WHERE MachineId = ? AND Status = ? AND JobType = ?",
Arguments: []interface{}{machineId, status, jobType},
},
)
return handleContainerJobQuery(rows, err)
}
func handleContainerJobQuery(rows gorqlite.QueryResult, err error) []ContainerJob {
var jobs = []ContainerJob{}
if err != nil {
fmt.Printf(" Cannot read from ContainerJob table: %s\n", err.Error())
}
for rows.Next() {
var Id string
var Status string
var EnvironmentId string
var MachineId string
var JobType string
err := rows.Scan(&Id, &Status, &EnvironmentId, &MachineId, &JobType)
if err != nil {
fmt.Printf(" Cannot run Scan: %s\n", err.Error())
}
loadedJob := ContainerJob{
Id: Id,
Status: Status,
EnvironmentId: EnvironmentId,
MachineId: MachineId,
JobType: JobType,
}
jobs = append(jobs, loadedJob)
}
return jobs
}
func stopPreviousContainer(environmentId string) {
//Stop old container
//Get all deployments and take the previous to get deploymentId that we use a container name
deployments := getDeploymentsByEnvironmentId(environmentId)
if len(deployments) > 1 {
//Stop the previous container
stopAndRemoveContainer(deployments[1].Id)
}
}
func stopAndRemoveContainer(deploymentId string) {
fmt.Printf("Removing container with ID %s\n", deploymentId)
scriptTemplate := createTemplate("caddyfile", `
#!/bin/sh
docker stop {{.DEPLOYMENT_ID}}
docker container rm -f {{.DEPLOYMENT_ID}}
`)
var templateBytes bytes.Buffer
templateData := map[string]string{
"DEPLOYMENT_ID": deploymentId,
}
if err := scriptTemplate.Execute(&templateBytes, templateData); err != nil {
fmt.Println("Cannot execute template for Caddyfile:", err)
}
scriptString := templateBytes.String()
err := executeScriptString(scriptString)
if err != nil {
fmt.Println("Cannot remove a container")
return
}
}
func startContainerJobsCheckerWorker() {
for range time.Tick(time.Second * 5) {
go func() {
containerJobs := getContainerJobsByMachineIdAndStatusAndJobType(thisMachine.Id, ContainerJobStatusPlanned, ContainerJobTypeDelete)
if len(containerJobs) > 0 {
fmt.Printf("Found %d ContainerJobs with status %s\n", len(containerJobs), ContainerJobStatusPlanned)
for _, containerJob := range containerJobs {
//Find all deployments
deployments := getLastDeploymentByEnvironmentId(containerJob.EnvironmentId)
if len(deployments) > 0 {
//Stop the last container only because all previous should be stopped and removed already
stopAndRemoveContainer(deployments[0].Id)
//Update ContainerJob status
updateContainerJobStatus(containerJob, ContainerJobStatusFinished)
}
}
}
}()
}
}
func updateContainerJobStatus(job ContainerJob, status string) error {
_, err := connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "UPDATE ContainerJob SET Status = ? WHERE Id = ?",
Arguments: []interface{}{status, job.Id},
},
},
)
if err != nil {
fmt.Printf("Cannot update a row in ContainerJob: %s\n", err.Error())
return err
}
return nil
}