This repository was archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfabfile.py
More file actions
113 lines (89 loc) · 3.03 KB
/
fabfile.py
File metadata and controls
113 lines (89 loc) · 3.03 KB
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
from contextlib import contextmanager as _contextmanager
from fabric.context_managers import prefix
from fabric.operations import get, run, sudo
from fabric.state import env
from fabric.contrib import django
import boto3
django.project('dq')
from django.conf import settings
running_instances = []
s = boto3.session.Session(profile_name='dq')
ec2 = s.resource("ec2")
def get_ec2_instances():
instances = ec2.instances.filter(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)
for i in instances:
ssh_access = "ubuntu@{0}".format(i.public_ip_address)
print 'servers >', ssh_access
running_instances.append(ssh_access)
get_ec2_instances()
environments = {
'production': {
'hosts': running_instances,
'source_code': '/home/ubuntu/www/dq.com/dq',
'supervisor_commands': [
'supervisorctl restart dq'
],
'virtualenv': {
'virtualenv_name': 'dq',
'virtualenv_sh': '/usr/local/bin/virtualenvwrapper.sh',
},
'git': {
'parent': 'origin',
'branch': 'master',
}
}
}
# Utils
@_contextmanager
def virtualenv():
""" Wrapper to run commands in the virtual env context """
environment = environments['default']
workon_home = environment['virtualenv'].get('workon_home',
'~/.virtualenvs')
with prefix('export WORKON_HOME={0}'.format(workon_home)):
virtualenv_sh = environment['virtualenv'].get('virtualenv_sh',
'/etc/bash_completion.d/virtualenvwrapper')
with prefix('source {0}'.format(virtualenv_sh)):
virtualenv_name = environment['virtualenv'].get('virtualenv_name')
with prefix('workon {0}'.format(virtualenv_name)):
source_code = environment['source_code']
with prefix('cd {0}'.format(source_code)):
yield
def django(command):
with virtualenv():
full_command = 'python manage.py {0}'.format(command)
run(full_command)
# setup
def production():
environments['default'] = environments['production']
env.hosts = environments['production']['hosts']
env.key_filename = 'djangoquilla.pem'
#tasks
def test_connection():
run('free -m')
def git_pull():
with virtualenv():
run('git pull %s %s' % (environments['default']['git']['parent'],
environments['default']['git']['branch']))
#run('git pull')
def pip_install():
with virtualenv():
run('pip install -r requirements.txt')
def pyclean():
with virtualenv():
run('find . -type f -name "*.py[co]" -exec rm -f \{\} \;')
def supervisor_restart():
for supervisor in environments['default']['supervisor_commands']:
sudo(supervisor)
def deploy():
git_pull()
pyclean()
supervisor_restart()
"""
Filters=[
{'Name': 'tag-key', 'Values': ['env']},
{'Name': 'tag-value', 'Values': ['qa']},
]
"""