yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/apsara/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 20 "yunion.io/x/cloudmux/pkg/multicloud/apsara" 21 "yunion.io/x/onecloud/pkg/util/shellutils" 22 ) 23 24 func init() { 25 type InstanceListOptions struct { 26 Id []string `help:"IDs of instances to show"` 27 Zone string `help:"Zone ID"` 28 Limit int `help:"page size"` 29 Offset int `help:"page offset"` 30 } 31 shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *apsara.SRegion, args *InstanceListOptions) error { 32 instances, total, e := cli.GetInstances(args.Zone, args.Id, args.Offset, args.Limit) 33 if e != nil { 34 return e 35 } 36 printList(instances, total, args.Offset, args.Limit, []string{}) 37 return nil 38 }) 39 40 type InstanceCreateOptions struct { 41 NAME string `help:"name of instance"` 42 IMAGE string `help:"image ID"` 43 CPU int `help:"CPU count"` 44 MEMORYGB int `help:"MemoryGB"` 45 Disk []int `help:"Data disk sizes int GB"` 46 STORAGE string `help:"Storage type"` 47 VSWITCH string `help:"Vswitch ID"` 48 PASSWD string `help:"password"` 49 PublicKey string `help:"PublicKey"` 50 } 51 shellutils.R(&InstanceCreateOptions{}, "instance-create", "Create a instance", func(cli *apsara.SRegion, args *InstanceCreateOptions) error { 52 instance, e := cli.CreateInstanceSimple(args.NAME, args.IMAGE, args.CPU, args.MEMORYGB, args.STORAGE, args.Disk, args.VSWITCH, args.PASSWD, args.PublicKey) 53 if e != nil { 54 return e 55 } 56 printObject(instance) 57 return nil 58 }) 59 60 type InstanceDiskOperationOptions struct { 61 ID string `help:"instance ID"` 62 DISK string `help:"disk ID"` 63 } 64 65 shellutils.R(&InstanceDiskOperationOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *apsara.SRegion, args *InstanceDiskOperationOptions) error { 66 err := cli.AttachDisk(args.ID, args.DISK) 67 if err != nil { 68 return err 69 } 70 return nil 71 }) 72 73 shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *apsara.SRegion, args *InstanceDiskOperationOptions) error { 74 err := cli.DetachDisk(args.ID, args.DISK) 75 if err != nil { 76 return err 77 } 78 return nil 79 }) 80 81 type InstanceOperationOptions struct { 82 ID string `help:"instance ID"` 83 } 84 shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *apsara.SRegion, args *InstanceOperationOptions) error { 85 err := cli.StartVM(args.ID) 86 if err != nil { 87 return err 88 } 89 return nil 90 }) 91 92 shellutils.R(&InstanceOperationOptions{}, "instance-auto-renew-info", "Show instance auto renew info", func(cli *apsara.SRegion, args *InstanceOperationOptions) error { 93 info, err := cli.GetInstanceAutoRenewAttribute(args.ID) 94 if err != nil { 95 return err 96 } 97 printObject(info) 98 return nil 99 }) 100 101 shellutils.R(&InstanceOperationOptions{}, "instance-eip-convert", "Convert instance public ip to eip", func(cli *apsara.SRegion, args *InstanceOperationOptions) error { 102 err := cli.ConvertPublicIpToEip(args.ID) 103 if err != nil { 104 return err 105 } 106 return nil 107 }) 108 109 shellutils.R(&InstanceOperationOptions{}, "instance-vnc", "Get a instance VNC url", func(cli *apsara.SRegion, args *InstanceOperationOptions) error { 110 url, err := cli.GetInstanceVNCUrl(args.ID) 111 if err != nil { 112 return err 113 } 114 fmt.Println(url) 115 return nil 116 }) 117 118 type InstanceStopOptions struct { 119 ID string `help:"instance ID"` 120 Force bool `help:"Force stop instance"` 121 } 122 shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *apsara.SRegion, args *InstanceStopOptions) error { 123 err := cli.StopVM(args.ID, args.Force) 124 if err != nil { 125 return err 126 } 127 return nil 128 }) 129 shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *apsara.SRegion, args *InstanceOperationOptions) error { 130 err := cli.DeleteVM(args.ID) 131 if err != nil { 132 return err 133 } 134 return nil 135 }) 136 137 /* 138 server-change-config 更改系统配置 139 server-reset 140 */ 141 type InstanceDeployOptions struct { 142 ID string `help:"instance ID"` 143 Name string `help:"new instance name"` 144 Hostname string `help:"new hostname"` 145 Keypair string `help:"Keypair Name"` 146 DeleteKeypair bool `help:"Remove SSH keypair"` 147 Password string `help:"new password"` 148 // ResetPassword bool `help:"Force reset password"` 149 Description string `help:"new instances description"` 150 } 151 152 shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *apsara.SRegion, args *InstanceDeployOptions) error { 153 err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description) 154 if err != nil { 155 return err 156 } 157 return nil 158 }) 159 160 type InstanceRebuildRootOptions struct { 161 ID string `help:"instance ID"` 162 Image string `help:"Image ID"` 163 Password string `help:"pasword"` 164 Keypair string `help:"keypair name"` 165 Size int `help:"system disk size in GB"` 166 } 167 168 shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *apsara.SRegion, args *InstanceRebuildRootOptions) error { 169 diskID, err := cli.ReplaceSystemDisk(args.ID, args.Image, args.Password, args.Keypair, args.Size) 170 if err != nil { 171 return err 172 } 173 fmt.Printf("New diskID is %s", diskID) 174 return nil 175 }) 176 177 type InstanceChangeConfigOptions struct { 178 ID string `help:"instance ID"` 179 InstanceTypeId string `help:"instance type"` 180 Disk []int `help:"Data disk sizes int GB"` 181 } 182 183 shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *apsara.SRegion, args *InstanceChangeConfigOptions) error { 184 instance, e := cli.GetInstance(args.ID) 185 if e != nil { 186 return e 187 } 188 189 err := cli.ChangeVMConfig2(instance.ZoneId, args.ID, args.InstanceTypeId, nil) 190 if err != nil { 191 return err 192 } 193 return nil 194 }) 195 196 type InstanceUpdatePasswordOptions struct { 197 ID string `help:"Instance ID"` 198 PASSWD string `help:"new password"` 199 } 200 shellutils.R(&InstanceUpdatePasswordOptions{}, "instance-update-password", "Update instance password", func(cli *apsara.SRegion, args *InstanceUpdatePasswordOptions) error { 201 err := cli.UpdateInstancePassword(args.ID, args.PASSWD) 202 return err 203 }) 204 205 type InstanceSetAutoRenewOptions struct { 206 ID string `help:"Instance ID"` 207 AutoRenew bool `help:"Is auto renew instance"` 208 Duration string 209 } 210 211 shellutils.R(&InstanceSetAutoRenewOptions{}, "instance-set-auto-renew", "Set instance auto renew", func(cli *apsara.SRegion, args *InstanceSetAutoRenewOptions) error { 212 return cli.SetInstanceAutoRenew(args.ID, args.AutoRenew) 213 }) 214 215 type InstanceUpdateNameOptions struct { 216 ID string `help:"Instance ID"` 217 NAME string `help:"new name"` 218 } 219 shellutils.R(&InstanceUpdateNameOptions{}, "instance-update-name", "Update instance name", func(cli *apsara.SRegion, args *InstanceUpdateNameOptions) error { 220 return cli.UpdateVM(args.ID, args.NAME) 221 }) 222 223 }