Skip to content

Commit b1429dc

Browse files
committed
Refactored 'run' command (scaleway#80)
1 parent f11348e commit b1429dc

File tree

2 files changed

+125
-69
lines changed

2 files changed

+125
-69
lines changed

pkg/cli/run.go

Lines changed: 22 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
package cli
66

77
import (
8-
"fmt"
9-
"os"
8+
"strings"
109

10+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1111
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1212

13-
api "github.com/scaleway/scaleway-cli/pkg/api"
14-
"github.com/scaleway/scaleway-cli/pkg/utils"
13+
"github.com/scaleway/scaleway-cli/pkg/commands"
1514
)
1615

1716
var cmdRun = &Command{
@@ -52,85 +51,39 @@ var runAttachFlag bool // -a, --attach flag
5251
var runDetachFlag bool // -d, --detach flag
5352
var runGateway string // -g, --gateway flag
5453

55-
func runRun(cmd *Command, args []string) {
54+
func runRun(cmd *Command, rawArgs []string) {
5655
if runHelpFlag {
5756
cmd.PrintUsage()
5857
}
59-
if len(args) < 1 {
58+
if len(rawArgs) < 1 {
6059
cmd.PrintShortUsage()
6160
}
62-
if runAttachFlag && len(args) > 1 {
61+
if runAttachFlag && len(rawArgs) > 1 {
6362
log.Fatalf("Conflicting options: -a and COMMAND")
6463
}
6564
if runAttachFlag && runDetachFlag {
6665
log.Fatalf("Conflicting options: -a and -d")
6766
}
68-
if runDetachFlag && len(args) > 1 {
67+
if runDetachFlag && len(rawArgs) > 1 {
6968
log.Fatalf("Conflicting options: -d and COMMAND")
7069
}
7170

72-
if runGateway == "" {
73-
runGateway = os.Getenv("SCW_GATEWAY")
71+
args := commands.RunArgs{
72+
Attach: runAttachFlag,
73+
Bootscript: runCreateBootscript,
74+
Command: rawArgs[1:],
75+
Detach: runDetachFlag,
76+
Gateway: runGateway,
77+
Image: rawArgs[0],
78+
Name: runCreateName,
79+
Tags: strings.Split(runCreateEnv, " "),
80+
Volumes: strings.Split(runCreateVolume, " "),
81+
// FIXME: DynamicIPRequired
82+
// FIXME: Timeout
7483
}
75-
76-
// create IMAGE
77-
log.Info("Server creation ...")
78-
dynamicIPRequired := runGateway == ""
79-
serverID, err := api.CreateServer(cmd.API, args[0], runCreateName, runCreateBootscript, runCreateEnv, runCreateVolume, dynamicIPRequired)
84+
ctx := cmd.GetContext(rawArgs)
85+
err := commands.RunRun(ctx, args)
8086
if err != nil {
81-
log.Fatalf("Failed to create server: %v", err)
82-
}
83-
log.Infof("Server created: %s", serverID)
84-
85-
// start SERVER
86-
log.Info("Server start requested ...")
87-
err = api.StartServer(cmd.API, serverID, false)
88-
if err != nil {
89-
log.Fatalf("Failed to start server %s: %v", serverID, err)
90-
}
91-
log.Info("Server is starting, this may take up to a minute ...")
92-
93-
if runDetachFlag {
94-
fmt.Println(serverID)
95-
return
96-
}
97-
98-
if runAttachFlag {
99-
// Attach to server serial
100-
log.Info("Attaching to server console ...")
101-
err = utils.AttachToSerial(serverID, cmd.API.Token, true)
102-
if err != nil {
103-
log.Fatalf("Cannot attach to server serial: %v", err)
104-
}
105-
} else {
106-
// Resolve gateway
107-
gateway, err := api.ResolveGateway(cmd.API, runGateway)
108-
if err != nil {
109-
log.Fatalf("Cannot resolve Gateway '%s': %v", runGateway, err)
110-
}
111-
112-
// waiting for server to be ready
113-
log.Debug("Waiting for server to be ready")
114-
// We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
115-
server, err := api.WaitForServerReady(cmd.API, serverID, gateway)
116-
if err != nil {
117-
log.Fatalf("Cannot get access to server %s: %v", serverID, err)
118-
}
119-
log.Debugf("SSH server is available: %s:22", server.PublicAddress.IP)
120-
log.Info("Server is ready !")
121-
122-
// exec -w SERVER COMMAND ARGS...
123-
if len(args) < 2 {
124-
log.Info("Connecting to server ...")
125-
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway)
126-
} else {
127-
log.Infof("Executing command: %s ...", args[1:])
128-
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, gateway)
129-
}
130-
if err != nil {
131-
log.Infof("Command execution failed: %v", err)
132-
os.Exit(1)
133-
}
134-
log.Info("Command successfuly executed")
87+
logrus.Fatalf("Cannot execute 'run': %v", err)
13588
}
13689
}

pkg/commands/run.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"fmt"
9+
"os"
10+
"strings"
11+
12+
"github.com/scaleway/scaleway-cli/pkg/api"
13+
"github.com/scaleway/scaleway-cli/pkg/utils"
14+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
15+
)
16+
17+
// RunArgs are flags for the `RunRun` function
18+
type RunArgs struct {
19+
Attach bool
20+
Bootscript string
21+
Command []string
22+
Detach bool
23+
Gateway string
24+
Image string
25+
Name string
26+
Tags []string
27+
Volumes []string
28+
// DynamicIPRequired
29+
// Timeout
30+
}
31+
32+
// RunRun is the handler for 'scw run'
33+
func RunRun(ctx CommandContext, args RunArgs) error {
34+
if args.Gateway == "" {
35+
args.Gateway = ctx.Getenv("SCW_GATEWAY")
36+
}
37+
38+
env := strings.Join(args.Tags, " ")
39+
volume := strings.Join(args.Volumes, " ")
40+
41+
// create IMAGE
42+
logrus.Info("Server creation ...")
43+
dynamicIPRequired := args.Gateway == ""
44+
serverID, err := api.CreateServer(ctx.API, args.Image, args.Name, args.Bootscript, env, volume, dynamicIPRequired)
45+
if err != nil {
46+
return fmt.Errorf("failed to create server: %v", err)
47+
}
48+
logrus.Infof("Server created: %s", serverID)
49+
50+
// start SERVER
51+
logrus.Info("Server start requested ...")
52+
err = api.StartServer(ctx.API, serverID, false)
53+
if err != nil {
54+
return fmt.Errorf("failed to start server %s: %v", serverID, err)
55+
}
56+
logrus.Info("Server is starting, this may take up to a minute ...")
57+
58+
if args.Detach {
59+
fmt.Fprintln(ctx.Stdout, serverID)
60+
return nil
61+
}
62+
63+
if args.Attach {
64+
// Attach to server serial
65+
logrus.Info("Attaching to server console ...")
66+
err = utils.AttachToSerial(serverID, ctx.API.Token, true)
67+
if err != nil {
68+
return fmt.Errorf("cannot attach to server serial: %v", err)
69+
}
70+
} else {
71+
// Resolve gateway
72+
gateway, err := api.ResolveGateway(ctx.API, args.Gateway)
73+
if err != nil {
74+
return fmt.Errorf("cannot resolve Gateway '%s': %v", args.Gateway, err)
75+
}
76+
77+
// waiting for server to be ready
78+
logrus.Debug("Waiting for server to be ready")
79+
// We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
80+
server, err := api.WaitForServerReady(ctx.API, serverID, gateway)
81+
if err != nil {
82+
return fmt.Errorf("cannot get access to server %s: %v", serverID, err)
83+
}
84+
logrus.Debugf("SSH server is available: %s:22", server.PublicAddress.IP)
85+
logrus.Info("Server is ready !")
86+
87+
// exec -w SERVER COMMAND ARGS...
88+
if len(args.Command) < 1 {
89+
logrus.Info("Connecting to server ...")
90+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway)
91+
} else {
92+
logrus.Infof("Executing command: %s ...", args.Command)
93+
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args.Command, false, gateway)
94+
}
95+
if err != nil {
96+
logrus.Infof("Command execution failed: %v", err)
97+
// FIXME: create a new error type for silent exiting
98+
os.Exit(1)
99+
}
100+
logrus.Info("Command successfuly executed")
101+
}
102+
return nil
103+
}

0 commit comments

Comments
 (0)