-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.go
59 lines (49 loc) · 1.94 KB
/
server.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
package server
import (
"context"
"fmt"
"time"
"github.com/UpCloudLtd/progress/messages"
"github.com/UpCloudLtd/upcloud-cli/v3/internal/commands"
"github.com/UpCloudLtd/upcloud-go-api/v8/upcloud/request"
)
const (
minStorageSize = 10
maxServerActions = 10
// Server state related consts
defaultStopType = request.ServerStopTypeSoft
defaultRestartTimeout = time.Duration(120) * time.Second
defaultRestartTimeoutAction = request.RestartTimeoutActionIgnore
customPlan = "custom"
avoidHostDescription = "Host to avoid when scheduling the server. Use this to make sure VMs do not reside on specific host. Refers to value from `host` attribute. Useful when building HA-environments."
hostDescription = "Schedule the server on a specific host. Refers to value from `host` attribute. Only available in private clouds."
)
// BaseServerCommand crestes the base "server" command
func BaseServerCommand() commands.Command {
return &serverCommand{
commands.New("server", "Manage servers"),
}
}
type serverCommand struct {
*commands.BaseCommand
}
// waitForServerState waits for server to reach given state and updates progress message with key matching given msg. Finally, progress message is updated back to given msg and either done state or timeout warning.
func waitForServerState(uuid, state string, exec commands.Executor, msg string) {
exec.PushProgressUpdateMessage(msg, fmt.Sprintf("Waiting for server %s to be in %s state", uuid, state))
ctx, cancel := context.WithTimeout(exec.Context(), 15*time.Minute)
defer cancel()
if _, err := exec.All().WaitForServerState(ctx, &request.WaitForServerStateRequest{
UUID: uuid,
DesiredState: state,
}); err != nil {
exec.PushProgressUpdate(messages.Update{
Key: msg,
Message: msg,
Status: messages.MessageStatusWarning,
Details: "Error: " + err.Error(),
})
return
}
exec.PushProgressUpdateMessage(msg, msg)
exec.PushProgressSuccess(msg)
}