forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_server.go
More file actions
213 lines (191 loc) · 6.46 KB
/
custom_server.go
File metadata and controls
213 lines (191 loc) · 6.46 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package baremetal
import (
"context"
"fmt"
"reflect"
"time"
"github.com/fatih/color"
"github.com/scaleway/scaleway-cli/internal/core"
"github.com/scaleway/scaleway-cli/internal/human"
baremetal "github.com/scaleway/scaleway-sdk-go/api/baremetal/v1"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/scw"
)
const (
serverActionTimeout = 20 * time.Minute
)
var (
serverPingStatusMarshalSpecs = human.EnumMarshalSpecs{
baremetal.ServerPingStatusPingStatusDown: &human.EnumMarshalSpec{Attribute: color.FgRed, Value: "down"},
baremetal.ServerPingStatusPingStatusUp: &human.EnumMarshalSpec{Attribute: color.FgGreen, Value: "up"},
baremetal.ServerPingStatusPingStatusUnknown: &human.EnumMarshalSpec{Attribute: color.Faint, Value: "unknown"},
}
)
func serverWaitCommand() *core.Command {
type serverWaitRequest struct {
ServerID string
Zone scw.Zone
}
return &core.Command{
Short: `Wait for a server to reach a stable state (delivery and installation)`,
Long: `Wait for a server to reach a stable state. This is similar to using --wait flag on other action commands, but without requiring a new action on the server.`,
Namespace: "baremetal",
Resource: "server",
Verb: "wait",
ArgsType: reflect.TypeOf(serverWaitRequest{}),
Run: func(ctx context.Context, argsI interface{}) (i interface{}, err error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
logger.Debugf("starting to wait for server to reach a stable delivery status")
server, err := api.WaitForServer(&baremetal.WaitForServerRequest{
ServerID: argsI.(*serverWaitRequest).ServerID,
Zone: argsI.(*serverWaitRequest).Zone,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}
if server.Status != baremetal.ServerStatusReady {
return nil, &core.CliError{
Err: fmt.Errorf("server did not reach a stable delivery status"),
Details: fmt.Sprintf("server %s is in %s status", server.ID, server.Status),
}
}
if server.Install == nil {
return server, nil
}
logger.Debugf("server reached a stable delivery status. Will now starting to wait for server to reach a stable installation status")
server, err = api.WaitForServerInstall(&baremetal.WaitForServerInstallRequest{
ServerID: argsI.(*serverWaitRequest).ServerID,
Zone: argsI.(*serverWaitRequest).Zone,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
if err != nil {
return nil, err
}
if server.Install.Status != baremetal.ServerInstallStatusCompleted {
return nil, &core.CliError{
Err: fmt.Errorf("server %s did not reach a stable installation status", server.ID),
Details: fmt.Sprintf("server %s is in %s status", server.ID, server.Status),
}
}
logger.Debugf("server reached a stable installation status")
return server, nil
},
ArgSpecs: core.ArgSpecs{
{
Name: "server-id",
Short: `ID of the server affected by the action.`,
Required: true,
Positional: true,
},
core.ZoneArgSpec(),
},
Examples: []*core.Example{
{
Short: "Wait for a server to reach a stable state",
ArgsJSON: `{"server_id": "11111111-1111-1111-1111-111111111111"}`,
},
},
}
}
// serverStartBuilder overrides the baremetalServerStart command
func serverStartBuilder(c *core.Command) *core.Command {
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
return api.WaitForServer(&baremetal.WaitForServerRequest{
Zone: argsI.(*baremetal.StartServerRequest).Zone,
ServerID: respI.(*baremetal.Server).ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
// serverStopBuilder overrides the baremetalServerStop command
func serverStopBuilder(c *core.Command) *core.Command {
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
return api.WaitForServer(&baremetal.WaitForServerRequest{
Zone: argsI.(*baremetal.StopServerRequest).Zone,
ServerID: respI.(*baremetal.Server).ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
// serverRebootBuilder overrides the baremetalServerReboot command
func serverRebootBuilder(c *core.Command) *core.Command {
c.ArgSpecs.GetByName("boot-type").Default = core.DefaultValueSetter("normal")
c.WaitFunc = func(ctx context.Context, argsI, respI interface{}) (interface{}, error) {
api := baremetal.NewAPI(core.ExtractClient(ctx))
return api.WaitForServer(&baremetal.WaitForServerRequest{
Zone: argsI.(*baremetal.RebootServerRequest).Zone,
ServerID: respI.(*baremetal.Server).ID,
Timeout: scw.TimeDurationPtr(serverActionTimeout),
RetryInterval: core.DefaultRetryInterval,
})
}
return c
}
type customServer struct {
baremetal.Server
OfferName string `json:"offer_name"`
}
func serverListBuilder(c *core.Command) *core.Command {
c.View = &core.View{
Fields: []*core.ViewField{
{
Label: "ID",
FieldName: "ID",
},
{
Label: "Name",
FieldName: "Name",
},
{
Label: "Offer Name",
FieldName: "OfferName",
},
{
Label: "Status",
FieldName: "Status",
},
{
Label: "Tags",
FieldName: "Tags",
},
{
Label: "Ping",
FieldName: "PingStatus",
},
},
}
// We add the server type to the list sent to the user
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
listServerResp, err := runner(ctx, argsI)
client := core.ExtractClient(ctx)
api := baremetal.NewAPI(client)
listOffers, err := api.ListOffers(&baremetal.ListOffersRequest{
Zone: argsI.(*baremetal.ListServersRequest).Zone,
}, scw.WithAllPages())
if err != nil {
return listServerResp, err
}
offerNameByID := make(map[string]string)
for _, offer := range listOffers.Offers {
offerNameByID[offer.ID] = offer.Name
}
var customRes []customServer
for _, server := range listServerResp.([]*baremetal.Server) {
customRes = append(customRes, customServer{
Server: *server,
OfferName: offerNameByID[server.OfferID],
})
}
return customRes, nil
}
return c
}