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)
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.')
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)
def test_get_instance_id_with_invalid_event(): """ Test the get_instance_id function with invalid event """ event = {} assert get_instance_id(event) is False
def test_get_instance_id(): """ Test the get_instance_id function with valid event """ event = {'detail': {'instance-id': 'i-abcde123'}} assert get_instance_id(event) == 'i-abcde123'