@@ -6,32 +6,80 @@ package commands
6
6
7
7
import (
8
8
"fmt"
9
+ "sync"
10
+ "time"
9
11
12
+ "github.com/scaleway/scaleway-cli/pkg/api"
10
13
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
11
14
)
12
15
13
16
// RestartArgs are flags for the `RunRestart` function
14
17
type RestartArgs struct {
18
+ Wait bool
19
+ Timeout float64
15
20
Servers []string
16
21
}
17
22
23
+ // restartIdentifiers resolves server IDs, restarts, and waits for them to be ready (-w)
24
+ func restartIdentifiers (ctx CommandContext , wait bool , servers []string , cr chan string ) {
25
+ var wg sync.WaitGroup
26
+ for _ , needle := range servers {
27
+ wg .Add (1 )
28
+ go func (needle string ) {
29
+ server := ctx .API .GetServerID (needle )
30
+ res := server
31
+ err := ctx .API .PostServerAction (server , "reboot" )
32
+ if err != nil {
33
+ if err .Error () != "server is being stopped or rebooted" {
34
+ logrus .Errorf ("failed to restart server %s: %s" , server , err )
35
+ }
36
+ res = ""
37
+ } else {
38
+ if wait {
39
+ // FIXME: handle gateway
40
+ api .WaitForServerReady (ctx .API , server , "" )
41
+ }
42
+ }
43
+ cr <- res
44
+ wg .Done ()
45
+ }(needle )
46
+ }
47
+ wg .Wait ()
48
+ close (cr )
49
+ }
50
+
18
51
// RunRestart is the handler for 'scw restart'
19
52
func RunRestart (ctx CommandContext , args RestartArgs ) error {
53
+ if args .Wait && args .Timeout > 0 {
54
+ go func () {
55
+ time .Sleep (time .Duration (args .Timeout * 1000 ) * time .Millisecond )
56
+ // FIXME: avoid use of fatalf
57
+ logrus .Fatalf ("Operation timed out" )
58
+ }()
59
+ }
60
+
61
+ cr := make (chan string )
62
+ go restartIdentifiers (ctx , args .Wait , args .Servers , cr )
63
+ done := false
20
64
hasError := false
21
- for _ , needle := range args .Servers {
22
- server := ctx .API .GetServerID (needle )
23
- err := ctx .API .PostServerAction (server , "reboot" )
24
- if err != nil {
25
- if err .Error () != "server is being stopped or rebooted" {
26
- logrus .Errorf ("failed to restart server %s: %s" , server , err )
65
+
66
+ for ! done {
67
+ select {
68
+ case uuid , more := <- cr :
69
+ if ! more {
70
+ done = true
71
+ break
72
+ }
73
+ if len (uuid ) > 0 {
74
+ fmt .Fprintln (ctx .Stdout , uuid )
75
+ } else {
27
76
hasError = true
28
77
}
29
- } else {
30
- fmt .Fprintln (ctx .Stdout , needle )
31
- }
32
- if hasError {
33
- return fmt .Errorf ("at least 1 server failed to restart" )
34
78
}
35
79
}
80
+
81
+ if hasError {
82
+ return fmt .Errorf ("at least 1 server failed to restart" )
83
+ }
36
84
return nil
37
85
}
0 commit comments