-
Notifications
You must be signed in to change notification settings - Fork 11
/
create.go
398 lines (339 loc) · 12.5 KB
/
create.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
package create
import (
"fmt"
"strconv"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/cmd"
"github.com/OctopusDeploy/cli/pkg/cmd/target/shared"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/executionscommon"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/machinescommon"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/accounts"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/environments"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/spf13/cobra"
)
type GetAllAccountsForSshTarget func() ([]accounts.IAccount, error)
const (
FlagName = "name"
FlagFingerprint = "fingerprint"
FlagHost = "host"
FlagPort = "port"
FlagAccount = "account"
FlagRuntime = "runtime"
FlagPlatform = "platform"
SelfContainedCalamari = "self-contained"
MonoCalamari = "mono"
LinuxX64 = "linux-x64"
LinuxArm64 = "linux-arm64"
LinuxArm = "linux-arm"
OsxX64 = "osx-x64"
)
type CreateFlags struct {
Name *flag.Flag[string]
Fingerprint *flag.Flag[string]
HostName *flag.Flag[string]
Port *flag.Flag[int]
Account *flag.Flag[string]
Runtime *flag.Flag[string]
Platform *flag.Flag[string]
*machinescommon.CreateTargetProxyFlags
*shared.CreateTargetEnvironmentFlags
*shared.CreateTargetRoleFlags
*machinescommon.CreateTargetMachinePolicyFlags
*shared.CreateTargetTenantFlags
*machinescommon.WebFlags
}
type CreateOptions struct {
*CreateFlags
GetAllAccountsForSshTarget
*machinescommon.CreateTargetProxyOptions
*shared.CreateTargetEnvironmentOptions
*shared.CreateTargetRoleOptions
*machinescommon.CreateTargetMachinePolicyOptions
*shared.CreateTargetTenantOptions
*cmd.Dependencies
}
func NewCreateFlags() *CreateFlags {
return &CreateFlags{
Name: flag.New[string](FlagName, false),
Account: flag.New[string](FlagAccount, false),
Fingerprint: flag.New[string](FlagFingerprint, true),
HostName: flag.New[string](FlagHost, false),
Port: flag.New[int](FlagPort, false),
Runtime: flag.New[string](FlagRuntime, false),
Platform: flag.New[string](FlagPlatform, false),
CreateTargetRoleFlags: shared.NewCreateTargetRoleFlags(),
CreateTargetProxyFlags: machinescommon.NewCreateTargetProxyFlags(),
CreateTargetMachinePolicyFlags: machinescommon.NewCreateTargetMachinePolicyFlags(),
CreateTargetEnvironmentFlags: shared.NewCreateTargetEnvironmentFlags(),
CreateTargetTenantFlags: shared.NewCreateTargetTenantFlags(),
WebFlags: machinescommon.NewWebFlags(),
}
}
func NewCreateOptions(createFlags *CreateFlags, dependencies *cmd.Dependencies) *CreateOptions {
return &CreateOptions{
CreateFlags: createFlags,
Dependencies: dependencies,
CreateTargetRoleOptions: shared.NewCreateTargetRoleOptions(dependencies),
CreateTargetProxyOptions: machinescommon.NewCreateTargetProxyOptions(dependencies),
CreateTargetMachinePolicyOptions: machinescommon.NewCreateTargetMachinePolicyOptions(dependencies),
CreateTargetEnvironmentOptions: shared.NewCreateTargetEnvironmentOptions(dependencies),
CreateTargetTenantOptions: shared.NewCreateTargetTenantOptions(dependencies),
GetAllAccountsForSshTarget: func() ([]accounts.IAccount, error) {
return getAllAccountsForSshTarget(dependencies.Client)
},
}
}
func NewCmdCreate(f factory.Factory) *cobra.Command {
createFlags := NewCreateFlags()
cmd := &cobra.Command{
Use: "create",
Short: "Create a SSH deployment target",
Long: "Create a SSH deployment target in Octopus Deploy",
Example: heredoc.Docf("$ %s deployment-target ssh create", constants.ExecutableName),
Aliases: []string{"new"},
RunE: func(c *cobra.Command, _ []string) error {
opts := NewCreateOptions(createFlags, cmd.NewDependencies(f, c))
return createRun(opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&createFlags.Name.Value, createFlags.Name.Name, "n", "", "A short, memorable, unique name for this deployment target.")
flags.StringVar(&createFlags.Account.Value, createFlags.Account.Name, "", "The name or ID of the SSH key pair or username/password account")
flags.StringVar(&createFlags.HostName.Value, createFlags.HostName.Name, "", "The hostname or IP address of the deployment target to connect to.")
flags.StringVar(&createFlags.Fingerprint.Value, createFlags.Fingerprint.Name, "", "The host fingerprint of the deployment target.")
flags.IntVar(&createFlags.Port.Value, createFlags.Port.Name, 0, "The port to connect to the deployment target on.")
flags.StringVar(&createFlags.Runtime.Value, createFlags.Runtime.Name, "", fmt.Sprintf("The runtime to use to run Calamari on the deployment target. Options are '%s' or '%s'", SelfContainedCalamari, MonoCalamari))
flags.StringVar(&createFlags.Platform.Value, createFlags.Platform.Name, "", fmt.Sprintf("The platform to use for the %s Calamari. Options are '%s', '%s', '%s' or '%s'", SelfContainedCalamari, LinuxX64, LinuxArm64, LinuxArm, OsxX64))
shared.RegisterCreateTargetEnvironmentFlags(cmd, createFlags.CreateTargetEnvironmentFlags)
shared.RegisterCreateTargetRoleFlags(cmd, createFlags.CreateTargetRoleFlags)
machinescommon.RegisterCreateTargetProxyFlags(cmd, createFlags.CreateTargetProxyFlags)
machinescommon.RegisterCreateTargetMachinePolicyFlags(cmd, createFlags.CreateTargetMachinePolicyFlags)
shared.RegisterCreateTargetTenantFlags(cmd, createFlags.CreateTargetTenantFlags)
machinescommon.RegisterWebFlag(cmd, createFlags.WebFlags)
return cmd
}
func createRun(opts *CreateOptions) error {
if !opts.NoPrompt {
if err := PromptMissing(opts); err != nil {
return err
}
}
envs, err := executionscommon.FindEnvironments(opts.Client, opts.Environments.Value)
if err != nil {
return err
}
environmentIds := util.SliceTransform(envs, func(e *environments.Environment) string { return e.ID })
account, err := getAccount(opts)
if err != nil {
return err
}
port := opts.Port.Value
if port == 0 {
port = 22
}
endpoint := machines.NewSSHEndpoint(opts.HostName.Value, port, opts.Fingerprint.Value)
endpoint.AccountID = account.GetID()
if opts.Runtime.Value == SelfContainedCalamari {
endpoint.DotNetCorePlatform = opts.Platform.Value
}
if opts.Proxy.Value != "" {
proxy, err := machinescommon.FindProxy(opts.CreateTargetProxyOptions, opts.CreateTargetProxyFlags)
if err != nil {
return err
}
endpoint.ProxyID = proxy.GetID()
}
deploymentTarget := machines.NewDeploymentTarget(opts.Name.Value, endpoint, environmentIds, shared.DistinctRoles(opts.Roles.Value))
machinePolicy, err := machinescommon.FindMachinePolicy(opts.GetAllMachinePoliciesCallback, opts.MachinePolicy.Value)
if err != nil {
return err
}
deploymentTarget.MachinePolicyID = machinePolicy.GetID()
err = shared.ConfigureTenant(deploymentTarget, opts.CreateTargetTenantFlags, opts.CreateTargetTenantOptions)
if err != nil {
return err
}
createdTarget, err := opts.Client.Machines.Add(deploymentTarget)
if err != nil {
return err
}
fmt.Fprintf(opts.Out, "Successfully created SSH deployment target '%s'.\n", deploymentTarget.Name)
if !opts.NoPrompt {
autoCmd := flag.GenerateAutomationCmd(opts.CmdPath, opts.Name, opts.HostName, opts.Port, opts.Fingerprint, opts.Runtime, opts.Platform, opts.Environments, opts.Roles, opts.Account, opts.Proxy, opts.MachinePolicy, opts.TenantedDeploymentMode, opts.Tenants, opts.TenantTags)
fmt.Fprintf(opts.Out, "\nAutomation Command: %s\n", autoCmd)
}
machinescommon.DoWebForTargets(createdTarget, opts.Dependencies, opts.WebFlags, "ssh")
return nil
}
func PromptMissing(opts *CreateOptions) error {
err := question.AskName(opts.Ask, "", "SSH", &opts.Name.Value)
if err != nil {
return err
}
err = shared.PromptForEnvironments(opts.CreateTargetEnvironmentOptions, opts.CreateTargetEnvironmentFlags)
if err != nil {
return err
}
err = shared.PromptForRoles(opts.CreateTargetRoleOptions, opts.CreateTargetRoleFlags)
if err != nil {
return err
}
err = machinescommon.PromptForMachinePolicy(opts.CreateTargetMachinePolicyOptions, opts.CreateTargetMachinePolicyFlags)
if err != nil {
return err
}
err = PromptForAccount(opts)
if err != nil {
return err
}
err = PromptForEndpoint(opts)
if err != nil {
return err
}
err = machinescommon.PromptForProxy(opts.CreateTargetProxyOptions, opts.CreateTargetProxyFlags)
if err != nil {
return err
}
err = PromptForDotNetConfig(opts)
if err != nil {
return err
}
err = shared.PromptForTenant(opts.CreateTargetTenantOptions, opts.CreateTargetTenantFlags)
if err != nil {
return err
}
return nil
}
func PromptForEndpoint(opts *CreateOptions) error {
if opts.HostName.Value == "" {
if err := opts.Ask(&survey.Input{
Message: "Host",
Help: "The hostname or IP address at which the deployment target can be reached.",
}, &opts.HostName.Value, survey.WithValidator(survey.Required)); err != nil {
return err
}
}
if opts.Port.Value == 0 {
var port string
if err := opts.Ask(&survey.Input{
Message: "Port",
Help: "Port number to connect over SSH to the deployment target. Default is 22",
}, &port); err != nil {
return err
}
if port == "" {
port = "22"
}
if p, err := strconv.Atoi(port); err == nil {
opts.Port.Value = p
} else {
return err
}
}
if opts.Fingerprint.Value == "" {
if err := opts.Ask(&survey.Input{
Message: "Host fingerprint",
Help: "The host fingerprint of the SSH deployment target.",
}, &opts.Fingerprint.Value, survey.WithValidator(survey.Required)); err != nil {
return err
}
}
return nil
}
func PromptForDotNetConfig(opts *CreateOptions) error {
if opts.Runtime.Value == "" {
selectedRuntime, err := selectors.SelectOptions(opts.Ask, "Select the target runtime\n", getTargetRuntimeOptions)
if err != nil {
return err
}
opts.Runtime.Value = selectedRuntime.Value
}
if opts.Runtime.Value == SelfContainedCalamari {
if opts.Platform.Value == "" {
selectedPlatform, err := selectors.SelectOptions(opts.Ask, "Select the target platform\n", getTargetPlatformOptions)
if err != nil {
return err
}
opts.Platform.Value = selectedPlatform.Value
}
}
return nil
}
func PromptForAccount(opts *CreateOptions) error {
var account accounts.IAccount
if opts.Account.Value == "" {
selectedAccount, err := selectors.Select(
opts.Ask,
"Select the account to use\n",
opts.GetAllAccountsForSshTarget,
func(a accounts.IAccount) string {
return a.GetName()
})
if err != nil {
return err
}
account = selectedAccount
} else {
a, err := getAccount(opts)
if err != nil {
return err
}
account = a
}
opts.Account.Value = account.GetName()
return nil
}
func getAllAccountsForSshTarget(client *client.Client) ([]accounts.IAccount, error) {
allAccounts, err := client.Accounts.GetAll()
if err != nil {
return nil, err
}
var accounts []accounts.IAccount
for _, a := range allAccounts {
if canBeUsedForSSH(a) {
accounts = append(accounts, a)
}
}
return accounts, nil
}
func canBeUsedForSSH(account accounts.IAccount) bool {
accountType := account.GetAccountType()
return accountType == accounts.AccountTypeSSHKeyPair || accountType == accounts.AccountTypeUsernamePassword
}
func getAccount(opts *CreateOptions) (accounts.IAccount, error) {
idOrName := opts.Account.Value
allAccounts, err := opts.GetAllAccountsForSshTarget()
if err != nil {
return nil, err
}
for _, a := range allAccounts {
if strings.EqualFold(a.GetID(), idOrName) || strings.EqualFold(a.GetName(), idOrName) {
return a, nil
}
}
return nil, fmt.Errorf("cannot find account %s", idOrName)
}
func getTargetRuntimeOptions() []*selectors.SelectOption[string] {
return []*selectors.SelectOption[string]{
{Display: "Self-contained Calamari", Value: SelfContainedCalamari},
{Display: "Calamari on Mono", Value: MonoCalamari},
}
}
func getTargetPlatformOptions() []*selectors.SelectOption[string] {
return []*selectors.SelectOption[string]{
{Display: "Linux x64", Value: LinuxX64},
{Display: "Linux ARM64", Value: LinuxArm64},
{Display: "Linux ARM", Value: LinuxArm},
{Display: "OSX x64", Value: OsxX64},
}
}