forked from aitoralmeida/c4a_data_repository
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfabfile.py
136 lines (111 loc) · 4.49 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
from fabric.api import run, env, sudo, prefix
from fabric.context_managers import cd
from fabric.network import ssh
import random
# Log configuration
ssh.util.log_to_file("paramiko.log", 10)
##### Server configuration
# Server Hosts
env.hosts = [
'10.48.1.172:5800'
#'odin.deusto.es:5800',
#'10.48.1.142:22',
# and.so.on ......
]
# Set the username
env.user = "city4age"
# Set database creation parameters (Remember to change your mapping.ttl file)
DB_USER = 'city4agedb' # User login
DB_PASS = 'city4agedb' # User password (stored encrypted in db)
DB_DATABASE = 'city4agedb' # Database name
###### Install methods
def _install_deps():
"""
Install needed dependencies
:return:
"""
# Determining GNU/Linux distribution
distribution = run('lsb_release -i | cut -f 2-')
if distribution == 'Ubuntu':
# Ubuntu based distro
run("echo Installing dependencies for UBUNTU GNU/Linux Distribution")
sudo('apt-get update && apt-get -y install python-dev postgresql-9.5 postgresql-server-dev-9.5 virtualenv '
'build-essential nginx openjdk-8-jre tomcat8 openjdk-8-jdk git libservlet3.1-java '
'libxml2-dev libgloox-dev')
else:
# Debian based distro
run("echo Installing dependencies for UBUNTU GNU/Linux Distribution")
sudo('apt-get update && apt-get -y install python-dev postgresql-9.4 postgresql-server-dev-9.4 virtualenv '
'build-essential nginx openjdk-8-jre tomcat8 openjdk-8-jdk git libxml2-dev libgloox-dev')
def _deploy():
"""
Deploy all city4age project in a destination directory and change the user to the actual one.
:return:
"""
with cd('/opt'):
sudo('git clone https://github.com/aitoralmeida/c4a_data_repository.git')
sudo('chown -R ' + env.user + ':' + env.user + ' c4a_data_repository')
with cd('/opt/c4a_data_repository'):
# run('virtualenv ./LinkedDataInterface')
run('virtualenv ./RestApiInterface')
with cd('/opt/c4a_data_repository'):
with prefix('source ./RestApiInterface/bin/activate'):
run('pip install -r ./RestApiInterface/requirements.txt')
run('deactivate')
def _create_database():
"""
Send user, password and table to create a new database and then, restore all data.
:return:
"""
# WORKAROUND: We need to change default PostgreSQL config file to change "peer" connection to "md5"
# to avoid some problems with psql.
random_numer = random.randint(100, 999)
temp_file = '/tmp/out.tmp.%s' % random_numer
with cd('/etc/postgresql/9.5/main'):
sudo("sed -e '90s/peer/md5/g' ./pg_hba.conf > " + temp_file + " && mv " + temp_file + " ./pg_hba.conf")
# Restart postgres
sudo('systemctl restart postgresql.service')
# Create user, database and grant access
sudo('psql -c "CREATE USER %s WITH NOCREATEDB NOCREATEUSER ENCRYPTED PASSWORD E\'%s\'"' %
(DB_USER, DB_PASS), user='postgres')
sudo('psql -c "CREATE DATABASE %s WITH OWNER %s"' % (DB_DATABASE, DB_USER), user='postgres')
sudo('psql -c "GRANT ALL PRIVILEGES ON DATABASE %s TO %s"' % (DB_DATABASE, DB_USER), user='postgres')
sudo('psql -c "FLUSH PRIVILEGES"', user='postgres')
def _install_rest_api():
"""
Install Rest API Interface in the server.
:return:
"""
with cd('/opt/c4a_data_repository/RestApiInterface/scripts'):
run('/bin/bash ./install.sh')
with cd('/opt/c4a_data_repository/Database'):
sudo('cp db_backup.sh /etc/cron.daily')
sudo('chmod +x /etc/cron.daily/db_backup.sh')
sudo('systemctl restart cron.service')
def _install_linked_data():
"""
Install Linked Data Interface in the server.
:return:
"""
with cd('/opt/c4a_data_repository/LinkedDataInterface/scripts'):
run('/bin/bash ./install.sh')
with cd('/etc'):
sudo('chown -R tomcat8:tomcat8 ./fuseki')
sudo('systemctl restart tomcat8.service')
# Main installation method.
def main_install():
"""
Install entire project in the server:
--> Project packages
--> Deploy project and prepare virtualenv
--> Create database user and restore data
--> Install Rest API Interface
--> Install Linked Data Interface
:return:
"""
# Call to every part of the program to install entire system.
_install_deps()
_deploy()
_create_database()
_install_rest_api()
_install_linked_data()