Skip to content

Commit

Permalink
Merge pull request #46 from lixiaojun629/develop
Browse files Browse the repository at this point in the history
fixbug: resize uhost with disk-size failed
  • Loading branch information
lixiaojun629 authored Nov 4, 2019
2 parents 1e2b87a + c32f9f8 commit 8d86a50
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export VERSION=0.1.27
export VERSION=0.1.28

.PHONY : install
install:
Expand Down
2 changes: 1 addition & 1 deletion base/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const DefaultBaseURL = "https://api.ucloud.cn/"
const DefaultProfile = "default"

//Version 版本号
const Version = "0.1.27"
const Version = "0.1.28"

//ConfigIns 配置实例, 程序加载时生成
var ConfigIns = &AggConfig{
Expand Down
4 changes: 0 additions & 4 deletions cmd/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,6 @@ func NewCmdDiskExpand() *cobra.Command {
Short: "Expand udisk size",
Long: "Expand udisk size",
Run: func(cmd *cobra.Command, args []string) {
if *req.Size > 8000 || *req.Size < 1 {
base.Cxt.Println("size-gb should be between 1 and 8000")
return
}
for _, id := range *udiskIDs {
id = base.PickResourceID(id)
req.UDiskId = &id
Expand Down
93 changes: 93 additions & 0 deletions cmd/doc_md.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright © 2018 NAME HERE [email protected]
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"fmt"
"io"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/ucloud/ucloud-sdk-go/ucloud/log"

"github.com/ucloud/ucloud-cli/base"
)

//NewCmdDoc ucloud doc
func NewCmdDoc(out io.Writer) *cobra.Command {
var dir, format string
cmd := &cobra.Command{
Use: "gendoc",
Short: "Generate documents for all commands",
Long: "Generate documents for all commands. Support markdown, rst and douku",
Run: func(c *cobra.Command, args []string) {
base.ConfigIns.Region = ""
base.ConfigIns.ProjectID = ""
base.ConfigIns.Zone = ""
rootCmd := NewCmdRoot()
addChildren(rootCmd)
switch format {
case "rst":
emptyStr := func(s string) string { return "" }
linkHandler := func(name, ref string) string {
return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
}
err := doc.GenReSTTreeCustom(rootCmd, dir, emptyStr, linkHandler)
if err != nil {
log.Fatal(err)
}

case "markdown":
err := doc.GenMarkdownTree(rootCmd, dir)
if err != nil {
log.Fatal(err)
}
case "douku":
prefix := "developer/cli/cmd/"
err := doc.GenDoukuTree(rootCmd, dir, prefix)
printCmdIndex(rootCmd, 0, "developer/cli/cmd")
if err != nil {
log.Fatal(err)
}
default:
fmt.Fprintf(out, "format %s is not supported\n", format)
}
},
}

cmd.Flags().StringVar(&dir, "dir", "", "Required. The directory where documents of commands are stored")
cmd.Flags().StringVar(&format, "format", "douku", "Required. Format of the doucments. Accept values: markdown, rst and douku")

cmd.Flags().SetFlagValues("format", "douku", "markdown", "rst")
cmd.Flags().SetFlagValuesFunc("dir", func() []string {
return base.GetFileList("")
})

cmd.MarkFlagRequired("dir")

return cmd
}

func printCmdIndex(curr *cobra.Command, indent int, prefix string) {
if curr.Name() == "help" {
return
}
fmt.Printf("%s* [%s](%s%s)\n", strings.Repeat(" ", indent), curr.Name(), prefix, "/"+curr.Name())
for _, cmd := range curr.Commands() {
printCmdIndex(cmd, indent+1, prefix+"/"+curr.Name())
}
}
55 changes: 0 additions & 55 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ package cmd

import (
"fmt"
"io"
"os"
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"

"github.com/ucloud/ucloud-sdk-go/ucloud/log"

Expand Down Expand Up @@ -68,59 +66,6 @@ func NewCmdRoot() *cobra.Command {
return cmd
}

//NewCmdDoc ucloud doc
func NewCmdDoc(out io.Writer) *cobra.Command {
var dir, format string
cmd := &cobra.Command{
Use: "gendoc",
Short: "Generate documents for all commands",
Long: "Generate documents for all commands. Support markdown, rst and douku",
Run: func(c *cobra.Command, args []string) {
base.ConfigIns.Region = ""
base.ConfigIns.ProjectID = ""
base.ConfigIns.Zone = ""
rootCmd := NewCmdRoot()
addChildren(rootCmd)
switch format {
case "rst":
emptyStr := func(s string) string { return "" }
linkHandler := func(name, ref string) string {
return fmt.Sprintf(":ref:`%s <%s>`", name, ref)
}
err := doc.GenReSTTreeCustom(rootCmd, dir, emptyStr, linkHandler)
if err != nil {
log.Fatal(err)
}

case "markdown":
err := doc.GenMarkdownTree(rootCmd, dir)
if err != nil {
log.Fatal(err)
}
case "douku":
err := doc.GenDoukuTree(rootCmd, dir, "developer/cli/cmd/")
if err != nil {
log.Fatal(err)
}
default:
fmt.Fprintf(out, "format %s is not supported\n", format)
}
},
}

cmd.Flags().StringVar(&dir, "dir", "", "Required. The directory where documents of commands are stored")
cmd.Flags().StringVar(&format, "format", "douku", "Required. Format of the doucments. Accept values: markdown, rst and douku")

cmd.Flags().SetFlagValues("format", "douku", "markdown", "rst")
cmd.Flags().SetFlagValuesFunc("dir", func() []string {
return base.GetFileList("")
})

cmd.MarkFlagRequired("dir")

return cmd
}

const helpTmpl = `Usage:{{if .Runnable}}
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}} [command]{{end}}{{if gt (len .Aliases) 0}}
Expand Down
116 changes: 92 additions & 24 deletions cmd/uhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,15 @@ func NewCmdUHostPoweroff(out io.Writer) *cobra.Command {
return cmd
}

func resizeUhost(req *uhost.ResizeUHostInstanceRequest) {

}

//NewCmdUHostResize ucloud uhost resize
func NewCmdUHostResize(out io.Writer) *cobra.Command {
var yes, async *bool
var bootDiskSize, dataDiskSize int
var dataDiskID string
var uhostIDs *[]string
req := base.BizClient.NewResizeUHostInstanceRequest()
cmd := &cobra.Command{
Expand All @@ -866,12 +872,6 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
} else {
*req.Memory *= 1024
}
if *req.DiskSpace == 0 {
req.DiskSpace = nil
}
if *req.BootDiskSpace == 0 {
req.BootDiskSpace = nil
}
for _, id := range *uhostIDs {
id = base.PickResourceID(id)
req.UHostId = &id
Expand All @@ -883,17 +883,17 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
inst := host.(*uhost.UHostInstanceSet)
if inst.State == "Running" {
if !*yes {
confirmText := "Resize uhost must be after stop it. Do you want to stop this uhost?"
confirmText := "Resize uhost must be done after the uhost is stopped. Do you want to stop this uhost?"
if len(*uhostIDs) > 1 {
confirmText = "Resize uhost must be after stop it. Do you want to stop those uhosts?"
confirmText = "Resize uhost must be done after the uhost is stopped. Do you want to stop those uhosts?"
}
agreeClose, err := ux.Prompt(confirmText)
if err != nil {
base.Cxt.Println(err)
return
}
if !agreeClose {
continue
return
}
}
_req := base.BizClient.NewStopUHostInstanceRequest()
Expand All @@ -903,31 +903,99 @@ func NewCmdUHostResize(out io.Writer) *cobra.Command {
_req.UHostId = &id
stopUhostIns(_req, false, out)
}
if req.CPU != nil || req.Memory != nil || *req.NetCapValue != 0 {
resp, err := base.BizClient.ResizeUHostInstance(req)
if err != nil {
base.HandleError(err)
} else {
text := fmt.Sprintf("uhost [%v] cpu, memory resized", resp.UhostId)
if *async {
fmt.Fprintln(out, text)
} else {
poller := base.NewPoller(describeUHostByID, out)
poller.Poll(resp.UhostId, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
}
}
}

resp, err := base.BizClient.ResizeUHostInstance(req)
if err != nil {
base.HandleError(err)
} else {
text := fmt.Sprintf("UHost:[%v] resized", resp.UhostId)
if *async {
fmt.Fprintln(out, text)
if dataDiskSize != 0 || bootDiskSize != 0 {
_req := base.BizClient.NewResizeAttachedDiskRequest()
var bootDisk uhost.UHostDiskSet
var dataDisks = map[string]uhost.UHostDiskSet{}
for _, disk := range inst.DiskSet {
if disk.IsBoot == "True" {
bootDisk = disk
} else if disk.IsBoot == "False" {
dataDisks[disk.DiskId] = disk
}
}
if bootDiskSize != 0 {
if bootDiskSize <= bootDisk.Size {
base.LogError(fmt.Sprintf("Error, disk does not support shrinkage. current system-disk-size %dg", bootDisk.Size))
continue
} else {
_req.DiskSpace = &bootDiskSize
_req.DiskId = &bootDisk.DiskId
}
} else if dataDiskSize != 0 {
var dataDisk uhost.UHostDiskSet
if len(dataDisks) > 1 {
if dataDiskID == "" {
base.LogError(fmt.Sprintf("Error, the uhost %s have %d data disks. data-disk-id should be assigned", id, len(dataDisks)))
continue
}
var ok bool
dataDisk, ok = dataDisks[dataDiskID]
if !ok {
base.LogError(fmt.Sprintf("Error, the disk %s does not exist", dataDiskID))
continue
}
} else if len(dataDisks) == 1 {
for _, disk := range dataDisks {
dataDisk = disk
}
} else if len(dataDisks) == 0 {
base.LogError(fmt.Sprintf("Error, the uhost %s have no data disk. data-disk-id should be assigned", id))
continue
}
if dataDiskSize <= dataDisk.Size {
base.LogError(fmt.Sprintf("Error, disk does not support shrinkage. current data-disk-size %dg", dataDisk.Size))
continue
}
_req.DiskSpace = &dataDiskSize
_req.DiskId = &dataDisk.DiskId
}
_req.ProjectId = req.ProjectId
_req.Region = req.Region
_req.Zone = req.Zone
_req.UHostId = &id
_, err := base.BizClient.ResizeAttachedDisk(_req)
if err != nil {
base.HandleError(err)
} else {
poller := base.NewPoller(describeUHostByID, out)
poller.Poll(resp.UhostId, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
text := fmt.Sprintf("uhost [%v] disk resized", id)
if *async {
fmt.Fprintln(out, text)
} else {
poller := base.NewPoller(describeUHostByID, out)
poller.Poll(id, *req.ProjectId, *req.Region, *req.Zone, text, []string{status.HOST_RUNNING, status.HOST_STOPPED, status.HOST_FAIL})
}
}
}
}
},
}
cmd.Flags().SortFlags = false
flags := cmd.Flags()
flags.SortFlags = false
uhostIDs = cmd.Flags().StringSlice("uhost-id", nil, "Required. ResourceIDs(or UhostIDs) of the uhost instances")
req.ProjectId = cmd.Flags().String("project-id", base.ConfigIns.ProjectID, "Optional. Assign project-id")
req.Region = cmd.Flags().String("region", base.ConfigIns.Region, "Optional. Assign region")
req.Zone = cmd.Flags().String("zone", "", "Optional. Assign availability zone")
bindProjectID(req, flags)
bindRegion(req, flags)
bindZone(req, flags)
req.CPU = cmd.Flags().Int("cpu", 0, "Optional. The number of virtual CPU cores. Series1 {1, 2, 4, 8, 12, 16, 24, 32}. Series2 {1,2,4,8,16}")
req.Memory = cmd.Flags().Int("memory-gb", 0, "Optional. memory size. Unit: GB. Range: [1, 128], multiple of 2")
req.DiskSpace = cmd.Flags().Int("data-disk-size-gb", 0, "Optional. Data disk size,unit GB. Range[10,1000], SSD disk range[100,500]. Step 10")
req.BootDiskSpace = cmd.Flags().Int("system-disk-size-gb", 0, "Optional. System disk size, unit GB. Range[20,100]. Step 10. System disk does not support shrinkage")
cmd.Flags().IntVar(&bootDiskSize, "system-disk-size-gb", 0, "Optional. System disk size, unit GB. Range[20,100]. Step 10. System disk does not support shrinkage")
cmd.Flags().IntVar(&dataDiskSize, "data-disk-size-gb", 0, "Optional. Data disk size,unit GB. Step 10. disk does not support shrinkage")
cmd.Flags().StringVar(&dataDiskID, "data-disk-id", "", "Optional. If the uhost specified has two or more data disks, this parameter should be assigned")
req.NetCapValue = cmd.Flags().Int("net-cap", 0, "Optional. NIC scale. 1,upgrade; 2,downgrade; 0,unchanged")
yes = cmd.Flags().BoolP("yes", "y", false, "Optional. Do not prompt for confirmation.")
async = cmd.Flags().BoolP("async", "a", false, "Optional. Do not wait for the long-running operation to finish.")
Expand Down

0 comments on commit 8d86a50

Please sign in to comment.