forked from Azimut-Prod/azimut-deploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agep.py
173 lines (115 loc) · 4.44 KB
/
agep.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from fabric.api import *
from fabric.contrib.files import upload_template, append
import config
@task
def setup_agep_vm():
"""Setup a new agep VM [$AG:NeedKomUser][$AG:NeedSudo]"""
execute(install_apache)
execute(install_php)
execute(create_folders)
execute(upload_default_page)
execute(set_user_rights)
execute(configure_apache)
execute(add_mysql_host)
execute(configure_logrotate)
execute(restart_apache)
if hasattr(env, 'fab_addsudo') and env.fab_addsudo == 'True':
execute(allow_sudo_user)
@task
def install_apache():
"""Install apache"""
sudo('apt-get install -y apache2')
@task
def install_php():
"""Install php"""
sudo('apt-get install -y php5 libapache2-mod-php5 php5-cli php5-curl php5-dev php5-gd php5-ldap php5-mcrypt php5-mysql php5-odbc php5-pgsql')
@task
def restart_apache():
"""Restart apache"""
sudo('service apache2 restart')
@task
def configure_apache():
"""Configure apache [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
# Disable default site
sudo('a2dissite 000-default')
# Copy config
upload_template('files/agep/apache.conf', '/etc/apache2/sites-available/apache.conf', {'user': env.fab_komuser})
# Enable config
sudo('a2ensite apache.conf', pty=True)
sudo('a2enmod rewrite')
@task
def configure_logrotate():
"""Configure logrotate to rotate apachelogs"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
sudo('echo -e "/home/' + env.fab_komuser + '/logs/*.log $(cat /etc/logrotate.d/apache2)" > /etc/logrotate.d/apache2')
@task
def allow_sudo_user():
"""Allow kom user to use sudo [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
sudo('adduser ' + env.fab_komuser + ' sudo')
@task
def upload_default_page():
"""Upload the default webpage. [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
upload_template('files/agep/index.html', '/home/' + env.fab_komuser + '/public_html/index.html', {
'vm_name': env.fab_komuser + '.agepoly.ch',
}, use_sudo=True)
@task
def create_folders():
"""Create folders [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
sudo('mkdir /home/' + env.fab_komuser + '/public_html/')
sudo('mkdir /home/' + env.fab_komuser + '/logs/')
@task
def add_mysql_host():
"""Add the mysql host"""
append('/etc/hosts', config.MYSQL_HOST + ' mysql')
@task
def set_user_rights():
"""Set user rights to access everything he needs [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
sudo('chown -R ' + env.fab_komuser + ':www-data /home/' + env.fab_komuser + '/public_html/')
sudo('chown -R ' + env.fab_komuser + ':www-data /home/' + env.fab_komuser + '/logs/')
@task
def setup_mysql():
"""Setup the mysql database and user [$AG:NeedKomUser][$AG:NeedMysqlPassword]"""
with hide('running'):
execute(create_mysql_database)
execute(add_mysql_user)
execute(add_mysql_rights)
execute(reload_mysql_rights)
def mysql_execute(sql):
"""Executes passed sql command using mysql shell."""
sql = sql.replace('"', r'\"')
return run('echo "%s" | mysql --user="%s" --password="%s"' % (sql, config.MYSQL_USER, config.MYSQL_PASSWORD))
@task
def create_mysql_database():
"""Create a new mysql database [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
mysql_execute("CREATE DATABASE %s DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;" % (env.fab_komuser,))
@task
def add_mysql_user():
"""Add mysql user [$AG:NeedKomUser][$AG:NeedMysqlPassword]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
if not hasattr(env, 'fab_mysqlpassword') or env.fab_mysqlpassword == '':
return
mysql_execute("CREATE USER '%s'@'%%' IDENTIFIED BY '%s';" % (env.fab_komuser, env.fab_mysqlpassword))
@task
def add_mysql_rights():
"""Add mysql rights [$AG:NeedKomUser]"""
if not hasattr(env, 'fab_komuser') or env.fab_komuser == '':
return
mysql_execute("GRANT ALL ON %s.* TO '%s'@'%%';" % (env.fab_komuser, env.fab_komuser))
@task
def reload_mysql_rights():
"""Reload mysql rights"""
mysql_execute("FLUSH PRIVILEGES;")