Exemple #1
0
def start_agent(environment, service, region, elb_name, health_interval,
                assignment_interval):
    # Identity:
    instance_id = get_instance_id()
    logger.debug('Resolved id: %s', instance_id)

    # Systemd:
    manager = Manager()
    systemd = SystemdUnits(manager)

    # AWS services:
    lb = get_elb(instance_id, elb_name, region)
    dynamo = boto.dynamodb2.connect_to_region(region)
    kms = boto.kms.connect_to_region(region)

    # DynamoDB:
    tables = DynamoDbTables(dynamo, environment=environment)
    tables.setup(['status', 'assignments', 'revisions', 'units', 'locks'])
    db = FlotillaAgentDynamo(instance_id, service, tables.status,
                             tables.assignments, tables.revisions,
                             tables.units, kms)
    locks = DynamoDbLocks(instance_id, tables.locks)

    # SQS:
    sqs = boto3.resource('sqs', region)
    scheduler_q = get_queue(sqs, 'flotilla-%s-scheduler' % environment)
    service_q = get_queue(sqs, 'flotilla-%s-service-%s' %
                          (environment, service))

    messaging = FlotillaAgentMessaging(service, instance_id, scheduler_q,
                                       service_q)

    # Assemble into agent:
    agent = FlotillaAgent(service, db, locks, systemd, messaging, lb)

    # Start loops:
    funcs = [
        RepeatingFunc('health', agent.health, health_interval),
        RepeatingFunc('assignment', agent.assignment, assignment_interval),
    ]

    if service_q:
        funcs.append(RepeatingFunc('message', messaging.receive, 0))

    map(RepeatingFunc.start, funcs)
    logger.info('Startup complete.')
Exemple #2
0
def start_scheduler(environment, domain, regions, lock_interval, loop_interval,
                    provision_interval):
    instance_id = get_instance_id()

    coreos = CoreOsAmiIndex()
    cloudformation = FlotillaCloudFormation(environment, domain, coreos)

    funcs = []
    for region in regions:
        # DynamoDB:
        dynamo = boto.dynamodb2.connect_to_region(region)
        tables = DynamoDbTables(dynamo, environment=environment)
        tables.setup([
            'assignments', 'locks', 'regions', 'services', 'stacks', 'status'
        ])
        db = FlotillaSchedulerDynamo(tables.assignments, tables.regions,
                                     tables.services, tables.stacks,
                                     tables.status)
        locks = DynamoDbLocks(instance_id, tables.locks)

        # Assemble into scheduler:
        schedule = FlotillaScheduler(db, locks, lock_ttl=lock_interval * 3)
        provisioner = FlotillaProvisioner(environment, region, schedule, db,
                                          cloudformation)

        funcs += [
            RepeatingFunc('scheduler-lock-%s' % region, schedule.lock,
                          lock_interval),
            RepeatingFunc('scheduler-%s' % region, schedule.loop,
                          loop_interval),
            RepeatingFunc('provisioner-%s' % region, provisioner.provision,
                          provision_interval)
        ]

        queue_name = 'flotilla-%s-scheduler' % environment
        sqs = boto3.resource('sqs', region)
        message_q = get_queue(sqs, queue_name)
        if message_q:
            elb = boto3.client('elb', region)
            doctor = ServiceDoctor(db, elb)
            messaging = FlotillaSchedulerMessaging(message_q, schedule, doctor)

            funcs.append(
                RepeatingFunc('scheduler-message-%s' % region,
                              messaging.receive, 0))

    # Start loops:
    map(RepeatingFunc.start, funcs)
Exemple #3
0
def start_scheduler(environment, domain, regions, lock_interval, loop_interval,
                    provision_interval):
    instance_id = get_instance_id()

    coreos = CoreOsAmiIndex()
    cloudformation = FlotillaCloudFormation(environment, domain, coreos)

    funcs = []
    for region in regions:
        # DynamoDB:
        dynamo = boto.dynamodb2.connect_to_region(region)
        tables = DynamoDbTables(dynamo, environment=environment)
        tables.setup(['assignments', 'locks', 'regions', 'services', 'stacks',
                      'status'])
        db = FlotillaSchedulerDynamo(tables.assignments, tables.regions,
                                     tables.services, tables.stacks,
                                     tables.status)
        locks = DynamoDbLocks(instance_id, tables.locks)

        # Assemble into scheduler:
        schedule = FlotillaScheduler(db, locks, lock_ttl=lock_interval * 3)
        provisioner = FlotillaProvisioner(environment, region, schedule, db,
                                          cloudformation)

        funcs += [
            RepeatingFunc('scheduler-lock-%s' % region, schedule.lock,
                          lock_interval),
            RepeatingFunc('scheduler-%s' % region, schedule.loop,
                          loop_interval),
            RepeatingFunc('provisioner-%s' % region, provisioner.provision,
                          provision_interval)
        ]

        queue_name = 'flotilla-%s-scheduler' % environment
        sqs = boto3.resource('sqs', region)
        message_q = get_queue(sqs, queue_name)
        if message_q:
            elb = boto3.client('elb', region)
            doctor = ServiceDoctor(db, elb)
            messaging = FlotillaSchedulerMessaging(message_q, schedule, doctor)

            funcs.append(RepeatingFunc('scheduler-message-%s' % region,
                                       messaging.receive, 0))

    # Start loops:
    map(RepeatingFunc.start, funcs)
Exemple #4
0
 def test_get_queue_not_found(self):
     client_error = ClientError({'Error': {'Code': QUEUE_NOT_FOUND}}, '')
     self.sqs.get_queue_by_name.side_effect = client_error
     queue = get_queue(self.sqs, QUEUE_NAME)
     self.assertIsNone(queue)
Exemple #5
0
 def test_get_queue_found(self):
     queue = get_queue(self.sqs, QUEUE_NAME)
     self.assertIsNotNone(queue)