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