yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcso/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 huawei "yunion.io/x/cloudmux/pkg/multicloud/hcso" 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 type InstanceSaveImageOptions struct { 157 ID string `help:"Instance ID"` 158 IMAGE_NAME string `help:"Image name"` 159 Notes string `hlep:"Image desc"` 160 } 161 shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *huawei.SRegion, args *InstanceSaveImageOptions) error { 162 opts := cloudprovider.SaveImageOptions{ 163 Name: args.IMAGE_NAME, 164 Notes: args.Notes, 165 } 166 image, err := cli.SaveImage(args.ID, &opts) 167 if err != nil { 168 return err 169 } 170 printObject(image) 171 return nil 172 }) 173 174 type InstanceSetTagsOptions struct { 175 ID string `help:"Instance ID"` 176 Tags []string 177 } 178 shellutils.R(&InstanceSetTagsOptions{}, "instance-set-tags", "get intance metadata", func(cli *huawei.SRegion, args *InstanceSetTagsOptions) error { 179 tags := map[string]string{} 180 for i := range args.Tags { 181 splited := strings.Split(args.Tags[i], "=") 182 if len(splited) == 2 { 183 tags[splited[0]] = splited[1] 184 } 185 } 186 err := cli.CreateServerTags(args.ID, tags) 187 if err != nil { 188 return err 189 } 190 return nil 191 }) 192 193 type InstanceDelTagsOptions struct { 194 ID string `help:"Instance ID"` 195 Tags []string 196 } 197 shellutils.R(&InstanceDelTagsOptions{}, "instance-del-tags", "del intance metadata", func(cli *huawei.SRegion, args *InstanceDelTagsOptions) error { 198 199 err := cli.DeleteServerTags(args.ID, args.Tags) 200 if err != nil { 201 return err 202 } 203 return nil 204 }) 205 206 type InstanceUpdateNameOptions struct { 207 ID string `help:"Instance ID"` 208 Name string 209 } 210 shellutils.R(&InstanceUpdateNameOptions{}, "instance-set-name", "set intance name", func(cli *huawei.SRegion, args *InstanceUpdateNameOptions) error { 211 212 err := cli.UpdateVM(args.ID, args.Name) 213 if err != nil { 214 return err 215 } 216 return nil 217 }) 218 }