-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathdevtoolsLibrary.source
More file actions
280 lines (262 loc) · 16.2 KB
/
Copy pathdevtoolsLibrary.source
File metadata and controls
280 lines (262 loc) · 16.2 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/sh
# defaults
: "${MYSQL_HOST:=}" \
"${MYSQL_ROOT_PASS:=}" \
"${SQL_DATA_DRIVE:=}"
prepareVariables() {
CONFIGURATION="server=${MYSQL_HOST} rootpass=${MYSQL_ROOT_PASS} loginhost=%"
# defaults
CUSTOM_ROOT_USER=root
CUSTOM_USER=openemr
CUSTOM_PASSWORD=openemr
CUSTOM_DATABASE=openemr
CUSTOM_PORT=3306
if [ -n "${MYSQL_ROOT_USER:-}" ]; then
CONFIGURATION="${CONFIGURATION} root=${MYSQL_ROOT_USER}"
CUSTOM_ROOT_USER="${MYSQL_ROOT_USER}"
fi
if [ -n "${MYSQL_USER:-}" ]; then
CONFIGURATION="${CONFIGURATION} login=${MYSQL_USER}"
CUSTOM_USER="${MYSQL_USER}"
fi
if [ -n "${MYSQL_PASS:-}" ]; then
CONFIGURATION="${CONFIGURATION} pass=${MYSQL_PASS}"
CUSTOM_PASSWORD="${MYSQL_PASS}"
fi
if [ -n "${MYSQL_DATABASE:-}" ]; then
CONFIGURATION="${CONFIGURATION} dbname=${MYSQL_DATABASE}"
CUSTOM_DATABASE="${MYSQL_DATABASE}"
fi
if [ -n "${MYSQL_PORT:-}" ]; then
CONFIGURATION="${CONFIGURATION} port=${MYSQL_PORT}"
CUSTOM_PORT="${MYSQL_PORT}"
fi
if [ -n "${OE_USER:-}" ]; then
CONFIGURATION="${CONFIGURATION} iuser=${OE_USER}"
fi
if [ -n "${OE_PASS:-}" ]; then
CONFIGURATION="${CONFIGURATION} iuserpass=${OE_PASS}"
fi
if [ -n "${ENCRYPTION_STRATEGY:-}" ]; then
CONFIGURATION="${CONFIGURATION} encryption_strategy=${ENCRYPTION_STRATEGY}"
fi
}
setGlobalSettings() {
# Set requested openemr settings
OPENEMR_SETTINGS=$(printenv | grep '^OPENEMR_SETTING_')
[ -z "${OPENEMR_SETTINGS}" ] && return
echo "${OPENEMR_SETTINGS}" |
while IFS= read -r line; do
SETTING_TEMP=$(echo "${line}" | cut -d "=" -f 1)
# note am omitting the letter O on purpose
# this guarantees that the field we want is the second field
CORRECT_SETTING_TEMP=$(echo "${SETTING_TEMP}" | awk -F 'PENEMR_SETTING_' '{print $2}')
VALUE_TEMP=$(echo "${line}" | awk -F "${CORRECT_SETTING_TEMP}=" '{print $2}')
echo "Set ${CORRECT_SETTING_TEMP} to ${VALUE_TEMP}"
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = '${VALUE_TEMP}' WHERE gl_name = '${CORRECT_SETTING_TEMP}'" "${CUSTOM_DATABASE}"
done
}
resetOpenemr() {
echo "Remove database"
mariadb --skip-ssl -f -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "DROP DATABASE ${CUSTOM_DATABASE}"
echo "Remove database user"
mariadb --skip-ssl -f -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "Drop user '${CUSTOM_USER}'@'%';FLUSH PRIVILEGES;"
echo "Reset couchdb"
rsync --delete --recursive --links /couchdb/original/data /couchdb/
echo "Remove files"
rm -fr /var/www/localhost/htdocs/openemr/sites/*
rsync --delete --recursive --links --exclude .git /openemr/sites /var/www/localhost/htdocs/openemr/
chmod 666 /var/www/localhost/htdocs/openemr/sites/default/sqlconf.php
chown -R apache /var/www/localhost/htdocs/openemr/
}
installOpenemr() {
echo "Re-installing OpenEMR"
cp /root/auto_configure.php /var/www/localhost/htdocs/
php /var/www/localhost/htdocs/auto_configure.php -f "${CONFIGURATION}"
rm -f /var/www/localhost/htdocs/auto_configure.php
}
demoData() {
echo "Install demo data"
mariadb-dump --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" --add-drop-table --no-data "${CUSTOM_DATABASE}" | grep ^DROP | awk ' BEGIN { print "SET FOREIGN_KEY_CHECKS=0;" } { print $0 } END { print "SET FOREIGN_KEY_CHECKS=1;" } ' | mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" < /root/demo_5_0_0_5.sql
upgradeOpenEMR 5.0.0
changeEncodingCollation utf8mb4 utf8mb4_general_ci
}
# parameter 1 is original version
upgradeOpenEMR() {
sed -e "s@!empty(\$_POST\['form_submit'\])@true@" < /var/www/localhost/htdocs/openemr/sql_upgrade.php > /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
sed -i "s@\$form_old_version = \$_POST\['form_old_version'\];@\$form_old_version = '${1}';@" /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
sed -i "1s@^@<?php \$_GET['site'] = 'default'; ?>@" /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
php -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
rm -f /var/www/localhost/htdocs/openemr/sql_upgrade_temp.php
}
sqlDataDrive() {
echo "Installing sql data from drive"
[ -n "${SQL_DATA_DRIVE}" ] || return 0
(
cd "${SQL_DATA_DRIVE}" || exit
# Loop over all sql files inside of the current directory
for f in *.sql ; do
echo "Loading sql data from ${f}"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" < "${f}"
done
)
}
# parameter 1 is identifier
backupOpenemr() {
mkdir -p "/snapshots/${1}"
mariadb-dump --skip-ssl --ignore-table="${CUSTOM_DATABASE}".onsite_activity_view --hex-blob -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" > "/snapshots/${1}/backup.sql"
rsync --delete --recursive --links /var/www/localhost/htdocs/openemr/sites "/snapshots/${1}/"
rsync --delete --recursive --links /couchdb/data "/snapshots/${1}/"
tar -C /snapshots -czf "${1}.tgz" "${1}"
rm -fr "/snapshots/${1}"
}
# parameter 1 is identifier
restoreOpenemr() (
cd /snapshots || exit
tar -C /snapshots -xzf "${1}.tgz"
# need to empty the database before the restore database import
mariadb-dump --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" --add-drop-table --no-data "${CUSTOM_DATABASE}" |
grep ^DROP |
awk ' BEGIN { print "SET FOREIGN_KEY_CHECKS=0;" } { print $0 } END { print "SET FOREIGN_KEY_CHECKS=1;" } ' |
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" "${CUSTOM_DATABASE}" < "/snapshots/${1}/backup.sql"
# note keeping same sqlconf.php in case snapshot is from somewhere else (ie. such as the demo farm) with different credentials
sqlconf=$(cat /var/www/localhost/htdocs/openemr/sites/default/sqlconf.php)
rsync --delete --recursive --links "/snapshots/${1}/sites" /var/www/localhost/htdocs/openemr/
echo "${sqlconf}" > /var/www/localhost/htdocs/openemr/sites/default/sqlconf.php
chown -R apache /var/www/localhost/htdocs/openemr/
if [ -d "/snapshots/${1}/data" ]; then
rsync --delete --recursive --links "/snapshots/${1}/data" /couchdb/
fi
rm -fr "/snapshots/${1}"
)
# parameter 1 is character set
# parameter 2 is collation
# shellcheck disable=SC2016
changeEncodingCollation() {
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER DATABASE `",TABLE_SCHEMA,"` CHARACTER SET = '"${1}"' COLLATE = '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`;' |
grep '^ALTER' |
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e 'SELECT concat("ALTER TABLE `",TABLE_SCHEMA,"`.`",TABLE_NAME,"` CONVERT TO CHARACTER SET '"${1}"' COLLATE '"${2}"';") as _sql FROM `information_schema`.`TABLES` where `TABLE_SCHEMA` like "'"${CUSTOM_DATABASE}"'" and `TABLE_TYPE`="BASE TABLE" group by `TABLE_SCHEMA`, `TABLE_NAME`;' |
grep '^ALTER' |
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}"
}
forceHttps() {
sed -i 's@#RewriteEngine On@ RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
sed -i 's@#RewriteCond %{HTTPS} off@ RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
# shellcheck disable=SC2016
sed -i 's@#RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@ RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
}
unForceHttps() {
sed -i 's@[^#]RewriteEngine On@#RewriteEngine On@' /etc/apache2/conf.d/openemr.conf
sed -i 's@[^#]RewriteCond %{HTTPS} off@#RewriteCond %{HTTPS} off@' /etc/apache2/conf.d/openemr.conf
# shellcheck disable=SC2016
sed -i 's@[^#]RewriteRule (\.\*) https://%{HTTP_HOST}/\$1 \[R,L\]@#RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]@' /etc/apache2/conf.d/openemr.conf
}
# parameter 1 is identifier
setupClientCert() {
if [ ! -d "/certs/${1}" ]; then
mkdir -p "/certs/${1}"
fi
cp "/certs/${1}.zip" "/certs/${1}/"
(
cd "/certs/${1}" &&
unzip "${1}.zip"
)
# server certificate
cp "/certs/${1}/Server.crt" /etc/ssl/certs/customclientbased.cert.pem
rm -f /etc/ssl/certs/webserver.cert.pem
ln -s /etc/ssl/certs/customclientbased.cert.pem /etc/ssl/certs/webserver.cert.pem
# server key
cp "/certs/${1}/Server.key" /etc/ssl/private/customclientbased.key.pem
rm -f /etc/ssl/private/webserver.key.pem
ln -s /etc/ssl/private/customclientbased.key.pem /etc/ssl/private/webserver.key.pem
# ca certificate
cp "/certs/${1}/CertificateAuthority.crt" /etc/ssl/certs/CAcustomclientbased.cert.pem
rm -f /etc/ssl/certs/CAcustomclientbasedwebserver.cert.pem
ln -s /etc/ssl/certs/CAcustomclientbased.cert.pem /etc/ssl/certs/CAcustomclientbasedwebserver.cert.pem
# ca key
cp "/certs/${1}/CertificateAuthority.key" /etc/ssl/private/CAcustomclientbased.key.pem
rm -f /etc/ssl/private/CAcustomclientbasedwebserver.key.pem
ln -s /etc/ssl/private/CAcustomclientbased.key.pem /etc/ssl/private/CAcustomclientbasedwebserver.key.pem
# cleanup
rm -fr "/certs/${1}"
# configure apache
sed -i "s@#SSLVerifyClient@ SSLVerifyClient@" /etc/apache2/conf.d/openemr.conf
sed -i "s@#SSLVerifyDepth@ SSLVerifyDepth@" /etc/apache2/conf.d/openemr.conf
sed -i "s@#SSLOptions@ SSLOptions@" /etc/apache2/conf.d/openemr.conf
sed -i "s@#SSLCACertificateFile@ SSLCACertificateFile@" /etc/apache2/conf.d/openemr.conf
# configure openemr
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = 1 WHERE gl_name = 'is_client_ssl_enabled'" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = '/etc/ssl/certs/CAcustomclientbasedwebserver.cert.pem' WHERE gl_name = 'certificate_authority_crt'" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = '/etc/ssl/private/CAcustomclientbasedwebserver.key.pem' WHERE gl_name = 'certificate_authority_key'" "${CUSTOM_DATABASE}"
}
toggleOnSelfSignedCert() {
# server certificate
rm -f /etc/ssl/certs/webserver.cert.pem
ln -s /etc/ssl/certs/selfsigned.cert.pem /etc/ssl/certs/webserver.cert.pem
# server key
rm -f /etc/ssl/private/webserver.key.pem
ln -s /etc/ssl/private/selfsigned.key.pem /etc/ssl/private/webserver.key.pem
# configure apache
sed -i "s@[^#]SSLVerifyClient@#SSLVerifyClient@" /etc/apache2/conf.d/openemr.conf
sed -i "s@[^#]SSLVerifyDepth@#SSLVerifyDepth@" /etc/apache2/conf.d/openemr.conf
sed -i "s@[^#]SSLOptions@#SSLOptions@" /etc/apache2/conf.d/openemr.conf
sed -i "s@[^#]SSLCACertificateFile@#SSLCACertificateFile@" /etc/apache2/conf.d/openemr.conf
# configure openemr
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = 0 WHERE gl_name = 'is_client_ssl_enabled'" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = '' WHERE gl_name = 'certificate_authority_crt'" "${CUSTOM_DATABASE}"
mariadb --skip-ssl -u "${CUSTOM_USER}" --password="${CUSTOM_PASSWORD}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "UPDATE globals SET gl_value = '' WHERE gl_name = 'certificate_authority_key'" "${CUSTOM_DATABASE}"
}
# parameter 1 is number of random patients to create and import
# parameter 2 has to be either "true" or "false" and is for the development setting in the import_ccda.php script call
importRandomPatients() {
echo "Setting up for following number of random patients (each patient will take several seconds): ${1}"
echo "Development mode: ${2}"
(
if [ ! -d /root/synthea ]; then
echo "Setting up synthea first"
apk update
apk add openjdk11-jre
mkdir /root/synthea
cd /root/synthea || exit
wget https://github.com/synthetichealth/synthea/releases/download/master-branch-latest/synthea-with-dependencies.jar
fi
rm -fr /root/synthea/output
cd /root/synthea &&
java -jar synthea-with-dependencies.jar --exporter.fhir.export false --exporter.ccda.export true --generate.only_alive_patients true -p "${1}"
)
sed -i "s@exit;@//exit;@" /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php
php /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php --sourcePath=/root/synthea/output/ccda --site=default --openemrPath=/var/www/localhost/htdocs/openemr --isDev="${2}"
sed -i "s@//exit;@exit;@" /var/www/localhost/htdocs/openemr/contrib/util/ccda_import/import_ccda.php
echo "Completed run for following number of random patients: ${1}"
}
# parameter 1 is number of multisites to create
generateMultisiteBank() {
echo "Setting up a multisite bank with following number of multisites (labeled run1, run2, run3, ...): run1...run${1}"
# Activate newer versions of InstallerAuto that support environment variable activation.
export OPENEMR_ENABLE_INSTALLER_AUTO=1
# Activate older versions of InstallerAuto that require `sed` activation.
sed -i "s@exit;@//exit;@" /var/www/localhost/htdocs/openemr/contrib/util/installScripts/InstallerAuto.php
a=1
while [ "${a}" -le "${1}" ]; do
run="run${a}"
echo "dropping ${run} sql database, sql user, and directory if they already exist (just ignore any errors that are displayed)"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "DROP DATABASE ${run}"
mariadb --skip-ssl -u "${CUSTOM_ROOT_USER}" --password="${MYSQL_ROOT_PASS}" -h "${MYSQL_HOST}" -P "${CUSTOM_PORT}" -e "DROP USER '${run}'"
rm -fr "/var/www/localhost/htdocs/openemr/sites/${run}"
rm /tmp/setup_dump.sql
echo "adding ${run} multisite"
php /var/www/localhost/htdocs/openemr/contrib/util/installScripts/InstallerAuto.php rootpass="${MYSQL_ROOT_PASS}" server="${MYSQL_HOST}" port="${CUSTOM_PORT}" loginhost=% "login=${run}" "pass=${run}" "dbname=${run}" "site=${run}" source_site_id=default clone_database=yes
chown -R apache "/var/www/localhost/htdocs/openemr/sites/${run}"
find "/var/www/localhost/htdocs/openemr/sites/${run}" -type d -print0 | xargs -0 chmod 500
find "/var/www/localhost/htdocs/openemr/sites/${run}" -type f -print0 | xargs -0 chmod 400
find "/var/www/localhost/htdocs/openemr/sites/${run}/documents" -type d -print0 | xargs -0 chmod 700
find "/var/www/localhost/htdocs/openemr/sites/${run}/documents" -type f -print0 | xargs -0 chmod 700
echo "completed adding ${run} multisite"
a=$(( a + 1 ))
done
sed -i "s@//exit;@exit;@" /var/www/localhost/htdocs/openemr/contrib/util/installScripts/InstallerAuto.php
echo "Completed setting up a multisite bank with following number of multisites (labeled run1, run2, run3, ...): run1...run${1}"
}