forked from coinbase/step
-
Notifications
You must be signed in to change notification settings - Fork 1
/
step.go
158 lines (141 loc) · 4.69 KB
/
step.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
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/coinbase/step/machine"
"github.com/coinbase/step/bifrost"
"github.com/coinbase/step/client"
"github.com/coinbase/step/deployer"
"github.com/coinbase/step/utils/run"
"github.com/coinbase/step/utils/to"
)
func main() {
default_name := "coinbase-step-deployer"
region, account_id := to.RegionAccount()
def_step_arn := to.Strp("")
if region != nil && account_id != nil {
def_step_arn = to.StepArn(region, account_id, &default_name)
}
// Step Subcommands
jsonCommand := flag.NewFlagSet("json", flag.ExitOnError)
dotCommand := flag.NewFlagSet("dot", flag.ExitOnError)
dotStates := dotCommand.String("states", "{}", "State Machine JSON")
// Other Subcommands
bootstrapCommand := flag.NewFlagSet("bootstrap", flag.ExitOnError)
deployCommand := flag.NewFlagSet("deploy", flag.ExitOnError)
// bootstrap args
bootstrapStates := bootstrapCommand.String("states", "{}", "State Machine JSON")
bootstrapLambda := bootstrapCommand.String("lambda", "", "lambda name or arn")
bootstrapStep := bootstrapCommand.String("step", "", "step function name or arn")
bootstrapBucket := bootstrapCommand.String("bucket", "", "s3 bucket to upload release to")
bootstrapZip := bootstrapCommand.String("zip", "lambda.zip", "zip of lambda")
bootstrapProject := bootstrapCommand.String("project", "", "project name")
bootstrapConfig := bootstrapCommand.String("config", "", "config name")
bootstrapRegion := bootstrapCommand.String("region", "", "AWS region")
bootstrapAccount := bootstrapCommand.String("account", "", "AWS account id")
// deploy args
deployStates := deployCommand.String("states", "{}", "State Machine JSON")
deployLambda := deployCommand.String("lambda", "", "lambda name or arn")
deployStep := deployCommand.String("step", "", "step function name or arn")
deployBucket := deployCommand.String("bucket", "", "s3 bucket to upload release to")
deployDeployer := deployCommand.String("deployer", *def_step_arn, "step function deployer name or arn")
deployZip := deployCommand.String("zip", "lambda.zip", "zip of lambda")
deployProject := deployCommand.String("project", "", "project name")
deployConfig := deployCommand.String("config", "", "config name")
deployRegion := deployCommand.String("region", "", "AWS region")
deployAccount := deployCommand.String("account", "", "AWS account id")
// By Default Run Lambda Function
if len(os.Args) == 1 {
fmt.Println("Starting Lambda")
run.LambdaTasks(deployer.TaskHandlers())
}
switch os.Args[1] {
case "json":
jsonCommand.Parse(os.Args[2:])
case "dot":
dotCommand.Parse(os.Args[2:])
case "bootstrap":
bootstrapCommand.Parse(os.Args[2:])
case "deploy":
deployCommand.Parse(os.Args[2:])
default:
fmt.Println("Usage of step: step <json|bootstrap|deploy|dot> <args> (No args starts Lambda)")
fmt.Println("json")
jsonCommand.PrintDefaults()
fmt.Println("dot")
dotCommand.PrintDefaults()
fmt.Println("bootstrap")
bootstrapCommand.PrintDefaults()
fmt.Println("deploy")
deployCommand.PrintDefaults()
os.Exit(1)
}
// Create the State machine
if jsonCommand.Parsed() {
run.JSON(deployer.StateMachine())
} else if dotCommand.Parsed() {
run.Dot(machine.FromJSON([]byte(*dotStates)))
} else if bootstrapCommand.Parsed() {
r := newRelease(
bootstrapProject,
bootstrapConfig,
bootstrapLambda,
bootstrapStep,
bootstrapBucket,
bootstrapStates,
bootstrapRegion,
bootstrapAccount,
)
bootstrapRun(r, bootstrapZip)
} else if deployCommand.Parsed() {
region, account_id := to.RegionAccountOrExit()
r := newRelease(
deployProject,
deployConfig,
deployLambda,
deployStep,
deployBucket,
deployStates,
deployRegion,
deployAccount,
)
arn := to.StepArn(region, account_id, deployDeployer)
deployRun(r, deployZip, arn)
} else {
fmt.Println("ERROR: Command Line Not Parsed")
os.Exit(1)
}
}
func check(err error) {
if err == nil {
return
}
fmt.Println("ERROR", err)
os.Exit(1)
}
func bootstrapRun(release *deployer.Release, zip *string) {
err := client.Bootstrap(release, zip)
check(err)
}
func deployRun(release *deployer.Release, zip *string, deployer_arn *string) {
err := client.Deploy(release, zip, deployer_arn)
check(err)
}
func newRelease(project *string, config *string, lambda *string, step *string, bucket *string, states *string, region *string, account_id *string) *deployer.Release {
return &deployer.Release{
Release: bifrost.Release{
AwsRegion: region,
AwsAccountID: account_id,
ReleaseID: to.TimeUUID("release-"),
CreatedAt: to.Timep(time.Now()),
ProjectName: project,
ConfigName: config,
Bucket: bucket,
},
StateMachineJSON: states,
LambdaName: lambda,
StepFnName: step,
}
}