Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bvarent/50732588c935020f1ae3 to your computer and use it in GitHub Desktop.
Save bvarent/50732588c935020f1ae3 to your computer and use it in GitHub Desktop.
AWS EB ebextensions grab environment config from bucket
#!/usr/bin/env python
#source: https://gist.github.com/heywbj/da10d99f66df6361db9f
import boto.utils
import boto.beanstalk
good_statuses = ('Launching', 'Updating', 'Ready')
def get_eb_environment_description():
identity_document = boto.utils.get_instance_identity()['document']
connection = boto.beanstalk.connect_to_region(identity_document['region'])
envs = (e for e in
connection.describe_environments()
['DescribeEnvironmentsResponse']
['DescribeEnvironmentsResult']
['Environments']
)
for env in envs:
if env['Status'] not in good_statuses:
continue
resources = (
connection.describe_environment_resources(
environment_name=env['EnvironmentName']
)
['DescribeEnvironmentResourcesResponse']
['DescribeEnvironmentResourcesResult']
['EnvironmentResources']
)
for instance in resources['Instances']:
if instance['Id'] == identity_document['instanceId']:
return env
return None
#!/usr/bin/env python
# see .ebextensions/04-grab-env-config-from-bucket
import os
import pwd
import boto
import eb_env_descr
# Figure out the EB environment name for instance running this script.
env_descr = eb_env_descr.get_eb_environment_description()
if env_descr is None:
raise RuntimeError('Could not get the EB environment description.')
# Figure out the locations to get config from and where to put it
environment_name = env_descr['EnvironmentName']
app_source_path = os.getcwd()
configs_bucket_name = os.environ.get('CONFIGS_BUCKET', 'eb-app-env-configs')
configs_for_envs_in_bucket_root_path = os.environ.get('CONFIGS_BUCKET', '')
app_config_dir_relative_path = os.environ.get('APP_CONFIG_PATH', '')
local_config_dir_path = app_source_path + ('/' if app_config_dir_relative_path else '') + app_config_dir_relative_path
bucket_config_dir_path = configs_for_envs_in_bucket_root_path + ('/' if configs_for_envs_in_bucket_root_path else '') + environment_name
# Connect to the bucket and retrieve the files
s3_conn = boto.connect_s3()
bucket_with_conf_file = s3_conn.get_bucket(configs_bucket_name)
config_files_list = bucket_with_conf_file.list(bucket_config_dir_path + '/', '/')
app_user = pwd.getpwnam(os.environ['EB_CONFIG_APP_USER'])
print 'copying config files from "[%s]:%s" to "%s"' % (configs_bucket_name, bucket_config_dir_path, local_config_dir_path)
for file in config_files_list:
filename = file.name.split('/')[-1]
if not filename: continue
target_path = local_config_dir_path + '/' + filename
file.get_contents_to_filename(target_path)
os.chown(target_path, app_user.pw_uid, app_user.pw_gid)
os.chmod(target_path, 0o660)
# Grabs any environment config file from a folder named after this environment
# in "the" designated EB bucket, and copies it to the designated location in the app source.
#
# Parameters (as environment variables):
# CONFIGS_BUCKET : (optional) Name of the S3 bucket containing config files for all EB envs. Default: 'eb-app-env-configs'.
# CONFIGS_ROOT : (optional) Root path in the bucket containing the directories named after the environments. Default ''
# APP_CONFIG_PATH : (optional) Relative path to the config files insides the app sources. Default ''
# run before any source files are unzipped
commands:
# update boto to latest version
01_update_boto:
command: 'easy_install -U boto'
# run while the app is "on deck" (cd $EB_CONFIG_APP_ONDECK)
container_commands:
# invoke the script to grab the config file(s) for this environment
01_run_script:
command: python .ebextensions/04-grab-env-config-from-bucket/main.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment