Skip to content

Instantly share code, notes, and snippets.

@iansheridan
Last active August 29, 2015 14:14
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 iansheridan/aed225d8bcf956f48149 to your computer and use it in GitHub Desktop.
Save iansheridan/aed225d8bcf956f48149 to your computer and use it in GitHub Desktop.
Add a Status endpoint to an Elasticbeanstalk Docker Env. Nginx config via .ebextentions
container_commands:
copy:
command: "cp .ebextensions/01update_nginx_config.py /opt/elasticbeanstalk/hooks/appdeploy/enact/"
make_exe:
command: "chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/01update_nginx_config.py"
#! /usr/bin/python
"""Modifies nginx configuration file on AWS Elastic Beanstalk to create
a status endpoint."""
__author__ = "Ian Sheridan <ian.sheridan@gmail.com>"
__version__ = "0.0.3"
import os
NGINX_CONF_FILE = '/etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker.conf'
NGINX_CONFIG = """
location /isalive {
access_log off;
default_type text/plain;
return 200 '{ "alive": true }';
}
"""
def file_contains_string(trigger='location /isalive'):
with open(NGINX_CONF_FILE, 'r') as f:
for line in f:
if trigger in line:
return True
return False
def add_string_after_block(block='location /', string=NGINX_CONFIG):
f = open(NGINX_CONF_FILE, 'r').readlines()
new_f = []
inside_block = False
for line in f:
new_f.append(line)
if block in line:
inside_block = True
if inside_block and '}' in line:
new_f += [l+'\n' for l in string.split('\n')]
inside_block = False
print new_f
# overwrite config file
f = open(NGINX_CONF_FILE, 'w')
for line in new_f:
f.write(line)
f.close()
def restart_nginx():
os.system("service nginx restart")
def main():
print '--- NginX conf file exists ---'
print NGINX_CONF_FILE
isfile = os.path.isfile(NGINX_CONF_FILE)
print isfile
if not isfile:
print 'abort'
return
print '--- Checking NginX configuration ---'
already_fixed = file_contains_string()
print already_fixed
if already_fixed:
print 'abort'
return
print '--- Changing NginX configuration ---'
add_string_after_block()
print '--- Restart NginX ---'
restart_nginx()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment