forked from bheisig/i-doit-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·183 lines (152 loc) · 5.41 KB
/
setup.sh
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
174
175
176
177
178
179
180
181
182
183
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
: "${INSTALL_DIR:="/var/www/html"}"
: "${MYSQL_HOSTNAME:="localhost"}"
: "${MYSQL_ROOT_USER:="root"}"
: "${MYSQL_ROOT_PASSWORD:="idoit"}"
: "${IDOIT_ADMIN_CENTER_PASSWORD:="admin"}"
: "${MYSQL_USER:="idoit"}"
: "${MYSQL_PASSWORD:="idoit"}"
: "${IDOIT_DEFAULT_TENANT:="CMDB"}"
: "${IDOIT_SYSTEM_DATABASE:="idoit_system"}"
: "${IDOIT_TENANT_DATABASE:="idoit_data"}"
: "${APACHE_USER:="www-data"}"
: "${APACHE_GROUP:="www-data"}"
: "${MARIADB_BIN:="$(command -v mysql)"}"
: "${MEMCACHED_HOST:=""}"
: "${MEMCACHED_PORT:=""}"
: "${APACHE_CONFIG_FILE:="/etc/apache2/sites-available/i-doit.conf"}"
: "${APACHE_HTACCESS_SUBSTITUTION:="## Insert content from .htaccess file here"}"
function report {
log "Installation directory: $INSTALL_DIR"
log "Default tenant: $IDOIT_DEFAULT_TENANT"
log "MariaDB hostname: $MYSQL_HOSTNAME"
log "i-doit system database: $IDOIT_SYSTEM_DATABASE"
log "i-doit tenant database: $IDOIT_TENANT_DATABASE"
log "MariaDB super-user: $MYSQL_ROOT_USER"
log "MariaDB i-doit user: $MYSQL_USER"
}
function runChecks {
test -d "$INSTALL_DIR" || \
abort "Installation directory '${INSTALL_DIR}' not found"
test -d "${INSTALL_DIR}/setup" || \
abort "Directory '${INSTALL_DIR}/setup' not accessible"
test -x "${INSTALL_DIR}/setup/install.sh" || \
abort "Install script '${INSTALL_DIR}/setup/install.sh' not found/executable"
test -x "$MARIADB_BIN" || \
abort "MariaDB client '${MARIADB_BIN}' not found"
}
function waitForDBMS {
while ! "$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" \
-e "SHOW DATABASES;" &> /dev/null; do
log "Wait for MariaDB…"
sleep 1
done
}
function execute {
log "Install i-doit"
addDB "$IDOIT_SYSTEM_DATABASE"
addDB "$IDOIT_TENANT_DATABASE"
runIdoitSetup
fixTenantTable
fixConfigFile
enableMemcached
updateApacheConfig
}
function addDB {
local dbName="$1"
log "Create database '${dbName}'"
"$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" \
-e"CREATE DATABASE IF NOT EXISTS $dbName;" || \
abort "SQL statement failed"
log "Grant MariaDB user '${MYSQL_USER}' access to database '${dbName}'"
"$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_ROOT_USER" -p"$MYSQL_ROOT_PASSWORD" \
-e"GRANT ALL PRIVILEGES ON ${dbName}.* TO '${MYSQL_USER}'@'%' IDENTIFIED BY '${MYSQL_PASSWORD}';" || \
abort "SQL statement failed"
}
function runIdoitSetup {
cd "${INSTALL_DIR}/setup" || abort "Change directory to '${INSTALL_DIR}/setup' failed"
log "Run i-doit's setup script"
./install.sh -n "$IDOIT_DEFAULT_TENANT" \
-s "$IDOIT_SYSTEM_DATABASE" -m "$IDOIT_TENANT_DATABASE" -h "$MYSQL_HOSTNAME" \
-u "$MYSQL_USER" \
-p "$MYSQL_PASSWORD" \
-a "$IDOIT_ADMIN_CENTER_PASSWORD" -q || \
abort "i-doit setup script returned an error"
}
function fixTenantTable {
log "Fix tenant table"
"$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_USER" -p"$MYSQL_PASSWORD" \
-e"UPDATE ${IDOIT_SYSTEM_DATABASE}.isys_mandator SET isys_mandator__db_user = '${MYSQL_USER}', isys_mandator__db_pass = '${MYSQL_PASSWORD}';" || \
abort "SQL statement failed"
}
function fixConfigFile {
local configFile="${INSTALL_DIR}/src/config.inc.php"
log "Fix configuration file '${configFile}'"
sed -i -- \
"s/'user' => '${MYSQL_ROOT_USER}'/'user' => '${MYSQL_USER}'/g" \
"$configFile" || \
abort "Unable to replace MariaDB username"
sed -i -- \
"s/'pass' => '${MYSQL_ROOT_PASSWORD}'/'pass' => '${MYSQL_PASSWORD}'/g" \
"$configFile" || \
abort "Unable to replace MariaDB password"
chown "$APACHE_USER":"$APACHE_GROUP" "$configFile" || abort "Unable to change ownership"
}
function enableMemcached {
if [[ -n "$MEMCACHED_HOST" ]]; then
log "Enable memcached: $MEMCACHED_HOST"
"$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_USER" -p"$MYSQL_PASSWORD" \
-e"INSERT INTO ${IDOIT_SYSTEM_DATABASE}.isys_settings (isys_settings__key, isys_settings__value) VALUES ('memcache.host', '${MEMCACHED_HOST}');" || \
abort "SQL statement failed"
fi
if [[ -n "$MEMCACHED_PORT" ]]; then
log "Configure memcached port: $MEMCACHED_PORT"
"$MARIADB_BIN" \
-h"$MYSQL_HOSTNAME" \
-u"$MYSQL_USER" -p"$MYSQL_PASSWORD" \
-e"INSERT INTO ${IDOIT_SYSTEM_DATABASE}.isys_settings (isys_settings__key, isys_settings__value) VALUES ('memcache.port', '${MEMCACHED_PORT}');" || \
abort "SQL statement failed"
fi
}
function updateApacheConfig {
local htaccessFile="${INSTALL_DIR}/.htaccess"
log "Update Apache's VHost config from i-doit's htaccess file"
sed -i \
-e "/${APACHE_HTACCESS_SUBSTITUTION}/ {" \
-e "r ${htaccessFile}" \
-e "d" \
-e "}" \
"$APACHE_CONFIG_FILE" || \
abort "Unable to change config file"
}
function finish {
log "Done. Have fun :-)"
exit 0
}
function log {
echo -e "$1" 1>&2
}
function abort {
echo -e "$1" 1>&2
echo "Operation failed. Please check what is wrong and try again." 1>&2
exit 1
}
if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
report
runChecks
waitForDBMS
execute
finish
fi