-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage.go
340 lines (282 loc) · 8.18 KB
/
package.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* Copyright © 2022-2024 Bopmatic, LLC. All Rights Reserved.
*
* See LICENSE file at the root of this package for license terms
*/
package main
import (
"flag"
"fmt"
"os"
_ "embed"
bopsdk "github.com/bopmatic/sdk/golang"
"github.com/bopmatic/sdk/golang/pb"
)
var pkgSubCommandTab = map[string]func(args []string){
"build": pkgBuildMain,
"deploy": pkgDeployMain,
"list": pkgListMain,
"delete": pkgDeleteMain,
"describe": pkgDescribeMain,
"help": pkgHelpMain,
}
func pkgMain(args []string) {
exitStatus := 0
pkgSubCommandName := "help"
if len(args) == 0 {
exitStatus = 1
} else {
pkgSubCommandName = args[0]
}
pkgSubCommand, ok := pkgSubCommandTab[pkgSubCommandName]
if !ok {
exitStatus = 1
pkgSubCommand = pkgHelpMain
}
if len(args) > 0 {
args = args[1:]
}
pkgSubCommand(args)
os.Exit(exitStatus)
}
func pkgBuildMain(args []string) {
type buildOpts struct {
common commonOpts
}
var opts buildOpts
f := flag.NewFlagSet("bopmatic package build", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err := f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
proj, err := bopsdk.NewProject(opts.common.projectFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if proj.Desc.BuildCmd == "" {
fmt.Printf("Project %v is a static site only; no build required\n",
proj.Desc.Name)
os.Exit(0)
}
err = proj.Build(os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to build %v: %v\n", proj.Desc.Name, err)
os.Exit(1)
}
err = proj.RemoveStalePackages()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to remove stale packages: %v\n", err)
os.Exit(1)
}
pkg, err := proj.NewPackageCreate("", os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to package %v: %v\n", proj.Desc.Name, err)
os.Exit(1)
}
fmt.Printf("Successfully built pkgId:%v (%v)\n", pkg.Id,
pkg.AbsTarballPath())
fmt.Printf("To deploy your package, next run:\n\t'bopmatic package deploy'\n")
}
func pkgDeployMain(args []string) {
sdkOpts, err := getAuthSdkOpts()
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to get user creds; did you run bompatic config? err: %v\n",
err)
os.Exit(1)
}
type deployOpts struct {
common commonOpts
}
var opts deployOpts
f := flag.NewFlagSet("bopmatic package deploy", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err = f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
proj, err := bopsdk.NewProject(opts.common.projectFilename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
pkg, err := proj.NewPackageExisting("")
if err != nil {
_ = proj.RemoveStalePackages()
pkg, err = proj.NewPackageCreate("", os.Stdout, os.Stderr)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to package %v: %v\n", proj.Desc.Name, err)
os.Exit(1)
}
}
validateNoConflicts(sdkOpts, pkg)
fmt.Printf("Deploying pkgId:%v (%v)...", pkg.Id, pkg.AbsTarballPath())
// @todo specify envId
deployId, err := pkg.Deploy("", sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("Started\nDeploying takes about 10 minutes. You can check deploy progress with:\n\t'bopmatic deploy describe --deployid %v'\n",
deployId)
}
func validateNoConflicts(sdkOpts []bopsdk.DeployOption, pkg *bopsdk.Package) {
// @todo for UX purposes consider evaluating conflicts client-side here
// rather than just relying on server-side conflict checks
}
func pkgListMain(args []string) {
sdkOpts, err := getAuthSdkOpts()
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to get user creds; did you run bompatic config? err: %v\n",
err)
os.Exit(1)
}
type listOpts struct {
common commonOpts
}
var opts listOpts
f := flag.NewFlagSet("bopmatic package list", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err = f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if opts.common.projectId == "" {
proj, err := bopsdk.NewProject(opts.common.projectFilename)
if err == nil {
opts.common.projectId = proj.Desc.Id
}
}
if opts.common.projectId == "" {
fmt.Printf("Listing packages for all projects...")
} else {
fmt.Printf("Listing packages for project %v...",
opts.common.projectId)
}
pkgs, err := bopsdk.ListPackages(opts.common.projectId, sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if len(pkgs) == 0 {
fmt.Printf("\nNo currently deployed packages\n")
} else {
fmt.Printf("\nProjectId\t\t\tPackageId\n")
for _, pkg := range pkgs {
fmt.Printf("%v\t\t%v\n", pkg.ProjId, pkg.PackageId)
}
}
}
//go:embed pkgHelp.txt
var pkgHelpText string
func pkgHelpMain(args []string) {
fmt.Printf(pkgHelpText)
}
func pkgDescribeMain(args []string) {
sdkOpts, err := getAuthSdkOpts()
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to get user creds; did you run bompatic config? err: %v\n",
err)
os.Exit(1)
}
type describeOpts struct {
common commonOpts
}
var opts describeOpts
f := flag.NewFlagSet("bopmatic package describe", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err = f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if opts.common.packageId == "" {
fmt.Fprintf(os.Stderr, "Please specify package id with --pkgid. If you don't know this, try 'bopmatic package list'\n")
os.Exit(1)
}
fmt.Printf("Describing pkgId:%v...", opts.common.packageId)
pkgDesc, err := bopsdk.Describe(opts.common.packageId, sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("\nPackageId %v:\n\tProjectId: %v\n\tState: %v\n\tSize: %v MiB\n\tUploadTime: %v\n",
pkgDesc.PackageId, pkgDesc.ProjId, pkgDesc.State,
pkgDesc.PackageSize/1024/1024, unixTime2UtcStr(pkgDesc.UploadTime))
switch pkgDesc.State {
case pb.PackageState_UPLOADING:
fmt.Printf("\nYour project is being uploaded to Bopmatic ServiceRunner\n")
case pb.PackageState_UPLOADED:
fmt.Printf("\nYour project package was uploaded Bopmatic ServiceRunner and will next be validated\n")
case pb.PackageState_PKG_VALIDATING:
fmt.Printf("\nBopmatic ServiceRunner is validating your project package\n")
case pb.PackageState_INVALID:
fmt.Printf("\nSomething is wrong with your project package and it cannot be deployed. Please delete it with:\n\t'bopmatic package destroy --pkgid %v'\n",
pkgDesc.PackageId)
case pb.PackageState_PKG_BUILDING:
fmt.Printf("\nBopmatic ServiceRunner is building infrastructure for your project package\n")
case pb.PackageState_BUILT:
fmt.Printf("\nBopmatic ServiceRunner has built your project.\n\n")
case pb.PackageState_PKG_DELETED:
fmt.Printf("\nBopmatic ServiceRunner has deleted your project package\n")
case pb.PackageState_PKG_SUPPORT_NEEDED:
fallthrough
case pb.PackageState_UNKNOWN_PKG_STATE:
fallthrough
default:
fmt.Printf("\nAn error occurred within Bopmatic ServiceRunner and a support staff member needs to examine the situation.\n")
}
}
func pkgDeleteMain(args []string) {
sdkOpts, err := getAuthSdkOpts()
if err != nil {
fmt.Fprintf(os.Stderr,
"Failed to get user creds; did you run bompatic config? err: %v\n",
err)
os.Exit(1)
}
type deleteOpts struct {
common commonOpts
}
var opts deleteOpts
f := flag.NewFlagSet("bopmatic package delete", flag.ExitOnError)
setCommonFlags(f, &opts.common)
err = f.Parse(args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if opts.common.packageId == "" {
fmt.Fprintf(os.Stderr, "Please specify package id with --pkgid. If you don't know this, try 'bopmatic package list'\n")
os.Exit(1)
}
fmt.Printf("Listing packages...")
pkgs, err := bopsdk.ListPackages(opts.common.projectId, sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
found := false
for _, pkg := range pkgs {
if pkg.PackageId == opts.common.packageId {
found = true
}
}
if !found {
fmt.Printf("\nPackage id %v no longer exists\n", opts.common.packageId)
os.Exit(1)
}
fmt.Printf("Deleting pkgId:%v...", opts.common.packageId)
err = bopsdk.DeletePackage(opts.common.packageId, sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("\nDeleted pkgId:%v", opts.common.packageId)
}