-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoni.go
309 lines (251 loc) · 7.36 KB
/
toni.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
)
const (
VERSION = "1.1.0"
configurationFileName = ".toni.yml"
apiTokenHeaderName = "X-Api-Token"
gitBranchCmd = "git symbolic-ref --short HEAD"
gitCommitCmd = "git rev-parse HEAD"
gitUnpushedCmd = "git cherry -v"
)
var usage = `toni is the CLI for Applikatoni.
Usage:
toni [-vhd] [-c=<commit SHA>] [-b=<commit branch>] -t <target> -m <comment>
Arguments:
REQUIRED:
-t Target of the deployment
-m The deployment comment
OPTIONAL:
-c The deployment commit SHA
(if unspecified toni uses the current git HEAD)
-b The branch of the commit SHA
(if unspecified toni uses the current git HEAD)
-d Make a dry run. See what will be deployed by showing diff.
-h Print the help and usage information
-v Print the version of the toni executable
Configuration:
When starting up, toni tries to read the ".toni.yml" configuration file in
the current working directoy. The configuration MUST specify the HOST,
APPLICATION, API TOKEN and the STAGES of deployments.
An example ".toni.yml" looks like this:
host: http://toni.shippingcompany.com
application: shippingcompany-main-application
api_token: 4fdd575f-FOOO-BAAR-af1e-ce3e9f75367d
stages:
production:
- CHECK_CONNECTION
- PRE_DEPLOYMENT
- CODE_DEPLOYMENT
- POST_DEPLOYMENT
staging:
- CHECK_CONNECTION
- PRE_DEPLOYMENT
- CODE_DEPLOYMENT
- POST_DEPLOYMENT
`
var (
target string
comment string
branch string
commitSHA string
dryRun bool
printVersion bool
printHelp bool
)
var (
currentDeploymentLocation string
currentDeploymentMx *sync.Mutex
)
func init() {
flag.StringVar(&target, "t", "", "Target of the deployment [required]")
flag.StringVar(&comment, "m", "", "Deployment comment [required]")
flag.StringVar(&commitSHA, "c", "", "Deployment commit")
flag.StringVar(&branch, "b", "", "Deployment branch")
flag.BoolVar(&dryRun, "d", false, "Dry run. See what will be deployed")
flag.BoolVar(&printHelp, "h", false, "Print the help and usage information")
flag.BoolVar(&printVersion, "v", false, "Print the version of the toni executable")
}
func main() {
flag.Parse()
if printVersion {
fmt.Println(VERSION)
os.Exit(0)
}
if printHelp {
printUsage()
os.Exit(0)
}
configurationFilePath, err := filepath.Abs(configurationFileName)
if err != nil {
configErrorExit("Error when trying to open configuration file (%s):\n",
configurationFileName,
err)
}
config, err := readConfiguration(configurationFilePath)
if err != nil {
configErrorExit("Error when trying to parse configuration file (%s):\n",
configurationFileName,
err)
}
err = config.Validate()
if err != nil {
configErrorExit("Configuration file is invalid (%s):\n",
configurationFileName,
err)
}
if target == "" {
fmt.Fprintf(os.Stderr, "No target specified (use -t option to specify target)\n")
fmt.Fprintf(os.Stderr, "\nTargets configured in .toni.yml:\n")
for target, _ := range config.Stages {
fmt.Fprintf(os.Stderr, "%s\n", target)
}
os.Exit(1)
}
if _, ok := config.Stages[target]; !ok {
fmt.Fprintf(os.Stderr, "Target %q not found in configuration file\n", target)
os.Exit(1)
}
if comment == "" {
fmt.Fprintf(os.Stderr, "No comment specified (use -m option to specify comment)\n")
os.Exit(1)
}
if branch == "" {
branch, err = runGitCmd(gitBranchCmd)
if branch == "" || err != nil {
fmt.Fprintf(os.Stderr, "Getting current branch with `%s` failed:\n", gitBranchCmd)
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
if commitSHA == "" {
commitSHA, err = runGitCmd(gitCommitCmd)
if commitSHA == "" || err != nil {
fmt.Fprintf(os.Stderr, "Getting current commit SHA with `%s` failed:\n", gitCommitCmd)
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
}
unpushed, err := runGitCmd(gitUnpushedCmd)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
if strings.Contains(unpushed, commitSHA) {
fmt.Fprintf(os.Stderr, "ERROR: %s has not been pushed to remote\n", commitSHA)
os.Exit(1)
}
if unpushed != "" {
fmt.Fprintf(os.Stderr, "WARNING: These commits have not been pushed to remote\n")
fmt.Fprintf(os.Stderr, "%s\n", unpushed)
}
currentDeploymentMx = &sync.Mutex{}
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
go func() {
<-sigChan
killCurrentDeployment(config)
}()
if dryRun {
fmt.Printf("Dry run. Showing diff...\n\n")
} else {
fmt.Print("Creating deployment...\n\n")
}
printDeploymentAttribute("host", config.Host)
printDeploymentAttribute("application", config.Application)
printDeploymentAttribute("target", target)
printDeploymentAttribute("sha", commitSHA)
printDeploymentAttribute("branch", branch)
printDeploymentAttribute("comment", comment)
printDeploymentAttribute("stages", strings.Join(config.Stages[target], ", "))
if dryRun {
diffUrl, err := buildDeploymentDiffURL(config.Host, config.Application, target, commitSHA)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
diff, err := getDeploymentDiff(config, diffUrl)
if err != nil {
fmt.Fprintf(os.Stderr, "creating a deployment failed: %s\n", err)
os.Exit(1)
}
fmt.Printf("\n\n")
printFormattedDiff(diff)
os.Exit(0)
}
deploymentURL, err := buildDeploymentURL(config.Host, config.Application)
if err != nil {
fmt.Fprintf(os.Stderr, "creating a deployment failed: %s\n", err)
os.Exit(1)
}
data := buildDeploymentData(target, commitSHA, branch, comment, config.Stages[target])
deploymentLocation, err := createDeployment(config, deploymentURL, data)
if err != nil {
fmt.Fprintf(os.Stderr, "creating a deployment failed: %s\n", err)
os.Exit(1)
}
fmt.Print("\nSuccessfully created. Streaming logs...\n\n")
setCurrentDeploymentLocation(deploymentLocation)
logURL, err := buildDeploymentLogURL(config.Host, deploymentLocation)
if err != nil {
fmt.Fprintf(os.Stderr, "streaming deployment logs failed: %s\n", err)
os.Exit(1)
}
err = streamDeploymentLog(config, logURL)
if err != nil {
fmt.Fprintf(os.Stderr, "streaming deployment logs failed: %s\n", err)
os.Exit(1)
}
}
func printUsage() {
fmt.Printf(usage)
}
func configErrorExit(message, configPath string, err error) {
fmt.Fprintf(os.Stderr, message, configPath)
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
func runGitCmd(gitCmd string) (string, error) {
argv := strings.Split(gitCmd, " ")
out, err := exec.Command(argv[0], argv[1:]...).Output()
if err != nil {
return "", err
}
return strings.Trim(string(out), "\n"), nil
}
func printDeploymentAttribute(name, val string) {
fmt.Printf("\t%-15s = %s\n", name, val)
}
func setCurrentDeploymentLocation(location string) {
currentDeploymentMx.Lock()
currentDeploymentLocation = location
currentDeploymentMx.Unlock()
}
func killCurrentDeployment(c *Configuration) {
currentDeploymentMx.Lock()
defer currentDeploymentMx.Unlock()
if currentDeploymentLocation == "" {
return
}
fmt.Printf("\nReceived INTERRUPT - ")
fmt.Printf("Sending kill request to server (%s)...\n\n",
currentDeploymentLocation)
killURL, err := buildDeploymentKillURL(c.Host, currentDeploymentLocation)
if err != nil {
fmt.Fprintf(os.Stderr, "Killing deployment failed: %s\n", err)
return
}
err = killDeployment(c, killURL)
if err != nil {
fmt.Fprintf(os.Stderr, "Killing deployment failed: %s\n", err)
return
}
}