yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/huawei/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 "context" 19 "fmt" 20 "strings" 21 22 "yunion.io/x/cloudmux/pkg/cloudprovider" 23 "yunion.io/x/cloudmux/pkg/multicloud/huawei" 24 "yunion.io/x/onecloud/pkg/util/shellutils" 25 ) 26 27 func init() { 28 type InstanceListOptions struct { 29 } 30 shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *huawei.SRegion, args *InstanceListOptions) error { 31 instances, e := cli.GetInstances() 32 if e != nil { 33 return e 34 } 35 printList(instances, 0, 0, 0, nil) 36 return nil 37 }) 38 39 type InstanceDiskOperationOptions struct { 40 ID string `help:"instance ID"` 41 DISK string `help:"disk ID"` 42 } 43 44 type InstanceDiskAttachOptions struct { 45 ID string `help:"instance ID"` 46 DISK string `help:"disk ID"` 47 DEVICE string `help:"disk device name. eg. /dev/sdb"` 48 } 49 50 shellutils.R(&InstanceDiskAttachOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *huawei.SRegion, args *InstanceDiskAttachOptions) error { 51 err := cli.AttachDisk(args.ID, args.DISK, args.DEVICE) 52 if err != nil { 53 return err 54 } 55 return nil 56 }) 57 58 shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *huawei.SRegion, args *InstanceDiskOperationOptions) error { 59 err := cli.DetachDisk(args.ID, args.DISK) 60 if err != nil { 61 return err 62 } 63 return nil 64 }) 65 66 type InstanceOperationOptions struct { 67 ID string `help:"instance ID"` 68 } 69 shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *huawei.SRegion, args *InstanceOperationOptions) error { 70 err := cli.StartVM(args.ID) 71 if err != nil { 72 return err 73 } 74 return nil 75 }) 76 77 type InstanceStopOptions struct { 78 ID string `help:"instance ID"` 79 Force bool `help:"Force stop instance"` 80 } 81 shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *huawei.SRegion, args *InstanceStopOptions) error { 82 err := cli.StopVM(args.ID, args.Force) 83 if err != nil { 84 return err 85 } 86 return nil 87 }) 88 shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *huawei.SRegion, args *InstanceOperationOptions) error { 89 err := cli.DeleteVM(args.ID) 90 if err != nil { 91 return err 92 } 93 return nil 94 }) 95 96 /* 97 server-change-config 更改系统配置 98 server-reset 99 */ 100 type InstanceDeployOptions struct { 101 ID string `help:"instance ID"` 102 Name string `help:"new instance name"` 103 Hostname string `help:"new hostname"` 104 Keypair string `help:"Keypair Name"` 105 DeleteKeypair bool `help:"Remove SSH keypair"` 106 Password string `help:"new password"` 107 // ResetPassword bool `help:"Force reset password"` 108 Description string `help:"new instances description"` 109 } 110 111 shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *huawei.SRegion, args *InstanceDeployOptions) error { 112 err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description) 113 if err != nil { 114 return err 115 } 116 return nil 117 }) 118 119 type InstanceRebuildRootOptions struct { 120 ID string `help:"instance ID"` 121 UserId string `help:"instance user ID"` 122 Image string `help:"Image ID"` 123 Password string `help:"admin password"` 124 PublicKeyName string `help:"public key name"` 125 UserData string `help:"cloud-init user data"` 126 } 127 128 shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *huawei.SRegion, args *InstanceRebuildRootOptions) error { 129 ctx := context.Background() 130 jobId, err := cli.ChangeRoot(ctx, args.UserId, args.ID, args.Image, args.Password, args.PublicKeyName, args.UserData) 131 if err != nil { 132 return err 133 } 134 fmt.Printf("ChangeRoot jobID is %s", jobId) 135 return nil 136 }) 137 138 type InstanceChangeConfigOptions struct { 139 ID string `help:"instance ID"` 140 InstanceType string `help:"instance type"` 141 } 142 143 shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *huawei.SRegion, args *InstanceChangeConfigOptions) error { 144 err := cli.ChangeVMConfig(args.ID, args.InstanceType) 145 if err != nil { 146 return err 147 } 148 return nil 149 }) 150 151 type InstanceOrderUnsubscribeOptions struct { 152 ID string `help:"instance ID"` 153 DOMAIN string `help:"domain ID"` 154 } 155 156 shellutils.R(&InstanceOrderUnsubscribeOptions{}, "instance-order-unsubscribe", "Unsubscribe a prepaid server", func(cli *huawei.SRegion, args *InstanceOrderUnsubscribeOptions) error { 157 instance, e := cli.GetInstanceByID(args.ID) 158 if e != nil { 159 return e 160 } 161 162 _, err := cli.UnsubscribeInstance(instance.GetId(), args.DOMAIN) 163 if err != nil { 164 return err 165 } 166 return nil 167 }) 168 169 type InstanceSaveImageOptions struct { 170 ID string `help:"Instance ID"` 171 IMAGE_NAME string `help:"Image name"` 172 Notes string `hlep:"Image desc"` 173 } 174 shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *huawei.SRegion, args *InstanceSaveImageOptions) error { 175 opts := cloudprovider.SaveImageOptions{ 176 Name: args.IMAGE_NAME, 177 Notes: args.Notes, 178 } 179 image, err := cli.SaveImage(args.ID, &opts) 180 if err != nil { 181 return err 182 } 183 printObject(image) 184 return nil 185 }) 186 187 type InstanceSetTagsOptions struct { 188 ID string `help:"Instance ID"` 189 Tags []string 190 } 191 shellutils.R(&InstanceSetTagsOptions{}, "instance-set-tags", "get intance metadata", func(cli *huawei.SRegion, args *InstanceSetTagsOptions) error { 192 tags := map[string]string{} 193 for i := range args.Tags { 194 splited := strings.Split(args.Tags[i], "=") 195 if len(splited) == 2 { 196 tags[splited[0]] = splited[1] 197 } 198 } 199 err := cli.CreateServerTags(args.ID, tags) 200 if err != nil { 201 return err 202 } 203 return nil 204 }) 205 206 type InstanceDelTagsOptions struct { 207 ID string `help:"Instance ID"` 208 Tags []string 209 } 210 shellutils.R(&InstanceDelTagsOptions{}, "instance-del-tags", "del intance metadata", func(cli *huawei.SRegion, args *InstanceDelTagsOptions) error { 211 212 err := cli.DeleteServerTags(args.ID, args.Tags) 213 if err != nil { 214 return err 215 } 216 return nil 217 }) 218 219 type InstanceUpdateNameOptions struct { 220 ID string `help:"Instance ID"` 221 Name string 222 } 223 shellutils.R(&InstanceUpdateNameOptions{}, "instance-set-name", "set intance name", func(cli *huawei.SRegion, args *InstanceUpdateNameOptions) error { 224 225 err := cli.UpdateVM(args.ID, args.Name) 226 if err != nil { 227 return err 228 } 229 return nil 230 }) 231 }