yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/google/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  	"fmt"
    19  	"strings"
    20  
    21  	"yunion.io/x/pkg/errors"
    22  
    23  	"yunion.io/x/cloudmux/pkg/cloudprovider"
    24  	"yunion.io/x/cloudmux/pkg/multicloud/google"
    25  	"yunion.io/x/onecloud/pkg/util/shellutils"
    26  )
    27  
    28  func init() {
    29  	type InstanceListOptions struct {
    30  		ZONE       string
    31  		MaxResults int
    32  		PageToken  string
    33  	}
    34  	shellutils.R(&InstanceListOptions{}, "instance-list", "List instances", func(cli *google.SRegion, args *InstanceListOptions) error {
    35  		instances, err := cli.GetInstances(args.ZONE, args.MaxResults, args.PageToken)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		printList(instances, 0, 0, 0, nil)
    40  		return nil
    41  	})
    42  
    43  	type InstanceIdOptions struct {
    44  		ID string
    45  	}
    46  	shellutils.R(&InstanceIdOptions{}, "instance-show", "Show instance", func(cli *google.SRegion, args *InstanceIdOptions) error {
    47  		instance, err := cli.GetInstance(args.ID)
    48  		if err != nil {
    49  			return err
    50  		}
    51  		printObject(instance)
    52  		return nil
    53  	})
    54  
    55  	shellutils.R(&InstanceIdOptions{}, "instance-delete", "Delete instance", func(cli *google.SRegion, args *InstanceIdOptions) error {
    56  		return cli.Delete(args.ID)
    57  	})
    58  
    59  	shellutils.R(&InstanceIdOptions{}, "instance-start", "Start instance", func(cli *google.SRegion, args *InstanceIdOptions) error {
    60  		return cli.StartInstance(args.ID)
    61  	})
    62  
    63  	shellutils.R(&InstanceIdOptions{}, "instance-stop", "Stop instance", func(cli *google.SRegion, args *InstanceIdOptions) error {
    64  		return cli.StopInstance(args.ID)
    65  	})
    66  
    67  	shellutils.R(&InstanceIdOptions{}, "instance-reset", "Reset instance", func(cli *google.SRegion, args *InstanceIdOptions) error {
    68  		return cli.ResetInstance(args.ID)
    69  	})
    70  
    71  	type InstanceEipOptions struct {
    72  		ID  string
    73  		EIP string `help:"eip address"`
    74  	}
    75  
    76  	shellutils.R(&InstanceEipOptions{}, "instance-dissociate-eip", "Dissociate instance eip", func(cli *google.SRegion, args *InstanceEipOptions) error {
    77  		return cli.DissociateInstanceEip(args.ID, args.EIP)
    78  	})
    79  
    80  	shellutils.R(&InstanceEipOptions{}, "instance-associate-eip", "Associate instance eip", func(cli *google.SRegion, args *InstanceEipOptions) error {
    81  		return cli.AssociateInstanceEip(args.ID, args.EIP)
    82  	})
    83  
    84  	type InstanceDetachDiskOptions struct {
    85  		ID         string
    86  		DeviceName string
    87  	}
    88  
    89  	shellutils.R(&InstanceDetachDiskOptions{}, "instance-detach-disk", "Detach instance disk", func(cli *google.SRegion, args *InstanceDetachDiskOptions) error {
    90  		return cli.DetachDisk(args.ID, args.DeviceName)
    91  	})
    92  
    93  	type InstanceSetPublicKeyOptions struct {
    94  		ID        string
    95  		PublicKey string
    96  	}
    97  
    98  	shellutils.R(&InstanceSetPublicKeyOptions{}, "instance-set-publickey", "Set instance public key", func(cli *google.SRegion, args *InstanceSetPublicKeyOptions) error {
    99  		instance, err := cli.GetInstance(args.ID)
   100  		if err != nil {
   101  			return errors.Wrap(err, "cli.GetInstance")
   102  		}
   103  		items := []google.SMetadataItem{}
   104  		for _, item := range instance.Metadata.Items {
   105  			if item.Key != google.METADATA_SSH_KEYS {
   106  				items = append(items, item)
   107  			}
   108  		}
   109  		if len(args.PublicKey) > 0 {
   110  			items = append(items, google.SMetadataItem{Key: google.METADATA_SSH_KEYS, Value: "root:" + args.PublicKey})
   111  		}
   112  		instance.Metadata.Items = items
   113  		return cli.SetMetadata(args.ID, instance.Metadata)
   114  	})
   115  
   116  	type InstanceAttachDiskOptions struct {
   117  		ID   string
   118  		DISK string
   119  		Boot bool
   120  	}
   121  
   122  	type InstanceSerialOutput struct {
   123  		ID   string
   124  		PORT int
   125  	}
   126  
   127  	shellutils.R(&InstanceSerialOutput{}, "instance-serial-output", "Get instance serial output", func(cli *google.SRegion, args *InstanceSerialOutput) error {
   128  		content, err := cli.GetSerialPortOutput(args.ID, args.PORT)
   129  		if err != nil {
   130  			return err
   131  		}
   132  		fmt.Printf("content: %s\n", content)
   133  		return nil
   134  	})
   135  
   136  	shellutils.R(&InstanceAttachDiskOptions{}, "instance-attach-disk", "Attach instance disk", func(cli *google.SRegion, args *InstanceAttachDiskOptions) error {
   137  		return cli.AttachDisk(args.ID, args.DISK, args.Boot)
   138  	})
   139  
   140  	type InstanceRebuildRootOptions struct {
   141  		ID         string
   142  		IMAGE      string
   143  		DiskSizeGb int
   144  	}
   145  
   146  	shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Rebuild instance root", func(cli *google.SRegion, args *InstanceRebuildRootOptions) error {
   147  		diskId, err := cli.RebuildRoot(args.ID, args.IMAGE, args.DiskSizeGb)
   148  		if err != nil {
   149  			return err
   150  		}
   151  		fmt.Println(diskId)
   152  		return nil
   153  	})
   154  
   155  	type InstanceChangeConfigOptions struct {
   156  		ID           string
   157  		ZONE         string
   158  		InstanceType string
   159  		Cpu          int
   160  		MemoryMb     int
   161  	}
   162  
   163  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Change instance config", func(cli *google.SRegion, args *InstanceChangeConfigOptions) error {
   164  		return cli.ChangeInstanceConfig(args.ID, args.ZONE, args.InstanceType, args.Cpu, args.MemoryMb)
   165  	})
   166  
   167  	type InstanceCreateOptions struct {
   168  		NAME         string
   169  		ZONE         string
   170  		IMAGE        string
   171  		InstanceType string
   172  		Cpu          int
   173  		MemoryMb     int
   174  		NETWORK      string
   175  		IpAddr       string
   176  		Desc         string
   177  		DISKS        []string `nargs:"+"`
   178  	}
   179  
   180  	shellutils.R(&InstanceCreateOptions{}, "instance-create", "Create instance", func(cli *google.SRegion, args *InstanceCreateOptions) error {
   181  		instance, err := cli.CreateInstance(args.ZONE, args.NAME, args.Desc, args.InstanceType, args.Cpu, args.MemoryMb, args.NETWORK, args.IpAddr, args.IMAGE, args.DISKS)
   182  		if err != nil {
   183  			return err
   184  		}
   185  		printObject(instance)
   186  		return nil
   187  	})
   188  
   189  	type InstanceSaveImageOptions struct {
   190  		DISK_ID    string `help:"Instance System disk ID"`
   191  		IMAGE_NAME string `help:"Image name"`
   192  		Notes      string `hlep:"Image desc"`
   193  	}
   194  	shellutils.R(&InstanceSaveImageOptions{}, "instance-save-image", "Save instance to image", func(cli *google.SRegion, args *InstanceSaveImageOptions) error {
   195  		opts := cloudprovider.SaveImageOptions{
   196  			Name:  args.IMAGE_NAME,
   197  			Notes: args.Notes,
   198  		}
   199  		image, err := cli.SaveImage(args.DISK_ID, &opts)
   200  		if err != nil {
   201  			return err
   202  		}
   203  		printObject(image)
   204  		return nil
   205  	})
   206  
   207  	type InstanceSetTagsOptions struct {
   208  		ID   string `help:"Instance ID"`
   209  		Tags []string
   210  	}
   211  	shellutils.R(&InstanceSetTagsOptions{}, "instance-set-tags", "get intance metadata", func(cli *google.SRegion, args *InstanceSetTagsOptions) error {
   212  		tags := map[string]string{}
   213  		for i := range args.Tags {
   214  			splited := strings.Split(args.Tags[i], "=")
   215  			if len(splited) == 2 {
   216  				tags[splited[0]] = splited[1]
   217  			}
   218  		}
   219  		instance, err := cli.GetInstance(args.ID)
   220  		if err != nil {
   221  			return errors.Wrap(err, "cli.GetInstance")
   222  		}
   223  		err = cli.SetLabels(args.ID, tags, instance.LabelFingerprint)
   224  		if err != nil {
   225  			return err
   226  		}
   227  		return nil
   228  	})
   229  
   230  }