yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/qcloud/shell/instance.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package shell 16 17 import ( 18 "fmt" 19 "strings" 20 21 "yunion.io/x/cloudmux/pkg/cloudprovider" 22 "yunion.io/x/cloudmux/pkg/multicloud/qcloud" 23 "yunion.io/x/onecloud/pkg/util/shellutils" 24 ) 25 26 func init() { 27 type InstanceListOptions struct { 28 Id []string `help:"IDs of instances to show"` 29 Zone string `help:"Zone ID"` 30 Limit int `help:"page size"` 31 Offset int `help:"page offset"` 32 } 33 shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *qcloud.SRegion, args *InstanceListOptions) error { 34 instances, total, e := cli.GetInstances(args.Zone, args.Id, args.Offset, args.Limit) 35 if e != nil { 36 return e 37 } 38 printList(instances, total, args.Offset, args.Limit, []string{}) 39 return nil 40 }) 41 42 type InstanceCreateOptions struct { 43 NAME string `help:"name of instance"` 44 IMAGE string `help:"image ID"` 45 CPU int `help:"CPU count"` 46 MEMORYGB int `help:"MemoryGB"` 47 Disk []int `help:"Data disk sizes int GB"` 48 STORAGE string `help:"Storage type" choices:"LOCAL_BASIC|LOCAL_SSD|CLOUD_BASIC|CLOUD_PREMIUM|CLOUD_SSD"` 49 NETWORK string `help:"Network ID"` 50 PASSWD string `help:"password"` 51 SECGROUP string `help:"Security group"` 52 PublicKey string `help:"PublicKey"` 53 Tag []string `help:"tags"` 54 } 55 56 shellutils.R(&InstanceCreateOptions{}, "instance-create", "Create a instance", func(cli *qcloud.SRegion, args *InstanceCreateOptions) error { 57 tags := make(map[string]string) 58 if len(args.Tag) > 0 { 59 for _, t := range args.Tag { 60 ts := strings.Split(t, ":") 61 if len(ts) >= 2 { 62 tags[ts[0]] = ts[1] 63 } else { 64 tags[ts[0]] = "" 65 } 66 } 67 } 68 instance, e := cli.CreateInstanceSimple(args.NAME, args.IMAGE, args.CPU, args.MEMORYGB, args.STORAGE, args.Disk, args.NETWORK, args.PASSWD, args.PublicKey, args.SECGROUP, tags) 69 if e != nil { 70 return e 71 } 72 printObject(instance) 73 return nil 74 }) 75 76 type InstanceDiskOperationOptions struct { 77 ID string `help:"instance ID"` 78 DISK string `help:"disk ID"` 79 } 80 81 shellutils.R(&InstanceDiskOperationOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *qcloud.SRegion, args *InstanceDiskOperationOptions) error { 82 err := cli.AttachDisk(args.ID, args.DISK) 83 if err != nil { 84 return err 85 } 86 return nil 87 }) 88 89 shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *qcloud.SRegion, args *InstanceDiskOperationOptions) error { 90 err := cli.DetachDisk(args.ID, args.DISK) 91 if err != nil { 92 return err 93 } 94 return nil 95 }) 96 97 type InstanceOperationOptions struct { 98 ID string `help:"instance ID"` 99 } 100 shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *qcloud.SRegion, args *InstanceOperationOptions) error { 101 err := cli.StartVM(args.ID) 102 if err != nil { 103 return err 104 } 105 return nil 106 }) 107 108 shellutils.R(&InstanceOperationOptions{}, "instance-convert-eip", "Convert public ip to eip for instance", func(cli *qcloud.SRegion, args *InstanceOperationOptions) error { 109 err := cli.ConvertPublicIpToEip(args.ID) 110 if err != nil { 111 return err 112 } 113 return nil 114 }) 115 116 shellutils.R(&InstanceOperationOptions{}, "instance-vnc", "Get a instance VNC url", func(cli *qcloud.SRegion, args *InstanceOperationOptions) error { 117 url, err := cli.GetInstanceVNCUrl(args.ID) 118 if err != nil { 119 return err 120 } 121 fmt.Println(url) 122 return nil 123 }) 124 125 type InstanceStopOptions struct { 126 ID string `help:"instance ID"` 127 Force bool `help:"Force stop instance"` 128 StopCharging bool `help:"Stop charging"` 129 } 130 shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *qcloud.SRegion, args *InstanceStopOptions) error { 131 opts := &cloudprovider.ServerStopOptions{ 132 IsForce: args.Force, 133 StopCharging: args.StopCharging, 134 } 135 err := cli.StopVM(args.ID, opts) 136 if err != nil { 137 return err 138 } 139 return nil 140 }) 141 shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *qcloud.SRegion, args *InstanceOperationOptions) error { 142 err := cli.DeleteVM(args.ID) 143 if err != nil { 144 return err 145 } 146 return nil 147 }) 148 149 /* 150 server-change-config 更改系统配置 151 server-reset 152 */ 153 type InstanceDeployOptions struct { 154 ID string `help:"instance ID"` 155 Name string `help:"new instance name"` 156 Hostname string `help:"new hostname"` 157 Keypair string `help:"Keypair Name"` 158 DeleteKeypair bool `help:"Remove SSH keypair"` 159 Password string `help:"new password"` 160 // ResetPassword bool `help:"Force reset password"` 161 Description string `help:"new instances description"` 162 } 163 164 shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *qcloud.SRegion, args *InstanceDeployOptions) error { 165 err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description) 166 if err != nil { 167 return err 168 } 169 return nil 170 }) 171 172 type InstanceRebuildRootOptions struct { 173 ID string `help:"instance ID"` 174 Image string `help:"Image ID"` 175 Password string `help:"pasword"` 176 Keypair string `help:"keypair name"` 177 Size int `help:"system disk size in GB"` 178 } 179 180 shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *qcloud.SRegion, args *InstanceRebuildRootOptions) error { 181 err := cli.ReplaceSystemDisk(args.ID, args.Image, args.Password, args.Keypair, args.Size) 182 if err != nil { 183 return err 184 } 185 return nil 186 }) 187 188 type InstanceChangeConfigOptions struct { 189 ID string `help:"instance ID"` 190 InstanceType string `help:"instance type"` 191 } 192 193 shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *qcloud.SRegion, args *InstanceChangeConfigOptions) error { 194 err := cli.ChangeVMConfig(args.ID, args.InstanceType) 195 if err != nil { 196 return err 197 } 198 return nil 199 }) 200 201 type InstanceUpdatePasswordOptions struct { 202 ID string `help:"Instance ID"` 203 PASSWD string `help:"new password"` 204 } 205 shellutils.R(&InstanceUpdatePasswordOptions{}, "instance-update-password", "Update instance password", func(cli *qcloud.SRegion, args *InstanceUpdatePasswordOptions) error { 206 err := cli.UpdateInstancePassword(args.ID, args.PASSWD) 207 return err 208 }) 209 210 type InstanceSetAutoRenewOptions struct { 211 ID string `help:"Instance ID"` 212 AutoRenew bool `help:"Set auto renew"` 213 } 214 shellutils.R(&InstanceSetAutoRenewOptions{}, "instance-set-auto-renew", "Set instance auto renew flag", func(cli *qcloud.SRegion, args *InstanceSetAutoRenewOptions) error { 215 return cli.SetInstanceAutoRenew(args.ID, args.AutoRenew) 216 }) 217 218 type InstanceSaveImageOptions struct { 219 ID string `help:"Instance ID"` 220 IMAGE_NAME string `help:"Image name"` 221 Notes string `hlep:"Image desc"` 222 } 223 shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *qcloud.SRegion, args *InstanceSaveImageOptions) error { 224 opts := cloudprovider.SaveImageOptions{ 225 Name: args.IMAGE_NAME, 226 Notes: args.Notes, 227 } 228 image, err := cli.SaveImage(args.ID, &opts) 229 if err != nil { 230 return err 231 } 232 printObject(image) 233 return nil 234 }) 235 236 type InstanceBandWidthOptions struct { 237 ID string `help:"Instance ID"` 238 BANDWIDTH int `help:"Bandwidth"` 239 InternetChargeType string `help:"InternetChargeType" default:"traffic" choices:"traffic|bandwidth"` 240 } 241 shellutils.R(&InstanceBandWidthOptions{}, "instance-change-bandwidth", "Change instance bandwidth", func(cli *qcloud.SRegion, args *InstanceBandWidthOptions) error { 242 return cli.UpdateInstanceBandwidth(args.ID, args.BANDWIDTH, args.InternetChargeType) 243 }) 244 245 }