Skip to content

Commit b846037

Browse files
committed
Support of scw stop -w, --wait option
1 parent c95a860 commit b846037

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ Same as Hack, without step 5
954954

955955
* `scw {create,run}`, prefixing root-volume with the server hostname ([#63](https://github.com/scaleway/scaleway-cli/issues/63))
956956
* `scw {create,run} IMAGE`, *IMAGE* can be a snapshot ([#19](https://github.com/scaleway/scaleway-cli/issues/19))
957+
* Support of `scw stop -w, --wait` option
957958

958959
#### Fixes
959960

api/helpers.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,15 @@ func WaitForServerReady(api *ScalewayAPI, serverID string) (*ScalewayServer, err
331331
return server, nil
332332
}
333333

334+
// WaitForServerStopped wait for a server state to be stopped
335+
func WaitForServerStopped(api *ScalewayAPI, serverID string) (*ScalewayServer, error) {
336+
server, err := WaitForServerState(api, serverID, "stopped")
337+
if err != nil {
338+
return nil, err
339+
}
340+
return server, nil
341+
}
342+
334343
// ByCreationDate sorts images by CreationDate field
335344
type ByCreationDate []ScalewayImageInterface
336345

commands/stop.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ package commands
77
import (
88
"fmt"
99
"os"
10+
"time"
1011

1112
log "github.com/Sirupsen/logrus"
1213

14+
"github.com/scaleway/scaleway-cli/api"
1315
types "github.com/scaleway/scaleway-cli/commands/types"
1416
)
1517

@@ -23,18 +25,23 @@ var cmdStop = &types.Command{
2325
$ scw stop -t my-running-server my-second-running-server
2426
$ scw stop $(scw ps -q)
2527
$ scw stop $(scw ps | grep mysql | awk '{print $1}')
28+
$ scw stop server && stop wait server
29+
$ scw stop -w server
2630
`,
2731
}
2832

2933
func init() {
3034
cmdStop.Flag.BoolVar(&stopT, []string{"t", "-terminate"}, false, "Stop and trash a server with its volumes")
3135
cmdStop.Flag.BoolVar(&stopHelp, []string{"h", "-help"}, false, "Print usage")
36+
cmdStop.Flag.BoolVar(&stopW, []string{"w", "-wait"}, false, "Synchronous stop. Wait for SSH to be ready")
3237
}
3338

3439
// Flags
3540
var stopT bool // -t flag
3641
var stopHelp bool // -h, --help flag
42+
var stopW bool // -w, --wait flat
3743

44+
// FIXME: parallelize stop when stopping multiple servers
3845
func runStop(cmd *types.Command, args []string) {
3946
if stopHelp {
4047
cmd.PrintUsage()
@@ -45,21 +52,31 @@ func runStop(cmd *types.Command, args []string) {
4552

4653
hasError := false
4754
for _, needle := range args {
48-
server := cmd.API.GetServerID(needle)
55+
serverID := cmd.API.GetServerID(needle)
4956
action := "poweroff"
5057
if stopT {
5158
action = "terminate"
5259
}
53-
err := cmd.API.PostServerAction(server, action)
60+
err := cmd.API.PostServerAction(serverID, action)
5461
if err != nil {
5562
if err.Error() != "server should be running" && err.Error() != "server is being stopped or rebooted" {
56-
log.Warningf("failed to stop server %s: %s", server, err)
63+
log.Warningf("failed to stop server %s: %s", serverID, err)
5764
hasError = true
5865
}
5966
} else {
67+
if stopW {
68+
// We wait for 10 seconds which is the minimal amount of time needed for a server to stop
69+
time.Sleep(10 * time.Second)
70+
_, err = api.WaitForServerStopped(cmd.API, serverID)
71+
if err != nil {
72+
log.Errorf("failed to wait for server %s: %v", serverID, err)
73+
hasError = true
74+
}
75+
}
6076
fmt.Println(needle)
6177
}
6278
}
79+
6380
if hasError {
6481
os.Exit(1)
6582
}

commands/wait.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ package commands
66

77
import (
88
"os"
9-
"time"
109

1110
log "github.com/Sirupsen/logrus"
1211

12+
"github.com/scaleway/scaleway-cli/api"
1313
types "github.com/scaleway/scaleway-cli/commands/types"
1414
)
1515

@@ -38,19 +38,14 @@ func runWait(cmd *types.Command, args []string) {
3838
hasError := false
3939
for _, needle := range args {
4040
serverIdentifier := cmd.API.GetServerID(needle)
41-
for {
42-
server, err := cmd.API.GetServer(serverIdentifier)
43-
if err != nil {
44-
log.Errorf("failed to retrieve information from server %s: %s", serverIdentifier, err)
45-
hasError = true
46-
break
47-
}
48-
if server.State == "stopped" {
49-
break
50-
}
51-
time.Sleep(1 * time.Second)
41+
42+
_, err := api.WaitForServerStopped(cmd.API, serverIdentifier)
43+
if err != nil {
44+
log.Errorf("failed to wait for server %s: %v", serverIdentifier, err)
45+
hasError = true
5246
}
5347
}
48+
5449
if hasError {
5550
os.Exit(1)
5651
}

0 commit comments

Comments
 (0)