Skip to content

Commit

Permalink
simplified conf generation
Browse files Browse the repository at this point in the history
  • Loading branch information
blturner committed Apr 11, 2012
1 parent 0ef9930 commit 1f2b229
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
50 changes: 31 additions & 19 deletions stitch/fabfile.py
Expand Up @@ -98,25 +98,18 @@ def site_on_host():
pass pass




def generate_conf(template_name, dest, filename, context={}): def generate_conf(template_name, filename, context={}):
output = StringIO.StringIO() output = StringIO.StringIO()
t = jinja_env.get_template(template_name) t = jinja_env.get_template(template_name)
t.stream(context).dump(output) t.stream(context).dump(output)

put(output, filename)
if not exists(dest):
run('mkdir -p %s' % dest)

target = os.path.join(dest, filename)
put(output, target)
output.close() output.close()




def generate_confs(): def generate_confs():
host = get_host_shortname(env.host) host = get_host_shortname(env.host)
apache_dir = os.path.join(_get('apache_dir'), host)
site = env.site site = env.site
sitepackages = get_site_packages() sitepackages = get_site_packages()
wsgi_dir = _get('wsgi_dir')


context = { context = {
'admin_media': os.path.join(sitepackages, 'django/contrib/admin/media'), 'admin_media': os.path.join(sitepackages, 'django/contrib/admin/media'),
Expand All @@ -126,10 +119,11 @@ def generate_confs():
'sitepackages': sitepackages, 'sitepackages': sitepackages,
'staging_domain': _get('staging_domain'), 'staging_domain': _get('staging_domain'),
'virtualenv_dir': os.path.join(_get('virtualenv_dir'), site), 'virtualenv_dir': os.path.join(_get('virtualenv_dir'), site),
'wsgi_dir': wsgi_dir 'wsgi_conf': env.wsgi_conf,
'wsgi_dir': _get('wsgi_dir'),
} }
generate_conf('apache/base.conf', apache_dir, '%s.conf' % site, context) generate_conf('apache/base.conf', env.apache_conf, context)
generate_conf('wsgi/base.conf', wsgi_dir, '%s.conf' % site, context) generate_conf('wsgi/base.conf', env.wsgi_conf, context)
generate_settings() generate_settings()




Expand All @@ -153,17 +147,17 @@ def init_settings_dir(path):


def generate_settings(): def generate_settings():
pp = pprint.PrettyPrinter() pp = pprint.PrettyPrinter()
settings_dir = _get('staging_settings') settings_dir = env.settings_dir
site_settings = os.path.join(settings_dir, env.site) if not exists(settings_dir):
if not exists(site_settings): run('mkdir -p %s' % settings_dir)
run('mkdir -p %s' % site_settings) init_settings_dir(settings_dir)
init_settings_dir(site_settings)
overrides = [[k, pp.pformat(v)] for k, v in _get('settings_overrides').iteritems()] overrides = [[k, pp.pformat(v)] for k, v in _get('settings_overrides').iteritems()]
context = { context = {
'original_settings': _get('original_settings'), 'original_settings': _get('original_settings'),
'settings_overrides': overrides 'settings_overrides': overrides
} }
generate_conf('settings/base.py', site_settings, 'settings.py', context) settings_file = os.path.join(settings_dir, 'settings.py')
generate_conf('settings/base.py', settings_file, context)




def git_clone(): def git_clone():
Expand Down Expand Up @@ -202,6 +196,19 @@ def setup_virtualenv():
add2virtualenv(env.project_path) add2virtualenv(env.project_path)




def setup_directories():
apache_dir = _get('apache_dir')
settings_dir = os.path.join(_get('staging_settings'), env.site)
wsgi_dir = _get('wsgi_dir')

if not exists(apache_dir):
run('mkdir -p %s' % apache_dir)
if not exists(settings_dir):
run('mkdir -p %s' % settings_dir)
if not exists(wsgi_dir):
run('mkdir -p %s' % wsgi_dir)


def _get(key): def _get(key):
try: try:
return env.config[key] return env.config[key]
Expand Down Expand Up @@ -241,11 +248,15 @@ def wrapped(*args, **kwargs):
env.site = site env.site = site
_load_config() _load_config()
if site_on_host(): if site_on_host():
base_dir = _get('base_dir')
env.apache_conf = os.path.join(base_dir, _get('apache_dir'), ('%s.conf' % env.site))
env.project_path = os.path.join(_get('virtualenv_dir'), env.site) env.project_path = os.path.join(_get('virtualenv_dir'), env.site)
env.repo_path = os.path.join(env.project_path, _get('project_name')) env.repo_path = os.path.join(env.project_path, _get('project_name'))
env.settings_dir = os.path.join(base_dir, _get('staging_settings'), env.site)
env.wsgi_conf = os.path.join(base_dir, _get('wsgi_dir'), ('%s.conf' % env.site))
fn() fn()
else: else:
print env.site + ' is not on this host.' print env.site + ' is not on this host. Skipping...'
restart() restart()
return wrapped return wrapped


Expand All @@ -259,6 +270,7 @@ def debug(*args, **kwargs):


@process_sites @process_sites
def setup(*args, **kwargs): def setup(*args, **kwargs):
setup_directories()
setup_virtualenv() setup_virtualenv()
generate_confs() generate_confs()
pip_install() pip_install()
Expand Down
2 changes: 1 addition & 1 deletion stitch/templates/apache/base.conf
Expand Up @@ -10,7 +10,7 @@
Allow from all Allow from all
</Directory> </Directory>


WSGIScriptAlias / {{ wsgi_dir }}/{{ site }}.conf WSGIScriptAlias / {{ wsgi_conf }}
WSGIProcessGroup {{ site }} WSGIProcessGroup {{ site }}
WSGIDaemonProcess {{ site }} WSGIDaemonProcess {{ site }}
WSGIApplicationGroup %{GLOBAL} WSGIApplicationGroup %{GLOBAL}
Expand Down

0 comments on commit 1f2b229

Please sign in to comment.