// describeEc2Instances Describes EC2 instances by container istance ID func describeEc2Instances(svc *ecs.ECS, cluster *string, containerInstances []*string) (*ec2.DescribeInstancesOutput, error) { params := &ecs.DescribeContainerInstancesInput{ Cluster: cluster, ContainerInstances: containerInstances, } ins, err := svc.DescribeContainerInstances(params) if err != nil { return nil, err } insfail := cli.Failure(ins.Failures, err) if insfail != nil { return nil, insfail } var ec2Instances = make([]*string, len(ins.ContainerInstances)) for i, v := range ins.ContainerInstances { ec2Instances[i] = v.Ec2InstanceId } ec2client := ec2.New(sess.InitSession()) ec2params := &ec2.DescribeInstancesInput{ DryRun: aws.Bool(false), InstanceIds: ec2Instances, } return ec2client.DescribeInstances(ec2params) }
// StartWait Starts a new task a waits until it started successfully. func StartWait(svc *ecs.ECS, maxTries *int, timeout *int64, taskDef *string, containerInstances []*string, cluster *string, startedBy *string, overrides *ecs.TaskOverride) (*ecs.DescribeTasksOutput, error) { tries := 0 counter := 0 start, err := StartTask( svc, taskDef, containerInstances, cluster, startedBy, nil, ) if err != nil { return nil, err } fail := cli.Failure(start.Failures, err) if fail != nil { return nil, fail } var tasks = make([]*string, len(start.Tasks)) for i, v := range start.Tasks { tasks[i] = v.TaskArn } for { resp, err := DescribeTasks( svc, tasks, &cliClusterName, ) descFail := cli.Failure(resp.Failures, err) if descFail != nil { return nil, descFail } for _, v := range resp.Tasks { if *v.LastStatus == *v.DesiredStatus { counter = counter + 1 } tries = tries + 1 } if counter >= len(resp.Tasks) { return resp, nil } if tries >= *maxTries { return resp, errors.New("Max tries (" + strconv.Itoa(*maxTries) + ") reached") } time.Sleep(time.Duration(*timeout) * time.Second) } }
func cliDescribeTasks(svc *ecs.ECS, args []string) ([]*string, error) { err := cliDescribeTasksParams(args).Parse(args) resp, err := DescribeTasks( svc, aws.StringSlice(strings.Split(cliTasks, ",")), &cliClusterName, ) if err != nil { return nil, err } fail := cli.Failure(resp.Failures, err) if fail != nil { return nil, fail } var ret = make([]*string, len(resp.Tasks)) for k := range resp.Tasks { ret[k] = resp.Tasks[k].TaskArn } return ret, err }
func cliStartTask(svc *ecs.ECS, args []string) ([]*string, error) { err := cliStartTaskParams(args).Parse(args) resp, err := StartTask( svc, &cliTaskDef, aws.StringSlice(strings.Split(cliContainerInstance, ",")), cli.String(cliClusterName), cli.String(cliStartedby), nil, ) if err != nil { return nil, err } fail := cli.Failure(resp.Failures, err) if fail != nil { return nil, fail } var ret = make([]*string, len(resp.Tasks)) for k := range resp.Tasks { ret[k] = resp.Tasks[k].TaskArn } return ret, nil }
func cliStartWait(svc *ecs.ECS, args []string) ([]*string, error) { err := cliStartWaitParams(args).Parse(args) containerInstances := aws.StringSlice(strings.Split(cliContainerInstance, ",")) resp, err := StartWait( svc, &cliMaxTries, &cliTimeout, &cliTaskDef, containerInstances, cli.String(cliClusterName), cli.String(cliStartedby), nil, ) if err != nil { return nil, err } fail := cli.Failure(resp.Failures, err) if fail != nil { return nil, fail } var ret = make([]*string, len(resp.Tasks)) for k := range resp.Tasks { ret[k] = resp.Tasks[k].TaskArn } dns, dnserr := describeEc2Instances(svc, cli.String(cliClusterName), containerInstances) if dnserr != nil { return ret, dnserr } var result = make([]*string, len(ret)+len(dns.Reservations)) copy(result, ret) for _, r := range dns.Reservations { for i, v := range r.Instances { result[len(ret)+i] = v.PublicDnsName } } return result, nil }