-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfabfile.py
159 lines (127 loc) ยท 5.07 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
159
# fabfile.py
from fabric.contrib.files import append, exists, sed, put
from fabric.api import env, local, run, sudo
import os
import json
# ํ์ฌ fabfile.py๊ฐ ์๋ ํด๋์ ๊ฒฝ๋ก
PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
# deploy.json์ด๋ผ๋ ํ์ผ์ ์ด์ด ์๋์ ๋ณ์๋ค์ ๋ด์์ค๋๋ค.
envs = json.load(open(os.path.join(PROJECT_DIR, "deploy.json")))
REPO_URL = envs['REPO_URL']
PROJECT_NAME = envs['PROJECT_NAME']
REMOTE_HOST = envs['REMOTE_HOST']
REMOTE_HOST_SSH = envs['REMOTE_HOST_SSH']
REMOTE_USER = envs['REMOTE_USER']
REMOTE_SSH_KEY = envs['SSH_KEY']
# SSH์ ์ ์ํ ์ ์ ๋ฅผ ์ง์ ํ๊ณ ,
env.user = REMOTE_USER
# SSH๋ก ์ ์ํ ์๋ฒ์ฃผ์๋ฅผ ๋ฃ์ด์ฃผ๊ณ ,
env.hosts = [
REMOTE_HOST_SSH,
]
env.key_filename = REMOTE_SSH_KEY
# ์๊ฒฉ ์๋ฒ์ค ์ด๋์ ํ๋ก์ ํธ๋ฅผ ์ ์ฅํ ์ง ์ง์ ํด์ค ๋ค,
project_folder = '/home/{}/{}'.format(env.user, PROJECT_NAME)
# ์ฐ๋ฆฌ ํ๋ก์ ํธ์ ํ์ํ apt ํจํค์ง๋ค์ ์ ์ด์ค๋๋ค.
apt_requirements = [
'curl',
'git',
'python3-dev',
'python3-pip',
'build-essential',
'apache2',
'libapache2-mod-wsgi-py3',
'python3-setuptools',
'libssl-dev',
'libffi-dev',
]
# _๋ก ์์ํ์ง ์๋ ํจ์๋ค์ fab new_server ์ฒ๋ผ ๋ช
๋ น์ค์์ ๋ฐ๋ก ์คํ์ด ๊ฐ๋ฅํฉ๋๋ค.
def new_server():
setup()
deploy()
def setup():
_get_latest_apt()
_install_apt_requirements(apt_requirements)
_make_virtualenv()
def deploy():
_get_latest_source()
_put_envs()
_update_virtualenv()
_make_virtualhost()
_grant_apache2()
_restart_apache2()
# put์ด๋ผ๋ ๋ฐฉ์์ผ๋ก ๋ก์ปฌ์ ํ์ผ์ ์๊ฒฉ์ง๋ก ์
๋ก๋ํ ์ ์์ต๋๋ค.
def _put_envs():
pass # activate for envs.json file
# put('envs.json', '~/{}/envs.json'.format(PROJECT_NAME))
# apt ํจํค์ง๋ฅผ ์
๋ฐ์ดํธ ํ ์ง ๊ฒฐ์ ํฉ๋๋ค.
def _get_latest_apt():
update_or_not = input('would you update?: [y/n]')
if update_or_not == 'y':
sudo('apt-get update && apt-get -y upgrade')
# ํ์ํ apt ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค.
def _install_apt_requirements(apt_requirements):
reqs = ''
for req in apt_requirements:
reqs += (' ' + req)
sudo('apt-get -y install {}'.format(reqs))
# virtualenv์ virtualenvwrapper๋ฅผ ๋ฐ์ ์ค์ ํฉ๋๋ค.
def _make_virtualenv():
if not exists('~/.virtualenvs'):
script = '''"# python virtualenv settings
export WORKON_HOME=~/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON="$(command \which python3)" # location of python3
source /usr/local/bin/virtualenvwrapper.sh"'''
run('mkdir ~/.virtualenvs')
sudo('pip3 install virtualenv virtualenvwrapper')
run('echo {} >> ~/.bashrc'.format(script))
# Git Repo์์ ์ต์ ์์ค๋ฅผ ๋ฐ์์ต๋๋ค.
# ๊น์ด ์๋ค๋ฉด fetch๋ฅผ, ์๋ค๋ฉด clone์ ์งํํฉ๋๋ค.
def _get_latest_source():
if exists(project_folder + '/.git'):
run('cd %s && git fetch' % (project_folder,))
else:
run('git clone %s %s' % (REPO_URL, project_folder))
current_commit = local("git log -n 1 --format=%H", capture=True)
run('cd %s && git reset --hard %s' % (project_folder, current_commit))
# Repo์์ ๋ฐ์์จ requirements.txt๋ฅผ ํตํด pip ํจํค์ง๋ฅผ virtualenv์ ์ค์นํด์ค๋๋ค.
def _update_virtualenv():
virtualenv_folder = project_folder + '/../.virtualenvs/{}'.format(PROJECT_NAME)
if not exists(virtualenv_folder + '/bin/pip'):
run('cd /home/%s/.virtualenvs && virtualenv %s' % (env.user, PROJECT_NAME))
run('%s/bin/pip install -r %s/requirements.txt' % (
virtualenv_folder, project_folder
))
# (optional) UFW์์ 80๋ฒ/tcpํฌํธ๋ฅผ ์ด์ด์ค๋๋ค.
def _ufw_allow():
sudo("ufw allow 'Apache Full'")
sudo("ufw reload")
# Apache2์ Virtualhost๋ฅผ ์ค์ ํด ์ค๋๋ค.
# ์ด ๋ถ๋ถ์์ wsgi.py์์ ํต์ , ๊ทธ๋ฆฌ๊ณ virtualenv ๋ด์ ํ์ด์ฌ ๊ฒฝ๋ก๋ฅผ ์ง์ ํด ์ค๋๋ค.
def _make_virtualhost():
script = """'<VirtualHost *:80>
ServerName {servername}
<Directory /home/{username}/{project_name}>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIDaemonProcess {project_name} python-home=/home/{username}/.virtualenvs/{project_name} python-path=/home/{username}/{project_name}
WSGIProcessGroup {project_name}
WSGIScriptAlias / /home/{username}/{project_name}/wsgi.py
ErrorLog ${{APACHE_LOG_DIR}}/error.log
CustomLog ${{APACHE_LOG_DIR}}/access.log combined
</VirtualHost>'""".format(
username=REMOTE_USER,
project_name=PROJECT_NAME,
servername=REMOTE_HOST,
)
sudo('echo {} > /etc/apache2/sites-available/{}.conf'.format(script, PROJECT_NAME))
sudo('a2ensite {}.conf'.format(PROJECT_NAME))
# Apache2๊ฐ ํ๋ก์ ํธ ํ์ผ์ ์ฝ์ ์ ์๋๋ก ๊ถํ์ ๋ถ์ฌํฉ๋๋ค.
def _grant_apache2():
sudo('chown -R :www-data ~/{}'.format(PROJECT_NAME))
sudo('chmod -R 775 ~/{}'.format(PROJECT_NAME))
# ๋ง์ง๋ง์ผ๋ก Apache2๋ฅผ ์ฌ์์ํฉ๋๋ค.
def _restart_apache2():
sudo('sudo service apache2 restart')