Skip to content

Instantly share code, notes, and snippets.

@deadbok
Created February 3, 2017 23:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deadbok/155bd5906b30bc34b75180b240e326ac to your computer and use it in GitHub Desktop.
Save deadbok/155bd5906b30bc34b75180b240e326ac to your computer and use it in GitHub Desktop.
Fabric file for deploying a flask installation including mongodb.
# -*- coding: utf-8 -*-
# Fabric file for deploying a flask installation including mongodb.
#
# Interesting tasks are:
#
# * purge: Remove everything but the application and the git repository.
# * purge_conf: Purge configurations.
# * purge_dep: Uninstall all packages.
# * deploy: Deploy the flask application to "/home/flask/*project_name*".
# * update: Update the app from the remote repository.
# * update_from_local(local_path): Update application from local sources.
#
# Version 0.2.0
#
# * Made all directories dynamically configurable in the beginning.
#
# Version 0.1.2
from fabric.api import cd, env, puts, run, put
from fabric.context_managers import settings
from fabric.contrib.files import exists
# Server data.
env.hosts = ['127.0.0.1:2222']
env.user = 'root'
env.password = 'test'
# Project data.
project_name = "cgadmin"
project_path = '/home/flask/{0}'.format(project_name)
project_app_path = '{}/{}'.format(project_path, project_name)
project_user = "admin"
project_passwd = "password"
project_git = 'https://deadbok@bitbucket.org/deadbok/vestacp_vps_install.git'
project_git_path = 'vestacp_vps_install/new-server/web'
# Software to install.
python_requirements = ['pymongo', 'Flask-PyMongo', 'Flask-WTF', 'Flask-Uploads']
debian_packages = ['python-dev', 'python-flask', 'python-flask-login',
'fabric', 'python-m2crypto',
'python-unidecode', 'python-pip', 'git', 'nginx', 'gunicorn',
'supervisor', 'htop', 'mc']
# Configurations
supervisor_conf = """
[program: {0}]
command = python {1}/run.py
directory = {1}
autostart = true
autorestart = true
stdout_logfile = {1}/stdout.log
stderr_logfile = {1}/stderr.log
redirect_stderr = true
""".format(project_name, project_path)
nginx_conf = """
server {{
listen 80;
root {1};
access_log {0}/nginx-access.log;
error_log {0}/nginx-error.log;
location / {{
proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {{
proxy_pass http://127.0.0.1:5000;
break;
}}
}}
location /static {{
alias {1}/static/;
autoindex on;
}}
}}
""".format(project_path, project_app_path)
mongod_conf = """
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
# bindIp: 127.0.0.1
#processManagement:
#security:
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
"""
# Tasks
def install_basics():
run('apt-get update')
run('apt-get install -y {}'.format(' '.join(debian_packages)))
run('pip install {}'.format(' '.join(python_requirements)))
if not exists('/home/flask'):
run('mkdir /home/flask')
def install_mongo():
run(
'apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6')
run(
"echo 'deb http://repo.mongodb.org/apt/debian jessie/mongodb-org/3.4 main' | tee /etc/apt/sources.list.d/mongodb-org-3.4.list")
run('apt-get update')
run('apt-get install -y mongodb-org')
run(" echo '{}' ".format(mongod_conf) + '> /etc/mongod.conf')
run('service mongod start')
run('systemctl enable mongod')
def get_app():
if not exists('/root/{}'.format(project_git_path)):
run(
'git clone {}'.format(project_git))
else:
with cd('/root/{}'.format(project_git_path)):
run('git pull')
if not exists('/home/flask/{}'.format(project_name)):
run('mkdir /home/flask/{}'.format(project_name))
with cd('/home/flask'):
run(
'cp -Rva /root/vestacp_vps_install/new-server/web/* /home/flask/cgadmin/')
# For tests
# with cd('/home/flask/{}'.format(project_name)):
# run('python {}_tests.py'.format(project_name))
def adjust_nginx():
run('service nginx start')
default_file = '/etc/nginx/sites-enabled/default'
if exists(default_file):
run('rm ' + default_file)
with cd('/etc/nginx/sites-available'):
run("echo '{}' ".format(nginx_conf) + '> {}'.format(project_name))
if not exists('/etc/nginx/sites-enabled/{}'.format(project_name)):
run('ln -s /etc/nginx/sites-available/{}'.format(project_name) + \
' /etc/nginx/sites-enabled/{}'.format(project_name))
run('service nginx restart')
run('systemctl enable nginx')
def adjust_supervisor():
run("echo '{}' ".format(supervisor_conf) + \
'> /etc/supervisor/conf.d/{}.conf'.format(project_name))
run('supervisorctl reread')
run('supervisorctl update')
def run_flask():
run('supervisorctl stop {}'.format(project_name))
run('supervisorctl start {}'.format(project_name))
run('supervisorctl status')
def purge_conf():
with settings(warn_only=True):
run('rm -f /etc/mongod.conf')
run('rm -f /etc/supervisor/conf.d/{}.conf'.format(project_name))
run('rm -f /etc/nginx/sites-available/{}'.format(project_name))
run('rm -f /etc/nginx/sites-enabled/{}'.format(project_name))
def purge_dep():
with settings(warn_only=True):
run('pip uninstall -y {}'.format(' '.join(python_requirements)))
run('apt-get purge -y mongodb-org')
run('apt-get purge -y {}'.format(' '.join(debian_packages)))
def purge():
purge_conf()
purge_dep()
def deploy():
install_basics()
install_mongo()
get_app()
adjust_nginx()
adjust_supervisor()
run_flask()
def update():
get_app()
run_flask()
def update_from_local(local_path):
put(local_path=local_path, remote_path='/home/flask/{}/{}'.format(project_name))
run_flask()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment