-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall.sh
executable file
·213 lines (187 loc) · 11.1 KB
/
install.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
if [ ! -f $DIR/.env ]; then
echo "*** ERROR .env file is missing, copy .env.sample to .env and edit"
exit 1
fi
source $DIR/.env
if ! type "git" > /dev/null; then
echo "ERROR: please install git"
exit 1
fi
echo ""
echo "*** WARNING - this will erase all existing data (apart from backups), if you just want to update then run ./update.sh"
echo ""
read -r -p " Are you sure you want to proceed? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
;;
*)
echo "Exiting..."
exit 1
;;
esac
if [ -d $DIR/data ]; then
echo ""
echo "*** data directory exists, taking backup just in case..."
echo ""
$DIR/backup.sh
echo ""
echo "*** Removing any existing data that might exist (not backups)"
echo ""
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/db
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/cloudlog
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/certs
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/conf.d
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/html
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm rm -rf /data/vhost.d
fi
echo ""
echo "*** Removing docker containers in case they are still running"
echo ""
docker-compose down
echo ""
echo "*** Creating directories and cloning cloudlog repo from git"
echo ""
mkdir -p $DIR/data/cloudlog
mkdir -p $DIR/data/backup/auto
git clone https://github.com/magicbug/Cloudlog.git $DIR/data/cloudlog/
echo ""
echo "*** Creating config.php and database.php configs"
echo ""
CONFIGFILE=/data/cloudlog/application/config/config.php
SAMPLE_CONFIGFILE=/data/cloudlog/application/config/config.sample.php
DBCONFIGFILE=/data/cloudlog/application/config/database.php
SAMPLE_DBCONFIGFILE=/data/cloudlog/application/config/database.sample.php
LANGUAGE_DIR="$DIR/data/cloudlog/application/language/$LANGUAGE"
if [ -f $DIR/${SAMPLE_CONFIGFILE} ]; then
# Copy template
cp $DIR/${SAMPLE_CONFIGFILE} $DIR/${CONFIGFILE}
# Update config
if [ ${HTTPS_PORT} -eq 443 ]; then
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['base_url'\] = '([^\']*)+'\;|\['base_url'\] = '${BASE_URL:-http://localhost/}'\;|g" ${CONFIGFILE}
else
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['base_url'\] = '([^\']*)+'\;|\['base_url'\] = '${BASE_URL}:${HTTPS_PORT}'\;|g" ${CONFIGFILE}
fi
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['directory'\] = '([^\']*)+'\;|\['directory'\] = '${WEB_DIRECOTRY}'\;|g" ${CONFIGFILE}
if [ ${LANGUAGE} != "english" ] && [ -d "$LANGUAGE_DIR" ]; then
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['language'\][[:space:]]=.*\;/\['language'\] = '${LANGUAGE}'\;/g" ${CONFIGFILE}
else
echo ""
echo "*** WARNING - Default language not changed: looks like '${LANGUAGE}' is not an available language."
echo ""
fi
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['callbook'\] = \"([^\']*)+\"\;/\['callbook'\] = \"${CALLBOOK:-hamqth}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['hamqth_username'\] = \"([^\']*)+\"\;/\['hamqth_username'\] = \"${HAMQTH_USERNAME//\//\\/}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['hamqth_password'\] = \"([^\']*)+\"\;/\['hamqth_password'\] = \"${HAMQTH_PASWORD//\//\\/}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['qrz_username'\] = \"([^\']*)+\"\;/\['qrz_username'\] = \"${QRZ_USERNAME//\//\\/}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['qrz_password'\] = \"([^\']*)+\"\;/\['qrz_password'\] = \"${QRZ_PASSWORD//\//\\/}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['locator'\] = \"([^\']*)+\"\;/\['locator'\] = \"${LOCATOR//\//\\/}\"\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/\['display_freq'\] = ([^\']*)+\;/\['display_freq'\] = ${DISPLAY_FREQ//\//\\/}\;/g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['sess_driver'\] = '([^\']*)+'\;|\['sess_driver'\] = '${SESSION_DRIVER:-files}'\;|g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['sess_save_path'\] = '([^\']*)+'\;|\['sess_save_path'\] = '${SESSION_SAVE_PATH:-/tmp}'\;|g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['sess_expiration'\] = '([^\']*)+'\;|\['sess_expiration'\] = '${SESSION_EXPIRATION:-0}'\;|g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['index_page'\] = 'index.php'\;|\['index_page'\] = ''\;|g" ${CONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s|\['proxy_ips'\] = '([^\']*)+'\;|\['proxy_ips'\] = '${PROXY_IPS:-10.0.0.0/8}'\;|g" ${CONFIGFILE}
echo ""
echo "*** config.php file has been created."
echo ""
else
echo ""
echo "*** No config template found.. can't install"
echo ""
exit 1
fi
if [ -f $DIR/${SAMPLE_DBCONFIGFILE} ]; then
# Copy template
cp $DIR/${SAMPLE_DBCONFIGFILE} $DIR/${DBCONFIGFILE}
# Update config for custom mysql
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/'hostname' => '([^\']*)+',/'hostname' => 'db',/g" ${DBCONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/'username' => '([^\']*)+',/'username' => '${MYSQL_USER}',/g" ${DBCONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/'password' => '([^\']*)+',/'password' => '${MYSQL_PASSWORD//\//\\/}',/g" ${DBCONFIGFILE}
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -ri "s/'database' => '([^\']*)+',/'database' => '${MYSQL_DATABASE}',/g" ${DBCONFIGFILE}
echo ""
echo "*** database.php config file has been created."
echo ""
else
echo ""
echo "*** No DB config template found.. can't install"
echo ""
exit 1
fi
echo ""
echo "*** Creating cron file"
echo ""
mkdir -p $DIR/data/cron.d
echo "SHELL=/bin/bash" > $DIR/data/cron.d/cloudlog
echo "PATH=/sbin:/bin:/usr/sbin:/usr/bin" >> $DIR/data/cron.d/cloudlog
echo "@daily cloudlog curl --silent http://${APP_NAME}_web:8080/index.php/lotw/load_users &>/dev/null" >> $DIR/data/cron.d/cloudlog
echo "@daily cloudlog curl --silent http://${APP_NAME}_web:8080/index.php/update/update_clublog_scp &>/dev/null" >> $DIR/data/cron.d/cloudlog
echo "@daily cloudlog curl --silent http://${APP_NAME}_web:8080/index.php/update/dxcc &>/dev/null" >> $DIR/data/cron.d/cloudlog
# Generate random offset for the crons so that the servers dont get hit on the hour/minute
OFFSET=$(docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm shuf -i5-55 -n1)
OFFSET_TRIMMED=$(echo $OFFSET | tr -dc '0-9')
if [ "${CLUBLOG_UPLOAD_CRON}" == true ]; then
echo "$OFFSET_TRIMMED * * * * cloudlog sleep $OFFSET_TRIMMED; curl --silent http://${APP_NAME}_web:8080/index.php/clublog/upload/${CALLSIGN} &>/dev/null" >> $DIR/data/cron.d/cloudlog
fi
if [ "${LOTW_UPLOAD_CRON}" == true ]; then
echo "$OFFSET_TRIMMED * * * * cloudlog sleep $OFFSET_TRIMMED; curl --silent http://${APP_NAME}_web:8080/lotw/lotw_upload &>/dev/null" >> $DIR/data/cron.d/cloudlog
fi
if [ "${AUTO_BACKUPS}" == true ]; then
echo "@daily cloudlog cron-backup.sh" >> $DIR/data/cron.d/cloudlog
fi
echo ""
echo "*** Generating self signed cert for local access or in case of letsencrypt issues"
echo ""
mkdir -p $DIR/data/certs
openssl req -new -newkey rsa:4096 -days 3650 -nodes -x509 -subj "/CN=${APP_HOSTNAME}" -keyout $DIR/data/certs/${APP_HOSTNAME}.key -out $DIR/data/certs/${APP_HOSTNAME}.crt
echo ""
echo "*** Backup page"
echo ""
echo "This feature is disabled as no security is implemented." > $DIR/data/cloudlog/backup/index.html
echo " To backup log use the ADIF export menu, or collect raw SQL backups from ./data/backup/ on the server filesystem" >> $DIR/data/cloudlog/backup/index.html
echo ""
echo "*** Copying .htaccess file"
echo ""
#cp $DIR/data/cloudlog/.htaccess.sample $DIR/data/cloudlog/.htaccess
echo ""
echo "*** Copying DB install.sql to mysql initdb.d dir"
echo ""
mkdir -p $DIR/data/initdb.d
cp $DIR/data/cloudlog/install/assets/install.sql $DIR/data/initdb.d/
echo ""
echo "*** Creating cloudlog user"
echo ""
HASHED_PW=$(docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm php -r 'echo password_hash("'"${CLOUDLOG_PASSWORD}"'", PASSWORD_DEFAULT);')
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm sed -i "/m0abc/c INSERT INTO \`users\` VALUES ('4','${CLOUDLOG_USERNAME}','${HASHED_PW}','${EMAIL}','99','${CALLSIGN}','${LOCATOR}','${FIRST_NAME}','${LAST_NAME}','151',null,null,null,null,null);" /data/initdb.d/install.sql
echo ""
echo "*** Removing Cloudlog install dir"
echo ""
rm -rf $DIR/data/cloudlog/install/
echo ""
echo "*** Setting permissions"
echo ""
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm chown -R 5001:5001 /data/cloudlog
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm chmod -R 755 /data/cloudlog
docker run -it --name install_worker --mount type=bind,source=$DIR/data/,target=/data --rm php:7-fpm chown -R 5001:5001 $DIR/data/backup/auto
echo ""
echo "*** Pulling images and building containers"
echo ""
docker-compose pull
docker-compose build
echo ""
echo "*** Install complete, if you are wanting to restore from a sql database backup then place the backup .sql or sql.gz file in ./data/initdb.d and remove install.sql before you run start.sh"
echo ""
read -r -p "Start now? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
$DIR/start.sh
exit 0
;;
*)
echo "Exiting..."
exit 1
;;
esac
echo ""
echo ""