Last active
August 29, 2015 14:14
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
"""Modifies nginx configuration file on AWS Elastic Beanstalk to create | |
a status endpoint.""" | |
__author__ = "Ian Sheridan <[email protected]>" | |
__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