Example #1
0
    # Step 5: Downscale ECS Service if necessary
    if original_running_count >= args.minimum_running_tasks:
        h1("Step 5: Downscale ECS Service")
        response = ecs.downscale_service(cluster=args.cluster_name, service=args.service_name)
        downscale_running_count = (response.get('services')[0]).get('runningCount')
        success("Downscaling service '%s' (from %d to %d tasks) succeeded"
                % (args.service_name, original_running_count, downscale_running_count))
        delta = 1
    else:
        h1("Step 5: Downscale ECS Service")
        success("Downscaling service is not necessary (not enough tasks are running)")
        delta = args.minimum_running_tasks - original_running_count

    # Step 6: Update ECS Service
    h1("Step 6: Update ECS Service")
    response = ecs.update_service(cluster=args.cluster_name, service=args.service_name, taskDefinition=task_definition_arn)
    running_count = (response.get('services')[0]).get('runningCount')
    success("Updating service '%s' with task definition '%s' succeeded" % (args.service_name, task_definition_arn))

    # Step 7: Upscale ECS Service
    h1("Step 7: Upscale ECS Service")
    response = ecs.upscale_service(cluster=args.cluster_name, service=args.service_name, delta=delta)
    upscale_running_count = (response.get('services')[0]).get('runningCount')
    success("Upscaling service '%s' (from %d to %d tasks) succeeded"
            % (args.service_name, running_count, upscale_running_count))

except Exception as e:
    error(e)
    sys.exit(1)
Example #2
0
    task_definition_arn = response.get('taskDefinition').get(
        'taskDefinitionArn')
    success("Registering task definition '%s' succeeded" % task_definition_arn)

    if serviceMode:

        # Step: Update ECS Service
        h1("Step: Update ECS Service")

        # test code
        h1("%s" % args.cluster_name)
        h1("%s" % args.service_name)
        h1("%s" % task_definition_arn)

        response = ecs.update_service(cluster=args.cluster_name,
                                      service=args.service_name,
                                      taskDefinition=task_definition_arn)
        running_count = (response.get('services')[0]).get('runningCount')
        success("Updating service '%s' with task definition '%s' succeeded" %
                (args.service_name, task_definition_arn))

    else:
        # Step: run task
        h1("Step: Run task")
        response = ecs.run_task(cluster=args.cluster_name,
                                family=args.task_definition_name)
        success("Task %s succeeded" %
                (response.get('tasks')[0].get('taskArn')))

except Exception as e:
    error(e)
Example #3
0
class AwsProcess(Thread):
    def __init__(self, key, secret, region):
        super().__init__()
        self.ecs_service = ECSService(access_key=key, secret_key=secret, region=region)

    def run(self):
        while True:
            try:
                service, mode = task_queue.get_nowait()
            except Empty:
                time.sleep(1)
                continue
            try:
                self.process(service, mode)
            except:
                service.status = ProcessStatus.error
                error("Unexpected error. service: %s.\n%s" % (service.service_name, traceback.format_exc()))
            finally:
                task_queue.task_done()

    def process(self, service, mode):
        if service.status == ProcessStatus.error:
            error("service '%s' previous process error. skipping." % service.service_name)
            return

        if mode == ProcessMode.registerTask:
            response = self.ecs_service.register_task_definition(task_definition=service.task_definition)
            service.task_definition_arn = response.get('taskDefinition').get('taskDefinitionArn')
            success("Registering task definition '%s' succeeded (arn: '%s')" % (service.task_name, service.task_definition_arn))
            # for register task rate limit
            time.sleep(3)

        elif mode == ProcessMode.checkService:
            try:
                response = self.ecs_service.describe_service(service.task_environment.cluster_name, service.service_name)
            except ServiceNotFoundException:
                error("Service '%s' not Found." % (service.service_name))
                return
            if response['services'][0]['status'] == 'INACTIVE':
                error("Service '%s' status is INACTIVE." % (service.service_name))
                return
            service.original_task_definition = (response.get('services')[0]).get('taskDefinition')
            service.original_running_count = (response.get('services')[0]).get('runningCount')
            service.original_desired_count = (response.get('services')[0]).get('desiredCount')
            service.desired_count = service.original_desired_count
            service.service_exists = True
            success("Checking service '%s' succeeded (%d tasks running)" % (service.service_name, service.original_running_count))

        elif mode == ProcessMode.createService:
            response = self.ecs_service.create_service(cluster=service.task_environment.cluster_name, service=service.service_name, taskDefinition=service.task_definition_arn, desiredCount=service.task_environment.desired_count, maximumPercent=service.task_environment.maximum_percent, minimumHealthyPercent=service.task_environment.minimum_healthy_percent)
            service.original_running_count = (response.get('services')[0]).get('runningCount')
            service.original_desired_count = (response.get('services')[0]).get('desiredCount')
            service.desired_count = service.original_desired_count
            success("Create service '%s' succeeded (%d tasks running)" % (service.service_name, service.original_running_count))

        elif mode == ProcessMode.updateService:
            response = self.ecs_service.update_service(cluster=service.task_environment.cluster_name, service=service.service_name, taskDefinition=service.task_definition_arn, maximumPercent=service.task_environment.maximum_percent, minimumHealthyPercent=service.task_environment.minimum_healthy_percent, desiredCount=service.task_environment.desired_count)
            service.running_count = response.get('services')[0].get('runningCount')
            service.desired_count = response.get('services')[0].get('desiredCount')
            success("Update service '%s' with task definition '%s' succeeded" % (service.service_name, service.task_definition_arn))

        elif mode == ProcessMode.waitForStable:
            retryCount = 0
            while True:
                try:
                    response = self.ecs_service.wait_for_stable(cluster=service.task_environment.cluster_name, service=service.service_name)
                except WaiterError:
                    if retryCount > 2:
                        break
                    retryCount = retryCount + 1
                    continue
                break
            service.running_count = response.get('services')[0].get('runningCount')
            service.desired_count = response.get('services')[0].get('desiredCount')
            self.ecs_service.deregister_task_definition(service.original_task_definition)
            success("service '%s' (%d tasks) update completed"
                        % (service.service_name, service.running_count))