def deploy_webconf(): """ Deploy nginx and other wsgi server site configurations to the host """ deployed = [] log_dir = '/'.join([deployment_root(), 'log']) #TODO - incorrect - check for actual package to confirm installation if webserver_list(): if env.verbosity: print env.host, "DEPLOYING webconf:" if not exists(log_dir): run('ln -s /var/log log') #deploys confs for each domain based on sites app if 'apache2' in get_packages(): deployed += _deploy_webconf('/etc/apache2/sites-available', 'django-apache-template.txt') deployed += _deploy_webconf('/etc/nginx/sites-available', 'nginx-template.txt') elif 'gunicorn' in get_packages(): deployed += _deploy_webconf('/etc/nginx/sites-available', 'nginx-gunicorn-template.txt') upload_template('woven/maintenance.html', '/var/www/nginx-default/maintenance.html', use_sudo=True) sudo('chmod ugo+r /var/www/nginx-default/maintenance.html') else: print env.host, """WARNING: Apache or Nginx not installed""" return deployed
def deploy_webconf(): """ Deploy nginx and other wsgi server site configurations to the host """ deployed = [] log_dir = '/'.join([deployment_root(),'log']) #TODO - incorrect - check for actual package to confirm installation if webserver_list(): if env.verbosity: print env.host,"DEPLOYING webconf:" if not exists(log_dir): run('ln -s /var/log log') #deploys confs for each domain based on sites app if 'apache2' in get_packages(): deployed += _deploy_webconf('/etc/apache2/sites-available','django-apache-template.txt') deployed += _deploy_webconf('/etc/nginx/sites-available','nginx-template.txt') elif 'gunicorn' in get_packages(): deployed += _deploy_webconf('/etc/nginx/sites-available','nginx-gunicorn-template.txt') if not exists('/var/www/nginx-default'): sudo('mkdir /var/www/nginx-default') upload_template('woven/maintenance.html','/var/www/nginx-default/maintenance.html',use_sudo=True) sudo('chmod ugo+r /var/www/nginx-default/maintenance.html') else: print env.host,"""WARNING: Apache or Nginx not installed""" return deployed
def webserver_list(): """ list of webserver packages """ p = set(get_packages()) w = set(['apache2','gunicorn','uwsgi','nginx']) installed = p & w return list(installed)
def webserver_list(): """ list of webserver packages """ p = set(get_packages()) w = set(['apache2', 'gunicorn', 'uwsgi', 'nginx']) installed = p & w return list(installed)
def install_packages(): """ Install a set of baseline packages and configure where necessary """ if env.verbosity: print env.host, "INSTALLING & CONFIGURING NODE PACKAGES:" #Get a list of installed packages p = run("dpkg -l | awk '/ii/ {print $2}'").split('\n') #Remove apparmor - TODO we may enable this later if env.overwrite or not server_state('apparmor-disabled') and 'apparmor' in p: with settings(warn_only=True): sudo('/etc/init.d/apparmor stop') sudo('update-rc.d -f apparmor remove') set_server_state('apparmor-disabled') #The principle we will use is to only install configurations and packages #if they do not already exist (ie not manually installed or other method) env.installed_packages[env.host] = [] role = env.role_lookup[env.host_string] packages = get_packages() for package in packages: if not package in p: install_package(package) if env.verbosity: print ' * installed',package env.installed_packages[env.host].append(package) if env.overwrite or env.installed_packages[env.host]: #always store the latest complete list set_server_state('packages_installed', packages) env.installed_packages[env.host] = packages if env.overwrite and 'apache2' in env.installed_packages[env.host]: #some sensible defaults -might move to putting this config in a template sudo("rm -f /etc/apache2/sites-enabled/000-default") sed('/etc/apache2/apache2.conf',before='KeepAlive On',after='KeepAlive Off',use_sudo=True, backup='') sed('/etc/apache2/apache2.conf',before='StartServers 2', after='StartServers 1', use_sudo=True, backup='') sed('/etc/apache2/apache2.conf',before='MaxClients 150', after='MaxClients 100', use_sudo=True, backup='') for module in env.APACHE_DISABLE_MODULES: sudo('rm -f /etc/apache2/mods-enabled/%s*'% module) #Install base python packages #We'll use easy_install at this stage since it doesn't download if the package #is current whereas pip always downloads. #Once both these packages mature we'll move to using the standard Ubuntu packages if (env.overwrite or not server_state('pip-venv-wrapper-installed')) and 'python-setuptools' in packages: sudo("easy_install virtualenv") sudo("easy_install pip") sudo("easy_install virtualenvwrapper") if env.verbosity: print " * easy installed pip, virtualenv, virtualenvwrapper" set_server_state('pip-venv-wrapper-installed') user_profile_dir = os.path.join(deployment_user_home(), ".profile") if not contains("source /usr/local/bin/virtualenvwrapper.sh", user_profile_dir): append("export WORKON_HOME=$HOME/env", user_profile_dir) append("source /usr/local/bin/virtualenvwrapper.sh", user_profile_dir) #cleanup after easy_install sudo("rm -rf build")
def install_packages(): """ Install a set of baseline packages and configure where necessary """ if env.verbosity: print env.host, "INSTALLING & CONFIGURING NODE PACKAGES:" #Get a list of installed packages p = run("dpkg -l | awk '/ii/ {print $2}'").split('\n') #Remove apparmor - TODO we may enable this later if env.overwrite or not server_state('apparmor-disabled') and 'apparmor' in p: with settings(warn_only=True): sudo('/etc/init.d/apparmor stop') sudo('update-rc.d -f apparmor remove') set_server_state('apparmor-disabled') #The principle we will use is to only install configurations and packages #if they do not already exist (ie not manually installed or other method) env.installed_packages[env.host] = [] role = env.role_lookup[env.host_string] packages = get_packages() for package in packages: if not package in p: install_package(package) if env.verbosity: print ' * installed',package env.installed_packages[env.host].append(package) if env.overwrite or env.installed_packages[env.host]: #always store the latest complete list set_server_state('packages_installed', packages) env.installed_packages[env.host] = packages if env.overwrite and 'apache2' in env.installed_packages[env.host]: #some sensible defaults -might move to putting this config in a template sudo("rm -f /etc/apache2/sites-enabled/000-default") sed('/etc/apache2/apache2.conf',before='KeepAlive On',after='KeepAlive Off',use_sudo=True, backup='') sed('/etc/apache2/apache2.conf',before='StartServers 2', after='StartServers 1', use_sudo=True, backup='') sed('/etc/apache2/apache2.conf',before='MaxClients 150', after='MaxClients 100', use_sudo=True, backup='') for module in env.APACHE_DISABLE_MODULES: sudo('rm -f /etc/apache2/mods-enabled/%s*'% module) #Install base python packages #We'll use easy_install at this stage since it doesn't download if the package #is current whereas pip always downloads. #Once both these packages mature we'll move to using the standard Ubuntu packages if (env.overwrite or not server_state('pip-venv-wrapper-installed')) and 'python-setuptools' in packages: sudo("easy_install virtualenv") sudo("easy_install pip") sudo("easy_install virtualenvwrapper") if env.verbosity: print " * easy installed pip, virtualenv, virtualenvwrapper" set_server_state('pip-venv-wrapper-installed') if not contains("source /usr/local/bin/virtualenvwrapper.sh","/home/%s/.profile"% env.user): append("export WORKON_HOME=$HOME/env","/home/%s/.profile"% env.user) append("source /usr/local/bin/virtualenvwrapper.sh","/home/%s/.profile"% env.user) #cleanup after easy_install sudo("rm -rf build")
def uninstall_packages(): """ Uninstall unwanted packages """ p = server_state('packages_installed') if p: installed = set(p) else: return env.uninstalled_packages[env.host] = [] #first uninstall any that have been taken off the list packages = set(get_packages()) uninstall = installed - packages if uninstall and env.verbosity: print env.host,'UNINSTALLING HOST PACKAGES' for p in uninstall: if env.verbosity: print ' - uninstalling',p uninstall_package(p) env.uninstalled_packages[env.host].append(p) set_server_state('packages_installed',get_packages()) return
def deploy_wsgi(): """ deploy python wsgi file(s) """ if 'libapache2-mod-wsgi' in get_packages(): remote_dir = '/'.join([deployment_root(),'env',env.project_fullname,'wsgi']) wsgi = 'apache2' elif 'gunicorn' in get_packages(): remote_dir = '/etc/init' wsgi = 'gunicorn' deployed = [] #ensure project apps path is also added to environment variables as well as wsgi if env.PROJECT_APPS_PATH: pap = '/'.join([deployment_root(),'env', env.project_name,'project',env.project_package_name,env.PROJECT_APPS_PATH]) pap = ''.join(['export PYTHONPATH=$PYTHONPATH:',pap]) postactivate = '/'.join([deployment_root(),'env','postactivate']) if not exists(postactivate): append('#!/bin/bash', postactivate) run('chmod +x %s'% postactivate) if not contains('PYTHONPATH',postactivate): append(pap,postactivate) if env.verbosity: print env.host,"DEPLOYING wsgi", wsgi, remote_dir for file in _sitesettings_files(): deployed += mkdirs(remote_dir) with cd(remote_dir): settings_module = file.replace('.py','') context = {"deployment_root":deployment_root(), "user": env.user, "project_name": env.project_name, "project_package_name": env.project_package_name, "project_apps_path":env.PROJECT_APPS_PATH, "settings": settings_module, } if wsgi == 'apache2': filename = file.replace('.py','.wsgi') upload_template('/'.join(['woven','django-wsgi-template.txt']), filename, context, ) elif wsgi == 'gunicorn': filename = 'gunicorn-%s.conf'% env.project_name upload_template('/'.join(['woven','gunicorn.conf']), filename, context, backup=False, use_sudo=True ) if env.verbosity: print " * uploaded", filename #finally set the ownership/permissions #We'll use the group to allow www-data execute if wsgi == 'apache2': sudo("chown %s:www-data %s"% (env.user,filename)) run("chmod ug+xr %s"% filename) elif wsgi == 'gunicorn': sudo("chown root:root %s"% filename) sudo("chmod go+r %s"% filename) return deployed
def activate(): """ Activates the version specified in ``env.project_version`` if it is different from the current active version. An active version is just the version that is symlinked. """ env_path = '/'.join([deployment_root(),'env',env.project_fullname]) if not exists(env_path): print env.host,"ERROR: The version",env.project_version,"does not exist at" print env_path sys.exit(1) active = active_version() servers = webserver_list() if env.patch or active <> env.project_fullname: for s in servers: stop_webserver(s) if not env.patch and active <> env.project_fullname: if env.verbosity: print env.host, "ACTIVATING version", env_path if not env.nomigration: sync_db() #south migration if 'south' in env.INSTALLED_APPS and not env.nomigration and not env.manualmigration: migration() if env.manualmigration or env.MANUAL_MIGRATION: manual_migration() #activate sites activate_sites = [''.join([d.name.replace('.','_'),'-',env.project_version,'.conf']) for d in domain_sites()] if 'apache2' in get_packages(): site_paths = ['/etc/apache2','/etc/nginx'] else: site_paths = ['/etc/nginx'] #disable existing sites for path in site_paths: for site in _ls_sites('/'.join([path,'sites-enabled'])): if site not in activate_sites: sudo("rm %s/sites-enabled/%s"% (path,site)) #activate new sites for path in site_paths: for site in activate_sites: if not exists('/'.join([path,'sites-enabled',site])): sudo("chmod 644 %s" % '/'.join([path,'sites-available',site])) sudo("ln -s %s/sites-available/%s %s/sites-enabled/%s"% (path,site,path,site)) if env.verbosity: print " * enabled", "%s/sites-enabled/%s"% (path,site) #delete existing symlink ln_path = '/'.join([deployment_root(),'env',env.project_name]) run('rm -f '+ln_path) #run post deploy hooks post_exec_hook('post_deploy') #activate run('ln -s %s %s'% (env_path,ln_path)) if env.verbosity: print env.host,env.project_fullname, "ACTIVATED" else: if env.verbosity and not env.patch: print env.project_fullname,"is the active version" if env.patch or active <> env.project_fullname: for s in servers: start_webserver(s) print return
def install_packages(): """ Install a set of baseline packages and configure where necessary """ if env.verbosity: print env.host, "INSTALLING & CONFIGURING NODE PACKAGES:" # Get a list of installed packages. p = run("dpkg -l | awk '/ii/ {print $2}'").split("\n") # Remove apparmor - TODO we may enable this later. if env.overwrite or not server_state("apparmor-disabled") and "apparmor" in p: with settings(warn_only=True): sudo("/etc/init.d/apparmor stop") sudo("update-rc.d -f apparmor remove") set_server_state("apparmor-disabled") # The principle we will use is to only install configurations and # packages if they do not already exist (ie. not manually installed # or other method). env.installed_packages[env.host] = [] packages = get_packages() for package in packages: if not package in p: install_package(package) if env.verbosity: print " * installed", package env.installed_packages[env.host].append(package) if env.overwrite or env.installed_packages[env.host]: # Always store the latest complete list. set_server_state("packages_installed", packages) env.installed_packages[env.host] = packages if env.overwrite and "apache2" in env.installed_packages[env.host]: # Some sensible defaults -might move to putting this # config in a template. sudo("rm -f /etc/apache2/sites-enabled/000-default") sed("/etc/apache2/apache2.conf", before="KeepAlive On", after="KeepAlive Off", use_sudo=True, backup="") sed( "/etc/apache2/apache2.conf", before="StartServers 2", after="StartServers 1", use_sudo=True, backup="", ) sed( "/etc/apache2/apache2.conf", before="MaxClients 150", after="MaxClients 100", use_sudo=True, backup="", ) for module in env.APACHE_DISABLE_MODULES: sudo("rm -f /etc/apache2/mods-enabled/%s*" % module) # Install base python packages. # # We'll use easy_install at this stage since it doesn't download # if the package is current, whereas pip always downloads. Once # both these packages mature we'll move to using the standard # Ubuntu packages. if (env.overwrite or not server_state("pip-venv-wrapper-installed")) and "python-setuptools" in packages: sudo("easy_install virtualenv") sudo("easy_install pip") sudo("easy_install virtualenvwrapper") if env.verbosity: print " * easy installed pip, virtualenv, virtualenvwrapper" set_server_state("pip-venv-wrapper-installed") if not contains("/home/%s/.profile" % env.user, "source /usr/local/bin/virtualenvwrapper.sh"): append("/home/%s/.profile" % env.user, "export WORKON_HOME=$HOME/env") append("/home/%s/.profile" % env.user, "source /usr/local/bin/virtualenvwrapper.sh") # Cleanup after easy_install. sudo("rm -rf build")
def activate(): """ Activates the version specified in ``env.project_version`` if it is different from the current active version. An active version is just the version that is symlinked. """ env_path = '/'.join([deployment_root(), 'env', env.project_fullname]) if not exists(env_path): print env.host, "ERROR: The version", env.project_version, "does not exist at" print env_path sys.exit(1) active = active_version() servers = webserver_list() if env.patch or active <> env.project_fullname: for s in servers: stop_webserver(s) if not env.patch and active <> env.project_fullname: if env.verbosity: print env.host, "ACTIVATING version", env_path if not env.nomigration: sync_db() #south migration if 'south' in env.INSTALLED_APPS and not env.nomigration and not env.manualmigration: migration() if env.manualmigration or env.MANUAL_MIGRATION: manual_migration() #activate sites activate_sites = [ ''.join( [d.name.replace('.', '_'), '-', env.project_version, '.conf']) for d in domain_sites() ] if 'apache2' in get_packages(): site_paths = ['/etc/apache2', '/etc/nginx'] else: site_paths = ['/etc/nginx'] #disable existing sites for path in site_paths: for site in _ls_sites('/'.join([path, 'sites-enabled'])): if site not in activate_sites: sudo("rm %s/sites-enabled/%s" % (path, site)) #activate new sites for path in site_paths: for site in activate_sites: if not exists('/'.join([path, 'sites-enabled', site])): sudo("chmod 644 %s" % '/'.join([path, 'sites-available', site])) sudo("ln -s %s/sites-available/%s %s/sites-enabled/%s" % (path, site, path, site)) if env.verbosity: print " * enabled", "%s/sites-enabled/%s" % (path, site) #delete existing symlink ln_path = '/'.join([deployment_root(), 'env', env.project_name]) run('rm -f ' + ln_path) #run post deploy hooks post_exec_hook('post_deploy') #activate run('ln -s %s %s' % (env_path, ln_path)) if env.verbosity: print env.host, env.project_fullname, "ACTIVATED" else: if env.verbosity and not env.patch: print env.project_fullname, "is the active version" if env.patch or active <> env.project_fullname: for s in servers: start_webserver(s) print return
def deploy_wsgi(): """ deploy python wsgi file(s) """ if 'libapache2-mod-wsgi' in get_packages(): remote_dir = '/'.join( [deployment_root(), 'env', env.project_fullname, 'wsgi']) wsgi = 'apache2' elif 'gunicorn' in get_packages(): remote_dir = '/etc/init' wsgi = 'gunicorn' deployed = [] #ensure project apps path is also added to environment variables as well as wsgi if env.PROJECT_APPS_PATH: pap = '/'.join([ deployment_root(), 'env', env.project_name, 'project', env.project_package_name, env.PROJECT_APPS_PATH ]) pap = ''.join(['export PYTHONPATH=$PYTHONPATH:', pap]) postactivate = '/'.join([deployment_root(), 'env', 'postactivate']) if not exists(postactivate): append('#!/bin/bash', postactivate) run('chmod +x %s' % postactivate) if not contains('PYTHONPATH', postactivate): append(pap, postactivate) if env.verbosity: print env.host, "DEPLOYING wsgi", wsgi, remote_dir for file in _sitesettings_files(): deployed += mkdirs(remote_dir) with cd(remote_dir): settings_module = file.replace('.py', '') context = { "deployment_root": deployment_root(), "user": env.user, "project_name": env.project_name, "project_package_name": env.project_package_name, "project_apps_path": env.PROJECT_APPS_PATH, "settings": settings_module, } if wsgi == 'apache2': filename = file.replace('.py', '.wsgi') upload_template( '/'.join(['woven', 'django-wsgi-template.txt']), filename, context, ) elif wsgi == 'gunicorn': filename = 'gunicorn-%s.conf' % env.project_name upload_template('/'.join(['woven', 'gunicorn.conf']), filename, context, backup=False, use_sudo=True) if env.verbosity: print " * uploaded", filename #finally set the ownership/permissions #We'll use the group to allow www-data execute if wsgi == 'apache2': sudo("chown %s:www-data %s" % (env.user, filename)) run("chmod ug+xr %s" % filename) elif wsgi == 'gunicorn': sudo("chown root:root %s" % filename) sudo("chmod go+r %s" % filename) return deployed