-
Notifications
You must be signed in to change notification settings - Fork 42
/
deploy.go
287 lines (221 loc) · 8.26 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
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
package command
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/suborbital/e2core/e2/templater"
"github.com/suborbital/e2core/e2/util"
"github.com/suborbital/e2core/e2core/release"
)
type deployData struct {
E2CoreTag string
EnvToken string
BuilderDomain string
StorageClassName string
}
const defaultRepo string = "suborbital/e2core"
const defaultBranch = "v" + release.E2CoreServerDotVersion
// DeployCommand returns the SE2 deploy command.
func DeployCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "deploy",
Short: "Deploy E2 Core to Kubernetes",
Long: "Deploy E2 Core to Kubernetes",
RunE: func(cmd *cobra.Command, args []string) error {
shouldReset := cmd.Flags().Changed(resetFlag)
repo, _ := cmd.Flags().GetString(repoFlag)
branch, _ := cmd.Flags().GetString(branchFlag)
forceUpdateTemplates, _ := cmd.Flags().GetBool(updateTemplatesFlag)
if err := introAcceptance(); err != nil {
return err
}
cwd, err := os.Getwd()
if err != nil {
return errors.Wrap(err, "🚫 failed to Getwd")
}
workingDirectory, err := filepath.Abs(cwd)
if err != nil {
return errors.Wrap(err, "🚫 failed to get absolute path")
}
if forceUpdateTemplates {
util.LogInfo(fmt.Sprintf("updating (forced) templates from %s (%s)", repo, branch))
_, err = templater.UpdateTemplates(repo, branch)
if err != nil {
return errors.Wrap(err, "🚫 failed to UpdateTemplates")
}
}
templatesPath, err := templater.TemplatesExist(repo, branch)
// Fetch templates if they don't exist
if err != nil {
util.LogInfo(fmt.Sprintf("updating templates from %s (%s)", repo, branch))
templatesPath, err = templater.UpdateTemplates(repo, branch)
if err != nil {
return errors.Wrap(err, "🚫 failed to UpdateTemplates")
}
}
// if the --reset flag was passed or there's no existing manifests
// then we need to 'build the world' from scratch.
if shouldReset || !manifestsExist(workingDirectory) {
util.LogStart("preparing deployment")
// if there are any existing deployment manifests sitting around, let's replace them.
if err := removeExistingManifests(workingDirectory); err != nil {
return errors.Wrap(err, "🚫 failed to removeExistingManifests")
}
_, err = util.Mkdir(workingDirectory, ".suborbital")
if err != nil {
return errors.Wrap(err, "🚫 failed to Mkdir")
}
envToken, err := getEnvToken()
if err != nil {
return errors.Wrap(err, "🚫 failed to getEnvToken")
}
data := deployData{
E2CoreTag: "v" + release.E2CoreServerDotVersion,
EnvToken: envToken,
}
templateName := "e2core-k8s"
data.StorageClassName, err = getStorageClass()
if err != nil {
return errors.Wrap(err, "🚫 failed to getStorageClass")
}
if err := templater.ExecTmplDir(workingDirectory, "", templatesPath, templateName, data); err != nil {
return errors.Wrap(err, "🚫 failed to ExecTmplDir")
}
util.LogDone("ready to start installation")
}
dryRun, _ := cmd.Flags().GetBool(dryRunFlag)
if dryRun {
util.LogInfo("aborting due to dry-run, manifest files remain in " + workingDirectory)
return nil
}
util.LogStart("installing...")
if _, err := util.Command.Run("kubectl apply -f https://github.com/kedacore/keda/releases/download/v2.4.0/keda-2.4.0.yaml"); err != nil {
return errors.Wrap(err, "🚫 failed to install KEDA")
}
// we don't care if this fails, so don't check error.
util.Command.Run("kubectl create ns suborbital")
if err := createConfigMap(cwd); err != nil {
return errors.Wrap(err, "🚫 failed to createConfigMap")
}
if _, err := util.Command.Run("kubectl apply -f .suborbital/"); err != nil {
return errors.Wrap(err, "🚫 failed to kubectl apply")
}
util.LogDone("installation complete!")
return nil
},
}
cmd.Flags().String(repoFlag, defaultRepo, "git repo to download templates from")
cmd.Flags().String(branchFlag, defaultBranch, "git branch to download templates from")
cmd.Flags().Bool(dryRunFlag, false, "prepare the deployment in the .suborbital directory, but do not apply it")
cmd.Flags().Bool(resetFlag, false, "reset the deployment to default (replaces Kubernetes manifests)")
cmd.Flags().Bool(updateTemplatesFlag, false, "forces templates to be updated")
return cmd
}
// TODO: update this
func introAcceptance() error {
fmt.Print(`
Suborbital Extension Engine (SE2) Installer
BEFORE YOU CONTINUE:
- You must first run "subo se2 create token <email>" to get an environment token
- You must have kubectl installed in PATH, and it must be connected to the cluster you'd like to use
- You must be able to set up DNS records for the builder service after this installation completes
- Choose the DNS name you'd like to use before continuing, e.g. builder.acmeco.com
- Subo will attempt to determine the default storage class for your Kubernetes cluster,
but if is unable to do so you will need to provide one
- See the SE2 documentation for more details
- Subo will install the KEDA autoscaler into your cluster. It will not affect any existing deployments.
Are you ready to continue? (y/N): `)
answer, err := util.ReadStdinString()
if err != nil {
return errors.Wrap(err, "failed to ReadStdinString")
}
if !strings.EqualFold(answer, "y") {
return errors.New("aborting")
}
return nil
}
// getEnvToken gets the environment token from stdin.
func getEnvToken() (string, error) {
existing, err := util.ReadEnvironmentToken()
if err == nil {
util.LogInfo("using cached environment token")
return existing, nil
}
fmt.Print("Enter your environment token: ")
token, err := util.ReadStdinString()
if err != nil {
return "", errors.Wrap(err, "failed to ReadStdinString")
}
if len(token) != 32 {
return "", errors.New("token must be 32 characters in length")
}
if err := util.WriteEnvironmentToken(token); err != nil {
util.LogWarn(err.Error())
return token, nil
} else {
util.LogInfo("saved environment token to cache")
}
return token, nil
}
// getStorageClass gets the storage class to use.
func getStorageClass() (string, error) {
defaultClass, err := detectStorageClass()
if err != nil {
// that's fine, continue.
fmt.Println("failed to automatically detect Kubernetes storage class:", err.Error())
} else if defaultClass != "" {
fmt.Println("using default storage class: ", defaultClass)
return defaultClass, nil
}
fmt.Print("Enter the Kubernetes storage class to use: ")
storageClass, err := util.ReadStdinString()
if err != nil {
return "", errors.Wrap(err, "failed to ReadStdinString")
}
if len(storageClass) == 0 {
return "", errors.New("storage class must not be empty")
}
return storageClass, nil
}
func detectStorageClass() (string, error) {
output, err := util.Command.Run("kubectl get storageclass --output=name")
if err != nil {
return "", errors.Wrap(err, "failed to get default storageclass")
}
// output will look like: storageclass.storage.k8s.io/do-block-storage
// so split on the / and return the last part.
outputParts := strings.Split(output, "/")
if len(outputParts) != 2 {
return "", errors.New("could not automatically determine storage class")
}
return outputParts[1], nil
}
func createConfigMap(cwd string) error {
configFilepath := filepath.Join(cwd, "config", "se2-config.yaml")
_, err := os.Stat(configFilepath)
if err != nil {
return errors.Wrap(err, "failed to Stat se2-config.yaml")
}
if _, err := util.Command.Run(fmt.Sprintf("kubectl create configmap se2-config --from-file=se2-config.yaml=%s -n suborbital", configFilepath)); err != nil {
return errors.Wrap(err, "failed to create configmap (you may need to run `kubectl delete configmap se2-config -n suborbital`)")
}
return nil
}
func manifestsExist(workingDirectory string) bool {
if _, err := os.Stat(filepath.Join(workingDirectory, ".suborbital")); err == nil {
return true
}
return false
}
func removeExistingManifests(workingDirectory string) error {
// start with a clean slate.
if _, err := os.Stat(filepath.Join(workingDirectory, ".suborbital")); err == nil {
if err := os.RemoveAll(filepath.Join(workingDirectory, ".suborbital")); err != nil {
return errors.Wrap(err, "failed to RemoveAll .suborbital")
}
}
return nil
}