Skip to content

Commit

Permalink
Added support of -w flag in 'scw restart' (Fix #185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastien Rannou committed Sep 18, 2015
1 parent 394822a commit 6eaf2a4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,8 @@ Restart a running server.
Options:

-h, --help=false Print usage
-T, --timeout=0 Set timeout value to seconds
-w, --wait=false Synchronous restart. Wait for SSH to be ready
```


Expand Down Expand Up @@ -1130,6 +1132,8 @@ $ scw inspect myserver | jq '.[0].public_ip.address'
### master (unreleased)

* Support of `scw _userdata name VAR=@/path/to/file` ([#183](https://github.com/scaleway/scaleway-cli/issues/183))
* Support of `scw restart -w` ([#185](https://github.com/scaleway/scaleway-cli/issues/185))
* Restarting multiple servers in parallel ([#185](https://github.com/scaleway/scaleway-cli/issues/185))

View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.5.0...master)

Expand Down
8 changes: 7 additions & 1 deletion pkg/cli/cmd_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ var cmdRestart = &Command{
}

func init() {
cmdRestart.Flag.BoolVar(&restartW, []string{"w", "-wait"}, false, "Synchronous restart. Wait for SSH to be ready")
cmdRestart.Flag.Float64Var(&restartTimeout, []string{"T", "-timeout"}, 0, "Set timeout values to seconds")
cmdRestart.Flag.BoolVar(&restartHelp, []string{"h", "-help"}, false, "Print usage")
}

// Flags
var restartHelp bool // -h, --help flag
var restartW bool // -w flag
var restartTimeout float64 // -T flag
var restartHelp bool // -h, --help flag

func runRestart(cmd *Command, rawArgs []string) error {
if restartHelp {
Expand All @@ -29,6 +33,8 @@ func runRestart(cmd *Command, rawArgs []string) error {
}

args := commands.RestartArgs{
Timeout: restartTimeout,
Wait: restartW,
Servers: rawArgs,
}
ctx := cmd.GetContext(rawArgs)
Expand Down
70 changes: 59 additions & 11 deletions pkg/commands/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,80 @@ package commands

import (
"fmt"
"sync"
"time"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
)

// RestartArgs are flags for the `RunRestart` function
type RestartArgs struct {
Wait bool
Timeout float64
Servers []string
}

// restartIdentifiers resolves server IDs, restarts, and waits for them to be ready (-w)
func restartIdentifiers(ctx CommandContext, wait bool, servers []string, cr chan string) {
var wg sync.WaitGroup
for _, needle := range servers {
wg.Add(1)
go func(needle string) {
defer wg.Done()
server := ctx.API.GetServerID(needle)
res := server
err := ctx.API.PostServerAction(server, "reboot")
if err != nil {
if err.Error() != "server is being stopped or rebooted" {
logrus.Errorf("failed to restart server %s: %s", server, err)
}
res = ""
} else {
if wait {
// FIXME: handle gateway
api.WaitForServerReady(ctx.API, server, "")
}
}
cr <- res
}(needle)
}
wg.Wait()
close(cr)
}

// RunRestart is the handler for 'scw restart'
func RunRestart(ctx CommandContext, args RestartArgs) error {
if args.Wait && args.Timeout > 0 {
go func() {
time.Sleep(time.Duration(args.Timeout*1000) * time.Millisecond)
// FIXME: avoid use of fatalf
logrus.Fatalf("Operation timed out")
}()
}

cr := make(chan string)
go restartIdentifiers(ctx, args.Wait, args.Servers, cr)
done := false
hasError := false
for _, needle := range args.Servers {
server := ctx.API.GetServerID(needle)
err := ctx.API.PostServerAction(server, "reboot")
if err != nil {
if err.Error() != "server is being stopped or rebooted" {
logrus.Errorf("failed to restart server %s: %s", server, err)

for !done {
select {
case uuid, more := <-cr:
if !more {
done = true
break
}
if len(uuid) > 0 {
fmt.Fprintln(ctx.Stdout, uuid)
} else {
hasError = true
}
} else {
fmt.Fprintln(ctx.Stdout, needle)
}
if hasError {
return fmt.Errorf("at least 1 server failed to restart")
}
}

if hasError {
return fmt.Errorf("at least 1 server failed to restart")
}
return nil
}

0 comments on commit 6eaf2a4

Please sign in to comment.