yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/hcs/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  	"strconv"
    21  	"strings"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/hcs"
    25  	"yunion.io/x/onecloud/pkg/util/shellutils"
    26  )
    27  
    28  func init() {
    29  	type InstanceListOptions struct {
    30  		Ip string
    31  	}
    32  	shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *hcs.SRegion, args *InstanceListOptions) error {
    33  		instances, e := cli.GetInstances(args.Ip)
    34  		if e != nil {
    35  			return e
    36  		}
    37  		printList(instances, 0, 0, 0, nil)
    38  		return nil
    39  	})
    40  
    41  	type InstanceDiskOperationOptions struct {
    42  		ID   string `help:"instance ID"`
    43  		DISK string `help:"disk ID"`
    44  	}
    45  
    46  	type InstanceDiskAttachOptions struct {
    47  		ID     string `help:"instance ID"`
    48  		DISK   string `help:"disk ID"`
    49  		DEVICE string `help:"disk device name. eg. /dev/sdb"`
    50  	}
    51  
    52  	shellutils.R(&InstanceDiskAttachOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *hcs.SRegion, args *InstanceDiskAttachOptions) error {
    53  		err := cli.AttachDisk(args.ID, args.DISK, args.DEVICE)
    54  		if err != nil {
    55  			return err
    56  		}
    57  		return nil
    58  	})
    59  
    60  	shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *hcs.SRegion, args *InstanceDiskOperationOptions) error {
    61  		err := cli.DetachDisk(args.ID, args.DISK)
    62  		if err != nil {
    63  			return err
    64  		}
    65  		return nil
    66  	})
    67  
    68  	type InstanceOperationOptions struct {
    69  		ID string `help:"instance ID"`
    70  	}
    71  	shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *hcs.SRegion, args *InstanceOperationOptions) error {
    72  		err := cli.StartVM(args.ID)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		return nil
    77  	})
    78  
    79  	shellutils.R(&InstanceOperationOptions{}, "instance-interface-list", "List instance interface", func(cli *hcs.SRegion, args *InstanceOperationOptions) error {
    80  		ret, err := cli.GetInstanceInterfaces(args.ID)
    81  		if err != nil {
    82  			return err
    83  		}
    84  		printList(ret, 0, 0, 0, nil)
    85  		return nil
    86  	})
    87  
    88  	type InstanceStopOptions struct {
    89  		ID    string `help:"instance ID"`
    90  		Force bool   `help:"Force stop instance"`
    91  	}
    92  	shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *hcs.SRegion, args *InstanceStopOptions) error {
    93  		err := cli.StopVM(args.ID, args.Force)
    94  		if err != nil {
    95  			return err
    96  		}
    97  		return nil
    98  	})
    99  	shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *hcs.SRegion, args *InstanceOperationOptions) error {
   100  		err := cli.DeleteVM(args.ID)
   101  		if err != nil {
   102  			return err
   103  		}
   104  		return nil
   105  	})
   106  
   107  	/*
   108  		server-change-config 更改系统配置
   109  		server-reset
   110  	*/
   111  	type InstanceDeployOptions struct {
   112  		ID            string `help:"instance ID"`
   113  		Name          string `help:"new instance name"`
   114  		Hostname      string `help:"new hostname"`
   115  		Keypair       string `help:"Keypair Name"`
   116  		DeleteKeypair bool   `help:"Remove SSH keypair"`
   117  		Password      string `help:"new password"`
   118  		// ResetPassword bool   `help:"Force reset password"`
   119  		Description string `help:"new instances description"`
   120  	}
   121  
   122  	shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *hcs.SRegion, args *InstanceDeployOptions) error {
   123  		err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description)
   124  		if err != nil {
   125  			return err
   126  		}
   127  		return nil
   128  	})
   129  
   130  	type InstanceRebuildRootOptions struct {
   131  		ID            string `help:"instance ID"`
   132  		UserId        string `help:"instance user ID"`
   133  		Image         string `help:"Image ID"`
   134  		Password      string `help:"admin password"`
   135  		PublicKeyName string `help:"public key name"`
   136  		UserData      string `help:"cloud-init user data"`
   137  	}
   138  
   139  	shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *hcs.SRegion, args *InstanceRebuildRootOptions) error {
   140  		ctx := context.Background()
   141  		return cli.ChangeRoot(ctx, args.UserId, args.ID, args.Image, args.Password, args.PublicKeyName, args.UserData)
   142  	})
   143  
   144  	type InstanceChangeConfigOptions struct {
   145  		ID           string `help:"instance ID"`
   146  		InstanceType string `help:"instance type"`
   147  	}
   148  
   149  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *hcs.SRegion, args *InstanceChangeConfigOptions) error {
   150  		err := cli.ChangeVMConfig(args.ID, args.InstanceType)
   151  		if err != nil {
   152  			return err
   153  		}
   154  		return nil
   155  	})
   156  
   157  	type InstanceSaveImageOptions struct {
   158  		ID         string `help:"Instance ID"`
   159  		IMAGE_NAME string `help:"Image name"`
   160  		Notes      string `hlep:"Image desc"`
   161  	}
   162  	shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *hcs.SRegion, args *InstanceSaveImageOptions) error {
   163  		opts := cloudprovider.SaveImageOptions{
   164  			Name:  args.IMAGE_NAME,
   165  			Notes: args.Notes,
   166  		}
   167  		image, err := cli.SaveImage(args.ID, &opts)
   168  		if err != nil {
   169  			return err
   170  		}
   171  		printObject(image)
   172  		return nil
   173  	})
   174  
   175  	type InstanceSetTagsOptions struct {
   176  		ID   string `help:"Instance ID"`
   177  		Tags []string
   178  	}
   179  	shellutils.R(&InstanceSetTagsOptions{}, "instance-set-tags", "get intance metadata", func(cli *hcs.SRegion, args *InstanceSetTagsOptions) error {
   180  		tags := map[string]string{}
   181  		for i := range args.Tags {
   182  			splited := strings.Split(args.Tags[i], "=")
   183  			if len(splited) == 2 {
   184  				tags[splited[0]] = splited[1]
   185  			}
   186  		}
   187  		err := cli.CreateServerTags(args.ID, tags)
   188  		if err != nil {
   189  			return err
   190  		}
   191  		return nil
   192  	})
   193  
   194  	type InstanceDelTagsOptions struct {
   195  		ID   string `help:"Instance ID"`
   196  		Tags []string
   197  	}
   198  	shellutils.R(&InstanceDelTagsOptions{}, "instance-del-tags", "del intance metadata", func(cli *hcs.SRegion, args *InstanceDelTagsOptions) error {
   199  
   200  		err := cli.DeleteServerTags(args.ID, args.Tags)
   201  		if err != nil {
   202  			return err
   203  		}
   204  		return nil
   205  	})
   206  
   207  	type InstanceUpdateNameOptions struct {
   208  		ID   string `help:"Instance ID"`
   209  		Name string
   210  	}
   211  	shellutils.R(&InstanceUpdateNameOptions{}, "instance-set-name", "set intance name", func(cli *hcs.SRegion, args *InstanceUpdateNameOptions) error {
   212  
   213  		err := cli.UpdateVM(args.ID, args.Name)
   214  		if err != nil {
   215  			return err
   216  		}
   217  		return nil
   218  	})
   219  
   220  	type InstanceCreateOptions struct {
   221  		NAME          string
   222  		IMAGE_ID      string
   223  		INSTANCE_TYPE string
   224  		NETWORK_ID    string
   225  		IpAddr        string
   226  		Secgroup      string `default:"default"`
   227  		VPC_ID        string
   228  		ZONE_ID       string
   229  		Desc          string
   230  		Keypair       string
   231  		Password      string
   232  		UserData      string
   233  		ProjectId     string
   234  		DISKS         []string
   235  	}
   236  	shellutils.R(&InstanceCreateOptions{}, "instance-create", "create instance", func(cli *hcs.SRegion, args *InstanceCreateOptions) error {
   237  		sysDisk := cloudprovider.SDiskInfo{}
   238  		dataDisks := []cloudprovider.SDiskInfo{}
   239  		for i, disk := range args.DISKS {
   240  			info := strings.Split(args.DISKS[i], ":")
   241  			if len(info) != 2 {
   242  				return fmt.Errorf("invalid disk %s", disk)
   243  			}
   244  			if i == 0 {
   245  				sysDisk.StorageType = info[0]
   246  				sysDisk.SizeGB, _ = strconv.Atoi(info[1])
   247  			} else {
   248  				dataDisk := cloudprovider.SDiskInfo{}
   249  				dataDisk.StorageType = info[0]
   250  				dataDisk.SizeGB, _ = strconv.Atoi(info[1])
   251  				dataDisks = append(dataDisks, dataDisk)
   252  			}
   253  		}
   254  		vm, err := cli.CreateInstance(args.NAME, args.IMAGE_ID, args.INSTANCE_TYPE, args.NETWORK_ID, args.Secgroup, args.VPC_ID, args.ZONE_ID, args.Desc, sysDisk, dataDisks, args.IpAddr, args.Keypair, args.Password, args.UserData, nil, args.ProjectId, nil)
   255  		if err != nil {
   256  			return err
   257  		}
   258  		printObject(vm)
   259  		return nil
   260  	})
   261  }