Skip to content

Instantly share code, notes, and snippets.

@rod-dot-codes
Created June 8, 2013 18:28
Show Gist options
  • Save rod-dot-codes/5736123 to your computer and use it in GitHub Desktop.
Save rod-dot-codes/5736123 to your computer and use it in GitHub Desktop.
This is a simple Fabric application that can be used to deploy a particular machine within our enviroment. We use Debian based servers. #TODO: Sudo for our users.
#!/usr/bin/python
##Default Installer for Remote Servers
from fabric.api import env,local,run,sudo,put,cd,settings,lcd
from fabric.contrib.files import upload_template,exists
import os
env.user = 'fabricuser'
env.hosts = [""]
def install():
""" Install's Git.
"""
run('apt-get update',pty=True)
run('apt-get install git-core -y ',pty=True)
def pull_config():
with settings(warn_only=True):
with cd("/usr/local/deployment"):
if run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git pull -f'").return_code != 0:
#Try and update the index first
with settings(warn_only=True):
run("rm -rf ~/deployment/")
run("mkdir ~/deployment/")
run("mkdir ~/deployment/keys")
with settings(warn_only=False):
put(os.getcwd()+'/keys/id_rsa','~/deployment/keys/id_rsa',mode=0600)
with settings(warn_only=True):
if run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git clone git@rationalproducts.co.za:deployment.git /usr/local/deployment'").return_code == 128:
with cd("/usr/local/deployment"):
run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git pull -f'")
def install_python():
""" Install's Python, and ensures that the requirements for the 'Deployment' application are met.
"""
run("apt-get install python python-pip virtualenv -y")
run("pip install -r /usr/local/deployment/requirements.txt")
def deploy_repo(repo,folder="/code/"):
""" Deploy's a repo to the 'code' folder, there should always be a 'code' folder.
"""
#Get the pretty name of the repo
pretty_repo = repo.split(":")[-1].replace(".git","")
with cd("/code"):
with settings(warn_only=True):
if run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git clone %s %s%s '" % (repo,folder,pretty_repo)).return_code == 128:
with cd("/code/%s/" % pretty_repo):
run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git fetch origin; git reset --hard origin;'")
#run("ssh-agent bash -c 'ssh-add ~/deployment/keys/id_rsa; git pull;'")
class StaticLocation():
def __init__(self,location,file_system_location):
self.location = location
self.file_system_location = file_system_location
class UWSGIServer():
def __init__(self,location,server):
self.location = location
self.server = server
def deploy_static_nginx(url,ports=80,ssl_secure=False,static_locations = [],redirect_html_to_index = False):
""" Deploy's a static site onto Nginx.
@note: redirect_html_to_index . If you are using it as an Backbone app with only 1 html file, this redirects all .html requests to index.html
"""
with lcd(os.getcwd()+"/fabric_templates/"):
upload_template("nginx.text","/etc/nginx/sites-available/%s%s" % (url,ports), \
context={"url":url,"ports":ports,"ssl_secure":ssl_secure,"static_locations":static_locations,"redirect_html_to_index":redirect_html_to_index},use_jinja=True,template_dir=os.getcwd()+"/fabric_templates/")
#Symbolic link it to sites-enabled.
with settings(warn_only=True):
run("ln -s /etc/nginx/sites-available/%s%s /etc/nginx/sites-enabled/%s%s" % (url,ports,url,ports))
#Check config, will fail if not correct.
run("/etc/init.d/nginx configtest")
#Restart nginx and ..
run("/etc/init.d/nginx restart")
#Make it live.
def deploy_django_nginx(url,ports=80,ssl_secure=False,static_locations = [],uwsgi_servers = []):
""" Deploy a Django site to Nginx.
"""
with lcd(os.getcwd()+"/fabric_templates/"):
upload_template("nginx_django.text","/etc/nginx/sites-available/%s%s" % (url,ports), \
context={"url":url,"ports":ports,"ssl_secure":ssl_secure,"static_locations":static_locations,"uwsgi_servers":uwsgi_servers},use_jinja=True,template_dir=os.getcwd()+"/fabric_templates/")
#Symbolic link it to sites-enabled.
with settings(warn_only=True):
run("ln -s /etc/nginx/sites-available/%s%s /etc/nginx/sites-enabled/%s%s" % (url,ports,url,ports))
#Check config, will fail if not correct.
run("/etc/init.d/nginx configtest")
#Restart nginx and ..
run("/etc/init.d/nginx restart")
#Make it live.
def deploy_django_supervisor(module,server,home,processes=1,uid=1000,gid=2000,harakiri=20):
""" Deploy a Django site to Supervisor and start the running process.
"""
with cd("/code/%s/" % module):
run("find . -name '.pyc' -exec rm -rf {} \;")
#Install VirtualEnv
run("virtualenv --no-site-packages venv")
run("./venv/bin/pip install -r requirements.txt")
#If settings.production.py exists, replace settings.py with it.
if exists("%s/settings.production.py" % module):
run("mv %s/settings.production.py %s/settings.py" % (module,module))
#Settings file must be moved before syncing and migrating DB.
run("./venv/bin/python manage.py syncdb --migrate")
with lcd(os.getcwd()+"/fabric_templates/"):
upload_template("supervisor_django.text","/etc/supervisor/conf.d/%s.conf" % module, \
context={"module":module,"server":server,"processes":processes,"uid":gid,"gid":gid,"harakiri":harakiri,"home":home},use_jinja=True,template_dir=os.getcwd()+"/fabric_templates/")
#Update configuration options
run("/usr/bin/supervisorctl update")
run("/usr/bin/supervisorctl restart %s" % module)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment