forked from marians/rebrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
158 lines (129 loc) · 4.47 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python
"""
Fabfile to deploy rebrow
"""
import os
import io
import boto3
from fabric.api import (cd, env, execute, hide, lcd, local, put, run,
runs_once, settings, task)
env.application = 'rebrow'
env.forward_agent = True
env.timeout = 15
env.user = "des"
env.connection_attempts = 5
env.use_ssh_config = True
env.remote_dir = '/data/apps/{}'.format(env.application)
env.home = '/home/des'
env.virtualenv = env.application
env.remote_release_dir = "%s/releases" % env.remote_dir
env.local_dir = os.path.dirname(os.path.realpath(__file__))
env.supervisorctl_path = "/bin/supervisorctl"
def aws_get_instance_dns_names(environment='dev'):
"""Get the instance dns names"""
names = []
aws_client = boto3.client('ec2', region_name='us-east-1')
f = [
{
'Name': 'tag:client',
'Values': [env.application]
},
{
'Name': 'tag:organization',
'Values': ['des']
},
{
'Name': 'tag:role',
'Values': ['web']
},
{
'Name': 'tag:env',
'Values': [environment]
},
{
'Name': 'network-interface.status',
'Values': ['in-use']
}
]
response = aws_client.describe_instances(Filters=f)
for r in response['Reservations']:
for i in r['Instances']:
names.append(i['PrivateDnsName'])
return names
@task
def deploy(symlink='current', branch='', client='', environment='', variant='',
hostfilter='', deploy_command=''):
"""Main deploy task"""
if hostfilter:
env.hosts = get_hosts(hostfilter)
else:
env.hosts = aws_get_instance_dns_names(environment=environment)
execute(archive)
execute(create_release_folder)
execute(transfer_archive)
execute(unzip_archive)
execute(create_sym_link, name=symlink)
execute(pip_install, symlink=symlink)
execute(delete_archive)
execute(update_supervisorctl)
def get_hosts(h):
"""Parse host filters"""
if h:
x = [y.strip() for y in h.split(";")]
if x:
return x
return False
@runs_once
def archive():
with lcd(env.local_dir):
env.commit = local('git log --pretty=format:\'%h\' -n 1', capture=True)
base = local('basename $(git rev-parse --show-toplevel)', capture=True)
env.archive_file = "%s-%s.zip" % (base, env.commit,)
local('git archive HEAD --format=zip > %s' % env.archive_file)
def create_release_folder():
run('mkdir -p {}'.format(env.remote_release_dir))
with cd(env.remote_release_dir):
run('rm -rf %s' % env.commit)
run('mkdir %s' % env.commit)
def transfer_archive():
local('scp {} {}@{}:{}/{}'.format(env.archive_file, env.user, env.host,
env.remote_release_dir, env.commit))
def unzip_archive():
with cd(env.remote_dir):
with cd("%s/%s" % (env.remote_release_dir, env.commit,)):
with hide('output'):
run('unzip %s' % env.archive_file)
def create_sym_link(name):
with cd(env.remote_dir):
run('rm -rf %s' % name)
run('ln -s %s/%s %s' % (env.remote_release_dir, env.commit, name,))
def delete_archive():
with cd(env.remote_release_dir):
run('rm -rf {}/{}'.format(env.commit, env.archive_file))
with lcd(env.local_dir):
local('rm -rf {}'.format(env.archive_file))
@task
def update_supervisorctl():
"""Update supervisord, restart process for new changes to take effect"""
run("sudo %s reread" % (env.supervisorctl_path,))
run("sudo %s update" % (env.supervisorctl_path,))
run("sudo %s restart all" % (env.supervisorctl_path,))
@task
def pip_install(symlink='current'):
"""Install packages"""
virtualenv = '{}/.virtualenvs/{}/'.format(env.home, env.virtualenv)
with settings(warn_only=True):
dir_exists = run('[ -d {} ] && echo 1'.format(virtualenv))
if dir_exists != '1':
run('export SLUGIFY_USES_TEXT_UNIDECODE=yes')
run('mkvirtualenv {}'.format(env.virtualenv))
prod_file = '{}/{}/requirements.txt'
requirements = prod_file.format(env.remote_dir, symlink)
install = '{}/.virtualenvs/{}/bin/pip install -r {}'
with hide('output'):
run(install.format(env.home, env.virtualenv, requirements))
@task
def git_version(symlink='current'):
"""Git version."""
git_version = '{}/{}/git_version'.format(env.remote_dir, symlink)
put(io.StringIO(env.commit), git_version)