yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/proxmox/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  	"strconv"
    19  
    20  	"yunion.io/x/cloudmux/pkg/multicloud/proxmox"
    21  	"yunion.io/x/onecloud/pkg/util/shellutils"
    22  )
    23  
    24  func init() {
    25  	type InstanceListOptions struct {
    26  		HOST_ID string
    27  	}
    28  	shellutils.R(&InstanceListOptions{}, "instance-list", "list instances", func(cli *proxmox.SRegion, args *InstanceListOptions) error {
    29  		instances, err := cli.GetInstances(args.HOST_ID)
    30  		if err != nil {
    31  			return err
    32  		}
    33  		printList(instances, 0, 0, 0, []string{})
    34  		return nil
    35  	})
    36  
    37  	type InstanceIdOptions struct {
    38  		ID string
    39  	}
    40  
    41  	shellutils.R(&InstanceIdOptions{}, "instance-show", "show instance", func(cli *proxmox.SRegion, args *InstanceIdOptions) error {
    42  		ret, err := cli.GetInstance(args.ID)
    43  		if err != nil {
    44  			return err
    45  		}
    46  		printObject(ret)
    47  		return nil
    48  	})
    49  
    50  	shellutils.R(&InstanceIdOptions{}, "instance-stop", "stop instance", func(cli *proxmox.SRegion, args *InstanceIdOptions) error {
    51  		id, _ := strconv.Atoi(args.ID)
    52  		return cli.StopVm(id)
    53  	})
    54  
    55  	shellutils.R(&InstanceIdOptions{}, "instance-delete", "delete instance", func(cli *proxmox.SRegion, args *InstanceIdOptions) error {
    56  		id, _ := strconv.Atoi(args.ID)
    57  		return cli.DeleteVM(id)
    58  	})
    59  
    60  	type InstanceStartOptions struct {
    61  		ID        string
    62  		Password  string
    63  		PublicKey string
    64  	}
    65  
    66  	shellutils.R(&InstanceStartOptions{}, "instance-start", "Start instance", func(cli *proxmox.SRegion, args *InstanceStartOptions) error {
    67  		id, _ := strconv.Atoi(args.ID)
    68  		return cli.StartVm(id)
    69  	})
    70  
    71  	type InstanceAttachDiskOptions struct {
    72  		ID      string
    73  		DISK_ID string
    74  	}
    75  
    76  	shellutils.R(&InstanceAttachDiskOptions{}, "instance-attach-disk", "Attach instance disk", func(cli *proxmox.SRegion, args *InstanceAttachDiskOptions) error {
    77  		id, _ := strconv.Atoi(args.ID)
    78  		return cli.AttachDisk(id, args.DISK_ID)
    79  	})
    80  
    81  	shellutils.R(&InstanceAttachDiskOptions{}, "instance-detach-disk", "Attach instance disk", func(cli *proxmox.SRegion, args *InstanceAttachDiskOptions) error {
    82  		id, _ := strconv.Atoi(args.ID)
    83  		return cli.DetachDisk(id, args.DISK_ID)
    84  	})
    85  
    86  	type InstanceChangeConfigOptions struct {
    87  		ID    string
    88  		Cpu   int
    89  		MemMb int
    90  	}
    91  
    92  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Change instance config", func(cli *proxmox.SRegion, args *InstanceChangeConfigOptions) error {
    93  		id, _ := strconv.Atoi(args.ID)
    94  		return cli.ChangeConfig(id, args.Cpu, args.MemMb)
    95  	})
    96  
    97  	type InstanceCreateOptions struct {
    98  		Name  string
    99  		Node  string
   100  		Cpu   int
   101  		MemMb int
   102  	}
   103  
   104  	shellutils.R(&InstanceCreateOptions{}, "instance-create", "create instance ", func(cli *proxmox.SRegion, args *InstanceCreateOptions) error {
   105  		ret, err := cli.GenVM(args.Name, args.Node, args.Cpu, args.MemMb)
   106  		printObject(ret)
   107  		return err
   108  	})
   109  
   110  }