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/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"
27 func (command) RunCommand(prog string, args []string, stdin io.Reader, stdout, stderr io.Writer) int {
31 fmt.Fprintf(stderr, "%s\n", err)
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 {
48 } else if err != nil {
52 if len(flags.Args()) != 0 {
56 logger := ctxlog.New(stderr, "text", "info")
59 logger.WithError(err).Error("fatal")
60 // suppress output from the other error-printing func
63 logger.Info("exiting")
66 loader := config.NewLoader(stdin, logger)
67 loader.Path = *configFile
68 cfg, err := loader.Load()
72 cluster, err := cfg.GetCluster("")
76 key, err := ssh.ParsePrivateKey([]byte(cluster.Containers.DispatchPrivateKey))
78 err = fmt.Errorf("error parsing configured Containers.DispatchPrivateKey: %s", err)
81 driver, ok := dispatchcloud.Drivers[cluster.Containers.CloudVMs.Driver]
83 err = fmt.Errorf("unsupported cloud driver %q", cluster.Containers.CloudVMs.Driver)
87 *imageID = cluster.Containers.CloudVMs.ImageID
89 it, err := chooseInstanceType(cluster, *instanceType)
93 tags := cloud.SharedResourceTags(cluster.Containers.CloudVMs.ResourceTags)
94 tagKeyPrefix := cluster.Containers.CloudVMs.TagKeyPrefix
95 tags[tagKeyPrefix+"CloudTestPID"] = fmt.Sprintf("%d", os.Getpid())
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(),
106 DriverParameters: cluster.Containers.CloudVMs.DriverParameters,
107 ImageID: cloud.ImageID(*imageID),
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')
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 == "" {
132 var best arvados.InstanceType
133 for _, it := range cluster.InstanceTypes {
134 if first || best.Price > it.Price {
140 } else if it, ok := cluster.InstanceTypes[name]; !ok {
141 return it, fmt.Errorf("requested instance type %q is not configured", name)