-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_job.go
167 lines (133 loc) · 3.66 KB
/
image_job.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
/*
Deployments
*/
package main
import (
"bytes"
"fmt"
"time"
"github.com/rqlite/gorqlite"
)
const ImageJobTypeDelete = "container_del"
const ImageJobStatusPlanned = "planned"
const ImageJobStatusFinished = "finished"
type ImageJob struct {
Id string
Status string
EnvironmentId string
JobType string
}
func addImageJob(job ImageJob) ImageJob {
id, err := NanoId(7)
if err != nil {
fmt.Println("Cannot generate new NanoId for ImageJob:", err)
return job
}
job.Id = id
_, err = connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "INSERT INTO ImageJob( Id, Status, EnvironmentId, JobType) VALUES(?, ?, ?, ?)",
Arguments: []interface{}{job.Id, job.Status, job.EnvironmentId, job.JobType},
},
},
)
if err != nil {
fmt.Printf(" Cannot write to ImageJob table: %s\n", err.Error())
}
fmt.Printf("New ImageJob is scheduled for EnvironmentId %s\n", job.EnvironmentId)
return job
}
func getImageJobsByStatus(status string) []ImageJob {
rows, err := connection.QueryOneParameterized(
gorqlite.ParameterizedStatement{
Query: "SELECT Id, Status, EnvironmentId, JobType from ImageJob WHERE Status = ?",
Arguments: []interface{}{status},
},
)
return handleImageJobQuery(rows, err)
}
func handleImageJobQuery(rows gorqlite.QueryResult, err error) []ImageJob {
var jobs = []ImageJob{}
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 JobType string
err := rows.Scan(&Id, &Status, &EnvironmentId, &JobType)
if err != nil {
fmt.Printf(" Cannot run Scan: %s\n", err.Error())
}
loadedJob := ImageJob{
Id: Id,
Status: Status,
EnvironmentId: EnvironmentId,
JobType: JobType,
}
jobs = append(jobs, loadedJob)
}
return jobs
}
func removeImage(imageId string) {
fmt.Printf("Removing image with ID %s\n", imageId)
scriptTemplate := createTemplate("caddyfile", `
#!/bin/sh
docker image rm -f {{.IMAGE_ID}}
docker image rm -f {{.CONTAINER_REGISTRY_IP}}:7000/{{.IMAGE_ID}}
`)
var templateBytes bytes.Buffer
templateData := map[string]string{
"IMAGE_ID": imageId,
"CONTAINER_REGISTRY_IP": containerRegistryIp,
}
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 an image with ID %s", imageId)
return
}
}
func startImageJobsCheckerWorker() {
for range time.Tick(time.Second * 5) {
go func() {
imageJobs := getImageJobsByStatus(ImageJobStatusPlanned)
if len(imageJobs) > 0 {
fmt.Printf("Found %d ImageJobs with status %s\n", len(imageJobs), ImageJobStatusPlanned)
for _, imageJob := range imageJobs {
//Find all deployments
images := getImagesByEnvironmentId(imageJob.EnvironmentId)
if len(images) > 0 {
//Remove 2 last images because we keep only 2 last images
removeImage(images[0].Id)
if len(images) > 1 {
removeImage(images[1].Id)
}
//Update ImageJob status
updateImageJobStatus(imageJob, ImageJobStatusFinished)
}
}
}
}()
}
}
func updateImageJobStatus(job ImageJob, status string) error {
_, err := connection.WriteParameterized(
[]gorqlite.ParameterizedStatement{
{
Query: "UPDATE ImageJob 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
}