func TestRunTask(t *testing.T) {
	mockEcs, _, client, ctrl := setupTestController(t, nil)
	defer ctrl.Finish()

	clusterName := "clusterName"
	td := "taskDef"
	startedBy := "startedBy"
	count := 5
	client.(*ecsClient).params = &config.CliParams{
		Cluster: clusterName,
	}

	mockEcs.EXPECT().RunTask(gomock.Any()).Do(func(input interface{}) {
		req := input.(*ecs.RunTaskInput)
		if clusterName != aws.StringValue(req.Cluster) {
			t.Errorf("clusterName should be [%s]. Got [%s]", clusterName, aws.StringValue(req.Cluster))
		}
		if td != aws.StringValue(req.TaskDefinition) {
			t.Errorf("taskDefinition should be [%s]. Got [%s]", td, aws.StringValue(req.TaskDefinition))
		}
		if startedBy != aws.StringValue(req.StartedBy) {
			t.Errorf("startedBy should be [%s]. Got [%s]", startedBy, aws.StringValue(req.StartedBy))
		}
		if int64(count) != aws.Int64Value(req.Count) {
			t.Errorf("count should be [%s]. Got [%s]", count, aws.Int64Value(req.Count))
		}
	}).Return(&ecs.RunTaskOutput{}, nil)

	_, err := client.RunTask(td, startedBy, count)
	if err != nil {
		t.Fatal(err)
	}
}
示例#2
0
// waitForServiceTasks continuously polls ECS (by calling describeService) and waits for service to get stable
// with desiredCount == runningCount
func waitForServiceTasks(service *Service, ecsServiceName string) error {
	timeoutMessage := fmt.Sprintf("Timeout waiting for service %s to get stable", ecsServiceName)

	return waitUntilComplete(func(retryCount int) (bool, error) {

		ecsService, err := service.describeService()
		if err != nil {
			return false, err
		}

		desiredCount := aws.Int64Value(ecsService.DesiredCount)
		runningCount := aws.Int64Value(ecsService.RunningCount)

		logFields := log.Fields{
			"serviceName":  ecsServiceName,
			"desiredCount": desiredCount,
			"runningCount": runningCount,
		}
		if len(ecsService.Deployments) == 1 && desiredCount == runningCount {
			log.WithFields(logFields).Info("ECS Service has reached a stable state")
			return true, nil
		}

		if retryCount%2 == 0 {
			log.WithFields(logFields).Info("Describe ECS Service status")
		} else {
			log.WithFields(logFields).Debug("Describe ECS Service status")
		}
		return false, nil
	}, service, timeoutMessage, true)
}
示例#3
0
func (client *ecsClient) UpdateService(serviceName, taskDefinition string, count int64, deploymentConfig *ecs.DeploymentConfiguration) error {
	input := &ecs.UpdateServiceInput{
		DesiredCount:            aws.Int64(count),
		Service:                 aws.String(serviceName),
		Cluster:                 aws.String(client.params.Cluster),
		DeploymentConfiguration: deploymentConfig,
	}
	if taskDefinition != "" {
		input.TaskDefinition = aws.String(taskDefinition)
	}
	_, err := client.client.UpdateService(input)
	if err != nil {
		log.WithFields(log.Fields{
			"service": serviceName,
			"error":   err,
		}).Error("Error updating service")
		return err
	}
	fields := log.Fields{
		"service": serviceName,
		"count":   count,
	}
	if taskDefinition != "" {
		fields["taskDefinition"] = taskDefinition
	}
	if deploymentConfig != nil && deploymentConfig.MaximumPercent != nil {
		fields["deployment-max-percent"] = aws.Int64Value(deploymentConfig.MaximumPercent)
	}
	if deploymentConfig != nil && deploymentConfig.MinimumHealthyPercent != nil {
		fields["deployment-min-healthy-percent"] = aws.Int64Value(deploymentConfig.MinimumHealthyPercent)
	}
	log.WithFields(fields).Debug("Updated ECS service")
	return nil
}
示例#4
0
func (r *RDSDBInstance) buildDBInstance(dbInstance *rds.DBInstance) DBInstanceDetails {
	dbInstanceDetails := DBInstanceDetails{
		Identifier:       aws.StringValue(dbInstance.DBInstanceIdentifier),
		Status:           aws.StringValue(dbInstance.DBInstanceStatus),
		Engine:           aws.StringValue(dbInstance.Engine),
		EngineVersion:    aws.StringValue(dbInstance.EngineVersion),
		DBName:           aws.StringValue(dbInstance.DBName),
		MasterUsername:   aws.StringValue(dbInstance.MasterUsername),
		AllocatedStorage: aws.Int64Value(dbInstance.AllocatedStorage),
	}

	if dbInstance.Endpoint != nil {
		dbInstanceDetails.Address = aws.StringValue(dbInstance.Endpoint.Address)
		dbInstanceDetails.Port = aws.Int64Value(dbInstance.Endpoint.Port)
	}

	if dbInstance.PendingModifiedValues != nil {
		emptyPendingModifiedValues := &rds.PendingModifiedValues{}
		if *dbInstance.PendingModifiedValues != *emptyPendingModifiedValues {
			dbInstanceDetails.PendingModifications = true
		}
	}

	return dbInstanceDetails
}
func rootBlockDeviceToSet(
	bdm []*ec2.BlockDeviceMapping,
	rootDevName *string,
) *schema.Set {
	set := &schema.Set{F: hashRootBlockDevice}

	if rootDevName != nil {
		for _, val := range bdm {
			if aws.StringValue(val.DeviceName) == aws.StringValue(rootDevName) {
				m := make(map[string]interface{})
				if val.Ebs.DeleteOnTermination != nil {
					m["delete_on_termination"] = aws.BoolValue(val.Ebs.DeleteOnTermination)
				}

				if val.Ebs.VolumeSize != nil {
					m["volume_size"] = aws.Int64Value(val.Ebs.VolumeSize)
				}

				if val.Ebs.VolumeType != nil {
					m["volume_type"] = aws.StringValue(val.Ebs.VolumeType)
				}

				if val.Ebs.Iops != nil {
					m["iops"] = aws.Int64Value(val.Ebs.Iops)
				}

				set.Add(m)
			}
		}
	}

	return set
}
示例#6
0
func (client *ecsClient) CreateService(serviceName, taskDefName string, deploymentConfig *ecs.DeploymentConfiguration) error {
	_, err := client.client.CreateService(&ecs.CreateServiceInput{
		DesiredCount:            aws.Int64(0),            // Required
		ServiceName:             aws.String(serviceName), // Required
		TaskDefinition:          aws.String(taskDefName), // Required
		Cluster:                 aws.String(client.params.Cluster),
		DeploymentConfiguration: deploymentConfig,
	})
	if err != nil {
		log.WithFields(log.Fields{
			"service": serviceName,
			"error":   err,
		}).Error("Error creating service")
		return err
	}

	fields := log.Fields{
		"service":        serviceName,
		"taskDefinition": taskDefName,
	}
	if deploymentConfig != nil && deploymentConfig.MaximumPercent != nil {
		fields["deployment-max-percent"] = aws.Int64Value(deploymentConfig.MaximumPercent)
	}
	if deploymentConfig != nil && deploymentConfig.MinimumHealthyPercent != nil {
		fields["deployment-min-healthy-percent"] = aws.Int64Value(deploymentConfig.MinimumHealthyPercent)
	}

	log.WithFields(fields).Info("Created an ECS service")
	return nil
}
func TestLoadContext(t *testing.T) {
	deploymentMaxPercent := 150

	flagSet := flag.NewFlagSet("ecs-cli-up", 0)
	flagSet.String(DeploymentMaxPercentFlag, strconv.Itoa(deploymentMaxPercent), "")
	cliContext := cli.NewContext(nil, flagSet, nil)
	service := &Service{
		projectContext: &Context{CLIContext: cliContext},
	}

	if err := service.LoadContext(); err != nil {
		t.Fatal("Unexpected error while loading context in load context test")
	}

	observedDeploymentConfig := service.DeploymentConfig()
	if aws.Int64Value(observedDeploymentConfig.MaximumPercent) != int64(deploymentMaxPercent) {
		t.Errorf("Expected DeploymentConfig.MaxPercent to be [%s] but got [%s]",
			deploymentMaxPercent, aws.Int64Value(observedDeploymentConfig.MaximumPercent))
	}
	if observedDeploymentConfig.MinimumHealthyPercent != nil {
		t.Errorf("Expected DeploymentConfig.MinimumHealthyPercent to be nil but got [%s]",
			aws.Int64Value(observedDeploymentConfig.MinimumHealthyPercent))
	}

}
示例#8
0
// Marshal parses the response from the aws sdk into an awsm ScalingPolicy
func (s *ScalingPolicy) Marshal(policy *autoscaling.ScalingPolicy, region string) {
	adjustment := int(aws.Int64Value(policy.ScalingAdjustment))
	adjustmentStr := fmt.Sprint(adjustment)
	if adjustment >= 1 {
		adjustmentStr = fmt.Sprintf("+%d", adjustment)
	}

	var alarmArns []string
	var alarmNames []string

	for _, alarm := range policy.Alarms {
		arnStr := aws.StringValue(alarm.AlarmARN)
		alarmArns = append(alarmArns, arnStr)

		arn, err := ParseArn(arnStr)
		if err == nil {
			alarmNames = append(alarmNames, arn.Resource)
		} else {
			alarmNames = append(alarmNames, "?????")
		}
	}

	s.Name = aws.StringValue(policy.PolicyName)
	s.Arn = aws.StringValue(policy.PolicyARN)
	s.AdjustmentType = aws.StringValue(policy.AdjustmentType)
	s.Adjustment = adjustment
	s.AdjustmentStr = adjustmentStr
	s.Cooldown = fmt.Sprint(aws.Int64Value(policy.Cooldown))
	s.AutoScaleGroupName = aws.StringValue(policy.AutoScalingGroupName)
	s.AlarmArns = alarmArns
	s.AlarmNames = strings.Join(alarmNames, ", ")

	s.Region = region
}
示例#9
0
func (e *LoadBalancer) Find(c *fi.Context) (*LoadBalancer, error) {
	cloud := c.Cloud.(*awsup.AWSCloud)

	elbName := fi.StringValue(e.ID)
	if elbName == "" {
		elbName = fi.StringValue(e.Name)
	}

	lb, err := findELB(cloud, elbName)
	if err != nil {
		return nil, err
	}
	if lb == nil {
		return nil, nil
	}

	actual := &LoadBalancer{}
	actual.Name = e.Name
	actual.ID = lb.LoadBalancerName
	actual.DNSName = lb.DNSName
	actual.HostedZoneId = lb.CanonicalHostedZoneNameID
	for _, subnet := range lb.Subnets {
		actual.Subnets = append(actual.Subnets, &Subnet{ID: subnet})
	}

	for _, sg := range lb.SecurityGroups {
		actual.SecurityGroups = append(actual.SecurityGroups, &SecurityGroup{ID: sg})
	}

	actual.Listeners = make(map[string]*LoadBalancerListener)

	for _, ld := range lb.ListenerDescriptions {
		l := ld.Listener
		loadBalancerPort := strconv.FormatInt(aws.Int64Value(l.LoadBalancerPort), 10)

		actualListener := &LoadBalancerListener{}
		actualListener.InstancePort = int(aws.Int64Value(l.InstancePort))
		actual.Listeners[loadBalancerPort] = actualListener
	}

	// Avoid spurious mismatches
	if subnetSlicesEqualIgnoreOrder(actual.Subnets, e.Subnets) {
		actual.Subnets = e.Subnets
	}
	if e.DNSName == nil {
		e.DNSName = actual.DNSName
	}
	if e.HostedZoneId == nil {
		e.HostedZoneId = actual.HostedZoneId
	}
	if e.ID == nil {
		e.ID = actual.ID
	}

	return actual, nil
}
示例#10
0
文件: alarms.go 项目: murdinc/awsm
// Marshal parses the response from the aws sdk into an awsm Alarm
func (a *Alarm) Marshal(alarm *cloudwatch.MetricAlarm, region string) {
	var dimensions []string
	var operator string

	for _, dim := range alarm.Dimensions {
		dimensions = append(dimensions, aws.StringValue(dim.Name)+" = "+aws.StringValue(dim.Value))
	}

	switch aws.StringValue(alarm.ComparisonOperator) {
	case "GreaterThanThreshold":
		operator = ">"

	case "GreaterThanOrEqualToThreshold":
		operator = ">="

	case "LessThanThreshold":
		operator = "<"

	case "LessThanOrEqualToThreshold":
		operator = "<="
	}

	var actionArns []string
	var actionNames []string

	for _, action := range alarm.AlarmActions {
		arnStr := aws.StringValue(action)
		actionArns = append(actionArns, arnStr)

		arn, err := ParseArn(arnStr)
		if err == nil {
			actionNames = append(actionNames, arn.PolicyName)
		} else {
			actionNames = append(actionNames, "??????")
		}
	}

	a.Name = aws.StringValue(alarm.AlarmName)
	a.Arn = aws.StringValue(alarm.AlarmArn)
	a.Description = aws.StringValue(alarm.AlarmDescription)
	a.State = aws.StringValue(alarm.StateValue)
	a.Trigger = fmt.Sprintf("%s %s %d (%s)", aws.StringValue(alarm.MetricName), operator, int(aws.Float64Value(alarm.Threshold)), aws.StringValue(alarm.Statistic))
	a.Period = fmt.Sprint(aws.Int64Value(alarm.Period))
	a.EvalPeriods = fmt.Sprint(aws.Int64Value(alarm.EvaluationPeriods))
	a.ActionArns = actionArns
	a.ActionNames = strings.Join(actionNames, ", ")
	a.Dimensions = strings.Join(dimensions, ", ")
	a.Namespace = aws.StringValue(alarm.Namespace)
	a.Region = region
}
示例#11
0
func (r *RDSDBCluster) buildDBCluster(dbCluster *rds.DBCluster) DBClusterDetails {
	dbClusterDetails := DBClusterDetails{
		Identifier:       aws.StringValue(dbCluster.DBClusterIdentifier),
		Status:           aws.StringValue(dbCluster.Status),
		Engine:           aws.StringValue(dbCluster.Engine),
		EngineVersion:    aws.StringValue(dbCluster.EngineVersion),
		DatabaseName:     aws.StringValue(dbCluster.DatabaseName),
		MasterUsername:   aws.StringValue(dbCluster.MasterUsername),
		AllocatedStorage: aws.Int64Value(dbCluster.AllocatedStorage),
		Endpoint:         aws.StringValue(dbCluster.Endpoint),
		Port:             aws.Int64Value(dbCluster.Port),
	}

	return dbClusterDetails
}
func (s *CloudFormationStack) buildStackDetails(stack *cloudformation.Stack) StackDetails {
	stackDetails := StackDetails{
		StackName:        aws.StringValue(stack.StackName),
		Capabilities:     aws.StringValueSlice(stack.Capabilities),
		DisableRollback:  aws.BoolValue(stack.DisableRollback),
		Description:      aws.StringValue(stack.Description),
		NotificationARNs: aws.StringValueSlice(stack.NotificationARNs),
		StackID:          aws.StringValue(stack.StackId),
		StackStatus:      s.stackStatus(aws.StringValue(stack.StackStatus)),
		TimeoutInMinutes: aws.Int64Value(stack.TimeoutInMinutes),
	}

	if stack.Parameters != nil && len(stack.Parameters) > 0 {
		stackDetails.Parameters = make(map[string]string)
		for _, parameter := range stack.Parameters {
			stackDetails.Parameters[aws.StringValue(parameter.ParameterKey)] = aws.StringValue(parameter.ParameterValue)
		}
	}

	if stack.Outputs != nil && len(stack.Outputs) > 0 {
		stackDetails.Outputs = make(map[string]string)
		for _, output := range stack.Outputs {
			stackDetails.Outputs[aws.StringValue(output.OutputKey)] = aws.StringValue(output.OutputValue)
		}
	}

	return stackDetails
}
示例#13
0
文件: volumes.go 项目: koding/koding
// Size returns the given volume ids size. Returns 0 if the size is not
// available
func (v Volumes) SizeFromVolumeId(id string) int {
	vol, ok := v[id]
	if !ok {
		return 0
	}
	return int(aws.Int64Value(vol.Size))
}
示例#14
0
// Down stops any running containers(tasks) by calling Stop() and deletes an active ECS Service
// NoOp if the service is inactive
func (s *Service) Down() error {
	// describe the service
	ecsService, err := s.describeService()
	if err != nil {
		return err
	}

	ecsServiceName := aws.StringValue(ecsService.ServiceName)
	// if already deleted, NoOp
	if aws.StringValue(ecsService.Status) != ecsActiveResourceCode {
		log.WithFields(log.Fields{
			"serviceName": ecsServiceName,
		}).Info("ECS Service is already deleted")
		return nil
	}

	// stop any running tasks
	if aws.Int64Value(ecsService.DesiredCount) != 0 {
		if err = s.Stop(); err != nil {
			return err
		}
	}

	// deleteService
	if err = s.Context().ECSClient.DeleteService(ecsServiceName); err != nil {
		return err
	}
	return waitForServiceTasks(s, ecsServiceName)
}
示例#15
0
func (s3 *s3driver) CheckDataAndGetSize(dpconn, itemlocation, fileName string) (exist bool, size int64, err error) {
	bucket := getAwsInfoFromDpconn(dpconn)

	destFullPathFileName := bucket + "/" + itemlocation + "/" + fileName
	log.Info(destFullPathFileName)

	AWS_REGION = Env("AWS_REGION", false)

	svc := s3aws.New(session.New(&aws.Config{Region: aws.String(AWS_REGION)}))
	result, err := svc.ListObjects(&s3aws.ListObjectsInput{Bucket: aws.String(bucket),
		Prefix: aws.String(itemlocation + "/" + fileName)})
	if err != nil {
		log.Error("Failed to list objects", err)
		return exist, size, err
	}

	exist = false
	for _, v := range result.Contents {
		log.Infof("Tag:%s, key:%s, size:%v\n", aws.StringValue(v.ETag), aws.StringValue(v.Key), aws.Int64Value(v.Size))
		if aws.StringValue(v.Key) == fileName {
			size = aws.Int64Value(v.Size)
			exist = true
		}
	}

	return
}
func ebsBlockDevicesToSet(bdm []*ec2.BlockDeviceMapping, rootDevName *string) *schema.Set {
	set := &schema.Set{F: hashEphemeralBlockDevice}

	for _, val := range bdm {
		if val.Ebs != nil {
			m := make(map[string]interface{})

			ebs := val.Ebs

			if val.DeviceName != nil {
				if aws.StringValue(rootDevName) == aws.StringValue(val.DeviceName) {
					continue
				}

				m["device_name"] = aws.StringValue(val.DeviceName)
			}

			if ebs.DeleteOnTermination != nil {
				m["delete_on_termination"] = aws.BoolValue(ebs.DeleteOnTermination)
			}

			if ebs.SnapshotId != nil {
				m["snapshot_id"] = aws.StringValue(ebs.SnapshotId)
			}

			if ebs.Encrypted != nil {
				m["encrypted"] = aws.BoolValue(ebs.Encrypted)
			}

			if ebs.VolumeSize != nil {
				m["volume_size"] = aws.Int64Value(ebs.VolumeSize)
			}

			if ebs.VolumeType != nil {
				m["volume_type"] = aws.StringValue(ebs.VolumeType)
			}

			if ebs.Iops != nil {
				m["iops"] = aws.Int64Value(ebs.Iops)
			}

			set.Add(m)
		}
	}

	return set
}
示例#17
0
// Marshal parses the response from the aws sdk into an awsm Security Group
func (s *SecurityGroup) Marshal(securitygroup *ec2.SecurityGroup, region string, vpcList *Vpcs) {

	vpc := vpcList.GetVpcName(aws.StringValue(securitygroup.VpcId))

	s.Name = aws.StringValue(securitygroup.GroupName)
	s.Class = GetTagValue("Class", securitygroup.Tags)
	s.GroupID = aws.StringValue(securitygroup.GroupId)
	s.Description = aws.StringValue(securitygroup.Description)
	s.Vpc = vpc
	s.VpcID = aws.StringValue(securitygroup.VpcId)
	s.Region = region

	// Get the ingress grants
	for _, grant := range securitygroup.IpPermissions {

		var cidr []string
		for _, ipRange := range grant.IpRanges {
			cidr = append(cidr, aws.StringValue(ipRange.CidrIp))
		}

		s.SecurityGroupGrants = append(s.SecurityGroupGrants, config.SecurityGroupGrant{
			Type:       "ingress",
			FromPort:   int(aws.Int64Value(grant.FromPort)),
			ToPort:     int(aws.Int64Value(grant.ToPort)),
			IPProtocol: aws.StringValue(grant.IpProtocol),
			CidrIP:     cidr,
		})
	}

	// Get the egress grants
	for _, grant := range securitygroup.IpPermissionsEgress {

		var cidr []string
		for _, ipRange := range grant.IpRanges {
			cidr = append(cidr, aws.StringValue(ipRange.CidrIp))
		}

		s.SecurityGroupGrants = append(s.SecurityGroupGrants, config.SecurityGroupGrant{
			Type:       "egress",
			FromPort:   int(aws.Int64Value(grant.FromPort)),
			ToPort:     int(aws.Int64Value(grant.ToPort)),
			IPProtocol: aws.StringValue(grant.IpProtocol),
			CidrIP:     cidr,
		})
	}

}
示例#18
0
// Up creates the task definition and service and starts the containers if necessary.
// It does so by calling DescribeService to see if its present, else's calls Create() and Start()
// If the compose file had changed, it would update the service with the new task definition
// by calling UpdateService with the new task definition
func (s *Service) Up() error {
	// describe service to get the task definition and count running
	ecsService, err := s.describeService()
	var missingServiceErr bool
	if err != nil {
		if strings.Contains(err.Error(), ecsMissingResourceCode) {
			missingServiceErr = true
		} else {
			return err
		}
	}

	// get the current snapshot of compose yml
	// and update this instance with the latest task definition
	newTaskDefinition, err := getOrCreateTaskDefinition(s)
	if err != nil {
		return err
	}

	// if ECS service was not created before, or is inactive, create and start the ECS Service
	if missingServiceErr || aws.StringValue(ecsService.Status) != ecsActiveResourceCode {
		// uses the latest task definition to create the service
		err = s.createService()
		if err != nil {
			return err
		}
		return s.Start()
	}

	oldTaskDefinitionId := getIdFromArn(ecsService.TaskDefinition)
	newTaskDefinitionId := getIdFromArn(newTaskDefinition.TaskDefinitionArn)

	oldCount := aws.Int64Value(ecsService.DesiredCount)
	newCount := int64(1)
	if oldCount != 0 {
		newCount = oldCount // get the current non-zero count
	}

	// if both the task definitions are the same, just start the service
	if oldTaskDefinitionId == newTaskDefinitionId {
		return s.startService(ecsService)
	}

	ecsServiceName := aws.StringValue(ecsService.ServiceName)

	// if the task definitions were different, updateService with new task definition
	// this creates a deployment in ECS and slowly takes down the containers with old ones and starts new ones
	err = s.Context().ECSClient.UpdateService(ecsServiceName, newTaskDefinitionId, newCount)
	if err != nil {
		return err
	}
	log.WithFields(log.Fields{
		"serviceName":    ecsServiceName,
		"taskDefinition": newTaskDefinitionId,
		"desiredCount":   newCount,
	}).Info("Updated the ECS service with a new task definition. " +
		"Old containers will be stopped automatically, and replaced with new ones")
	return waitForServiceTasks(s, ecsServiceName)
}
示例#19
0
func (d *dumper) start(infoWriter io.Writer) (done chan error, err error) {
	out := d.openWriters()
	w := dyndump.NewSimpleEncoder(out)

	fmt.Fprintf(infoWriter, "Beginning scan: table=%q readCapacity=%d parallel=%d itemCount=%d totalSize=%s\n",
		*d.tableName, *d.readCapacity, *d.parallel,
		aws.Int64Value(d.tableInfo.ItemCount), fmtBytes(aws.Int64Value(d.tableInfo.TableSizeBytes)))

	d.f = &dyndump.Fetcher{
		Dyn:            d.dyn,
		TableName:      *d.tableName,
		ConsistentRead: *d.consistentRead,
		MaxParallel:    *d.parallel,
		MaxItems:       int64(*d.maxItems),
		ReadCapacity:   float64(*d.readCapacity),
		Writer:         w,
	}

	done = make(chan error)
	d.abortChan = make(chan struct{}, 1)
	d.startTime = time.Now()

	go func() {
		rerr := make(chan error)
		go func() { rerr <- d.f.Run() }()

		select {
		case <-d.abortChan:
			d.f.Stop()
			<-rerr
			out.Abort()
			done <- errors.New("Aborted")

		case err := <-rerr:
			if err != nil {
				out.Abort()
				done <- err
			} else {
				done <- out.Close()
			}
		}
	}()

	return done, nil
}
示例#20
0
文件: goofys.go 项目: x5u/goofys
// returned inode has nil Id
func (fs *Goofys) LookUpInodeMaybeDir(name string, fullName string) (inode *Inode, err error) {
	errObjectChan := make(chan error, 1)
	objectChan := make(chan s3.HeadObjectOutput, 1)
	errDirChan := make(chan error, 1)
	dirChan := make(chan s3.ListObjectsOutput, 1)

	go fs.LookUpInodeNotDir(fullName, objectChan, errObjectChan)
	go fs.LookUpInodeDir(fullName, dirChan, errDirChan)

	notFound := false

	for {
		select {
		case resp := <-objectChan:
			// XXX/TODO if both object and object/ exists, return dir
			inode = NewInode(&name, &fullName, fs.flags)
			inode.Attributes = &fuseops.InodeAttributes{
				Size:   uint64(aws.Int64Value(resp.ContentLength)),
				Nlink:  1,
				Mode:   fs.flags.FileMode,
				Atime:  *resp.LastModified,
				Mtime:  *resp.LastModified,
				Ctime:  *resp.LastModified,
				Crtime: *resp.LastModified,
				Uid:    fs.flags.Uid,
				Gid:    fs.flags.Gid,
			}
			return
		case err = <-errObjectChan:
			if err == fuse.ENOENT {
				if notFound {
					return nil, err
				} else {
					notFound = true
					err = nil
				}
			} else {
				return
			}
		case resp := <-dirChan:
			if len(resp.CommonPrefixes) != 0 || len(resp.Contents) != 0 {
				inode = NewInode(&name, &fullName, fs.flags)
				inode.Attributes = &fs.rootAttrs
				return
			} else {
				// 404
				if notFound {
					return nil, fuse.ENOENT
				} else {
					notFound = true
				}
			}
		case err = <-errDirChan:
			return
		}
	}
}
func TestCreateWithoutDeploymentConfig(t *testing.T) {
	flagSet := flag.NewFlagSet("ecs-cli-up", 0)
	cliContext := cli.NewContext(nil, flagSet, nil)

	createServiceTest(
		t,
		cliContext,
		func(deploymentConfig *ecs.DeploymentConfiguration) {
			if deploymentConfig.MaximumPercent != nil {
				t.Errorf("Expected DeploymentConfig.MaximumPercent to be nil but got [%s]",
					aws.Int64Value(deploymentConfig.MaximumPercent))
			}
			if deploymentConfig.MinimumHealthyPercent != nil {
				t.Errorf("Expected DeploymentConfig.MinimumHealthyPercent to be nil but got [%s]",
					aws.Int64Value(deploymentConfig.MinimumHealthyPercent))
			}
		},
	)
}
示例#22
0
// Marshal parses the response from the aws sdk into an awsm AutoScale Group
func (a *AutoScaleGroup) Marshal(autoscalegroup *autoscaling.Group, region string, subList *Subnets) {
	a.Name = aws.StringValue(autoscalegroup.AutoScalingGroupName)
	a.Class = GetTagValue("Class", autoscalegroup.Tags)
	a.HealthCheckType = aws.StringValue(autoscalegroup.HealthCheckType)
	a.HealthCheckGracePeriod = int(aws.Int64Value(autoscalegroup.HealthCheckGracePeriod))
	a.LaunchConfig = aws.StringValue(autoscalegroup.LaunchConfigurationName)
	a.LoadBalancers = aws.StringValueSlice(autoscalegroup.LoadBalancerNames)
	a.InstanceCount = len(autoscalegroup.Instances)
	a.DesiredCapacity = int(aws.Int64Value(autoscalegroup.DesiredCapacity))
	a.MinSize = int(aws.Int64Value(autoscalegroup.MinSize))
	a.MaxSize = int(aws.Int64Value(autoscalegroup.MaxSize))
	a.DefaultCooldown = int(aws.Int64Value(autoscalegroup.DefaultCooldown))
	a.AvailabilityZones = aws.StringValueSlice(autoscalegroup.AvailabilityZones)
	a.SubnetID = aws.StringValue(autoscalegroup.VPCZoneIdentifier)
	a.SubnetName = subList.GetSubnetName(a.SubnetID)
	a.VpcID = subList.GetVpcIDBySubnetID(a.SubnetID)
	a.VpcName = subList.GetVpcNameBySubnetID(a.SubnetID)
	a.Region = region
}
示例#23
0
// PortString returns a formatted string with container's network bindings
// in a comma seperated fashion
func (c *Container) PortString() string {
	result := []string{}
	for _, port := range c.ecsContainer.NetworkBindings {
		protocol := ecs.TransportProtocolTcp
		if port.Protocol != nil {
			protocol = aws.StringValue(port.Protocol)
		}
		ipAddr := aws.StringValue(port.BindIP)
		if c.ec2IPAddress != "" {
			ipAddr = c.ec2IPAddress
		}
		result = append(result, fmt.Sprintf("%s:%d->%d/%s",
			ipAddr,
			aws.Int64Value(port.HostPort),
			aws.Int64Value(port.ContainerPort),
			protocol))
	}
	return strings.Join(result, ", ")
}
示例#24
0
func (s *ShardWorker) GetRecords(it string) ([]*kinesis.Record, string, int64, error) {
	resp, err := s.kinesis.GetRecords(&kinesis.GetRecordsInput{
		Limit:         &s.GetRecordsLimit,
		ShardIterator: &it,
	})
	if err != nil {
		return nil, "", 0, err
	}
	return resp.Records, aws.StringValue(resp.NextShardIterator), aws.Int64Value(resp.MillisBehindLatest), nil
}
func dataSourceAwsEcsContainerDefinitionRead(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).ecsconn

	desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{
		TaskDefinition: aws.String(d.Get("task_definition").(string)),
	})

	if err != nil {
		return err
	}

	taskDefinition := *desc.TaskDefinition
	for _, def := range taskDefinition.ContainerDefinitions {
		if aws.StringValue(def.Name) != d.Get("container_name").(string) {
			continue
		}

		d.SetId(fmt.Sprintf("%s/%s", aws.StringValue(taskDefinition.TaskDefinitionArn), d.Get("container_name").(string)))
		d.Set("image", aws.StringValue(def.Image))
		image := aws.StringValue(def.Image)
		if strings.Contains(image, ":") {
			d.Set("image_digest", strings.Split(image, ":")[1])
		}
		d.Set("cpu", aws.Int64Value(def.Cpu))
		d.Set("memory", aws.Int64Value(def.Memory))
		d.Set("memory_reservation", aws.Int64Value(def.MemoryReservation))
		d.Set("disable_networking", aws.BoolValue(def.DisableNetworking))
		d.Set("docker_labels", aws.StringValueMap(def.DockerLabels))

		var environment = map[string]string{}
		for _, keyValuePair := range def.Environment {
			environment[aws.StringValue(keyValuePair.Name)] = aws.StringValue(keyValuePair.Value)
		}
		d.Set("environment", environment)
	}

	if d.Id() == "" {
		return fmt.Errorf("container with name %q not found in task definition %q", d.Get("container_name").(string), d.Get("task_definition").(string))
	}

	return nil
}
示例#26
0
文件: client.go 项目: koding/koding
// Snapshot gives the ID and size of the snapshot that image uses.
//
// NOTE(rjeczalik): The API was built for provider/aws migration of koding
// instances, which had only a single EBS.
func (i *Image) Snapshot() (string, int64, error) {
	for _, dev := range i.BlockDeviceMappings {
		if dev.Ebs == nil {
			continue
		}

		return aws.StringValue(dev.Ebs.SnapshotId), aws.Int64Value(dev.Ebs.VolumeSize), nil
	}

	return "", 0, fmt.Errorf("no snapshots found for %s image", i.ID())
}
示例#27
0
文件: volumes.go 项目: koding/koding
// GreaterTan filters out volumes which are greater than the given storage size
func (v Volumes) GreaterThan(storage int) Volumes {
	filtered := make(Volumes, 0)

	for id, volume := range v {
		if aws.Int64Value(volume.Size) > int64(storage) {
			filtered[id] = volume
		}
	}

	return filtered
}
func TestCpuStatsSetNotSetToInfinity(t *testing.T) {
	// timestamps will be used to simulate +Inf CPU Usage
	// timestamps[0] = timestamps[1]
	timestamps := []time.Time{
		parseNanoTime("2015-02-12T21:22:05.131117533Z"),
		parseNanoTime("2015-02-12T21:22:05.131117533Z"),
		parseNanoTime("2015-02-12T21:22:05.333776335Z"),
	}
	cpuTimes := []uint64{
		22400432,
		116499979,
		248503503,
	}
	memoryUtilizationInBytes := []uint64{
		3649536,
		3649536,
		3649536,
	}

	// Create and add container stats
	queueLength := 3
	queue := NewQueue(queueLength)
	for i, time := range timestamps {
		queue.Add(&ContainerStats{cpuUsage: cpuTimes[i], memoryUsage: memoryUtilizationInBytes[i], timestamp: time})
	}
	cpuStatsSet, err := queue.GetCPUStatsSet()
	if err != nil {
		t.Errorf("Error getting cpu stats set: %v", err)
	}

	// Compute expected usage by using the 1st and 2nd data point in the input queue
	// queue.Add should have ignored the 0th item as it has the same timestamp as the
	// 1st item
	expectedCpuUsage := 100 * float32(cpuTimes[2]-cpuTimes[1]) / float32(timestamps[2].Nanosecond()-timestamps[1].Nanosecond())
	max := float32(aws.Float64Value(cpuStatsSet.Max))
	if max != expectedCpuUsage {
		t.Errorf("Computed cpuStatsSet.Max (%f) != expected value (%f)", max, expectedCpuUsage)
	}
	sum := float32(aws.Float64Value(cpuStatsSet.Sum))
	if sum != expectedCpuUsage {
		t.Errorf("Computed cpuStatsSet.Sum (%f) != expected value (%f)", sum, expectedCpuUsage)
	}
	min := float32(aws.Float64Value(cpuStatsSet.Min))
	if min != expectedCpuUsage {
		t.Errorf("Computed cpuStatsSet.Min (%f) != expected value (%f)", min, expectedCpuUsage)
	}

	// Expected sample count is 1 and not 2 as one data point would be discarded on
	// account of invalid timestamp
	sampleCount := aws.Int64Value(cpuStatsSet.SampleCount)
	if sampleCount != 1 {
		t.Errorf("Computed cpuStatsSet.SampleCount (%d) != expected value (%d)", sampleCount, 1)
	}
}
示例#29
0
// updateService calls the underlying ECS.UpdateService with the specified count
func (s *Service) updateService(count int64) error {
	serviceName := s.getServiceName()
	deploymentConfig := s.DeploymentConfig()
	if err := s.Context().ECSClient.UpdateServiceCount(serviceName, count, deploymentConfig); err != nil {
		return err
	}
	fields := log.Fields{
		"serviceName":  serviceName,
		"desiredCount": count,
	}
	if deploymentConfig != nil && deploymentConfig.MaximumPercent != nil {
		fields["deployment-max-percent"] = aws.Int64Value(deploymentConfig.MaximumPercent)
	}
	if deploymentConfig != nil && deploymentConfig.MinimumHealthyPercent != nil {
		fields["deployment-min-healthy-percent"] = aws.Int64Value(deploymentConfig.MinimumHealthyPercent)
	}

	log.WithFields(fields).Info("Updated ECS service successfully")
	return waitForServiceTasks(s, serviceName)
}
示例#30
0
// startService checks if the service has a zero desired count and updates the count to 1 (of each container)
func (s *Service) startService(ecsService *ecs.Service) error {
	desiredCount := aws.Int64Value(ecsService.DesiredCount)
	if desiredCount != 0 {
		serviceName := aws.StringValue(ecsService.ServiceName)
		//NoOp
		log.WithFields(log.Fields{
			"serviceName":  serviceName,
			"desiredCount": desiredCount,
		}).Info("ECS Service is already running")

		return waitForServiceTasks(s, serviceName)
	}
	return s.updateService(int64(1))
}