-
Notifications
You must be signed in to change notification settings - Fork 162
/
cmd.go
441 lines (322 loc) · 13 KB
/
cmd.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package cmd
import (
"fmt"
"github.com/cppforlife/go-patch/patch"
cmdconf "github.com/cloudfoundry/bosh-cli/cmd/config"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshtpl "github.com/cloudfoundry/bosh-cli/director/template"
boshrel "github.com/cloudfoundry/bosh-cli/release"
boshreldir "github.com/cloudfoundry/bosh-cli/releasedir"
boshssh "github.com/cloudfoundry/bosh-cli/ssh"
boshui "github.com/cloudfoundry/bosh-cli/ui"
boshuit "github.com/cloudfoundry/bosh-cli/ui/task"
)
type Cmd struct {
BoshOpts BoshOpts
Opts interface{}
deps BasicDeps
}
func NewCmd(boshOpts BoshOpts, opts interface{}, deps BasicDeps) Cmd {
return Cmd{boshOpts, opts, deps}
}
type cmdConveniencePanic struct {
Err error
}
func (c Cmd) Execute() (cmdErr error) {
// Catch convenience panics from panicIfErr
defer func() {
if r := recover(); r != nil {
if cp, ok := r.(cmdConveniencePanic); ok {
cmdErr = cp.Err
} else {
panic(r)
}
}
}()
c.configureUI()
c.configureFS()
deps := c.deps
switch opts := c.Opts.(type) {
case *CreateEnvOpts:
envProvider := func(path string, vars boshtpl.Variables, ops patch.Ops) DeploymentPreparer {
return NewEnvFactory(deps, path, vars, ops).Preparer()
}
stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
return NewCreateEnvCmd(deps.UI, envProvider).Run(stage, *opts)
case *DeleteEnvOpts:
envProvider := func(path string, vars boshtpl.Variables, ops patch.Ops) DeploymentDeleter {
return NewEnvFactory(deps, path, vars, ops).Deleter()
}
stage := boshui.NewStage(deps.UI, deps.Time, deps.Logger)
return NewDeleteCmd(deps.UI, envProvider).Run(stage, *opts)
case *EnvironmentsOpts:
return NewEnvironmentsCmd(c.config(), deps.UI).Run()
case *EnvironmentOpts:
sessionFactory := func(config cmdconf.Config) Session {
return NewSessionFromOpts(c.BoshOpts, config, deps.UI, false, false, deps.FS, deps.Logger)
}
return NewEnvironmentCmd(sessionFactory, c.config(), deps.UI).Run(*opts)
case *LogInOpts:
sessionFactory := func(config cmdconf.Config) Session {
return NewSessionFromOpts(c.BoshOpts, config, deps.UI, true, true, deps.FS, deps.Logger)
}
config := c.config()
basicStrategy := NewBasicLoginStrategy(sessionFactory, config, deps.UI)
uaaStrategy := NewUAALoginStrategy(sessionFactory, config, deps.UI, deps.Logger)
sess := NewSessionFromOpts(c.BoshOpts, c.config(), deps.UI, true, true, deps.FS, deps.Logger)
anonDirector, err := sess.AnonymousDirector()
if err != nil {
return err
}
return NewLogInCmd(basicStrategy, uaaStrategy, anonDirector).Run()
case *LogOutOpts:
config := c.config()
sess := NewSessionFromOpts(c.BoshOpts, config, deps.UI, true, true, deps.FS, deps.Logger)
return NewLogOutCmd(sess.Environment(), config, deps.UI).Run()
case *TaskOpts:
eventsTaskReporter := boshuit.NewReporter(deps.UI, true)
plainTaskReporter := boshuit.NewReporter(deps.UI, false)
return NewTaskCmd(eventsTaskReporter, plainTaskReporter, c.director()).Run(*opts)
case *TasksOpts:
return NewTasksCmd(deps.UI, c.director()).Run(*opts)
case *CancelTaskOpts:
return NewCancelTaskCmd(c.director()).Run(*opts)
case *DeploymentOpts:
sessionFactory := func(config cmdconf.Config) Session {
return NewSessionFromOpts(c.BoshOpts, config, deps.UI, true, false, deps.FS, deps.Logger)
}
return NewDeploymentCmd(sessionFactory, c.config(), deps.UI).Run(*opts)
case *DeploymentsOpts:
return NewDeploymentsCmd(deps.UI, c.director()).Run()
case *DeleteDeploymentOpts:
return NewDeleteDeploymentCmd(deps.UI, c.deployment()).Run(*opts)
case *ReleasesOpts:
return NewReleasesCmd(deps.UI, c.director()).Run()
case *UploadReleaseOpts:
relProv, relDirProv := c.releaseProviders()
releaseDirFactory := func(dir DirOrCWDArg) (boshrel.Reader, boshreldir.ReleaseDir) {
releaseReader := relDirProv.NewReleaseReader(dir.Path)
releaseDir := relDirProv.NewFSReleaseDir(dir.Path)
return releaseReader, releaseDir
}
releaseWriter := relProv.NewArchiveWriter()
releaseArchiveFactory := func(path string) boshdir.ReleaseArchive {
return boshdir.NewFSReleaseArchive(path, deps.FS)
}
cmd := NewUploadReleaseCmd(
releaseDirFactory, releaseWriter, c.director(), releaseArchiveFactory, deps.UI)
return cmd.Run(*opts)
case *DeleteReleaseOpts:
return NewDeleteReleaseCmd(deps.UI, c.director()).Run(*opts)
case *StemcellsOpts:
return NewStemcellsCmd(deps.UI, c.director()).Run()
case *UploadStemcellOpts:
stemcellArchiveFactory := func(path string) boshdir.StemcellArchive {
return boshdir.NewFSStemcellArchive(path, deps.FS)
}
return NewUploadStemcellCmd(c.director(), stemcellArchiveFactory, deps.UI).Run(*opts)
case *DeleteStemcellOpts:
return NewDeleteStemcellCmd(deps.UI, c.director()).Run(*opts)
case *LocksOpts:
return NewLocksCmd(deps.UI, c.director()).Run()
case *ErrandsOpts:
return NewErrandsCmd(deps.UI, c.deployment()).Run()
case *RunErrandOpts:
director, deployment := c.directorAndDeployment()
downloader := NewUIDownloader(director, deps.SHA1Calc, deps.Time, deps.FS, deps.UI)
return NewRunErrandCmd(deployment, downloader, deps.UI).Run(*opts)
case *DisksOpts:
return NewDisksCmd(deps.UI, c.director()).Run(*opts)
case *DeleteDiskOpts:
return NewDeleteDiskCmd(deps.UI, c.director()).Run(*opts)
case *SnapshotsOpts:
return NewSnapshotsCmd(deps.UI, c.deployment()).Run(*opts)
case *TakeSnapshotOpts:
return NewTakeSnapshotCmd(c.deployment()).Run(*opts)
case *DeleteSnapshotOpts:
return NewDeleteSnapshotCmd(deps.UI, c.deployment()).Run(*opts)
case *DeleteSnapshotsOpts:
return NewDeleteSnapshotsCmd(deps.UI, c.deployment()).Run()
case *BuildManifestOpts:
return NewBuildManifestCmd(deps.UI).Run(*opts)
case *CloudConfigOpts:
return NewCloudConfigCmd(deps.UI, c.director()).Run()
case *UpdateCloudConfigOpts:
return NewUpdateCloudConfigCmd(deps.UI, c.director()).Run(*opts)
case *RuntimeConfigOpts:
return NewRuntimeConfigCmd(deps.UI, c.director()).Run()
case *UpdateRuntimeConfigOpts:
director := c.director()
releaseManager := c.releaseManager(director)
return NewUpdateRuntimeConfigCmd(deps.UI, director, releaseManager).Run(*opts)
case *ManifestOpts:
return NewManifestCmd(deps.UI, c.deployment()).Run()
case *EventsOpts:
return NewEventsCmd(deps.UI, c.director()).Run(*opts)
case *InspectReleaseOpts:
return NewInspectReleaseCmd(deps.UI, c.director()).Run(*opts)
case *VMsOpts:
return NewVMsCmd(deps.UI, c.director()).Run(*opts)
case *InstancesOpts:
return NewInstancesCmd(deps.UI, c.deployment()).Run(*opts)
case *UpdateResurrectionOpts:
return NewUpdateResurrectionCmd(c.director()).Run(*opts)
case *DeployOpts:
director, deployment := c.directorAndDeployment()
releaseManager := c.releaseManager(director)
return NewDeployCmd(deps.UI, deployment, releaseManager).Run(*opts)
case *StartOpts:
return NewStartCmd(deps.UI, c.deployment()).Run(*opts)
case *StopOpts:
return NewStopCmd(deps.UI, c.deployment()).Run(*opts)
case *RestartOpts:
return NewRestartCmd(deps.UI, c.deployment()).Run(*opts)
case *RecreateOpts:
return NewRecreateCmd(deps.UI, c.deployment()).Run(*opts)
case *CloudCheckOpts:
return NewCloudCheckCmd(c.deployment(), deps.UI).Run(*opts)
case *CleanUpOpts:
return NewCleanUpCmd(deps.UI, c.director()).Run(*opts)
case *LogsOpts:
director, deployment := c.directorAndDeployment()
downloader := NewUIDownloader(director, deps.SHA1Calc, deps.Time, deps.FS, deps.UI)
sshProvider := boshssh.NewProvider(deps.CmdRunner, deps.FS, deps.UI, deps.Logger)
nonIntSSHRunner := sshProvider.NewSSHRunner(false)
return NewLogsCmd(deployment, downloader, deps.UUIDGen, nonIntSSHRunner).Run(*opts)
case *SSHOpts:
sshProvider := boshssh.NewProvider(deps.CmdRunner, deps.FS, deps.UI, deps.Logger)
intSSHRunner := sshProvider.NewSSHRunner(true)
nonIntSSHRunner := sshProvider.NewSSHRunner(false)
resultsSSHRunner := sshProvider.NewResultsSSHRunner(false)
return NewSSHCmd(c.deployment(), deps.UUIDGen, intSSHRunner, nonIntSSHRunner, resultsSSHRunner, deps.UI).Run(*opts)
case *SCPOpts:
sshProvider := boshssh.NewProvider(deps.CmdRunner, deps.FS, deps.UI, deps.Logger)
scpRunner := sshProvider.NewSCPRunner()
return NewSCPCmd(c.deployment(), deps.UUIDGen, scpRunner, deps.UI).Run(*opts)
case *ExportReleaseOpts:
director, deployment := c.directorAndDeployment()
downloader := NewUIDownloader(director, deps.SHA1Calc, deps.Time, deps.FS, deps.UI)
return NewExportReleaseCmd(deployment, downloader).Run(*opts)
case *InitReleaseOpts:
return NewInitReleaseCmd(c.releaseDir(opts.Directory)).Run(*opts)
case *ResetReleaseOpts:
return NewResetReleaseCmd(c.releaseDir(opts.Directory)).Run(*opts)
case *GenerateJobOpts:
return NewGenerateJobCmd(c.releaseDir(opts.Directory)).Run(*opts)
case *GeneratePackageOpts:
return NewGeneratePackageCmd(c.releaseDir(opts.Directory)).Run(*opts)
case *FinalizeReleaseOpts:
_, relDirProv := c.releaseProviders()
releaseReader := relDirProv.NewReleaseReader(opts.Directory.Path)
releaseDir := relDirProv.NewFSReleaseDir(opts.Directory.Path)
return NewFinalizeReleaseCmd(releaseReader, releaseDir, deps.UI).Run(*opts)
case *CreateReleaseOpts:
_, relDirProv := c.releaseProviders()
releaseDirFactory := func(dir DirOrCWDArg) (boshrel.Reader, boshreldir.ReleaseDir) {
releaseReader := relDirProv.NewReleaseReader(dir.Path)
releaseDir := relDirProv.NewFSReleaseDir(dir.Path)
return releaseReader, releaseDir
}
_, err := NewCreateReleaseCmd(releaseDirFactory, deps.UI).Run(*opts)
return err
case *BlobsOpts:
return NewBlobsCmd(c.blobsDir(opts.Directory), deps.UI).Run()
case *AddBlobOpts:
return NewAddBlobCmd(c.blobsDir(opts.Directory), deps.FS, deps.UI).Run(*opts)
case *RemoveBlobOpts:
return NewRemoveBlobCmd(c.blobsDir(opts.Directory), deps.UI).Run(*opts)
case *UploadBlobsOpts:
return NewUploadBlobsCmd(c.blobsDir(opts.Directory)).Run()
case *SyncBlobsOpts:
return NewSyncBlobsCmd(c.blobsDir(opts.Directory), opts.ParallelOpt).Run()
case *MessageOpts:
deps.UI.PrintBlock(opts.Message)
return nil
default:
return fmt.Errorf("Unhandled command: %#v", c.Opts)
}
}
func (c Cmd) configureUI() {
c.deps.UI.EnableTTY(c.BoshOpts.TTYOpt)
if !c.BoshOpts.NoColorOpt {
c.deps.UI.EnableColor()
}
if c.BoshOpts.JSONOpt {
c.deps.UI.EnableJSON()
}
if c.BoshOpts.NonInteractiveOpt {
c.deps.UI.EnableNonInteractive()
}
}
func (c Cmd) configureFS() {
tmpDirPath, err := c.deps.FS.ExpandPath("~/.bosh/tmp")
c.panicIfErr(err)
err = c.deps.FS.ChangeTempRoot(tmpDirPath)
c.panicIfErr(err)
}
func (c Cmd) config() cmdconf.Config {
config, err := cmdconf.NewFSConfigFromPath(c.BoshOpts.ConfigPathOpt, c.deps.FS)
c.panicIfErr(err)
return config
}
func (c Cmd) session() Session {
return NewSessionFromOpts(c.BoshOpts, c.config(), c.deps.UI, true, true, c.deps.FS, c.deps.Logger)
}
func (c Cmd) director() boshdir.Director {
director, err := c.session().Director()
c.panicIfErr(err)
return director
}
func (c Cmd) deployment() boshdir.Deployment {
deployment, err := c.session().Deployment()
c.panicIfErr(err)
return deployment
}
func (c Cmd) directorAndDeployment() (boshdir.Director, boshdir.Deployment) {
sess := c.session()
director, err := sess.Director()
c.panicIfErr(err)
deployment, err := sess.Deployment()
c.panicIfErr(err)
return director, deployment
}
func (c Cmd) releaseProviders() (boshrel.Provider, boshreldir.Provider) {
indexReporter := boshui.NewIndexReporter(c.deps.UI)
blobsReporter := boshui.NewBlobsReporter(c.deps.UI)
releaseIndexReporter := boshui.NewReleaseIndexReporter(c.deps.UI)
releaseProvider := boshrel.NewProvider(
c.deps.CmdRunner, c.deps.Compressor, c.deps.SHA1Calc, c.deps.FS, c.deps.Logger)
releaseDirProvider := boshreldir.NewProvider(
indexReporter, releaseIndexReporter, blobsReporter, releaseProvider,
c.deps.SHA1Calc, c.deps.CmdRunner, c.deps.UUIDGen, c.deps.Time, c.deps.FS, c.deps.Logger)
return releaseProvider, releaseDirProvider
}
func (c Cmd) releaseManager(director boshdir.Director) ReleaseManager {
relProv, relDirProv := c.releaseProviders()
releaseDirFactory := func(dir DirOrCWDArg) (boshrel.Reader, boshreldir.ReleaseDir) {
releaseReader := relDirProv.NewReleaseReader(dir.Path)
releaseDir := relDirProv.NewFSReleaseDir(dir.Path)
return releaseReader, releaseDir
}
createReleaseCmd := NewCreateReleaseCmd(releaseDirFactory, c.deps.UI)
releaseWriter := relProv.NewArchiveWriter()
releaseArchiveFactory := func(path string) boshdir.ReleaseArchive {
return boshdir.NewFSReleaseArchive(path, c.deps.FS)
}
uploadReleaseCmd := NewUploadReleaseCmd(
releaseDirFactory, releaseWriter, director, releaseArchiveFactory, c.deps.UI)
return NewReleaseManager(createReleaseCmd, uploadReleaseCmd)
}
func (c Cmd) blobsDir(dir DirOrCWDArg) boshreldir.BlobsDir {
_, relDirProv := c.releaseProviders()
return relDirProv.NewFSBlobsDir(dir.Path)
}
func (c Cmd) releaseDir(dir DirOrCWDArg) boshreldir.ReleaseDir {
_, relDirProv := c.releaseProviders()
return relDirProv.NewFSReleaseDir(dir.Path)
}
func (c Cmd) panicIfErr(err error) {
if err != nil {
panic(cmdConveniencePanic{err})
}
}