// RunCreate is the handler for 'scw create' func RunCreate(ctx CommandContext, args CreateArgs) error { if args.TmpSSHKey { err := AddSSHKeyToTags(ctx, &args.Tags, args.Image) if err != nil { return err } } env := strings.Join(args.Tags, " ") volume := strings.Join(args.Volumes, " ") serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{ ImageName: args.Image, Name: args.Name, Bootscript: args.Bootscript, Env: env, AdditionalVolumes: volume, DynamicIPRequired: args.IP == "", IP: args.IP, }) if err != nil { return err } fmt.Fprintln(ctx.Stdout, serverID) return nil }
// RunCreate is the handler for 'scw create' func RunCreate(ctx CommandContext, args CreateArgs) error { if args.TmpSSHKey { err := AddSSHKeyToTags(ctx, &args.Tags, args.Image) if err != nil { return err } } env := strings.Join(args.Tags, " ") volume := strings.Join(args.Volumes, " ") config := api.ConfigCreateServer{ ImageName: args.Image, Name: args.Name, Bootscript: args.Bootscript, Env: env, AdditionalVolumes: volume, DynamicIPRequired: false, IP: args.IP, CommercialType: args.CommercialType, } if args.IP == "dynamic" || args.IP == "" { config.DynamicIPRequired = true config.IP = "" } else if args.IP == "none" || args.IP == "no" { config.IP = "" } serverID, err := api.CreateServer(ctx.API, &config) if err != nil { return err } fmt.Fprintln(ctx.Stdout, serverID) return nil }
// RunCreate is the handler for 'scw create' func RunCreate(ctx CommandContext, args CreateArgs) error { if args.TmpSSHKey { err := AddSSHKeyToTags(ctx, &args.Tags, args.Image) if err != nil { return err } } env := strings.Join(args.Tags, " ") volume := strings.Join(args.Volumes, " ") serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, true) if err != nil { return err } fmt.Fprintln(ctx.Stdout, serverID) return nil }
// Run is the handler for 'scw run' func Run(ctx CommandContext, args RunArgs) error { if args.Gateway == "" { args.Gateway = ctx.Getenv("SCW_GATEWAY") } if args.TmpSSHKey { err := AddSSHKeyToTags(ctx, &args.Tags, args.Image) if err != nil { return err } } env := strings.Join(args.Tags, " ") volume := strings.Join(args.Volumes, " ") // create IMAGE logrus.Info("Server creation ...") serverID, err := api.CreateServer(ctx.API, &api.ConfigCreateServer{ ImageName: args.Image, Name: args.Name, Bootscript: args.Bootscript, Env: env, AdditionalVolumes: volume, DynamicIPRequired: args.Gateway == "", IP: args.IP, }) if err != nil { return fmt.Errorf("failed to create server: %v", err) } logrus.Infof("Server created: %s", serverID) if args.AutoRemove { defer ctx.API.DeleteServerSafe(serverID) } // start SERVER logrus.Info("Server start requested ...") if err = api.StartServer(ctx.API, serverID, false); err != nil { return fmt.Errorf("failed to start server %s: %v", serverID, err) } logrus.Info("Server is starting, this may take up to a minute ...") if args.Userdata != "" { addUserData(ctx, strings.Split(args.Userdata, " "), serverID) } // Sync cache on disk ctx.API.Sync() if args.Detach { fmt.Fprintln(ctx.Stdout, serverID) return nil } closeTimeout := make(chan struct{}) timeoutExit := make(chan struct{}) if args.Timeout > 0 { go func() { select { case <-time.After(time.Duration(args.Timeout) * time.Second): close(timeoutExit) case <-closeTimeout: break } }() } if args.ShowBoot { return runShowBoot(ctx, args, serverID, closeTimeout, timeoutExit) } else if args.Attach { // Attach to server serial logrus.Info("Attaching to server console ...") gottycli, done, err := utils.AttachToSerial(serverID, ctx.API.Token) close(closeTimeout) if err != nil { return fmt.Errorf("cannot attach to server serial: %v", err) } <-done gottycli.Close() } else { notif, gateway, err := waitSSHConnection(ctx, args, serverID) if err != nil { close(closeTimeout) return err } select { case <-timeoutExit: return fmt.Errorf("Operation timed out") case sshConnection := <-notif: close(closeTimeout) if sshConnection.err != nil { return sshConnection.err } if fingerprints := ctx.API.GetSSHFingerprintFromServer(serverID); len(fingerprints) > 0 { for i := range fingerprints { fmt.Fprintf(ctx.Stdout, "%s\n", fingerprints[i]) } } server := sshConnection.server // exec -w SERVER COMMAND ARGS... if len(args.Command) < 1 { logrus.Info("Connecting to server ...") if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway); err != nil { return fmt.Errorf("Connection to server failed: %v", err) } } else { logrus.Infof("Executing command: %s ...", args.Command) if err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, false, gateway); err != nil { return fmt.Errorf("command execution failed: %v", err) } logrus.Info("Command successfuly executed") } } } return nil }
// Run is the handler for 'scw run' func Run(ctx CommandContext, args RunArgs) error { if args.Gateway == "" { args.Gateway = ctx.Getenv("SCW_GATEWAY") } if args.TmpSSHKey { err := AddSSHKeyToTags(ctx, &args.Tags, args.Image) if err != nil { return err } } env := strings.Join(args.Tags, " ") volume := strings.Join(args.Volumes, " ") // create IMAGE logrus.Info("Server creation ...") dynamicIPRequired := args.Gateway == "" serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, dynamicIPRequired) if err != nil { return fmt.Errorf("failed to create server: %v", err) } logrus.Infof("Server created: %s", serverID) if args.AutoRemove { defer ctx.API.DeleteServerSafe(serverID) } // start SERVER logrus.Info("Server start requested ...") err = api.StartServer(ctx.API, serverID, false) if err != nil { return fmt.Errorf("failed to start server %s: %v", serverID, err) } logrus.Info("Server is starting, this may take up to a minute ...") if args.Detach { fmt.Fprintln(ctx.Stdout, serverID) return nil } else { // Sync cache on disk ctx.API.Sync() } if args.Attach { // Attach to server serial logrus.Info("Attaching to server console ...") err = utils.AttachToSerial(serverID, ctx.API.Token, true) if err != nil { return fmt.Errorf("cannot attach to server serial: %v", err) } } else { // Resolve gateway gateway, err := api.ResolveGateway(ctx.API, args.Gateway) if err != nil { return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err) } // waiting for server to be ready logrus.Debug("Waiting for server to be ready") // We wait for 30 seconds, which is the minimal amount of time needed by a server to boot server, err := api.WaitForServerReady(ctx.API, serverID, gateway) if err != nil { return fmt.Errorf("cannot get access to server %s: %v", serverID, err) } logrus.Debugf("SSH server is available: %s:22", server.PublicAddress.IP) logrus.Info("Server is ready !") // exec -w SERVER COMMAND ARGS... if len(args.Command) < 1 { logrus.Info("Connecting to server ...") err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway) } else { logrus.Infof("Executing command: %s ...", args.Command) err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, false, gateway) } if err != nil { return fmt.Errorf("command execution failed: %v", err) } logrus.Info("Command successfuly executed") } return nil }