Skip to content

Commit 3a501fb

Browse files
authored
Merge pull request #50 from liquidweb/resize-add-to-plan
add plan support for `cloud server resize`
2 parents e7772a6 + 2c88978 commit 3a501fb

File tree

4 files changed

+293
-197
lines changed

4 files changed

+293
-197
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ to the command.
7070
Current commands supported in a `plan` file:
7171

7272
- cloud server create
73+
- cloud server resize
7374
- cloud template restore
7475

7576
Example:

cmd/cloudServerResize.go

Lines changed: 11 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ import (
2020

2121
"github.com/spf13/cobra"
2222

23-
"github.com/liquidweb/liquidweb-cli/types/api"
24-
"github.com/liquidweb/liquidweb-cli/validate"
23+
"github.com/liquidweb/liquidweb-cli/instance"
2524
)
2625

2726
var cloudServerResizeCmd = &cobra.Command{
@@ -68,204 +67,22 @@ going to a config with more diskspace, and --skip-fs-resize wasn't passed.
6867
During all resizes, the Cloud Server is online as the disk synchronizes.
6968
`,
7069
Run: func(cmd *cobra.Command, args []string) {
71-
uniqIdFlag, _ := cmd.Flags().GetString("uniq-id")
72-
diskspaceFlag, _ := cmd.Flags().GetInt64("diskspace")
73-
configIdFlag, _ := cmd.Flags().GetInt64("config-id")
74-
memoryFlag, _ := cmd.Flags().GetInt64("memory")
75-
skipFsResizeFlag, _ := cmd.Flags().GetBool("skip-fs-resize")
76-
vcpuFlag, _ := cmd.Flags().GetInt64("vcpu")
77-
privateParentFlag, _ := cmd.Flags().GetString("private-parent")
70+
params := &instance.CloudServerResizeParams{}
7871

79-
validateFields := map[interface{}]interface{}{
80-
uniqIdFlag: "UniqId",
81-
}
82-
// must validate UniqId now because we call api methods with this uniq_id before below validate
83-
if err := validate.Validate(validateFields); err != nil {
84-
lwCliInst.Die(err)
85-
}
86-
87-
// convert bool to int for api
88-
skipFsResizeInt := 0
89-
if skipFsResizeFlag {
90-
skipFsResizeInt = 1
91-
}
92-
93-
if configIdFlag == -1 && privateParentFlag == "" {
94-
lwCliInst.Die(fmt.Errorf("flag --config-id required when --private-parent is not given"))
95-
}
96-
97-
resizeArgs := map[string]interface{}{
98-
"uniq_id": uniqIdFlag,
99-
"skip_fs_resize": skipFsResizeInt,
100-
"newsize": configIdFlag,
101-
}
102-
103-
// get details of existing configuration
104-
var cloudServerDetails apiTypes.CloudServerDetails
105-
if err := lwCliInst.CallLwApiInto("bleed/storm/server/details",
106-
map[string]interface{}{"uniq_id": uniqIdFlag},
107-
&cloudServerDetails); err != nil {
108-
lwCliInst.Die(err)
109-
}
110-
111-
var liveResize bool
112-
var twoRebootResize bool
113-
if privateParentFlag == "" {
114-
// non private parent resize
115-
if memoryFlag != -1 || diskspaceFlag != -1 || vcpuFlag != -1 {
116-
lwCliInst.Die(fmt.Errorf("cannot pass --memory --diskspace or --vcpu when --private-parent is not given"))
117-
}
118-
119-
// if already on the given config, nothing to do
120-
if cloudServerDetails.ConfigId == configIdFlag {
121-
lwCliInst.Die(fmt.Errorf("already on config-id [%d]; not initiating a resize", configIdFlag))
122-
}
123-
124-
validateFields[configIdFlag] = "PositiveInt64"
125-
if err := validate.Validate(validateFields); err != nil {
126-
lwCliInst.Die(err)
127-
}
72+
params.UniqId, _ = cmd.Flags().GetString("uniq-id")
73+
params.DiskSpace, _ = cmd.Flags().GetInt64("diskspace")
74+
params.ConfigId, _ = cmd.Flags().GetInt64("config-id")
75+
params.Memory, _ = cmd.Flags().GetInt64("memory")
76+
params.SkipFsResize, _ = cmd.Flags().GetBool("skip-fs-resize")
77+
params.Vcpu, _ = cmd.Flags().GetInt64("vcpu")
78+
params.PrivateParent, _ = cmd.Flags().GetString("private-parent")
12879

129-
// determine reboot expectation.
130-
// resize up full: 2 reboot
131-
// resize up quick (skip-fs-resize) 1 reboot
132-
// resize down: 1 reboot
133-
var configDetails apiTypes.CloudConfigDetails
134-
if err := lwCliInst.CallLwApiInto("bleed/storm/config/details",
135-
map[string]interface{}{"id": configIdFlag}, &configDetails); err != nil {
136-
lwCliInst.Die(err)
137-
}
138-
139-
if configDetails.Disk >= cloudServerDetails.DiskSpace {
140-
// disk space going up..
141-
if !skipFsResizeFlag {
142-
// .. and not skipping fs resize, will be 2 reboots.
143-
twoRebootResize = true
144-
}
145-
}
146-
} else {
147-
// private parent resize specific logic
148-
if memoryFlag == -1 && diskspaceFlag == -1 && vcpuFlag == -1 {
149-
lwCliInst.Die(fmt.Errorf(
150-
"resizes on private parents require at least least one of: --memory --diskspace --vcpu flags"))
151-
}
152-
153-
privateParentUniqId, err := lwCliInst.DerivePrivateParentUniqId(privateParentFlag)
154-
if err != nil {
155-
lwCliInst.Die(err)
156-
}
157-
158-
var (
159-
diskspaceChanging bool
160-
vcpuChanging bool
161-
memoryChanging bool
162-
memoryCanLive bool
163-
vcpuCanLive bool
164-
)
165-
// record what resources are changing
166-
if diskspaceFlag != -1 {
167-
if cloudServerDetails.DiskSpace != diskspaceFlag {
168-
diskspaceChanging = true
169-
}
170-
}
171-
if vcpuFlag != -1 {
172-
if cloudServerDetails.Vcpu != vcpuFlag {
173-
vcpuChanging = true
174-
}
175-
}
176-
if memoryFlag != -1 {
177-
if cloudServerDetails.Memory != memoryFlag {
178-
memoryChanging = true
179-
}
180-
}
181-
// allow resizes to a private parent even if its old non private parent config had exact same specs
182-
if cloudServerDetails.ConfigId == 0 && cloudServerDetails.PrivateParent != privateParentUniqId {
183-
if !diskspaceChanging && !vcpuChanging && !memoryChanging {
184-
lwCliInst.Die(fmt.Errorf(
185-
"private parent resize, but passed diskspace, memory, vcpu values match existing values"))
186-
}
187-
}
188-
189-
resizeArgs["newsize"] = 0 // 0 indicates private parent resize
190-
resizeArgs["parent"] = privateParentUniqId // uniq_id of the private parent
191-
validateFields[privateParentUniqId] = "UniqId"
192-
// server/resize api method always wants diskspace, vcpu, memory passed for pp resize, even if not changing
193-
// value. So set to current value, then override based on passed flags.
194-
resizeArgs["diskspace"] = cloudServerDetails.DiskSpace
195-
resizeArgs["memory"] = cloudServerDetails.Memory
196-
resizeArgs["vcpu"] = cloudServerDetails.Vcpu
197-
198-
if diskspaceFlag != -1 {
199-
resizeArgs["diskspace"] = diskspaceFlag // desired diskspace
200-
validateFields[diskspaceFlag] = "PositiveInt64"
201-
}
202-
if memoryFlag != -1 {
203-
resizeArgs["memory"] = memoryFlag // desired memory
204-
validateFields[memoryFlag] = "PositiveInt64"
205-
}
206-
if vcpuFlag != -1 {
207-
resizeArgs["vcpu"] = vcpuFlag // desired vcpus
208-
validateFields[vcpuFlag] = "PositiveInt64"
209-
}
210-
211-
// determine if this will be a live resize
212-
if _, exists := resizeArgs["memory"]; exists {
213-
if memoryFlag >= cloudServerDetails.Memory {
214-
// asking for more RAM
215-
memoryCanLive = true
216-
}
217-
}
218-
if _, exists := resizeArgs["vcpu"]; exists {
219-
if vcpuFlag >= cloudServerDetails.Vcpu {
220-
// asking for more vcpu
221-
vcpuCanLive = true
222-
}
223-
}
224-
225-
if memoryFlag != -1 && vcpuFlag != -1 {
226-
if vcpuCanLive && memoryCanLive {
227-
liveResize = true
228-
}
229-
} else if memoryCanLive {
230-
liveResize = true
231-
} else if vcpuCanLive {
232-
liveResize = true
233-
}
234-
235-
// if diskspace allocation changes its not currently ever done live regardless of memory, vcpu
236-
if diskspaceFlag != -1 {
237-
if resizeArgs["diskspace"] != cloudServerDetails.DiskSpace {
238-
liveResize = false
239-
}
240-
}
241-
}
242-
243-
if err := validate.Validate(validateFields); err != nil {
244-
lwCliInst.Die(err)
245-
}
246-
247-
_, err := lwCliInst.LwCliApiClient.Call("bleed/server/resize", resizeArgs)
80+
status, err := lwCliInst.CloudServerResize(params)
24881
if err != nil {
24982
lwCliInst.Die(err)
25083
}
25184

252-
fmt.Printf("server resized started! You can check progress with 'cloud server status --uniq-id %s'\n\n", uniqIdFlag)
253-
254-
if liveResize {
255-
fmt.Printf("\nthis resize will be performed live without downtime.\n")
256-
} else {
257-
rebootExpectation := "one reboot"
258-
if twoRebootResize {
259-
rebootExpectation = "two reboots"
260-
}
261-
fmt.Printf(
262-
"\nexpect %s during this process. Your server will be online as the disk is copied to the destination.\n",
263-
rebootExpectation)
264-
if twoRebootResize {
265-
fmt.Printf(
266-
"\tTIP: Avoid the second reboot by passing --skip-fs-resize. See usage for additional details.\n")
267-
}
268-
}
85+
fmt.Print(status)
26986
},
27087
}
27188

0 commit comments

Comments
 (0)