yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/openstack/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  	"yunion.io/x/cloudmux/pkg/multicloud/openstack"
    19  	"yunion.io/x/onecloud/pkg/util/shellutils"
    20  )
    21  
    22  func init() {
    23  	type InstanceListOptions struct {
    24  		Host string `help:"Host name for filter instance list"`
    25  	}
    26  	shellutils.R(&InstanceListOptions{}, "instance-list", "List instances", func(cli *openstack.SRegion, args *InstanceListOptions) error {
    27  		instances, err := cli.GetInstances(args.Host)
    28  		if err != nil {
    29  			return err
    30  		}
    31  		printList(instances, 0, 0, 0, nil)
    32  		return nil
    33  	})
    34  
    35  	type InstanceOptions struct {
    36  		ID string `help:"Instance ID"`
    37  	}
    38  
    39  	shellutils.R(&InstanceOptions{}, "instance-network-list", "List instance network", func(cli *openstack.SRegion, args *InstanceOptions) error {
    40  		ports, err := cli.GetInstancePorts(args.ID)
    41  		if err != nil {
    42  			return err
    43  		}
    44  		printObject(ports)
    45  		return nil
    46  	})
    47  
    48  	shellutils.R(&InstanceOptions{}, "instance-show", "Show instance", func(cli *openstack.SRegion, args *InstanceOptions) error {
    49  		instance, err := cli.GetInstance(args.ID)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		printObject(instance)
    54  		return nil
    55  	})
    56  
    57  	shellutils.R(&InstanceOptions{}, "instance-metadata", "Show instance metadata", func(cli *openstack.SRegion, args *InstanceOptions) error {
    58  		metadata, err := cli.GetInstanceMetadata(args.ID)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		printObject(metadata)
    63  		return nil
    64  	})
    65  
    66  	shellutils.R(&InstanceOptions{}, "instance-vnc", "Show instance vnc url", func(cli *openstack.SRegion, args *InstanceOptions) error {
    67  		ret, err := cli.GetInstanceVNCUrl(args.ID, true)
    68  		if err != nil {
    69  			ret, err = cli.GetInstanceVNC(args.ID, true)
    70  		}
    71  		if err != nil {
    72  			return err
    73  		}
    74  		printObject(ret)
    75  		return nil
    76  	})
    77  
    78  	type InstanceDeployOptions struct {
    79  		ID       string `help:"Instance ID"`
    80  		Password string `help:"Instance password"`
    81  		Name     string `help:"Instance name"`
    82  	}
    83  
    84  	shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy instance", func(cli *openstack.SRegion, args *InstanceDeployOptions) error {
    85  		return cli.DeployVM(args.ID, args.Name, args.Password, "", false, "")
    86  	})
    87  
    88  	type InstanceChangeConfigOptions struct {
    89  		ID        string `help:"Instance ID"`
    90  		FLAVOR_ID string `help:"Flavor ID"`
    91  	}
    92  
    93  	shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Change instance config", func(cli *openstack.SRegion, args *InstanceChangeConfigOptions) error {
    94  		instance, err := cli.GetInstance(args.ID)
    95  		if err != nil {
    96  			return err
    97  		}
    98  		return cli.ChangeConfig(instance, args.FLAVOR_ID)
    99  	})
   100  
   101  	type InstanceDiskOptions struct {
   102  		ID   string `help:"Instance ID"`
   103  		DISK string `help:"DiskId"`
   104  	}
   105  
   106  	shellutils.R(&InstanceDiskOptions{}, "instance-detach-disk", "Detach instance disk", func(cli *openstack.SRegion, args *InstanceDiskOptions) error {
   107  		return cli.DetachDisk(args.ID, args.DISK)
   108  	})
   109  
   110  	type InstanceMigrateOptions struct {
   111  		ID   string `help:"Instance ID"`
   112  		Host string `help:"host id"`
   113  	}
   114  	type MigrationOptions struct {
   115  		ID          string `help:"Instance ID"`
   116  		Type        string `help:"migration choices:evacuation|live-migration|migration|resize"`
   117  		Migrationid string `help:"migration ID"`
   118  	}
   119  	shellutils.R(&InstanceMigrateOptions{}, "instance-migrate", "migrate instance", func(cli *openstack.SRegion, args *InstanceMigrateOptions) error {
   120  		return cli.MigrateVM(args.ID, args.Host)
   121  	})
   122  
   123  	shellutils.R(&InstanceMigrateOptions{}, "instance-live-migrate", "migrate instance", func(cli *openstack.SRegion, args *InstanceMigrateOptions) error {
   124  		return cli.LiveMigrateVM(args.ID, args.Host)
   125  	})
   126  
   127  	shellutils.R(&MigrationOptions{}, "instance-migration-list", "list live migration", func(cli *openstack.SRegion, args *MigrationOptions) error {
   128  		return cli.ListServerMigration(args.ID)
   129  	})
   130  	shellutils.R(&MigrationOptions{}, "instance-migration-delete", "delet migration", func(cli *openstack.SRegion, args *MigrationOptions) error {
   131  		return cli.DeleteMigration(args.ID, args.Migrationid)
   132  	})
   133  	shellutils.R(&MigrationOptions{}, "instance-migration-forceComplete", "deletmigration", func(cli *openstack.SRegion, args *MigrationOptions) error {
   134  		return cli.ForceCompleteMigration(args.ID, args.Migrationid)
   135  	})
   136  
   137  	shellutils.R(&MigrationOptions{}, "migrations-show", "show migrations", func(cli *openstack.SRegion, args *MigrationOptions) error {
   138  		_, err := cli.GetMigrations(args.ID, args.Type)
   139  		return err
   140  	})
   141  }