-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.go
169 lines (140 loc) · 4.39 KB
/
deploy.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
/* 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 deploySubCommandTab = map[string]func(args []string){
"list": deployListMain,
"describe": deployDescribeMain,
"help": deployHelpMain,
}
//go:embed deployHelp.txt
var deployHelpText string
func deployHelpMain(args []string) {
fmt.Printf(deployHelpText)
}
func deployListMain(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 deploy 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
}
}
fmt.Printf("Listing deployments for project %v...", opts.common.projectId)
// @todo add envId
deployments, err := bopsdk.ListDeployments(opts.common.projectId, "",
sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if len(deployments) == 0 {
fmt.Printf("\nNo currently deployed packages\n")
} else {
fmt.Printf("\nDeploymentId\n")
for _, deployId := range deployments {
fmt.Printf("%v\n", deployId)
}
}
}
func deployMain(args []string) {
exitStatus := 0
deploySubCommandName := "help"
if len(args) == 0 {
exitStatus = 1
} else {
deploySubCommandName = args[0]
}
deploySubCommand, ok := deploySubCommandTab[deploySubCommandName]
if !ok {
exitStatus = 1
deploySubCommand = deployHelpMain
}
if len(args) > 0 {
args = args[1:]
}
deploySubCommand(args)
os.Exit(exitStatus)
}
func deployDescribeMain(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 deploy 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.deployId == "" {
fmt.Fprintf(os.Stderr, "Please specify deployment id with --deployid. If you don't know this, try 'bopmatic deployment list'\n")
os.Exit(1)
}
fmt.Printf("Describing deployId:%v...", opts.common.deployId)
deployDesc, err := bopsdk.DescribeDeployment(opts.common.deployId,
sdkOpts...)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
fmt.Printf("\nDeployment Id:%v\n\tProject Id:%v\n\tPackage Id:%v\n\tEnvironment Id:%v\n\tType:%v\n\tInitiator:%v\n\tState:%v\n\tDetail:%v\n\tCreate Time: %v\n\tValidation Start Time: %v\n\tBuild Start Time: %v\n\tDeploy Start Time: %v\n\tCompletion Time: %v\n",
deployDesc.Id, deployDesc.Header.ProjId, deployDesc.Header.PkgId,
deployDesc.Header.EnvId, deployDesc.Header.Type,
deployDesc.Header.Initiator, deployDesc.State, deployDesc.StateDetail,
unixTime2UtcStr(deployDesc.CreateTime),
unixTime2UtcStr(deployDesc.ValidationStartTime),
unixTime2UtcStr(deployDesc.BuildStartTime),
unixTime2UtcStr(deployDesc.DeployStartTime),
unixTime2UtcStr(deployDesc.EndTime))
switch deployDesc.State {
case pb.DeploymentState_CREATED:
fmt.Printf("\nYour deployment has been created and will be validated shortly\n")
case pb.DeploymentState_DPLY_VALIDATING:
fmt.Printf("\nBopmatic ServiceRunner is validating your project package\n")
case pb.DeploymentState_DPLY_BUILDING:
fmt.Printf("\nBopmatic ServiceRunner is building infrastructure for your project package\n")
case pb.DeploymentState_DEPLOYING:
fmt.Printf("\nBopmatic ServiceRunner is deploying your package into production\n")
case pb.DeploymentState_SUCCESS:
fmt.Printf("\nBopmatic ServiceRunner has successully completed this deployment of your package\n")
case pb.DeploymentState_FAILED:
fallthrough
case pb.DeploymentState_UNKNOWN_DEPLOY_STATE:
fmt.Printf("\nAn error occurred within Bopmatic ServiceRunner and a support staff member needs to examine the situation.\n")
}
}