add aws cli

This commit is contained in:
TangBin
2018-10-23 17:54:30 +08:00
parent 7db7bcb2ca
commit 58be5ccda9
20 changed files with 650 additions and 8 deletions
+122
View File
@@ -0,0 +1,122 @@
package main
import (
"fmt"
"os"
"yunion.io/x/log"
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
"yunion.io/x/structarg"
_ "yunion.io/x/onecloud/pkg/util/aws/shell"
)
type BaseOptions struct {
Help bool `help:"Show help"`
AccessKey string `help:"Access key" default:"$AWS_ACCESS_KEY"`
Secret string `help:"Secret" default:"$AWS_SECRET"`
RegionId string `help:"RegionId" default:"$AWS_REGION"`
SUBCOMMAND string `help:"awscli subcommand" subcommand:"true"`
}
func getSubcommandParser() (*structarg.ArgumentParser, error) {
parse, e := structarg.NewArgumentParser(&BaseOptions{},
"awscli",
"Command-line interface to aws API.",
`See "awscli help COMMAND" for help on a specific command.`)
if e != nil {
return nil, e
}
subcmd := parse.GetSubcommand()
if subcmd == nil {
return nil, fmt.Errorf("No subcommand argument.")
}
type HelpOptions struct {
SUBCOMMAND string `help:"sub-command name"`
}
shellutils.R(&HelpOptions{}, "help", "Show help of a subcommand", func(args *HelpOptions) error {
helpstr, e := subcmd.SubHelpString(args.SUBCOMMAND)
if e != nil {
return e
} else {
fmt.Print(helpstr)
return nil
}
})
for _, v := range shellutils.CommandTable {
_, e := subcmd.AddSubParser(v.Options, v.Command, v.Desc, v.Callback)
if e != nil {
return nil, e
}
}
return parse, nil
}
func showErrorAndExit(e error) {
log.Errorf("%s", e)
os.Exit(1)
}
func newClient(options *BaseOptions) (*aws.SRegion, error) {
if len(options.AccessKey) == 0 {
return nil, fmt.Errorf("Missing accessKey")
}
if len(options.Secret) == 0 {
return nil, fmt.Errorf("Missing secret")
}
cli, err := aws.NewAwsClient("", "", options.AccessKey, options.Secret)
if err != nil {
return nil, err
}
region := cli.GetRegion(options.RegionId)
if region == nil {
return nil, fmt.Errorf("No such region %s", options.RegionId)
}
return region, nil
}
func main() {
parser, e := getSubcommandParser()
if e != nil {
showErrorAndExit(e)
}
e = parser.ParseArgs(os.Args[1:], false)
options := parser.Options().(*BaseOptions)
if options.Help {
fmt.Print(parser.HelpString())
} else {
subcmd := parser.GetSubcommand()
subparser := subcmd.GetSubParser()
if e != nil {
if subparser != nil {
fmt.Print(subparser.Usage())
} else {
fmt.Print(parser.Usage())
}
showErrorAndExit(e)
} else {
suboptions := subparser.Options()
if options.SUBCOMMAND == "help" {
e = subcmd.Invoke(suboptions)
} else {
var region *aws.SRegion
region, e = newClient(options)
if e != nil {
showErrorAndExit(e)
}
e = subcmd.Invoke(region, suboptions)
}
if e != nil {
showErrorAndExit(e)
}
}
}
}
-1
View File
@@ -156,7 +156,6 @@ func init() {
return e
}
// todo : add create disks
err := cli.ChangeVMConfig(instance.ZoneId, args.ID, args.Ncpu, args.Vmem, nil)
if err != nil {
return err
+2 -2
View File
@@ -127,7 +127,7 @@ func (self *SEipAddress) ChangeBandwidth(bw int) error {
return self.region.UpdateEipBandwidth(self.AllocationId, bw)
}
func (self *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) {
func (self *SRegion) GetEips(eipId string, offset int, limit int) ([]SEipAddress, int, error) {
params := ec2.DescribeAddressesInput{}
if len(eipId) > 0 {
params.SetAllocationIds([]*string{&eipId})
@@ -156,7 +156,7 @@ func (self *SRegion) GetEips(eipId string) ([]SEipAddress, int, error) {
}
func (region *SRegion) GetEip(eipId string) (*SEipAddress, error) {
eips, total, err := region.GetEips(eipId)
eips, total, err := region.GetEips(eipId, 0, 0)
if err != nil {
return nil, err
}
+2 -2
View File
@@ -122,7 +122,7 @@ func (self *SRegion) createNetwork(zoneId string, vpcId string, name string, cid
}
func (self *SRegion) getNetwork(networkId string) (*SNetwork, error) {
networks, total, err := self.GetNetwroks([]string{networkId}, "")
networks, total, err := self.GetNetwroks([]string{networkId}, "",0 ,0)
if err != nil {
return nil, err
}
@@ -139,7 +139,7 @@ func (self *SRegion) deleteNetwork(networkId string) error {
return err
}
func (self *SRegion) GetNetwroks(ids []string, vpcId string) ([]SNetwork, int, error) {
func (self *SRegion) GetNetwroks(ids []string, vpcId string, limit int, offset int) ([]SNetwork, int, error) {
params := &ec2.DescribeSubnetsInput{}
if len(ids) > 0 {
_ids := make([]*string, len(ids))
+2 -2
View File
@@ -214,7 +214,7 @@ func (self *SRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
return nil, err
}
eips, total, err := self.GetEips("")
eips, total, err := self.GetEips("", 0,0)
if err != nil {
return nil, err
}
@@ -327,7 +327,7 @@ func (self *SRegion) CreateIVpc(name string, desc string, cidr string) (cloudpro
}
func (self *SRegion) GetIEipById(eipId string) (cloudprovider.ICloudEIP, error) {
eips, total, err := self.GetEips(eipId)
eips, total, err := self.GetEips(eipId, 0, 0)
if err != nil {
return nil, err
}
+35
View File
@@ -0,0 +1,35 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type DiskListOptions struct {
Instance string `help:"Instance ID"`
Zone string `help:"Zone ID"`
Category string `help:"Disk category"`
Offset int `help:"List offset"`
Limit int `help:"List limit"`
}
shellutils.R(&DiskListOptions{}, "disk-list", "List disks", func(cli *aws.SRegion, args *DiskListOptions) error {
disks, total, e := cli.GetDisks(args.Instance, args.Zone, args.Category, nil, args.Offset, args.Limit)
if e != nil {
return e
}
printList(disks, total, args.Offset, args.Limit, []string{})
return nil
})
type DiskDeleteOptions struct {
Instance string `help:"Instance ID"`
}
shellutils.R(&DiskDeleteOptions{}, "disk-delete", "List disks", func(cli *aws.SRegion, args *DiskDeleteOptions) error {
e := cli.DeleteDisk(args.Instance)
if e != nil {
return e
}
return nil
})
}
+1
View File
@@ -0,0 +1 @@
package shell // import "yunion.io/x/onecloud/pkg/util/aws/shell"
+53
View File
@@ -0,0 +1,53 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type EipListOptions struct {
Offset int `help:"List offset"`
Limit int `help:"List limit"`
}
shellutils.R(&EipListOptions{}, "eip-list", "List eips", func(cli *aws.SRegion, args *EipListOptions) error {
eips, total, e := cli.GetEips("", args.Offset, args.Limit)
if e != nil {
return e
}
printList(eips, total, args.Offset, args.Limit, []string{})
return nil
})
type EipAllocateOptions struct {
}
shellutils.R(&EipAllocateOptions{}, "eip-create", "Allocate an EIP", func(cli *aws.SRegion, args *EipAllocateOptions) error {
eip, err := cli.AllocateEIP("vpc")
if err != nil {
return err
}
printObject(eip)
return nil
})
type EipReleaseOptions struct {
ID string `help:"EIP allocation ID"`
}
shellutils.R(&EipReleaseOptions{}, "eip-delete", "Release an EIP", func(cli *aws.SRegion, args *EipReleaseOptions) error {
err := cli.DeallocateEIP(args.ID)
return err
})
type EipAssociateOptions struct {
ID string `help:"EIP allocation ID"`
INSTANCE string `help:"Instance ID"`
}
shellutils.R(&EipAssociateOptions{}, "eip-associate", "Associate an EIP", func(cli *aws.SRegion, args *EipAssociateOptions) error {
err := cli.AssociateEip(args.ID, args.INSTANCE)
return err
})
shellutils.R(&EipAssociateOptions{}, "eip-dissociate", "Dissociate an EIP", func(cli *aws.SRegion, args *EipAssociateOptions) error {
err := cli.DissociateEip(args.ID, args.INSTANCE)
return err
})
}
+32
View File
@@ -0,0 +1,32 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type ImageListOptions struct {
Status string `help:"image status type" choices:"Creating|Available|UnAvailable|CreateFailed"`
Owner string `help:"Owner type" choices:"system|self|others|marketplace"`
Id []string `help:"Image ID"`
Name string `help:"image name"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&ImageListOptions{}, "image-list", "List images", func(cli *aws.SRegion, args *ImageListOptions) error {
images, total, e := cli.GetImages(aws.ImageStatusType(args.Status), aws.ImageOwnerType(args.Owner), args.Id, args.Name, args.Offset, args.Limit)
if e != nil {
return e
}
printList(images, total, args.Offset, args.Limit, []string{})
return nil
})
type ImageDeleteOptions struct {
ID string `help:"ID or Name to delete"`
}
shellutils.R(&ImageDeleteOptions{}, "image-delete", "Delete image", func(cli *aws.SRegion, args *ImageDeleteOptions) error {
return cli.DeleteImage(args.ID)
})
}
+157
View File
@@ -0,0 +1,157 @@
package shell
import (
"fmt"
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type InstanceListOptions struct {
Id []string `help:"IDs of instances to show"`
Zone string `help:"Zone ID"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&InstanceListOptions{}, "instance-list", "List intances", func(cli *aws.SRegion, args *InstanceListOptions) error {
instances, total, e := cli.GetInstances(args.Zone, args.Id, args.Offset, args.Limit)
if e != nil {
return e
}
printList(instances, total, args.Offset, args.Limit, []string{})
return nil
})
type InstanceCrateOptions struct {
NAME string `help:"name of instance"`
IMAGE string `help:"image ID"`
CPU int `help:"CPU count"`
MEMORYGB int `help:"MemoryGB"`
Disk []int `help:"Data disk sizes int GB"`
STORAGE string `help:"Storage type"`
VSWITCH string `help:"Vswitch ID"`
PASSWD string `help:"password"`
PublicKey string `help:"PublicKey"`
}
shellutils.R(&InstanceCrateOptions{}, "instance-create", "Create a instance", func(cli *aws.SRegion, args *InstanceCrateOptions) error {
// instance, e := cli.CreateInstanceSimple(args.NAME, args.IMAGE, args.CPU, args.MEMORYGB, args.STORAGE, args.Disk, args.VSWITCH, args.PASSWD, args.PublicKey)
// if e != nil {
// return e
// }
// printObject(instance)
return nil
})
type InstanceDiskOperationOptions struct {
ID string `help:"instance ID"`
DISK string `help:"disk ID"`
}
shellutils.R(&InstanceDiskOperationOptions{}, "instance-attach-disk", "Attach a disk to instance", func(cli *aws.SRegion, args *InstanceDiskOperationOptions) error {
err := cli.AttachDisk(args.ID, args.DISK)
if err != nil {
return err
}
return nil
})
shellutils.R(&InstanceDiskOperationOptions{}, "instance-detach-disk", "Detach a disk to instance", func(cli *aws.SRegion, args *InstanceDiskOperationOptions) error {
err := cli.DetachDisk(args.ID, args.DISK)
if err != nil {
return err
}
return nil
})
type InstanceOperationOptions struct {
ID string `help:"instance ID"`
}
shellutils.R(&InstanceOperationOptions{}, "instance-start", "Start a instance", func(cli *aws.SRegion, args *InstanceOperationOptions) error {
err := cli.StartVM(args.ID)
if err != nil {
return err
}
return nil
})
type InstanceStopOptions struct {
ID string `help:"instance ID"`
Force bool `help:"Force stop instance"`
}
shellutils.R(&InstanceStopOptions{}, "instance-stop", "Stop a instance", func(cli *aws.SRegion, args *InstanceStopOptions) error {
err := cli.StopVM(args.ID, args.Force)
if err != nil {
return err
}
return nil
})
shellutils.R(&InstanceOperationOptions{}, "instance-delete", "Delete a instance", func(cli *aws.SRegion, args *InstanceOperationOptions) error {
err := cli.DeleteVM(args.ID)
if err != nil {
return err
}
return nil
})
/*
server-change-config 更改系统配置
server-reset
*/
type InstanceDeployOptions struct {
ID string `help:"instance ID"`
Name string `help:"new instance name"`
Hostname string `help:"new hostname"`
Keypair string `help:"Keypair Name"`
DeleteKeypair bool `help:"Remove SSH keypair"`
Password string `help:"new password"`
// ResetPassword bool `help:"Force reset password"`
Description string `help:"new instances description"`
}
shellutils.R(&InstanceDeployOptions{}, "instance-deploy", "Deploy keypair/password to a stopped virtual server", func(cli *aws.SRegion, args *InstanceDeployOptions) error {
err := cli.DeployVM(args.ID, args.Name, args.Password, args.Keypair, args.DeleteKeypair, args.Description)
if err != nil {
return err
}
return nil
})
type InstanceRebuildRootOptions struct {
ID string `help:"instance ID"`
Image string `help:"Image ID"`
Password string `help:"pasword"`
Keypair string `help:"keypair name"`
Size int `help:"system disk size in GB"`
}
shellutils.R(&InstanceRebuildRootOptions{}, "instance-rebuild-root", "Reinstall virtual server system image", func(cli *aws.SRegion, args *InstanceRebuildRootOptions) error {
diskID, err := cli.ReplaceSystemDisk(args.ID, args.Image, args.Password, args.Keypair, args.Size)
if err != nil {
return err
}
fmt.Printf("New diskID is %s", diskID)
return nil
})
type InstanceChangeConfigOptions struct {
ID string `help:"instance ID"`
Ncpu int `help:"number of CPU"`
Vmem int `help:"MiB of memory"`
Disk []int `help:"Data disk sizes int GB"`
}
shellutils.R(&InstanceChangeConfigOptions{}, "instance-change-config", "Deploy keypair/password to a stopped virtual server", func(cli *aws.SRegion, args *InstanceChangeConfigOptions) error {
instance, e := cli.GetInstance(args.ID)
if e != nil {
return e
}
// todo : add create disks
err := cli.ChangeVMConfig(instance.ZoneId, args.ID, args.Ncpu, args.Vmem, nil)
if err != nil {
return err
}
return nil
})
}
+34
View File
@@ -0,0 +1,34 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type InstanceTypeListOptions struct {
}
shellutils.R(&InstanceTypeListOptions{}, "instance-type-list", "List intance types", func(cli *aws.SRegion, args *InstanceTypeListOptions) error {
instanceTypes, e := cli.GetInstanceTypes()
if e != nil {
return e
}
printList(instanceTypes, 0, 0, 0, []string{})
return nil
})
type InstanceMatchOptions struct {
CPU int `help:"CPU count"`
MEM int `help:"Memory in MB"`
GPU int `help:"GPU size"`
Zone string `help:"Test in zone"`
}
shellutils.R(&InstanceMatchOptions{}, "instance-type-select", "Select matching instance types", func(cli *aws.SRegion, args *InstanceMatchOptions) error {
instanceTypes, e := cli.GetMatchInstanceTypes(args.CPU, args.MEM, args.GPU, args.Zone)
if e != nil {
return e
}
printList(instanceTypes, 0, 0, 0, []string{})
return nil
})
}
+34
View File
@@ -0,0 +1,34 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type KeyPairListOptions struct {
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&KeyPairListOptions{}, "keypair-list", "List keypairs", func(cli *aws.SRegion, args *KeyPairListOptions) error {
keypairs, total, e := cli.GetKeypairs("", "", args.Offset, args.Limit)
if e != nil {
return e
}
printList(keypairs, total, args.Offset, args.Limit, []string{})
return nil
})
type KeyPairImportOptions struct {
NAME string `help:"Name of new keypair"`
PUBKEY string `help:"Public key string"`
}
shellutils.R(&KeyPairImportOptions{}, "keypair-import", "Import a keypair", func(cli *aws.SRegion, args *KeyPairImportOptions) error {
keypair, err := cli.ImportKeypair(args.NAME, args.PUBKEY)
if err != nil {
return err
}
printObject(keypair)
return nil
})
}
+21
View File
@@ -0,0 +1,21 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type VSwitchListOptions struct {
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&VSwitchListOptions{}, "vswitch-list", "List vswitches", func(cli *aws.SRegion, args *VSwitchListOptions) error {
vswitches, total, e := cli.GetNetwroks(nil, "", args.Offset, args.Limit)
if e != nil {
return e
}
printList(vswitches, total, args.Offset, args.Limit, []string{})
return nil
})
}
+11
View File
@@ -0,0 +1,11 @@
package shell
import "yunion.io/x/onecloud/pkg/util/printutils"
func printList(data interface{}, total, offset, limit int, columns []string) {
printutils.PrintInterfaceList(data, total, offset, limit, columns)
}
func printObject(obj interface{}) {
printutils.PrintInterfaceObject(obj)
}
+16
View File
@@ -0,0 +1,16 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type RegionListOptions struct {
}
shellutils.R(&RegionListOptions{}, "region-list", "List regions", func(cli *aws.SRegion, args *RegionListOptions) error {
regions := cli.GetClient().GetRegions()
printList(regions, 0, 0, 0, nil)
return nil
})
}
+34
View File
@@ -0,0 +1,34 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type SecurityGroupListOptions struct {
VpcId string `help:"VPC ID"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&SecurityGroupListOptions{}, "security-group-list", "List security group", func(cli *aws.SRegion, args *SecurityGroupListOptions) error {
secgrps, total, e := cli.GetSecurityGroups(args.VpcId, args.Offset, args.Limit)
if e != nil {
return e
}
printList(secgrps, total, args.Offset, args.Limit, []string{})
return nil
})
type SecurityGroupShowOptions struct {
ID string `help:"ID or name of security group"`
}
shellutils.R(&SecurityGroupShowOptions{}, "security-group-show", "Show details of a security group", func(cli *aws.SRegion, args *SecurityGroupShowOptions) error {
secgrp, err := cli.GetSecurityGroupDetails(args.ID)
if err != nil {
return err
}
printObject(secgrp)
return nil
})
}
+46
View File
@@ -0,0 +1,46 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type SnapshotListOptions struct {
DiskId string `help:"Disk ID"`
InstanceId string `help:"Instance ID"`
SnapshotIds []string `helo:"Snapshot ids"`
Name string `help:"Snapshot Name"`
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&SnapshotListOptions{}, "snapshot-list", "List snapshot", func(cli *aws.SRegion, args *SnapshotListOptions) error {
if snapshots, total, err := cli.GetSnapshots(args.InstanceId, args.DiskId, args.Name, args.SnapshotIds, args.Offset, args.Limit); err != nil {
return err
} else {
printList(snapshots, total, args.Offset, args.Limit, []string{})
return nil
}
})
type SnapshotDeleteOptions struct {
ID string `help:"Snapshot ID"`
}
shellutils.R(&SnapshotDeleteOptions{}, "snapshot-delete", "Delete snapshot", func(cli *aws.SRegion, args *SnapshotDeleteOptions) error {
// todo://
return nil
})
type SnapshotCreateOptions struct {
DiskId string `help:"Disk ID"`
Name string `help:"Snapeshot Name"`
Desc string `help:"Snapshot Desc"`
}
shellutils.R(&SnapshotCreateOptions{}, "snapshot-create", "Create snapshot", func(cli *aws.SRegion, args *SnapshotCreateOptions) error {
_, err := cli.CreateSnapshot(args.DiskId, args.Name, args.Desc)
return err
})
}
+21
View File
@@ -0,0 +1,21 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type VpcListOptions struct {
Limit int `help:"page size"`
Offset int `help:"page offset"`
}
shellutils.R(&VpcListOptions{}, "vpc-list", "List vpcs", func(cli *aws.SRegion, args *VpcListOptions) error {
vpcs, total, e := cli.GetVpcs(nil, args.Offset, args.Limit)
if e != nil {
return e
}
printList(vpcs, total, args.Offset, args.Limit, []string{})
return nil
})
}
+26
View File
@@ -0,0 +1,26 @@
package shell
import (
"yunion.io/x/onecloud/pkg/util/aws"
"yunion.io/x/onecloud/pkg/util/shellutils"
)
func init() {
type ZoneListOptions struct {
Details bool `help:"show Details"`
// ChargeType string `help:"charge type" choices:"PrePaid|PostPaid" default:"PrePaid"`
// SpotStrategy string `help:"Spot strategy, NoSpot|SpotWithPriceLimit|SpotAsPriceGo" choices:"NoSpot|SpotWithPriceLimit|SpotAsPriceGo" default:"NoSpot"`
}
shellutils.R(&ZoneListOptions{}, "zone-list", "List zones", func(cli *aws.SRegion, args *ZoneListOptions) error {
zones, e := cli.GetIZones()
if e != nil {
return e
}
cols := []string{"zone_id", "local_name", "available_resource_creation", "available_disk_categories"}
if args.Details {
cols = []string{}
}
printList(zones, 0, 0, 0, cols)
return nil
})
}
+1 -1
View File
@@ -186,7 +186,7 @@ func (self *SVpc) getWireByZoneId(zoneId string) *SWire {
}
func (self *SVpc) fetchNetworks() error {
networks, _, err := self.region.GetNetwroks(nil, self.VpcId)
networks, _, err := self.region.GetNetwroks(nil, self.VpcId, 0, 0)
if err != nil {
return err
}