/
main.go
145 lines (125 loc) · 3.27 KB
/
main.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/function61/gokit/dynversion"
"github.com/function61/gokit/logex"
"github.com/function61/gokit/ossignal"
"github.com/spf13/cobra"
)
func main() {
logger := logex.StandardLogger()
app := &cobra.Command{
Use: os.Args[0],
Short: "Deployer deploys your projects",
Version: dynversion.Version,
}
app.AddCommand(releasesEntry(logger))
asInteractive := false
keepCache := false
deployCmd := &cobra.Command{
Use: `deploy [serviceId] [releaseId]`,
Short: "Directly deploys the service",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
exitWithErrorIfErr(deployInternal(
ossignal.InterruptOrTerminateBackgroundCtx(logger),
args[0],
args[1],
asInteractive,
keepCache,
))
},
}
deployCmd.Flags().BoolVarP(&asInteractive, "interactive", "i", asInteractive, "Enters interactive mode (prompt)")
deployCmd.Flags().BoolVarP(&keepCache, "keep-cache", "", keepCache, "Do not remove workdir (could be dangerous cross-releases!)")
app.AddCommand(deployCmd)
/*
app.AddCommand(&cobra.Command{
Use: "destroy [serviceId]",
Short: "Destroys all resources used by service",
Args: cobra.ExactArgs(1),
Run: func(_ *cobra.Command, args []string) {
panic("not implemented yet")
},
})
*/
app.AddCommand(&cobra.Command{
Use: "package [friendlyVersion] [outputPackageLocation]",
Short: "Packages a spec into a zip",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
exitWithErrorIfErr(makePackage(args[0], args[1]))
},
})
app.AddCommand(&cobra.Command{
Use: `deployment-init [serviceId] [releaseId]`,
Short: "Creates a new deployment stub for you to use",
Args: cobra.ExactArgs(2),
Run: func(_ *cobra.Command, args []string) {
exitWithErrorIfErr(deploymentCreateConfig(
ossignal.InterruptOrTerminateBackgroundCtx(logger),
args[0],
args[1],
))
},
})
app.AddCommand(&cobra.Command{
Use: "manifest-new",
Short: "Creates a new manifest stub for you to use in new project",
Args: cobra.NoArgs,
Run: func(_ *cobra.Command, args []string) {
exitWithErrorIfErr(manifestStubCreate(os.Stdout))
},
})
app.AddCommand(&cobra.Command{
Use: "launch-via-shim [binary] [args..]",
Hidden: true, // internal implementation
Args: cobra.MinimumNArgs(1),
Run: func(_ *cobra.Command, args []string) {
exitWithErrorIfErr(launchViaShim(args))
},
})
if err := app.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
/*
deployments/
|-- anotherservice
| |-- state
| | `-- my-fictional-state.json
| `-- work
| |-- manifest.json
| `-- version.json
`-- hq
|-- state
| `-- terraform-state.json
`-- work
|-- manifest.json
`-- version.json
*/
func deploymentDir(serviceId string) string {
abs, err := filepath.Abs("deployments/" + serviceId)
if err != nil {
panic(err)
}
return abs
}
func workDir(serviceId string) string {
return deploymentDir(serviceId) + "/work"
}
func stateDir(serviceId string) string {
return deploymentDir(serviceId) + "/state"
}
func userConfigPath(serviceId string) string {
return deploymentDir(serviceId) + "/user-config.json"
}
func exitWithErrorIfErr(err error) {
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}