yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/azure/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/azure" 24 "yunion.io/x/onecloud/pkg/util/shellutils" 25 ) 26 27 func init() { 28 type InstanceListOptions struct { 29 Classic bool `help:"List classic instance"` 30 ScaleSets bool `help:"List Scale Sets instance"` 31 } 32 shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *azure.SRegion, args *InstanceListOptions) error { 33 instances, err := cli.GetInstances() 34 if err != nil { 35 return err 36 } 37 printList(instances, len(instances), 0, 0, []string{}) 38 return nil 39 }) 40 41 shellutils.R(&InstanceListOptions{}, "classic-instance-list", "List classic instance", func(cli *azure.SRegion, args *InstanceListOptions) error { 42 instances, err := cli.GetClassicInstances() 43 if err != nil { 44 return err 45 } 46 printList(instances, len(instances), 0, 0, []string{}) 47 return nil 48 }) 49 50 type SClassicInstacneIdOptions struct { 51 ID string 52 } 53 54 shellutils.R(&SClassicInstacneIdOptions{}, "classic-instance-disk-list", "List classic instance disks", func(cli *azure.SRegion, args *SClassicInstacneIdOptions) error { 55 disks, err := cli.GetClassicInstanceDisks(args.ID) 56 if err != nil { 57 return err 58 } 59 printList(disks, len(disks), 0, 0, []string{}) 60 return nil 61 }) 62 63 shellutils.R(&InstanceListOptions{}, "instance-scaleset-list", "List classic instance", func(cli *azure.SRegion, args *InstanceListOptions) error { 64 instances, err := cli.GetInstanceScaleSets() 65 if err != nil { 66 return err 67 } 68 printList(instances, len(instances), 0, 0, []string{}) 69 return nil 70 }) 71 72 type InstanceSizeListOptions struct { 73 } 74 75 shellutils.R(&InstanceSizeListOptions{}, "instance-size-list", "List intances", func(cli *azure.SRegion, args *InstanceSizeListOptions) error { 76 vmSizes, err := cli.ListVmSizes() 77 if err != nil { 78 return err 79 } 80 printList(vmSizes, 0, 0, 0, nil) 81 return nil 82 }) 83 shellutils.R(&InstanceSizeListOptions{}, "resource-sku-list", "List resource sku", func(cli *azure.SRegion, args *InstanceSizeListOptions) error { 84 skus, err := cli.GetClient().ListResourceSkus() 85 if err != nil { 86 return err 87 } 88 printList(skus, len(skus), 0, 0, []string{}) 89 return nil 90 }) 91 92 type InstanceCreateOptions struct { 93 NAME string `help:"Name of instance"` 94 IMAGE string `help:"image ID"` 95 CPU int `help:"CPU count"` 96 MEMORYMB int `help:"MemoryMb"` 97 InstanceType string `help:"Instance Type"` 98 SYSDISKSIZEGB int `help:"System Disk Size"` 99 Disk []int `help:"Data disk sizes int GB"` 100 STORAGE string `help:"Storage type"` 101 NETWORK string `help:"Network ID"` 102 PASSWD string `help:"password"` 103 PublicKey string `help:"PublicKey"` 104 OsType string `help:"Operation system type" choices:"Linux|Windows"` 105 } 106 shellutils.R(&InstanceCreateOptions{}, "instance-create", "Create a instance", func(cli *azure.SRegion, args *InstanceCreateOptions) error { 107 instance, e := cli.CreateInstanceSimple(args.NAME, args.IMAGE, args.OsType, args.CPU, args.MEMORYMB, args.SYSDISKSIZEGB, args.STORAGE, args.Disk, args.NETWORK, args.PASSWD, args.PublicKey) 108 if e != nil { 109 return e 110 } 111 printObject(instance) 112 return nil 113 }) 114 115 type InstanceOptions struct { 116 ID string `help:"Instance ID"` 117 } 118 shellutils.R(&InstanceOptions{}, "instance-show", "Show intance detail", func(cli *azure.SRegion, args *InstanceOptions) error { 119 if instance, err := cli.GetInstance(args.ID); err != nil { 120 return err 121 } else { 122 printObject(instance) 123 return nil 124 } 125 }) 126 127 shellutils.R(&InstanceOptions{}, "instance-start", "Start intance", func(cli *azure.SRegion, args *InstanceOptions) error { 128 return cli.StartVM(args.ID) 129 }) 130 131 shellutils.R(&InstanceOptions{}, "instance-delete", "Delete intance", func(cli *azure.SRegion, args *InstanceOptions) error { 132 return cli.DeleteVM(args.ID) 133 }) 134 135 shellutils.R(&InstanceOptions{}, "instance-stop", "Stop intance", func(cli *azure.SRegion, args *InstanceOptions) error { 136 return cli.StopVM(args.ID, true) 137 }) 138 139 type InstanceRebuildOptions struct { 140 ID string `help:"Instance ID"` 141 CPU int `help:"Instance CPU core"` 142 MEMORYMB int `help:"Instance Memory MB"` 143 IMAGE string `help:"Image ID"` 144 Password string `help:"pasword"` 145 PublicKey string `help:"Public Key"` 146 Size int `help:"system disk size in GB"` 147 } 148 shellutils.R(&InstanceRebuildOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *azure.SRegion, args *InstanceRebuildOptions) error { 149 instance, err := cli.GetInstance(args.ID) 150 if err != nil { 151 return err 152 } 153 diskId, err := cli.ReplaceSystemDisk(instance, args.CPU, args.MEMORYMB, args.IMAGE, args.Password, args.PublicKey, args.Size) 154 if err != nil { 155 return err 156 } 157 fmt.Printf("New diskId is %s", diskId) 158 return nil 159 }) 160 161 type InstanceDiskOptions struct { 162 ID string `help:"Instance ID"` 163 DISK string `help:"Disk ID"` 164 } 165 shellutils.R(&InstanceDiskOptions{}, "instance-attach-disk", "Attach a disk to intance", func(cli *azure.SRegion, args *InstanceDiskOptions) error { 166 return cli.AttachDisk(args.ID, args.DISK) 167 }) 168 169 shellutils.R(&InstanceDiskOptions{}, "instance-detach-disk", "Attach a disk to intance", func(cli *azure.SRegion, args *InstanceDiskOptions) error { 170 return cli.DetachDisk(args.ID, args.DISK) 171 }) 172 173 type InstanceConfigOptions struct { 174 ID string `help:"Instance ID"` 175 INSTANCE_TYPE string `help:"Instance Vm Size"` 176 } 177 178 shellutils.R(&InstanceConfigOptions{}, "instance-change-config", "Attach a disk to intance", func(cli *azure.SRegion, args *InstanceConfigOptions) error { 179 return cli.ChangeConfig(args.ID, args.INSTANCE_TYPE) 180 }) 181 182 type InstanceDeployOptions struct { 183 ID string `help:"Instance ID"` 184 OsType string `help:"Instance Os Type" choices:"Linux|Windows" default:"Linux"` 185 Password string `help:"Password for instance"` 186 PublicKey string `helo:"Deploy ssh_key for instance"` 187 } 188 189 shellutils.R(&InstanceDeployOptions{}, "instance-reset-password", "Reset intance password", func(cli *azure.SRegion, args *InstanceDeployOptions) error { 190 return cli.DeployVM(context.Background(), args.ID, args.OsType, "", args.Password, args.PublicKey, false, "") 191 }) 192 193 type InstanceSecurityGroupOptions struct { 194 ID string `help:"Instance ID"` 195 SecurityGroup string `help:"Security Group ID or Name"` 196 } 197 198 shellutils.R(&InstanceSecurityGroupOptions{}, "instance-set-secgrp", "Attach a disk to intance", func(cli *azure.SRegion, args *InstanceSecurityGroupOptions) error { 199 return cli.SetSecurityGroup(args.ID, args.SecurityGroup) 200 }) 201 202 type InstanceSaveImageOptions struct { 203 DISK_ID string `help:"Instance Os Disk ID"` 204 IMAGE_NAME string `help:"Image name"` 205 Notes string `hlep:"Image desc"` 206 OsType string `help:"Os Type" choices:"Linux|Windows" default:"Linux"` 207 } 208 shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *azure.SRegion, args *InstanceSaveImageOptions) error { 209 opts := cloudprovider.SaveImageOptions{ 210 Name: args.IMAGE_NAME, 211 Notes: args.Notes, 212 } 213 image, err := cli.SaveImage(args.OsType, args.DISK_ID, &opts) 214 if err != nil { 215 return err 216 } 217 printObject(image) 218 return nil 219 }) 220 221 shellutils.R(&InstanceOptions{}, "instance-get-tags", "get intance tags", func(cli *azure.SRegion, args *InstanceOptions) error { 222 tags, err := cli.GetClient().GetTags(args.ID) 223 if err != nil { 224 return err 225 } 226 printObject(tags) 227 return nil 228 }) 229 230 type InstanceSetTagsOptions struct { 231 ID string `help:"Instance ID"` 232 Tags []string 233 } 234 shellutils.R(&InstanceSetTagsOptions{}, "instance-set-tags", "set intance metadata", func(cli *azure.SRegion, args *InstanceSetTagsOptions) error { 235 tags := map[string]string{} 236 for i := range args.Tags { 237 splited := strings.Split(args.Tags[i], "=") 238 if len(splited) == 2 { 239 tags[splited[0]] = splited[1] 240 } 241 } 242 result, err := cli.GetClient().SetTags(args.ID, tags) 243 if err != nil { 244 return err 245 } 246 printObject(result) 247 return nil 248 }) 249 250 }