Skip to content

Instantly share code, notes, and snippets.

@jamesmbeams
Created July 18, 2017 14:24
Show Gist options
  • Save jamesmbeams/ccefaba3191af20a22ea5baea2d2b2e9 to your computer and use it in GitHub Desktop.
Save jamesmbeams/ccefaba3191af20a22ea5baea2d2b2e9 to your computer and use it in GitHub Desktop.
ElasticBeanstalk Cron setup w/ script to determine auto scaling group leader
#read cron command into temporary file
echo "0 2 * * * . /opt/elasticbeanstalk/support/envvars; /var/app/current/path/to/command" > /tmp/mycron
#install new cron file
crontab /tmp/mycron
#delete unwanted mycron file
rm /tmp/mycron
container_commands:
remove_cron_jobs:
command: "crontab -r || exit 0"
set_cron_script_permissions:
command: "chmod 700 cron_setup.sh"
set_group_leader_permissions:
command: "chmod 755 group_leader.py"
run_cron_setup:
command: "bash cron_setup.sh"
#!/usr/bin/python
import sys, urllib2, boto.ec2.autoscale
this_instance = urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read()
instance_list = boto.ec2.autoscale.connect_to_region('us-east-1').get_all_autoscaling_instances()
instance_list = [ i for i in instance_list if i.lifecycle_state == "InService" ]
asgroup = [ i for i in instance_list if i.instance_id == this_instance ][0].group_name
instances = [ i.instance_id for i in instance_list if i.group_name == asgroup ]
instances.sort()
sys.exit( instances[0] != this_instance )
public function is_group_leader() {
// NOTE:
// Python script exit codes:
// 0 - clean exit without any errors / problems
// 1 - there was some issue / error / problem and that is why the program is exiting
$script = ROOT . DS . 'group_leader.py';
exec($script, $output, $return_value);
$this->out($return_value == 0 ? 'Leader' : 'Not Leader');
return $return_value == 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment