yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/ucloud/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  	"yunion.io/x/cloudmux/pkg/multicloud/ucloud"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type InstanceListOptions struct {
    24  	}
    25  	shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *ucloud.SRegion, args *InstanceListOptions) error {
    26  		instances, e := cli.GetInstances("", "")
    27  		if e != nil {
    28  			return e
    29  		}
    30  		printList(instances, 0, 0, 0, nil)
    31  		return nil
    32  	})
    33  
    34  	type InstanceDiskOperationOptions struct {
    35  		ZONE string `help:"zone ID"`
    36  		ID   string `help:"instance ID"`
    37  		DISK string `help:"disk ID"`
    38  	}
    39  
    40  	type InstanceDiskAttachOptions struct {
    41  		ID     string `help:"instance ID"`
    42  		DISK   string `help:"disk ID"`
    43  		DEVICE string `help:"disk device name. eg. /dev/sdb"`
    44  	}
    45  
    46  	shellutils.R(&InstanceDiskAttachOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *ucloud.SRegion, args *InstanceDiskAttachOptions) error {
    47  		err := cli.AttachDisk(args.ID, args.DISK, args.DEVICE)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		return nil
    52  	})
    53  
    54  	shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *ucloud.SRegion, args *InstanceDiskOperationOptions) error {
    55  		err := cli.DetachDisk(args.ZONE, args.ID, args.DISK)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		return nil
    60  	})
    61  
    62  	type InstanceCrateOptions struct {
    63  		NAME         string `help:"name of instance"`
    64  		IMAGE        string `help:"image ID"`
    65  		INSTANCETYPE string `help:"intance type.eg. N2.c1.m1"`
    66  		PASSWORD     string `help:"password"`
    67  		VPC          string `help:"VPC ID"`
    68  		NETWORK      string `help:"Network ID"`
    69  		SECGROUP     string `help:"secgroup ID"`
    70  		ZONE         string `help:"zone ID"`
    71  		STORAGE      string `help:"Storage type" choices:"CLOUD_NORMAL|CLOUD_SSD|LOCAL_NORMAL|LOCAL_SSD"`
    72  		DISKSIZE     int    `help:"root disk size GB"`
    73  	}
    74  	shellutils.R(&InstanceCrateOptions{}, "instance-create", "Create a instance", func(cli *ucloud.SRegion, args *InstanceCrateOptions) error {
    75  		disk := ucloud.SDisk{DiskType: args.STORAGE, SizeGB: args.DISKSIZE}
    76  		i, err := ucloud.ParseInstanceType(args.INSTANCETYPE)
    77  		if err != nil {
    78  			return err
    79  		}
    80  		instance, e := cli.CreateInstance(args.NAME, args.IMAGE, i.UHostType, args.PASSWORD, args.VPC, args.NETWORK, args.SECGROUP, args.ZONE, "", "", i.CPU, i.MemoryMB, i.GPU, []ucloud.SDisk{disk}, nil)
    81  		if e != nil {
    82  			return e
    83  		}
    84  		printObject(instance)
    85  		return nil
    86  	})
    87  
    88  	type InstanceOperationOptions struct {
    89  		ID string `help:"instance ID"`
    90  	}
    91  	shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *ucloud.SRegion, args *InstanceOperationOptions) error {
    92  		err := cli.StartVM(args.ID)
    93  		if err != nil {
    94  			return err
    95  		}
    96  		return nil
    97  	})
    98  
    99  	type InstanceStopOptions struct {
   100  		ID string `help:"instance ID"`
   101  	}
   102  	shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *ucloud.SRegion, args *InstanceStopOptions) error {
   103  		err := cli.StopVM(args.ID)
   104  		if err != nil {
   105  			return err
   106  		}
   107  		return nil
   108  	})
   109  	shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *ucloud.SRegion, args *InstanceOperationOptions) error {
   110  		err := cli.DeleteVM(args.ID)
   111  		if err != nil {
   112  			return err
   113  		}
   114  		return nil
   115  	})
   116  
   117  	/*
   118  		server-change-config 更改系统配置
   119  		server-reset
   120  	*/
   121  	type InstanceDeployOptions struct {
   122  		ID       string `help:"instance ID"`
   123  		Password string `help:"new password"`
   124  	}
   125  
   126  	shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *ucloud.SRegion, args *InstanceDeployOptions) error {
   127  		err := cli.ResetVMPasswd(args.ID, args.Password)
   128  		if err != nil {
   129  			return err
   130  		}
   131  		return nil
   132  	})
   133  
   134  	type InstanceRebuildRootOptions struct {
   135  		ID       string `help:"instance ID"`
   136  		Image    string `help:"Image ID"`
   137  		Password string `help:"admin password"`
   138  	}
   139  
   140  	shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *ucloud.SRegion, args *InstanceRebuildRootOptions) error {
   141  		err := cli.RebuildRoot(args.ID, args.Image, args.Password)
   142  		if err != nil {
   143  			return err
   144  		}
   145  
   146  		return nil
   147  	})
   148  
   149  	type InstanceChangeConfigOptions struct {
   150  		ID   string `help:"instance ID"`
   151  		NCPU int    `help:"cpu"`
   152  		MEM  int    `help:"memory MB"`
   153  	}
   154  
   155  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *ucloud.SRegion, args *InstanceChangeConfigOptions) error {
   156  		err := cli.ResizeVM(args.ID, args.NCPU, args.MEM)
   157  		if err != nil {
   158  			return err
   159  		}
   160  		return nil
   161  	})
   162  }