Skip to content

Commit

Permalink
fix work with new gunicorn version if wsgi not in root directory
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaly4uk committed Mar 10, 2016
1 parent 0d78a9a commit 7a2fb78
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion aws_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__author__ = 'vitaly'

VERSION = (0, 4, 0)
VERSION = (0, 4, 1)
68 changes: 46 additions & 22 deletions aws_tools/new_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@
from aws_tools import VERSION


def find_wsgi_file(path):
for root, dir_names, file_names in os.walk(path):
if 'wsgi.py' in file_names:
return root
for new_path in [dir_name for dir_name in dir_names if not dir_name[0] == '.']:
wsgi_path = find_wsgi_file(new_path)
if wsgi_path:
return wsgi_path


def main():
parser = argparse.ArgumentParser(
description='Create config files and start new project. Should be started in project directory.')
Expand Down Expand Up @@ -83,7 +93,8 @@ def main():
if not os.path.exists('./venv'):
print('Creating virtual environment...')
subprocess.check_call('virtualenv venv --python={0}'.format(args.python), shell=True)
subprocess.check_call('./venv/bin/pip install -r requirements.txt', shell=True)
if os.path.exists('./requirements.txt'):
subprocess.check_call('./venv/bin/pip install -r requirements.txt', shell=True)

if not os.path.exists('logs'):
print('Creating logs directory...')
Expand Down Expand Up @@ -159,35 +170,48 @@ def main():
port_file.write('PORT={port}'.format(port=start_port))
os.chown('./.port', int(os.getenv('SUDO_UID')), int(os.getenv('SUDO_GID')))

if not args.debug:
# Prepare nginx config
print('Configure nginx...')
template_loader = jinja2.FileSystemLoader([data_files_path, root])
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_or_select_template(['template', 'template_nginx'])

template_vars = {
'domain': args.domain,
'root': root,
'port': start_port,
'user': os.getenv('SUDO_USER'),
}
# Prepare nginx config
print('Configure nginx...')
template_loader = jinja2.FileSystemLoader([data_files_path, root])
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_or_select_template(['template', 'template_nginx'])

wsgi_root = find_wsgi_file('.')
if wsgi_root == '.':
wsgi_root = ''
else:
wsgi_root += '.'

template_vars = {
'domain': args.domain,
'root': root,
'port': start_port,
'user': os.getenv('SUDO_USER'),
'wsgi_root': wsgi_root,
}
nging_config_output = template.render(template_vars)
print(nging_config_output)

# Prepare supervisor config
print('Configure supervisor...')
template_loader = jinja2.FileSystemLoader([data_files_path, root])
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_or_select_template(['template', 'template_supervisor'])
print(template.filename)
supervisor_config_output = template.render(template_vars)
print(supervisor_config_output)

if not args.debug:
# write configs to files
with io.open(os.path.join(output_file_path, args.domain), 'w') as tmpl:
tmpl.write(template.render(template_vars))
tmpl.write(nging_config_output)

symlink_path = os.path.join(enabled_sites_path, args.domain)
if not os.path.exists(symlink_path):
os.symlink(os.path.join(output_file_path, args.domain), symlink_path)

# Prepare supervisor config
print('Configure supervisor...')
template_loader = jinja2.FileSystemLoader([data_files_path, root])
template_env = jinja2.Environment(loader=template_loader)
template = template_env.get_or_select_template(['template', 'template_supervisor'])

with io.open(os.path.join(output_supervisor_path, args.domain + '.conf'), 'w') as tmpl:
tmpl.write(template.render(template_vars))
tmpl.write(supervisor_config_output)

# Restart servers
print('Restarting services...')
Expand Down
2 changes: 1 addition & 1 deletion aws_tools/templates/template_supervisor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[program:{{ domain }}-web]
command = {{ root }}/venv/bin/gunicorn wsgi:application -b 127.0.0.1:{{ port }} -w 1
command = {{ root }}/venv/bin/gunicorn {{ wsgi_root}}wsgi:application -b 127.0.0.1:{{ port }} -w 1
process_name = {{ domain }}-web
user = {{ user }}
directory = {{ root }}
Expand Down

0 comments on commit 7a2fb78

Please sign in to comment.