yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/esxi/shell/virtualmachine.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  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/multicloud/esxi"
    24  	"yunion.io/x/onecloud/pkg/util/shellutils"
    25  )
    26  
    27  func init() {
    28  	type VirtualMachineListOptions struct {
    29  		Datacenter string `help:"Datacenter"`
    30  		HostIP     string `help:"HostIP"`
    31  		FakeRegex  string `help:"regex for fake template' name"`
    32  		Datastore  string `help:"Datastore"`
    33  		Template   bool   `help:"Whether it is tempalte virtual machine, default:false"`
    34  	}
    35  	shellutils.R(&VirtualMachineListOptions{}, "vm-list", "List vms of a host", func(cli *esxi.SESXiClient, args *VirtualMachineListOptions) error {
    36  		var (
    37  			vms []*esxi.SVirtualMachine
    38  			err error
    39  		)
    40  		switch {
    41  		case len(args.HostIP) > 0:
    42  			host, err := cli.FindHostByIp(args.HostIP)
    43  			if err != nil {
    44  				return err
    45  			}
    46  			if args.Template {
    47  				vms, err := host.GetTemplateVMs()
    48  				if err != nil {
    49  					return err
    50  				}
    51  				printList(vms, []string{})
    52  				return nil
    53  			}
    54  			vms, err := host.GetIVMs2()
    55  			if err != nil {
    56  				return err
    57  			}
    58  			printList(vms, []string{})
    59  			return nil
    60  		case len(args.Datacenter) > 0:
    61  			var fetcher esxi.VMFetcher
    62  			if len(args.Datastore) > 0 {
    63  				ds, err := getDatastore(cli, args.Datacenter, args.Datastore)
    64  				if err != nil {
    65  					return err
    66  				}
    67  				fetcher = ds
    68  			} else {
    69  				dc, err := cli.FindDatacenterByMoId(args.Datacenter)
    70  				if err != nil {
    71  					return errors.Wrap(err, "FindDatacenterByMoId")
    72  				}
    73  				fetcher = dc
    74  			}
    75  			if args.Template {
    76  				if len(args.FakeRegex) > 0 {
    77  					fmt.Printf("regex: %s\n", args.FakeRegex)
    78  					vms, err = fetcher.FetchFakeTempateVMs(args.FakeRegex)
    79  					if err != nil {
    80  						return errors.Wrap(err, "FetchFakeTempateVMs")
    81  					}
    82  				} else {
    83  					vms, err = fetcher.FetchTemplateVMs()
    84  					if err != nil {
    85  						return errors.Wrap(err, "FetchTemplateVMs")
    86  					}
    87  				}
    88  			} else {
    89  				vms, err = fetcher.FetchNoTemplateVMs()
    90  				if err != nil {
    91  					return errors.Wrap(err, "FetchNoTemplateVMs")
    92  				}
    93  			}
    94  		default:
    95  			return fmt.Errorf("Both Datacenter and HostIP cannot be empty")
    96  		}
    97  		printList(vms, []string{})
    98  		return nil
    99  	})
   100  
   101  	type VirtualMachineCloneOptions struct {
   102  		HOSTIP     string `help:"Host IP"`
   103  		TEMPLATEID string `help:"id of template ma"`
   104  		NAME       string `help:"New VM's name'"`
   105  		Uuid       string `help:"Uuid of new VM"`
   106  		CpuNum     int    `help:"Number of CPU"`
   107  		MemSize    int    `help:"Size of Memory(MB)"`
   108  	}
   109  	shellutils.R(&VirtualMachineCloneOptions{}, "vm-clone", "Clone vm", func(cli *esxi.SESXiClient,
   110  		args *VirtualMachineCloneOptions) error {
   111  		host, err := cli.FindHostByIp(args.HOSTIP)
   112  		if err != nil {
   113  			return err
   114  		}
   115  		idss, err := host.GetDataStores()
   116  		if err != nil {
   117  			return err
   118  		}
   119  		if len(idss) == 0 {
   120  			return fmt.Errorf("no datastore")
   121  		}
   122  		temVm, err := host.GetTemplateVMById(args.TEMPLATEID)
   123  		if err != nil {
   124  			return err
   125  		}
   126  		createParams := esxi.SCreateVMParam{
   127  			Name: args.NAME,
   128  			Uuid: args.Uuid,
   129  			Cpu:  args.CpuNum,
   130  			Mem:  args.MemSize,
   131  		}
   132  		vm, err := host.CloneVM(context.Background(), temVm, nil, idss[0].(*esxi.SDatastore), createParams)
   133  		if err != nil {
   134  			return errors.Wrap(err, "SHost.CloneVMFromTemplate")
   135  		}
   136  		printObject(vm)
   137  		return nil
   138  	})
   139  
   140  	type VirtualMachineShowOptions struct {
   141  		Datacenter string `help:"Datacenter"`
   142  		HostIP     string `help:"Host IP"`
   143  		VMID       string `help:"VM ID"`
   144  	}
   145  	getVM := func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) (*esxi.SVirtualMachine, error) {
   146  		var vm *esxi.SVirtualMachine
   147  		switch {
   148  		case len(args.HostIP) > 0:
   149  			host, err := cli.FindHostByIp(args.HostIP)
   150  			if err != nil {
   151  				return nil, errors.Wrap(err, "FindHostByIp")
   152  			}
   153  			ivm, err := host.GetIVMById(args.VMID)
   154  			if err != nil && errors.Cause(err) != errors.ErrNotFound {
   155  				return nil, err
   156  			}
   157  			if err != nil {
   158  				vm, err = host.GetTemplateVMById(args.VMID)
   159  				if err != nil {
   160  					return nil, errors.Wrap(err, "GetTemplateVMById")
   161  				}
   162  			}
   163  			vm = ivm.(*esxi.SVirtualMachine)
   164  		case len(args.Datacenter) > 0:
   165  			dc, err := cli.FindDatacenterByMoId(args.Datacenter)
   166  			if err != nil {
   167  				return nil, errors.Wrap(err, "FindDatacenterByMoId")
   168  			}
   169  			vm, err = dc.FetchVMById(args.VMID)
   170  			if err != nil {
   171  				return nil, errors.Wrap(err, "FetchVMById")
   172  			}
   173  		default:
   174  			return nil, fmt.Errorf("Both Datacenter and HostIP cannot be empty")
   175  		}
   176  		return vm, nil
   177  	}
   178  	shellutils.R(&VirtualMachineShowOptions{}, "vm-show", "Show vm details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   179  		vm, err := getVM(cli, args)
   180  		if err != nil {
   181  			return err
   182  		}
   183  		printObject(vm)
   184  		return nil
   185  	})
   186  
   187  	shellutils.R(&VirtualMachineShowOptions{}, "vm-nics", "Show vm nics details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   188  		vm, err := getVM(cli, args)
   189  		if err != nil {
   190  			return err
   191  		}
   192  		vmnics, err := vm.GetINics()
   193  		if err != nil {
   194  			return err
   195  		}
   196  		printList(vmnics, []string{})
   197  		return nil
   198  	})
   199  
   200  	shellutils.R(&VirtualMachineShowOptions{}, "vm-snapshots", "Show vm snapshots details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   201  		vm, err := getVM(cli, args)
   202  		if err != nil {
   203  			return err
   204  		}
   205  		vmsps, err := vm.GetInstanceSnapshots()
   206  		if err != nil {
   207  			return err
   208  		}
   209  		printList(vmsps, []string{})
   210  		return nil
   211  	})
   212  
   213  	type VirtualMachineSnapshotCreateOptions struct {
   214  		VirtualMachineShowOptions
   215  		NAME string `help:"Name of snapshot"`
   216  		Desc string `help:"Description of snapshot"`
   217  	}
   218  	shellutils.R(&VirtualMachineSnapshotCreateOptions{}, "vm-snapshot-create", "Create vm snapshot", func(cli *esxi.SESXiClient, args *VirtualMachineSnapshotCreateOptions) error {
   219  		vm, err := getVM(cli, &args.VirtualMachineShowOptions)
   220  		if err != nil {
   221  			return err
   222  		}
   223  		sp, err := vm.CreateInstanceSnapshot(context.Background(), args.NAME, args.Desc)
   224  		if err != nil {
   225  			return err
   226  		}
   227  		printObject(sp)
   228  		return nil
   229  	})
   230  
   231  	type VirtualMachineSnapshotOptions struct {
   232  		VirtualMachineShowOptions
   233  		ID string `help:"ID of snapshot"`
   234  	}
   235  	shellutils.R(&VirtualMachineSnapshotOptions{}, "vm-snapshot-delete", "Delete vm snapshot", func(cli *esxi.SESXiClient, args *VirtualMachineSnapshotOptions) error {
   236  		vm, err := getVM(cli, &args.VirtualMachineShowOptions)
   237  		if err != nil {
   238  			return err
   239  		}
   240  		sp, err := vm.GetInstanceSnapshot(args.ID)
   241  		if err != nil {
   242  			return err
   243  		}
   244  		err = sp.Delete()
   245  		if err != nil {
   246  			return err
   247  		}
   248  		return nil
   249  	})
   250  	shellutils.R(&VirtualMachineSnapshotOptions{}, "vm-snapshot-reset", "Reset vm to snapshot", func(cli *esxi.SESXiClient, args *VirtualMachineSnapshotOptions) error {
   251  		vm, err := getVM(cli, &args.VirtualMachineShowOptions)
   252  		if err != nil {
   253  			return err
   254  		}
   255  		err = vm.ResetToInstanceSnapshot(context.Background(), args.ID)
   256  		if err != nil {
   257  			return err
   258  		}
   259  		return nil
   260  	})
   261  
   262  	shellutils.R(&VirtualMachineShowOptions{}, "vm-disks", "Show vm disks details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   263  		vm, err := getVM(cli, args)
   264  		if err != nil {
   265  			return err
   266  		}
   267  		vmdisks, err := vm.GetIDisks()
   268  		if err != nil {
   269  			return err
   270  		}
   271  		printList(vmdisks, []string{})
   272  		return nil
   273  	})
   274  
   275  	type VirtualMachineDiskResizeOptions struct {
   276  		VirtualMachineShowOptions
   277  		DISKIDX int   `help:"disk index"`
   278  		SIZEGB  int64 `help:"new size of disk"`
   279  	}
   280  	shellutils.R(&VirtualMachineDiskResizeOptions{}, "vm-disk-resize", "Resize a vm disk", func(cli *esxi.SESXiClient, args *VirtualMachineDiskResizeOptions) error {
   281  		vm, err := getVM(cli, &args.VirtualMachineShowOptions)
   282  		if err != nil {
   283  			return err
   284  		}
   285  		vmdisks, err := vm.GetIDisks()
   286  		if err != nil {
   287  			return err
   288  		}
   289  		if args.DISKIDX < 0 || args.DISKIDX >= len(vmdisks) {
   290  			return fmt.Errorf("Out of index: %d", args.DISKIDX)
   291  		}
   292  		disk := vmdisks[args.DISKIDX]
   293  		ctx := context.Background()
   294  		return disk.Resize(ctx, args.SIZEGB*1024)
   295  	})
   296  
   297  	shellutils.R(&VirtualMachineShowOptions{}, "vm-vnc", "Show vm VNC details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   298  		vm, err := getVM(cli, args)
   299  		if err != nil {
   300  			return err
   301  		}
   302  		info, err := vm.GetVNCInfo(nil)
   303  		if err != nil {
   304  			return err
   305  		}
   306  		printObject(info)
   307  		return nil
   308  	})
   309  
   310  	shellutils.R(&VirtualMachineShowOptions{}, "vm-vmrc", "Show vm VMRC connection", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   311  		vm, err := getVM(cli, args)
   312  		if err != nil {
   313  			return err
   314  		}
   315  		info, err := vm.GetVmrcInfo()
   316  		if err != nil {
   317  			return err
   318  		}
   319  		printObject(info)
   320  		return nil
   321  	})
   322  
   323  	shellutils.R(&VirtualMachineShowOptions{}, "vm-webmks", "Show vm webmks connection", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   324  		vm, err := getVM(cli, args)
   325  		if err != nil {
   326  			return err
   327  		}
   328  		info, err := vm.GetWebmksInfo()
   329  		if err != nil {
   330  			return err
   331  		}
   332  		printObject(info)
   333  		return nil
   334  	})
   335  
   336  	shellutils.R(&VirtualMachineShowOptions{}, "vm-file-status", "Show vm files details", func(cli *esxi.SESXiClient, args *VirtualMachineShowOptions) error {
   337  		vm, err := getVM(cli, args)
   338  		if err != nil {
   339  			return err
   340  		}
   341  		err = vm.CheckFileInfo(context.Background())
   342  		if err != nil {
   343  			return err
   344  		}
   345  		return nil
   346  	})
   347  
   348  }