forked from harbur/captain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
captain.go
205 lines (173 loc) · 5.07 KB
/
captain.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
package captain // import "github.com/harbur/captain"
import (
"os"
)
// Debug can be turned on to enable debug mode.
var Debug bool
// StatusError provides error code and id
type StatusError struct {
error error
status int
}
// Pre function executes commands on pre section before build
func Pre(config Config, app App) {
for _, value := range app.Pre {
info("Running pre command: %s", value)
res := execute("bash", "-c", value)
if res != nil {
err("Pre execution returned non-zero status")
os.Exit(TestFailed)
}
}
}
// Post function executes commands on pre section after build
func Post(config Config, app App) {
for _, value := range app.Post {
info("Running post command: %s", value)
res := execute("bash", "-c", value)
if res != nil {
err("Post execution returned non-zero status")
os.Exit(TestFailed)
}
}
}
type BuildOptions struct {
Config Config
Force bool
All_branches bool
Long_sha bool
}
// Build function compiles the Containers of the project
func Build(opts BuildOptions) {
config := opts.Config
var rev = getRevision(opts.Long_sha)
// For each App
for _, app := range config.GetApps() {
// If no Git repo exist
if !isGit() {
// Perfoming [build latest]
debug("No local git repository found, just building latest")
// Execute Pre commands
Pre(config, app)
// Build latest image
res := buildImage(app, "latest", opts.Force)
if res != nil {
os.Exit(BuildFailed)
}
} else {
// Skip build if there are no local changes and the commit is already built
if !isDirty() && imageExist(app, rev) && !opts.Force {
// Performing [skip rev|tag rev@latest|tag rev@branch]
info("Skipping build of %s:%s - image is already built", app.Image, rev)
// Tag commit image
tagImage(app, rev, "latest")
// Tag branch image
for _,branch := range getBranches(opts.All_branches) {
res := tagImage(app, rev, branch)
if res != nil {
os.Exit(TagFailed)
}
}
} else {
// Performing [build latest|tag latest@rev|tag latest@branch]
// Execute Pre commands
Pre(config, app)
// Build latest image
res := buildImage(app, "latest", opts.Force)
if res != nil {
os.Exit(BuildFailed)
}
if isDirty() {
debug("Skipping tag of %s:%s - local changes exist", app.Image, rev)
} else {
// Tag commit image
tagImage(app, "latest", rev)
// Tag branch image
for _,branch := range getBranches(opts.All_branches) {
res := tagImage(app, "latest", branch)
if res != nil {
os.Exit(TagFailed)
}
}
}
}
}
Post(config, app)
}
}
// Test function executes the tests of the project
func Test(opts BuildOptions) {
config := opts.Config
for _, app := range config.GetApps() {
for _, value := range app.Test {
info("Running test command: %s", value)
res := execute("bash", "-c", value)
if res != nil {
err("Test execution returned non-zero status")
os.Exit(TestFailed)
}
}
}
}
// Push function pushes the containers to the remote registry
func Push(opts BuildOptions) {
config := opts.Config
// If no Git repo exist
if !isGit() {
err("No local git repository found, cannot push")
os.Exit(NoGit)
}
if isDirty() {
err("Git repository has local changes, cannot push")
os.Exit(GitDirty)
}
for _, app := range config.GetApps() {
for _,branch := range getBranches(opts.All_branches) {
info("Pushing image %s:%s", app.Image, branch)
execute("docker", "push", app.Image+":"+branch)
info("Pushing image %s:%s", app.Image, "latest")
execute("docker", "push", app.Image+":"+"latest")
}
}
}
// Pull function pulls the containers from the remote registry
func Pull(opts BuildOptions) {
config := opts.Config
for _, app := range config.GetApps() {
for _,branch := range getBranches(opts.All_branches) {
info("Pulling image %s:%s", app.Image, "latest")
execute("docker", "pull", app.Image+":"+"latest")
info("Pulling image %s:%s", app.Image, branch)
execute("docker", "pull", app.Image+":"+branch)
}
}
}
// Purge function purges the stale images
func Purge(opts BuildOptions) {
config := opts.Config
// For each App
for _, app := range config.GetApps() {
var tags =[]string{}
// Retrieve the list of the existing Image tags
for _,img := range getImages(app) {
tags=append(tags,img.RepoTags...)
}
// Remove from the list: The latest image
for key,tag := range tags { if (tag == app.Image+":latest") { tags=append(tags[:key], tags[key+1:]...) } }
// Remove from the list: The current commit-id
for key,tag := range tags { if (tag == app.Image+":"+getRevision(opts.Long_sha)) { tags=append(tags[:key], tags[key+1:]...) } }
// Remove from the list: The working-dir git branches
for _,branch := range getBranches(opts.All_branches) {
for key,tag := range tags { if (tag == app.Image+":"+branch) { tags=append(tags[:key], tags[key+1:]...) } }
}
// Proceed with deletion of Images
for _,tag := range tags {
info ("Deleting image %s", tag)
res := removeImage(tag)
if (res != nil) {
err("Deleting image failed: %s", res)
os.Exit(DeleteImageFailed)
}
}
}
}