예제 #1
0
파일: instance.go 프로젝트: aiden0z/spore
func (instance *Instance) Start() error {
	if instance.Attrs.Status != "Stopped" {
		return fmt.Errorf("Not stopped instance not support start action")
	}
	client := AliyunClient()

	// remove used tag
	tagArgs := ecs.RemoveTagsArgs{
		ResourceId:   instance.Attrs.InstanceId,
		ResourceType: "instance",
		RegionId:     common.Region(AliyunRegionId),
		Tag:          UsedInstanceTag,
	}
	client.RemoveTags(&tagArgs)

	// reinit disk
	err := client.ReInitDisk(instance.Disk.DiskId)
	if err != nil {
		log.Errorf("init instnace %s disk %s error %s", instance.Attrs.InstanceId, instance.Disk.DiskId, err)
		return err
	}

	// sleep few seconds, if not start instance will be failed because reinit disk task dot not finished
	time.Sleep(2 * time.Second)

	// start instance
	return client.StartInstance(instance.Attrs.InstanceId)
}
예제 #2
0
파일: cluster.go 프로젝트: aiden0z/spore
//  get all disks
func (c *Cluster) RefreshDisks() error {
	client := AliyunClient()
	diskArgs := ecs.DescribeDisksArgs{
		RegionId: common.Region(AliyunRegionId),
		ZoneId:   AliyunZoneId,
	}
	diskArgs.Pagination = NewPagination()
	disks, pagination, err := client.DescribeDisks(&diskArgs)
	if err != nil {
		return err
	}
	c.disks = disks
	for {
		nextPage := pagination.NextPage()
		if nextPage != nil {
			diskArgs.Pagination = *nextPage
			disks, pagination, err = client.DescribeDisks(&diskArgs)
			if err != nil {
				return err
			}
			c.disks = append(c.disks, disks...)
		} else {
			break
		}
	}
	log.Debugf("Total found %d disk", len(c.disks))
	return nil
}
예제 #3
0
func validateECSRegion(region string) (common.Region, error) {
	for _, v := range common.ValidRegions {
		if v == common.Region(region) {
			return v, nil
		}
	}

	return "", errInvalidRegion
}
예제 #4
0
파일: instance.go 프로젝트: aiden0z/spore
// set instance tag
func (instance *Instance) SetUsedTag() error {
	// remove used tag
	tagArgs := ecs.AddTagsArgs{
		ResourceId:   instance.Attrs.InstanceId,
		ResourceType: "instance",
		RegionId:     common.Region(AliyunRegionId),
		Tag:          UsedInstanceTag,
	}

	client := AliyunClient()
	return client.AddTags(&tagArgs)
}
예제 #5
0
파일: instance.go 프로젝트: aiden0z/spore
// remove instance tag
func (instance *Instance) Stop() error {
	if instance.Attrs.Status != "Running" {
		return fmt.Errorf("Not running instance not support stop action")
	}

	client := AliyunClient()

	// remove tag
	tagArgs := ecs.RemoveTagsArgs{
		ResourceId:   instance.Attrs.InstanceId,
		ResourceType: "instance",
		RegionId:     common.Region(AliyunRegionId),
		Tag:          UsedInstanceTag,
	}
	client.RemoveTags(&tagArgs)

	// stop
	return client.StopInstance(instance.Attrs.InstanceId, true)
}
예제 #6
0
파일: cluster.go 프로젝트: aiden0z/spore
// get all instances, instance is processed differently based on status, instance have four
// status: Starting, Running, Stoping, Stopped, refer:
// https://help.aliyun.com/document_detail/ecs/open-api/appendix/instancestatustable.html
// Running: instance with status running may being used or may be available
// Sopped: instance with status soppend shoult to be started, into starting status
func (c *Cluster) RefreshInstances() error {
	var instances, pageInstances []ecs.InstanceAttributesType
	client := AliyunClient()
	instanceArgs := ecs.DescribeInstancesArgs{
		RegionId:        common.Region(AliyunRegionId),
		ZoneId:          AliyunZoneId,
		SecurityGroupId: AliyunSecurityGroupId,
	}
	instanceArgs.Pagination = NewPagination()
	instances, pagination, err := client.DescribeInstances(&instanceArgs)
	if err != nil {
		return err
	}
	for {
		nextPage := pagination.NextPage()
		if nextPage != nil {
			instanceArgs.Pagination = *nextPage
			pageInstances, pagination, err = client.DescribeInstances(&instanceArgs)
			if err != nil {
				return err
			}
			instances = append(instances, pageInstances...)
		} else {
			break
		}
	}
	log.Debugf("Total found %d instances", len(instances))

	// query all used instance based on tag
	tagArgs := ecs.DescribeResourceByTagsArgs{
		ResourceType: "instance",
		RegionId:     common.Region(AliyunRegionId),
		Tag:          UsedInstanceTag,
	}
	tagArgs.Pagination = NewPagination()
	var resources, pageResources []ecs.ResourceItemType
	resources, pagination, err = client.DescribeResourceByTags(&tagArgs)
	if err != nil {
		return err
	}
	for {
		nextPage := pagination.NextPage()
		if nextPage != nil {
			tagArgs.Pagination = *nextPage
			pageResources, pagination, err = client.DescribeResourceByTags(&tagArgs)
			if err != nil {
				return err
			}
			resources = append(resources, pageResources...)
		} else {
			break
		}
	}
	// all used instance
	for _, resource := range resources {
		instance, ok := c.instances[resource.ResourceId]
		if !ok {
			instance = new(Instance)
			c.instances[resource.ResourceId] = instance
			c.used[resource.ResourceId] = instance
		} else {
			// used instance should not in any list
			if instance.list != nil {
				instance.list.Remove(instance)
			}
			c.used[resource.ResourceId] = instance
		}
	}

	log.Debugf("Total found %d used instances", len(c.used))

	// check instance status
	for _, node := range instances {
		instance, ok := c.instances[node.InstanceId]
		if !ok {
			instance = new(Instance)
		}

		// find instance disk id
		for _, disk := range c.disks {
			if disk.InstanceId == node.InstanceId {
				instance.Disk = disk
				break
			}
		}
		if len(instance.Disk.DiskId) == 0 {
			continue
		}
		instance.Attrs = node
		c.instances[node.InstanceId] = instance
		switch instance.Attrs.Status {
		case "Running":
			if _, ok := c.used[instance.Attrs.InstanceId]; !ok {
				// if instance in other list except running list, should remove it from the list
				if instance.list == nil {
					c.avavilable.Push(instance)
				} else if instance.list != c.avavilable {
					instance.list.Remove(instance)
					c.avavilable.Push(instance)
				}
			} else {
				c.used[instance.Attrs.InstanceId] = instance
			}
		case "Starting":
			if instance.list == nil {
				c.starting.Push(instance)
			} else if instance.list != c.starting {
				instance.list.Remove(instance)
				c.starting.Push(instance)
			}
		case "Stopping":
			if instance.list == nil {
				c.stopping.Push(instance)

			} else if instance.list != c.stopping {
				instance.list.Remove(instance)
				c.stopping.Push(instance)
			}
			// reinit disk, then start instance
			// FIXME if more than N instances to start, will sleep N*2 second
		case "Stopped":
			if instance.list != nil {
				instance.list.Remove(instance)
			}
			if err := instance.Start(); err != nil {
				log.Errorf("start instance %s failed, status %s,  %s", instance.Attrs.InstanceId, instance.Attrs.Status, err)
			} else {
				log.Debugf("instance %s has be started", instance.Attrs.InstanceId)
			}
		}
	}
	return nil
}
예제 #7
0
package slb

import (
	"encoding/json"
	"github.com/denverdino/aliyungo/common"
	"testing"
)

var client = NewClient("your accessId", "your accessId secret")
var loadBalancerId = "your lbid"
var region = common.Region("your region")
var serverId = "you vm id"
var deleteVServerGroupId = ""
var deleteVServerGroupIdList = make([]string, 0)

func TestCleanUp(t *testing.T) {
	err := client.DeleteLoadBalancerListener(loadBalancerId, 80)

	if err != nil {
		t.Errorf("Failed to DeleteLoadBalancerListener: %v", err)
	}

	err = client.DeleteLoadBalancerListener(loadBalancerId, 8080)

	if err != nil {
		t.Errorf("Failed to DeleteLoadBalancerListener: %v", err)
	}
}

func TestCreateVServerGroup(t *testing.T) {
	vBackendServer := VBackendServerType{