1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.arvados.org/arvados.git/lib/cloud"
16 "git.arvados.org/arvados.git/lib/cmd"
17 "git.arvados.org/arvados.git/lib/config"
18 "git.arvados.org/arvados.git/lib/dispatchcloud"
19 "git.arvados.org/arvados.git/sdk/go/arvados"
20 "git.arvados.org/arvados.git/sdk/go/ctxlog"
21 "golang.org/x/crypto/ssh"
28 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
32 fmt.Fprintf(stderr, "%s\n", err)
36 flags := flag.NewFlagSet("", flag.ContinueOnError)
37 flags.SetOutput(stderr)
38 configFile := flags.String("config", arvados.DefaultConfigFile, "Site configuration `file`")
39 instanceSetID := flags.String("instance-set-id", "zzzzz-zzzzz-zzzzzzcloudtest", "InstanceSetID tag `value` to use on the test instance")
40 imageID := flags.String("image-id", "", "Image ID to use when creating the test instance (if empty, use cluster config)")
41 instanceType := flags.String("instance-type", "", "Instance type to create (if empty, use cheapest type in config)")
42 destroyExisting := flags.Bool("destroy-existing", false, "Destroy any existing instances tagged with our InstanceSetID, instead of erroring out")
43 shellCommand := flags.String("command", "", "Run an interactive shell command on the test instance when it boots")
44 pauseBeforeDestroy := flags.Bool("pause-before-destroy", false, "Prompt and wait before destroying the test instance")
45 if ok, code := cmd.ParseFlags(flags, prog, args, "", stderr); !ok {
48 logger := ctxlog.New(stderr, "text", "info")
51 logger.WithError(err).Error("fatal")
52 // suppress output from the other error-printing func
55 logger.Info("exiting")
58 loader := config.NewLoader(stdin, logger)
59 loader.Path = *configFile
60 cfg, err := loader.Load()
64 cluster, err := cfg.GetCluster("")
68 key, err := ssh.ParsePrivateKey([]byte(cluster.Containers.DispatchPrivateKey))
70 err = fmt.Errorf("error parsing configured Containers.DispatchPrivateKey: %s", err)
73 driver, ok := dispatchcloud.Drivers[cluster.Containers.CloudVMs.Driver]
75 err = fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
79 *imageID = cluster.Containers.CloudVMs.ImageID
81 it, err := chooseInstanceType(cluster, *instanceType)
85 tags := cloud.SharedResourceTags(cluster.Containers.CloudVMs.ResourceTags)
86 tagKeyPrefix := cluster.Containers.CloudVMs.TagKeyPrefix
87 tags[tagKeyPrefix+"CloudTestPID"] = fmt.Sprintf("%d", os.Getpid())
91 TagKeyPrefix: tagKeyPrefix,
92 SetID: cloud.InstanceSetID(*instanceSetID),
93 DestroyExisting: *destroyExisting,
94 ProbeInterval: cluster.Containers.CloudVMs.ProbeInterval.Duration(),
95 SyncInterval: cluster.Containers.CloudVMs.SyncInterval.Duration(),
96 TimeoutBooting: cluster.Containers.CloudVMs.TimeoutBooting.Duration(),
98 DriverParameters: cluster.Containers.CloudVMs.DriverParameters,
99 ImageID: cloud.ImageID(*imageID),
102 SSHPort: cluster.Containers.CloudVMs.SSHPort,
103 BootProbeCommand: cluster.Containers.CloudVMs.BootProbeCommand,
104 ShellCommand: *shellCommand,
105 PauseBeforeDestroy: func() {
106 if *pauseBeforeDestroy {
107 logger.Info("waiting for operator to press Enter")
108 fmt.Fprint(stderr, "Press Enter to continue: ")
109 bufio.NewReader(stdin).ReadString('\n')
118 // Return the named instance type, or the cheapest type if name=="".
119 func chooseInstanceType(cluster *arvados.Cluster, name string) (arvados.InstanceType, error) {
120 if len(cluster.InstanceTypes) == 0 {
121 return arvados.InstanceType{}, errors.New("no instance types are configured")
122 } else if name == "" {
124 var best arvados.InstanceType
125 for _, it := range cluster.InstanceTypes {
126 if first || best.Price > it.Price {
132 } else if it, ok := cluster.InstanceTypes[name]; !ok {
133 return it, fmt.Errorf("requested instance type %q is not configured", name)