15954: Remove unused cmdArgs.
[arvados.git] / lib / cloud / cloudtest / cmd.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package cloudtest
6
7 import (
8         "bufio"
9         "errors"
10         "flag"
11         "fmt"
12         "io"
13         "os"
14
15         "git.arvados.org/arvados.git/lib/cloud"
16         "git.arvados.org/arvados.git/lib/config"
17         "git.arvados.org/arvados.git/lib/dispatchcloud"
18         "git.arvados.org/arvados.git/sdk/go/arvados"
19         "git.arvados.org/arvados.git/sdk/go/ctxlog"
20         "golang.org/x/crypto/ssh"
21 )
22
23 var Command command
24
25 type command struct{}
26
27 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
28         var err error
29         defer func() {
30                 if err != nil {
31                         fmt.Fprintf(stderr, "%s\n", err)
32                 }
33         }()
34
35         flags := flag.NewFlagSet("", flag.ContinueOnError)
36         flags.SetOutput(stderr)
37         configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
38         instanceSetID := flags.String("instance-set-id", "zzzzz-zzzzz-zzzzzzcloudtest", "InstanceSetID tag `value` to use on the test instance")
39         imageID := flags.String("image-id", "", "Image ID to use when creating the test instance (if empty, use cluster config)")
40         instanceType := flags.String("instance-type", "", "Instance type to create (if empty, use cheapest type in config)")
41         destroyExisting := flags.Bool("destroy-existing", false, "Destroy any existing instances tagged with our InstanceSetID, instead of erroring out")
42         shellCommand := flags.String("command", "", "Run an interactive shell command on the test instance when it boots")
43         pauseBeforeDestroy := flags.Bool("pause-before-destroy", false, "Prompt and wait before destroying the test instance")
44         err = flags.Parse(args)
45         if err == flag.ErrHelp {
46                 err = nil
47                 return 0
48         } else if err != nil {
49                 return 2
50         }
51
52         if len(flags.Args()) != 0 {
53                 flags.Usage()
54                 return 2
55         }
56         logger := ctxlog.New(stderr, "text", "info")
57         defer func() {
58                 if err != nil {
59                         logger.WithError(err).Error("fatal")
60                         // suppress output from the other error-printing func
61                         err = nil
62                 }
63                 logger.Info("exiting")
64         }()
65
66         loader := config.NewLoader(stdin, logger)
67         loader.Path = *configFile
68         cfg, err := loader.Load()
69         if err != nil {
70                 return 1
71         }
72         cluster, err := cfg.GetCluster("")
73         if err != nil {
74                 return 1
75         }
76         key, err := ssh.ParsePrivateKey([]byte(cluster.Containers.DispatchPrivateKey))
77         if err != nil {
78                 err = fmt.Errorf("error parsing configured Containers.DispatchPrivateKey: %s", err)
79                 return 1
80         }
81         driver, ok := dispatchcloud.Drivers[cluster.Containers.CloudVMs.Driver]
82         if !ok {
83                 err = fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
84                 return 1
85         }
86         if *imageID == "" {
87                 *imageID = cluster.Containers.CloudVMs.ImageID
88         }
89         it, err := chooseInstanceType(cluster, *instanceType)
90         if err != nil {
91                 return 1
92         }
93         tags := cloud.SharedResourceTags(cluster.Containers.CloudVMs.ResourceTags)
94         tagKeyPrefix := cluster.Containers.CloudVMs.TagKeyPrefix
95         tags[tagKeyPrefix+"CloudTestPID"] = fmt.Sprintf("%d", os.Getpid())
96         if !(&tester{
97                 Logger:           logger,
98                 Tags:             tags,
99                 TagKeyPrefix:     tagKeyPrefix,
100                 SetID:            cloud.InstanceSetID(*instanceSetID),
101                 DestroyExisting:  *destroyExisting,
102                 ProbeInterval:    cluster.Containers.CloudVMs.ProbeInterval.Duration(),
103                 SyncInterval:     cluster.Containers.CloudVMs.SyncInterval.Duration(),
104                 TimeoutBooting:   cluster.Containers.CloudVMs.TimeoutBooting.Duration(),
105                 Driver:           driver,
106                 DriverParameters: cluster.Containers.CloudVMs.DriverParameters,
107                 ImageID:          cloud.ImageID(*imageID),
108                 InstanceType:     it,
109                 SSHKey:           key,
110                 SSHPort:          cluster.Containers.CloudVMs.SSHPort,
111                 BootProbeCommand: cluster.Containers.CloudVMs.BootProbeCommand,
112                 ShellCommand:     *shellCommand,
113                 PauseBeforeDestroy: func() {
114                         if *pauseBeforeDestroy {
115                                 logger.Info("waiting for operator to press Enter")
116                                 fmt.Fprint(stderr, "Press Enter to continue: ")
117                                 bufio.NewReader(stdin).ReadString('\n')
118                         }
119                 },
120         }).Run() {
121                 return 1
122         }
123         return 0
124 }
125
126 // Return the named instance type, or the cheapest type if name=="".
127 func chooseInstanceType(cluster *arvados.Cluster, name string) (arvados.InstanceType, error) {
128         if len(cluster.InstanceTypes) == 0 {
129                 return arvados.InstanceType{}, errors.New("no instance types are configured")
130         } else if name == "" {
131                 first := true
132                 var best arvados.InstanceType
133                 for _, it := range cluster.InstanceTypes {
134                         if first || best.Price > it.Price {
135                                 best = it
136                                 first = false
137                         }
138                 }
139                 return best, nil
140         } else if it, ok := cluster.InstanceTypes[name]; !ok {
141                 return it, fmt.Errorf("requested instance type %q is not configured", name)
142         } else {
143                 return it, nil
144         }
145 }