Created
July 15, 2012 10:38
-
-
Save tvjames/3116218 to your computer and use it in GitHub Desktop.
fabric apply_mono_webapp task
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
@task | |
def apply_nginx_role(hostname=''): | |
if (hostname == ''): | |
hostname = env.host.partition('.')[0] | |
# requires backports | |
system.install('nginx', 'squeeze-backports') | |
@task | |
def apply_nginx_reload(): | |
system.run_or_sudo('nginx -s reload') | |
@task | |
def apply_mono_webapp_role(hostname=''): | |
if (hostname == ''): | |
hostname = env.host.partition('.')[0] | |
base_group = 'app' | |
base_folder = '/srv/apps' | |
apply_nginx_role(hostname) | |
# http://humblecoder.co.uk/nancy/hosting-nancy-on-debian-with-nginx | |
system.install('supervisor') | |
system.run_or_sudo('addgroup --system {group}'.format(group=base_group)) | |
@task | |
def apply_mono_webapp(appname, srcfolder, baseport, numbackend = 1, staticfolder='', cname='', suffix=''): | |
app_owner = appname | |
app_group = 'www-data' | |
base_group = 'app' | |
base_folder = '/srv/apps' | |
# appname: name of the app | |
# srcfolder: local folder containing the app src will end up in approot | |
# baseport: base port number for internal listeners | |
# numbackend: number of backend process to spin up | |
# staticfolder: local folder for public html | |
# cname: alias for public access | |
# suffix: internal suffix for app name | |
# assumes apply_mono_webapp_role is installed | |
app_folder = "{base}/{app}".format(base=base_folder, app=appname) | |
app_folder_bin = app_folder + '/bin' | |
app_folder_conf = app_folder + '/conf' | |
app_folder_root = app_folder + '/app' | |
app_folder_repo = app_folder + '/repo' | |
app_folder_logs = app_folder + '/logs' | |
app_folder_static = app_folder + '/public_html' | |
# setup user and permissions (assumes shadow, group exists) | |
user_exists = fabric.contrib.files.contains('/etc/passwd', '^' + app_owner + ':x:', exact=False, use_sudo=True, escape=False) | |
if (not user_exists): | |
sudo('adduser --firstuid 5000 --no-create-home --home "{home}" --ingroup "{group}" --gecos "{name}" --shell /bin/false --disabled-password --disabled-login {user}'.format(home=app_folder, group=base_group, name=appname, user=app_owner)) | |
sudo('adduser {user} {group}'.format(user=app_owner, group=app_group)) | |
# ensure that each exist | |
sudo("mkdir -p %s" % app_folder) | |
sudo("chown -R %s:%s %s" %(app_owner, app_group, app_folder)) | |
sudo("chmod g+s %s" % app_folder, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_bin, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_conf, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_root, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_repo, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_logs, user = app_owner) | |
sudo("mkdir -p %s" % app_folder_static, user = app_owner) | |
# server 127.0.0.1:5000; | |
appname_upstream = '' | |
for x in xrange(int(numbackend)): | |
appname_upstream += ' server 127.0.0.1:{port};\n'.format(port=int(baseport)+x) | |
context = { | |
'appname': appname, | |
'appname_internal': appname + suffix, | |
'appname_cname': cname, | |
'appname_upstream': appname_upstream, | |
'numbackend': numbackend, | |
'baseport': baseport, | |
'app_owner': app_owner, | |
'app_folder': app_folder, | |
'app_folder_bin': app_folder_bin, | |
'app_folder_conf': app_folder_conf, | |
'app_folder_root': app_folder_root, | |
'app_folder_repo': app_folder_repo, | |
'app_folder_logs': app_folder_logs, | |
'app_folder_static': app_folder_static, | |
'mono_bin': '/opt/mono-2.10.9/bin/', | |
'mono_xsp4': '/opt/mono-2.10.9/bin/xsp4' | |
} | |
fabric.contrib.files.upload_template('templates/apps/nginx.conf', app_folder_conf, use_sudo=True, backup=False, mode=0644, context=context) | |
fabric.contrib.files.upload_template('templates/apps/supervisord.conf', app_folder_conf, use_sudo=True, backup=False, mode=0644, context=context) | |
fabric.contrib.files.upload_template('templates/apps/daemon', app_folder_bin, use_sudo=True, backup=False, mode=0644, context=context) | |
sudo("chown -R %s:%s %s" %('root', app_group, app_folder_bin + '/daemon')) | |
sudo('chmod og+x %s/daemon' % app_folder_bin) | |
# sync the contents of the app | |
local_path = srcfolder | |
if (not local_path.endswith('/')): | |
local_path = local_path + '/' | |
fabric.operations.put(local_path=local_path + '*', remote_path=app_folder_root, use_sudo=True, mirror_local_mode=False) | |
sudo("chown -R %s:%s %s" %(app_owner, app_group, app_folder_root)) | |
if (staticfolder != ''): | |
local_path = staticfolder | |
if (not local_path.endswith('/')): | |
local_path = local_path + '/' | |
fabric.operations.put(local_path=local_path + '*', remote_path=app_folder_static, use_sudo=True, mirror_local_mode=False) | |
sudo("chown -R %s:%s %s" %(app_owner, app_group, app_folder_static)) | |
# initiate changes | |
if (not fabric.contrib.files.exists('/etc/supervisor/conf.d/'+appname+'.conf', use_sudo=True, verbose=True)): | |
sudo('ln -s {target} {link}'.format(target=app_folder_conf + '/supervisord.conf', link='/etc/supervisor/conf.d/'+appname+'.conf')) | |
sudo("supervisorctl reread") | |
sudo("supervisorctl update") | |
sudo("supervisorctl restart {app}:*".format(app=appname)) | |
# wire-up public access | |
# http://serverfault.com/questions/277653/nginx-name-based-virtual-hosts-on-ipv6 | |
if (not fabric.contrib.files.exists('/etc/nginx/sites-enabled/'+appname+'.conf', use_sudo=True, verbose=True)): | |
sudo('ln -s {target} {link}'.format(target=app_folder_conf + '/nginx.conf', link='/etc/nginx/sites-enabled/'+appname+'.conf')) | |
apply_nginx_reload() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment