-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (171 loc) · 5.18 KB
/
main.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"unicode/utf8"
"github.com/dlclark/regexp2"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/go-co-op/gocron/v2"
"github.com/rotisserie/eris"
log "github.com/sirupsen/logrus"
)
// array of job names
var jobs []string
type JobConfig struct {
Name string
Config map[string]string
}
func init() {
// Log as JSON instead of the default ASCII formatter.
// log.SetFormatter(&log.JSONFormatter{})
log.SetFormatter(&log.TextFormatter{})
// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)
// // Only log the warning severity or above.
// log.SetLevel(log.WarnLevel)
log.SetLevel(log.DebugLevel)
}
func main() {
log.Info("Starting backup service")
// load config file
loadConfigFile()
// create backup folder
createBackupFolder()
// create a scheduler
s, err := gocron.NewScheduler()
if err != nil {
log.Fatal(eris.Wrap(err, "failed to create scheduler"))
}
defer func() { _ = s.Shutdown() }()
// create docker client
log.Debug("Creating docker client")
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
log.Fatal(eris.Wrap(err, "failed to create docker client"))
}
defer cli.Close()
backup := NewBackup(cli)
// get a list of every job
jobs = k.MapKeys("jobs")
// start the http server
// also handles if it is disabled
httpServer := &http.Server{
Addr: ":3333",
}
go startHttpServer(httpServer, backup)
defer func() {
err := httpServer.Shutdown(context.Background())
if err != nil {
log.Error(eris.Wrap(err, "failed to shutdown http server"))
}
}()
// schedule each job
for _, job := range jobs {
log.Infof("Scheduling job: %s", job)
jobConfig, err := getJobConfig(job)
if err != nil {
log.Error(eris.Wrapf(err, "failed to get job config for %s", job))
}
_, err = s.NewJob(gocron.CronJob(jobConfig.Config["cron"], false), gocron.NewTask(backup.QueueJob, jobConfig))
if err != nil {
log.Error(eris.Wrapf(err, "failed to schedule job %s", jobConfig.Name))
}
}
// start the scheduler
s.Start()
// wait for a signal to end the program
endSignal := make(chan os.Signal, 1)
signal.Notify(endSignal, syscall.SIGINT, syscall.SIGTERM)
<-endSignal
}
func (b Backup) QueueJob(jobConfig *JobConfig) {
ctx := context.Background()
log.Debug("Listing containers")
// gets a list of running containers
containers, err := b.Cli.ContainerList(ctx, types.ContainerListOptions{})
if err != nil {
log.Error(eris.Wrap(err, "failed to list containers"))
return
}
targetIds := []string{}
for _, container := range containers {
if isTargetContainer(container, jobConfig) {
targetIds = append(targetIds, container.ID)
}
}
log.Infof("Found %d target container(s) for job %s", len(targetIds), jobConfig.Name)
for _, target := range targetIds {
log.Infof("Queueing backup for container %s", target)
// tell queue an item entered
b.backupQueue <- struct{}{}
b.backupContainer(ctx, &BackupContainerOptions{
ContainerId: target,
JobConfig: jobConfig,
})
}
}
// test if a container is the target container
// based on the config for the job
func isTargetContainer(container types.Container, jobConfig *JobConfig) bool {
// check if the container is running
if container.State != "running" {
log.Debugf("Container %s is not running", container.ID)
return false
}
if jobConfig.Config["matchMethod"] == "exact" {
for _, name := range container.Names {
// clean up the container name
name = preprocesContainerName(name)
// check if the container has the target name
if name != jobConfig.Config["match"] {
log.Debugf("Container %s (%s) is not the target name of %s", name, container.ID, jobConfig.Config["match"])
return false
} else {
log.Debugf("Container %s (%s) is the target name of %s", name, container.ID, jobConfig.Config["match"])
}
}
}
if jobConfig.Config["matchMethod"] == "regex" {
// check if the container matches the regex
re, err := regexp2.Compile(jobConfig.Config["match"], 0)
if err != nil {
log.Fatal(eris.Wrapf(err, "failed to compile regex for job %s", jobConfig.Name))
}
for _, name := range container.Names {
// clean up the container name
name = preprocesContainerName(name)
// test with regex
isMatch, err := re.MatchString(name)
if err != nil {
log.Fatal(eris.Wrapf(err, "failed to match regex for job %s", jobConfig.Name))
}
// if the regex doesn't match, return false
if !isMatch {
log.Debugf("Container %s (%s) is not the target regex for job %s", name, container.ID, jobConfig.Name)
return false
} else {
log.Debugf("Container %s (%s) is the target regex for job %s", name, container.ID, jobConfig.Name)
}
}
}
return true
}
func preprocesContainerName(name string) string {
// remove the leading slash on container names
_, i := utf8.DecodeRuneInString(name)
return name[i:]
}
func getJobConfig(name string) (*JobConfig, error) {
if !k.Exists("jobs." + name) {
return nil, eris.Errorf("Job %s does not exist", name)
}
return &JobConfig{
Name: name,
Config: k.StringMap("jobs." + name),
}, nil
}