-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfabfile.py
138 lines (114 loc) · 4.38 KB
/
fabfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from __future__ import absolute_import
import os
from datetime import datetime
from fabric.api import *
from fabric.contrib import *
from fabric.contrib.files import exists
from fabric import utils
_private_pypi = os.getenv('PRIVATE_PYPI')
PIP_OPTIONS = '-i %s' % _private_pypi if _private_pypi else ''
"""
Environments
"""
ENVIRONMENTS = ('dev', 'staging', 'production')
@task
def local():
env.environment = 'dev'
env.repo = "https://github.com/ox-it/talks.ox.git"
env.hosts = ['talks.vm']
env.user = 'talks'
env.settings_module = 'talks.settings'
env.remote_install_dir = '/srv/talks/talks.vm'
env.remote_git_checkout = '/srv/talks/talks.ox'
env.requirements = ['requirements.txt']
env.secrets_dir = '/etc/puppet/secrets/talks-dev.oucs.ox.ac.uk'
@task
def virtual():
env.environment = 'dev'
env.repo = "https://github.com/ox-it/talks.ox.git"
env.hosts = ['192.168.33.178']
env.user = 'talks'
env.settings_module = 'talks.settings'
env.remote_install_dir = '/srv/talks/talks-dev.oucs.ox.ac.uk'
env.remote_git_checkout = '/srv/talks/talks.ox'
env.requirements = ['requirements.txt']
env.secrets_dir = '/srv/%s/secrets' % (env.user)
@task
def staging():
env.environment = 'staging'
env.repo = "https://github.com/ox-it/talks.ox.git"
env.hosts = ['talks-dev.oucs.ox.ac.uk']
env.user = 'talks'
env.settings_module = 'talks.settings'
env.remote_install_dir = '/srv/talks/talks-dev.oucs.ox.ac.uk'
env.remote_git_checkout = '/srv/talks/talks.ox'
env.requirements = ['requirements.txt']
env.secrets_dir = '/srv/%s/secrets' % (env.user)
@task
def production():
env.environment = 'production'
env.repo = "https://github.com/ox-it/talks.ox.git"
env.hosts = ['talks-prod.oucs.ox.ac.uk']
env.user = 'talks'
env.settings_module = 'talks.settings'
env.remote_install_dir = '/srv/talks/talks-prod.oucs.ox.ac.uk'
env.remote_git_checkout = '/srv/talks/talks.ox'
env.requirements = ['requirements.txt']
env.secrets_dir = '/srv/%s/secrets' % (env.user)
"""
Methods
"""
@task
def deploy(version):
if not version:
utils.abort('You must specify a version (whether branch or tag).')
require('user', provided_by=ENVIRONMENTS)
git_hash = git_branch(version)
versioned_path = '/srv/%s/talks.ox-%s-%s' % (env.user, datetime.now().strftime('%Y%m%d%H%M'), git_hash)
createvirtualenv(versioned_path)
with prefix('source %s' % os.path.join(versioned_path, 'bin', 'activate')):
install_dir = prepare(versioned_path)
install(install_dir)
run('rm %s/secrets.py' % install_dir)
run('ln -s %s/secrets.py %s/secrets.py' % (env.secrets_dir, install_dir))
run('rm -f %s' % env.remote_install_dir)
run('ln -s %s %s' % (versioned_path, env.remote_install_dir))
run('touch %s' % os.path.join(env.remote_install_dir, 'talks', 'wsgi.py'))
def prepare(venv):
static_dir = os.path.join(venv, 'static')
install_dir = os.path.join(venv, 'talks')
run('cp -r %s %s' % (os.path.join(env.remote_git_checkout, 'static'), static_dir))
run('cp -r %s %s' % (os.path.join(env.remote_git_checkout, 'talks'), install_dir))
run('cp -r %s %s' % (os.path.join(env.remote_git_checkout, 'manage.py'), venv))
for req in env.requirements:
run('pip install --upgrade -r %s %s' % (os.path.join(env.remote_git_checkout, req), PIP_OPTIONS))
return install_dir
def install(install_dir):
with cd(os.path.dirname(install_dir)):
run('python manage.py makemigrations --settings=%s' % env.settings_module)
run('python manage.py migrate --settings=%s' % env.settings_module)
run('python manage.py collectstatic --noinput --settings=%s' % env.settings_module)
@task
def rebuild_index():
with cd(env.remote_install_dir):
run('bin/python manage.py rebuild_index')
"""
Private methods
"""
def createvirtualenv(path):
run('python3 -m venv --system-site-packages %s' % path)
def git_check_existing_repo():
"""
Checks that git repo exists, create it if it doesn't
"""
if not exists(env.remote_git_checkout):
run('git clone %s %s' % (env.repo, env.remote_git_checkout))
def git_branch(name):
"""
Do a checkout on a branch
"""
git_check_existing_repo()
with cd(env.remote_git_checkout):
run('git fetch origin')
run('git checkout origin/%s' % name)
return run('git rev-parse --short HEAD')