yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aws/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  	"io/ioutil"
    21  
    22  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    23  	"yunion.io/x/cloudmux/pkg/multicloud/aws"
    24  	"yunion.io/x/onecloud/pkg/util/shellutils"
    25  )
    26  
    27  func init() {
    28  	type InstanceListOptions struct {
    29  		Id     []string `help:"IDs of instances to show"`
    30  		Zone   string   `help:"Zone ID"`
    31  		Limit  int      `help:"page size"`
    32  		Offset int      `help:"page offset"`
    33  	}
    34  	shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *aws.SRegion, args *InstanceListOptions) error {
    35  		instances, total, e := cli.GetInstances(args.Zone, args.Id, args.Offset, args.Limit)
    36  		if e != nil {
    37  			return e
    38  		}
    39  		printList(instances, total, args.Offset, args.Limit, []string{})
    40  		return nil
    41  	})
    42  
    43  	type InstanceCreateOptions struct {
    44  		NAME      string `help:"name of instance"`
    45  		IMAGE     string `help:"image ID"`
    46  		CPU       int    `help:"CPU count"`
    47  		MEMORYGB  int    `help:"MemoryGB"`
    48  		Disk      []int  `help:"Data disk sizes int GB"`
    49  		STORAGE   string `help:"Storage type" choices:"gp2|io1|st1|sc1|standard"`
    50  		NETWORK   string `help:"Network ID"`
    51  		PUBLICKEY string `help:"PublicKey file path"`
    52  	}
    53  	shellutils.R(&InstanceCreateOptions{}, "instance-create", "Create a instance", func(cli *aws.SRegion, args *InstanceCreateOptions) error {
    54  		content, err := ioutil.ReadFile(args.PUBLICKEY)
    55  		if err != nil {
    56  			return err
    57  		}
    58  		instance, e := cli.CreateInstanceSimple(args.NAME, args.IMAGE, args.CPU, args.MEMORYGB, args.STORAGE, args.Disk, args.NETWORK, string(content))
    59  		if e != nil {
    60  			return e
    61  		}
    62  		printObject(instance)
    63  		return nil
    64  	})
    65  
    66  	type InstanceDiskOperationOptions struct {
    67  		ID   string `help:"instance ID"`
    68  		DISK string `help:"disk ID"`
    69  	}
    70  
    71  	type InstanceDiskAttachOptions struct {
    72  		ID     string `help:"instance ID"`
    73  		DISK   string `help:"disk ID"`
    74  		DEVICE string `help:"disk device name. eg. /dev/sdb"`
    75  	}
    76  
    77  	shellutils.R(&InstanceDiskAttachOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *aws.SRegion, args *InstanceDiskAttachOptions) error {
    78  		err := cli.AttachDisk(args.ID, args.DISK, args.DEVICE)
    79  		if err != nil {
    80  			return err
    81  		}
    82  		return nil
    83  	})
    84  
    85  	shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *aws.SRegion, args *InstanceDiskOperationOptions) error {
    86  		err := cli.DetachDisk(args.ID, args.DISK)
    87  		if err != nil {
    88  			return err
    89  		}
    90  		return nil
    91  	})
    92  
    93  	type InstanceOperationOptions struct {
    94  		ID string `help:"instance ID"`
    95  	}
    96  	shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *aws.SRegion, args *InstanceOperationOptions) error {
    97  		err := cli.StartVM(args.ID)
    98  		if err != nil {
    99  			return err
   100  		}
   101  		return nil
   102  	})
   103  
   104  	type InstanceStopOptions struct {
   105  		ID    string `help:"instance ID"`
   106  		Force bool   `help:"Force stop instance"`
   107  	}
   108  	shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *aws.SRegion, args *InstanceStopOptions) error {
   109  		err := cli.StopVM(args.ID, args.Force)
   110  		if err != nil {
   111  			return err
   112  		}
   113  		return nil
   114  	})
   115  	shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *aws.SRegion, args *InstanceOperationOptions) error {
   116  		err := cli.DeleteVM(args.ID)
   117  		if err != nil {
   118  			return err
   119  		}
   120  		return nil
   121  	})
   122  
   123  	/*
   124  		server-change-config 更改系统配置
   125  		server-reset
   126  	*/
   127  	type InstanceDeployOptions struct {
   128  		ID            string `help:"instance ID"`
   129  		Name          string `help:"new instance name"`
   130  		Hostname      string `help:"new hostname"`
   131  		Keypair       string `help:"Keypair Name"`
   132  		DeleteKeypair bool   `help:"Remove SSH keypair"`
   133  		Password      string `help:"new password"`
   134  		// ResetPassword bool   `help:"Force reset password"`
   135  		Description string `help:"new instances description"`
   136  	}
   137  
   138  	shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *aws.SRegion, args *InstanceDeployOptions) error {
   139  		err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description)
   140  		if err != nil {
   141  			return err
   142  		}
   143  		return nil
   144  	})
   145  
   146  	type InstanceRebuildRootOptions struct {
   147  		ID    string `help:"instance ID"`
   148  		Image string `help:"Image ID"`
   149  		Size  int    `help:"system disk size in GB"`
   150  	}
   151  
   152  	shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *aws.SRegion, args *InstanceRebuildRootOptions) error {
   153  		ctx := context.Background()
   154  		img, err := cli.GetImage(args.Image)
   155  		if err != nil {
   156  			return err
   157  		}
   158  
   159  		diskID, err := cli.ReplaceSystemDisk(ctx, args.ID, img, args.Size, "", "")
   160  		if err != nil {
   161  			return err
   162  		}
   163  		fmt.Printf("New diskID is %s", diskID)
   164  		return nil
   165  	})
   166  
   167  	type InstanceChangeConfigOptions struct {
   168  		ID             string `help:"instance ID"`
   169  		InstanceTypeId string `help:"instance type"`
   170  		Disk           []int  `help:"Data disk sizes int GB"`
   171  	}
   172  
   173  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *aws.SRegion, args *InstanceChangeConfigOptions) error {
   174  		instance, e := cli.GetInstance(args.ID)
   175  		if e != nil {
   176  			return e
   177  		}
   178  
   179  		// todo : add create disks
   180  		err := cli.ChangeVMConfig2(instance.ZoneId, args.ID, args.InstanceTypeId, nil)
   181  		if err != nil {
   182  			return err
   183  		}
   184  		return nil
   185  	})
   186  
   187  	type InstanceSaveImageOptions struct {
   188  		ID         string `help:"Instance ID"`
   189  		IMAGE_NAME string `help:"Image name"`
   190  		Notes      string `hlep:"Image desc"`
   191  	}
   192  	shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *aws.SRegion, args *InstanceSaveImageOptions) error {
   193  		opts := cloudprovider.SaveImageOptions{
   194  			Name:  args.IMAGE_NAME,
   195  			Notes: args.Notes,
   196  		}
   197  		image, err := cli.SaveImage(args.ID, &opts)
   198  		if err != nil {
   199  			return err
   200  		}
   201  		printObject(image)
   202  		return nil
   203  	})
   204  
   205  }