Skip to content

Commit 5efa123

Browse files
committed
Merge pull request scaleway#84 from moul/feature-enhanced-logging
Refactored logging for 'scw run' (Fix scaleway#83)
2 parents be0bb74 + c23b2b2 commit 5efa123

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ Options:
124124
--api-endpoint=APIEndPoint Set the API endpoint
125125
-D, --debug=false Enable debug mode
126126
-h, --help=false Print usage
127+
-V, --verbose=false Enable verbose mode
127128
-v, --version=false Print version information and quit
128129

129130
Commands:
@@ -1039,6 +1040,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
10391040

10401041
#### Features
10411042

1043+
* Support of `scw -V/--verbose` option ([#83](https://github.com/scaleway/scaleway-cli/issues/83))
10421044
* Support of `scw inspect --browser` option
10431045
* Support of `scw _flush-cache` internal command
10441046
* `scw run --gateway ...` or `SCW_GATEWAY="..." scw run ...` now creates a server without public ip address ([#74](https://github.com/scaleway/scaleway-cli/issues/74))

api/helpers.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func GetIdentifier(api *ScalewayAPI, needle string) *ScalewayResolverResult {
139139
log.Errorf("Too many candidates for %s (%d)", needle, len(idents))
140140
for _, identifier := range idents {
141141
// FIXME: also print the name
142-
log.Infof("- %s", identifier.Identifier)
142+
fmt.Fprint(os.Stderr, "- %s\n", identifier.Identifier)
143143
}
144144
os.Exit(1)
145145
return nil
@@ -380,8 +380,14 @@ func WaitForServerState(api *ScalewayAPI, serverID string, targetState string) (
380380
var server *ScalewayServer
381381
var err error
382382

383+
var currentState string
384+
383385
for {
384386
server, err = api.GetServer(serverID)
387+
if currentState != server.State {
388+
log.Infof("Server changed state to '%s'", server.State)
389+
currentState = server.State
390+
}
385391
if err != nil {
386392
return nil, err
387393
}

commands/run.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package commands
77
import (
88
"fmt"
99
"os"
10-
"time"
1110

1211
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1312

@@ -76,21 +75,21 @@ func runRun(cmd *types.Command, args []string) {
7675
}
7776

7877
// create IMAGE
79-
log.Debugf("Creating a new server")
78+
log.Info("Server creation ...")
8079
dynamicIPRequired := runGateway == ""
8180
serverID, err := api.CreateServer(cmd.API, args[0], runCreateName, runCreateBootscript, runCreateEnv, runCreateVolume, dynamicIPRequired)
8281
if err != nil {
8382
log.Fatalf("Failed to create server: %v", err)
8483
}
85-
log.Debugf("Created server: %s", serverID)
84+
log.Infof("Server created: %s", serverID)
8685

8786
// start SERVER
88-
log.Debugf("Starting server")
87+
log.Info("Server start requested ...")
8988
err = api.StartServer(cmd.API, serverID, false)
9089
if err != nil {
9190
log.Fatalf("Failed to start server %s: %v", serverID, err)
9291
}
93-
log.Debugf("Server is booting")
92+
log.Info("Server is starting, this may take up to a minute ...")
9493

9594
if runDetachFlag {
9695
fmt.Println(serverID)
@@ -99,7 +98,7 @@ func runRun(cmd *types.Command, args []string) {
9998

10099
if runAttachFlag {
101100
// Attach to server serial
102-
log.Debugf("Attaching to server console")
101+
log.Info("Attaching to server console ...")
103102
err = utils.AttachToSerial(serverID, cmd.API.Token, true)
104103
if err != nil {
105104
log.Fatalf("Cannot attach to server serial: %v", err)
@@ -112,26 +111,27 @@ func runRun(cmd *types.Command, args []string) {
112111
}
113112

114113
// waiting for server to be ready
115-
log.Debugf("Waiting for server to be ready")
114+
log.Debug("Waiting for server to be ready")
116115
// We wait for 30 seconds, which is the minimal amount of time needed by a server to boot
117-
time.Sleep(30 * time.Second)
118116
server, err := api.WaitForServerReady(cmd.API, serverID, gateway)
119117
if err != nil {
120118
log.Fatalf("Cannot get access to server %s: %v", serverID, err)
121119
}
122-
log.Debugf("Server is ready: %s", server.PublicAddress.IP)
120+
log.Debugf("SSH server is available: %s:22", server.PublicAddress.IP)
121+
log.Info("Server is ready !")
123122

124123
// exec -w SERVER COMMAND ARGS...
125-
log.Debugf("Executing command")
126124
if len(args) < 2 {
125+
log.Info("Connecting to server ...")
127126
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, []string{}, false, gateway)
128127
} else {
128+
log.Infof("Executing command: %s ...", args[1:])
129129
err = utils.SSHExec(server.PublicAddress.IP, server.PrivateIP, args[1:], false, gateway)
130130
}
131131
if err != nil {
132-
log.Debugf("Command execution failed: %v", err)
132+
log.Infof("Command execution failed: %v", err)
133133
os.Exit(1)
134134
}
135-
log.Debugf("Command successfuly executed")
135+
log.Info("Command successfuly executed")
136136
}
137137
}

main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func commandUsage(name string) {
5050
var (
5151
flAPIEndPoint *string
5252
flDebug = flag.Bool([]string{"D", "-debug"}, false, "Enable debug mode")
53+
flVerbose = flag.Bool([]string{"V", "-verbose"}, false, "Enable verbose mode")
5354
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
5455
flSensitive = flag.Bool([]string{"-sensitive"}, false, "Show sensitive data in outputs, i.e. API Token/Organization")
5556
)
@@ -82,7 +83,7 @@ func main() {
8283
os.Setenv("DEBUG", "1")
8384
}
8485

85-
initLogging(os.Getenv("DEBUG") != "")
86+
initLogging(os.Getenv("DEBUG") != "", *flVerbose)
8687

8788
args := flag.Args()
8889
if len(args) < 1 {
@@ -173,11 +174,13 @@ func showVersion() {
173174
fmt.Printf("scw version %s, build %s\n", scwversion.VERSION, scwversion.GITCOMMIT)
174175
}
175176

176-
func initLogging(debug bool) {
177+
func initLogging(debug bool, verbose bool) {
177178
log.SetOutput(os.Stderr)
178179
if debug {
179180
log.SetLevel(log.DebugLevel)
180-
} else {
181+
} else if verbose {
181182
log.SetLevel(log.InfoLevel)
183+
} else {
184+
log.SetLevel(log.WarnLevel)
182185
}
183186
}

0 commit comments

Comments
 (0)