/
stop.go
60 lines (54 loc) · 1.55 KB
/
stop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.
package commands
import (
"fmt"
"time"
"github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/pkg/api"
)
// StopArgs are flags for the `RunStop` function
type StopArgs struct {
Terminate bool
Wait bool
Servers []string
}
// RunStop is the handler for 'scw stop'
func RunStop(ctx CommandContext, args StopArgs) error {
// FIXME: parallelize stop when stopping multiple servers
hasError := false
for _, needle := range args.Servers {
serverID, err := ctx.API.GetServerID(needle)
if err != nil {
return err
}
action := "poweroff"
if args.Terminate {
action = "terminate"
}
if err = ctx.API.PostServerAction(serverID, action); err != nil {
if err.Error() != "server should be running" && err.Error() != "server is being stopped or rebooted" {
logrus.Warningf("failed to stop server %s: %s", serverID, err)
hasError = true
}
} else {
if args.Wait {
// We wait for 10 seconds which is the minimal amount of time needed for a server to stop
time.Sleep(10 * time.Second)
if _, err = api.WaitForServerStopped(ctx.API, serverID); err != nil {
logrus.Errorf("failed to wait for server %s: %v", serverID, err)
hasError = true
}
}
if args.Terminate {
ctx.API.Cache.RemoveServer(serverID)
}
fmt.Fprintln(ctx.Stdout, needle)
}
}
if hasError {
return fmt.Errorf("at least 1 server failed to be stopped")
}
return nil
}