forked from BaobabHealthTrust/nlims_controller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure_apps.sh
executable file
·344 lines (316 loc) · 10.7 KB
/
configure_apps.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/bin/bash
# Default IP and port for LOCAL NLIMS
LOCAL_NLIMS_IP="localhost"
LOCAL_NLIMS_PORT="3009"
echo "-----------------------------------------------------------------------------------------"
echo "THIS SCRIPT HELPS IN SETTING UP NLIMS TO INTEGRATE WITH EMR, MASTER NLIMS, IBLIS AND MAHIS"
echo "-----------------------------------------------------------------------------------------"
echo
# Function to generate a random string
generate_random_string() {
echo $(cat /dev/urandom | tr -dc 'a-zA-Z' | fold -w 10 | head -n 1)
}
# Function to validate IP addresses
validate_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'
ip=($ip)
IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
# Function to validate ports
validate_port() {
local port=$1
if [[ $port -ge 1 && $port -le 65535 ]]; then
return 0
else
return 1
fi
}
# Function to prompt for IP and port
prompt_ip_port() {
local ip=""
local port=""
while true; do
read -p "Enter the IP Address for $1: " ip
validate_ip $ip && break
echo "Invalid IP address. Please try again."
done
while true; do
read -p "Enter the Port for $1: " port
validate_port $port && break
echo "Invalid port. Please enter a port between 1 and 65535."
done
eval $2="'$ip'"
eval $3="'$port'"
}
# Function to display entered data and ask for confirmation
confirm_data() {
echo "---------------------------------------"
echo "Please review the entered information:"
echo "---------------------------------------"
echo "Master NLIMS IP Address: $MASTER_IP"
echo "Master NLIMS Port: $MASTER_PORT"
echo "EMR IP Address: $EMR_IP"
echo "EMR Backend Port: $EMR_PORT"
if [[ "${MAHIS_AVAILABLE,,}" == "yes" || "${MAHIS_AVAILABLE,,}" == "y" ]]; then
echo "MAHIS IP Address: $MAHIS_IP"
echo "MAHIS Port: $MAHIS_PORT"
fi
echo "Local NLIMS Password: $LOCAL_NLIMS_PASSWORD"
echo "Default Master NLIMS Password: $DEFAULT_MASTER_NLIMS_PASSWORD"
echo "Master NLIMS Password: $MASTER_NLIMS_PASSWORD"
echo "EMR Password: $EMR_PASSWORD"
if [[ "${MAHIS_AVAILABLE,,}" == "yes" || "${MAHIS_AVAILABLE,,}" == "y" ]]; then
echo "MAHIS Password: $MAHIS_PASSWORD"
fi
if [[ "${IBLIS_AVAILABLE,,}" == "yes" || "${IBLIS_AVAILABLE,,}" == "y" ]]; then
echo "IBLIS Password: $IBLIS_PASSWORD"
fi
echo "---------------------------------------"
read -p "Is this information correct? (yes/no): " CONFIRMATION
if [[ "${CONFIRMATION,,}" == "yes" || "${CONFIRMATION,,}" == "y" ]]; then
echo "Proceeding"
else
echo "Let's re-enter the information."
main
fi
}
# Main function that prompts user for input and confirms before running the script
main() {
# Ask for master IP address and port
prompt_ip_port "Master NLIMS" MASTER_IP MASTER_PORT
# Ask for EMR IP address and port
prompt_ip_port "EMR-Backend" EMR_IP EMR_PORT
# Ask if MAHIS is available
read -p "Is MAHIS available? (yes/no): " MAHIS_AVAILABLE
if [[ "${MAHIS_AVAILABLE,,}" == "yes" || "${MAHIS_AVAILABLE,,}" == "y" ]]; then
prompt_ip_port "MAHIS" MAHIS_IP MAHIS_PORT
else
MAHIS_IP=""
MAHIS_PORT=""
fi
echo '---------------------------------------------------------------'
# Ask if IBLIS is available
read -p "Is IBLIS available? (yes/no): " IBLIS_AVAILABLE
if [[ "${IBLIS_AVAILABLE,,}" == "yes" || "${IBLIS_AVAILABLE,,}" == "y" ]]; then
read -p "Enter the password for IBLIS(Used by IBLIS to Send order to Local NLIMS): " IBLIS_PASSWORD
echo
else
IBLIS_PASSWORD=""
fi
# Ask for passwords for each application
read -p "Enter the password for LOCAL NLIMS(Used sending to send local NLIMS action to Master): " LOCAL_NLIMS_PASSWORD
echo
read -p "Enter the DEFAULT PASSWORD for MASTER NLIMS(Used for account creation): " DEFAULT_MASTER_NLIMS_PASSWORD
echo
read -p "Enter the password for MASTER NLIMS(Used by Master NLIMS for any other action e.g status update to Local NLIMS): " MASTER_NLIMS_PASSWORD
echo
read -p "Enter the password for EMR(Used to send Local NLIMS actions to EMR): " EMR_PASSWORD
echo
if [[ "${MAHIS_AVAILABLE,,}" == "yes" || "${MAHIS_AVAILABLE,,}" == "y" ]]; then
read -p "Enter the password for MAHIS: " MAHIS_PASSWORD
echo
else
MAHIS_PASSWORD=""
fi
# Generate a random string and prepend it to local_nlims_lab_daemon
RANDOM_STRING=$(generate_random_string)
RANDOM_STRINGX=$(generate_random_string)
NEW_LOCAL_NLIMS_USER="${RANDOM_STRING}_local_nlims${RANDOM_STRINGX}_lab_daemon"
EMR_RANDOM_STRING=$(generate_random_string)
NEW_EMR_LOCAL_NLIMS_USER="${EMR_RANDOM_STRING}_emr_local_nlims_lab_daemon"
# Confirm the entered data
confirm_data
# Create config/settings.yml with the passwords
cat <<EOL > config/settings.yml
local_nlims:
default: $DEFAULT_MASTER_NLIMS_PASSWORD
main: $LOCAL_NLIMS_PASSWORD
master_nlims:
default: $DEFAULT_MASTER_NLIMS_PASSWORD
main: $MASTER_NLIMS_PASSWORD
emr: $EMR_PASSWORD
mahis: $MAHIS_PASSWORD
EOL
# Create a file to store usernames and passwords
cat <<EOL > users_credentials.txt
Username: $NEW_LOCAL_NLIMS_USER, Password: $LOCAL_NLIMS_PASSWORD
Username: master_nlims_lab_daemon, Password: $MASTER_NLIMS_PASSWORD
Username: $NEW_EMR_LOCAL_NLIMS_USER, Password: $EMR_PASSWORD
EOL
if [[ "${MAHIS_AVAILABLE,,}" == "yes" || "${MAHIS_AVAILABLE,,}" == "y" ]]; then
echo "Username: mahis_nlims_lab_daemon, Password: $MAHIS_PASSWORD" >> users_credentials.txt
fi
if [[ "${IBLIS_AVAILABLE,,}" == "yes" || "${IBLIS_AVAILABLE,,}" == "y" ]]; then
echo "Username: iblis_nlims_lab_daemon, Password: $IBLIS_PASSWORD" >> users_credentials.txt
fi
# Overwrite the bin/system_config.rb file with the new content
cat <<EOL > bin/system_config.rb
# frozen_string_literal: true
MASTER_IP_ADDRESS = '$MASTER_IP'
Config.find_or_create_by(config_type: 'nlims_host').update(configs: { local_nlims: true })
users = [
{
username: '$NEW_LOCAL_NLIMS_USER',
password: BCrypt::Password.create('$LOCAL_NLIMS_PASSWORD'),
app_name: 'LOCAL NLIMS',
partner: 'EGPAF',
location: 'MALAWI',
token: 'xxxx',
token_expiry_time: '000000000',
app_uuid: '008aa778-af95-42d5-ba54-2f5ddc4f9e78'
},
{
username: 'master_nlims_lab_daemon',
password: BCrypt::Password.create('$MASTER_NLIMS_PASSWORD'),
app_name: 'MASTER NLIMS',
partner: 'EGPAF',
location: 'MALAWI',
token: 'xxxx',
token_expiry_time: '000000000',
app_uuid: 'c1bcdaa3-a835-4481-84dc-92dace6bea59'
},
{
username: '$NEW_EMR_LOCAL_NLIMS_USER',
password: BCrypt::Password.create('$EMR_PASSWORD'),
app_name: 'EMR',
partner: 'EGPAF',
location: 'MALAWI',
token: 'xxxx',
token_expiry_time: '000000000',
app_uuid: 'a6a52ccb-8215-45d8-90dc-826ee12f4055'
}$([ "$MAHIS_AVAILABLE" = "yes" ] && echo ",
{
username: 'mahis_nlims_lab_daemon',
password: BCrypt::Password.create('$MAHIS_PASSWORD'),
app_name: 'MAHIS',
partner: 'DHD',
location: 'MALAWI',
token: 'xxxx',
token_expiry_time: '000000000',
app_uuid: '2d299a0b-327a-4be0-b44d-bd3385303224'
}")$([ "$IBLIS_AVAILABLE" = "yes" ] && echo ",
{
username: 'iblis_nlims_lab_daemon',
password: BCrypt::Password.create('$IBLIS_PASSWORD'),
app_name: 'IBLIS',
partner: 'EGPAF',
location: 'MALAWI',
token: 'xxxx',
token_expiry_time: '000000000',
app_uuid: '7d398a0b-327a-4be0-b44d-bd3385303224'
}")
]
users.each do |user|
puts "Creating user for #{user[:app_name]} app"
user_obj = User.find_by_username(user[:username])
user_obj ||= User.create!(user)
user_obj&.update(user)
end
if Config.local_nlims?
Config.find_or_create_by(config_type: 'master_nlims')
.update(
configs: {
name: 'MASTER NLIMS',
address: "http://$MASTER_IP",
port: $MASTER_PORT,
username: '$NEW_LOCAL_NLIMS_USER'
}
)
Config.find_or_create_by(config_type: 'emr')
.update(
configs: {
name: 'EMR',
address: 'http://$EMR_IP',
port: $EMR_PORT,
username: '$NEW_EMR_LOCAL_NLIMS_USER'
}
)
$([ "$MAHIS_AVAILABLE" = "yes" ] && echo "
Config.find_or_create_by(config_type: 'mahis')
.update(
configs: {
name: 'MAHIS',
address: 'http://$MAHIS_IP',
port: $MAHIS_PORT,
username: 'mahis_nlims_lab_daemon'
}
)")
$([ "$IBLIS_AVAILABLE" = "yes" ] && echo "
Config.find_or_create_by(config_type: 'iblis')
.update(
configs: {
name: 'IBLIS',
address: 'http://localhost',
port: 8005,
username: 'iblis_nlims_lab_daemon'
}
)")
else
Config.find_or_create_by(config_type: 'local_nlims')
.update(
configs: {
name: 'LOCAL NLIMS',
address: 'http://$LOCAL_NLIMS_IP',
port: $LOCAL_NLIMS_PORT,
username: 'master_nlims_lab_daemon'
}
)
end
EOL
echo "Configuration complete! bin/system_config.rb has been updated."
# Run system_config.rb via bundle exec rails
echo "Running system_config.rb..."
bundle exec rails r bin/system_config.rb
echo "--------------------------------------------------------------------"
echo "Configuration Complete! Passwords can be found in the config/settings.yml"
echo "Usernames and passwords saved in users_credentials.txt."
echo "--------------------------------------------------------------------"
echo "Creating user in emr"
bundle exec rake emr:create_user
echo "--------------------------------------------------------------------"
echo "Creating user in master NLIMS"
bundle exec rake master_nlims:create_account
}
master_setup(){
cat <<EOL > bin/system_config.rb
# frozen_string_literal: true
Config.find_or_create_by(config_type: 'nlims_host').update(configs: { local_nlims: false })
Config.find_or_create_by(config_type: 'local_nlims')
.update(
configs: {
name: 'LOCAL NLIMS',
address: 'http://$LOCAL_NLIMS_IP',
port: $LOCAL_NLIMS_PORT,
username: 'master_nlims_lab_daemon'
}
)
EOL
read -p "Enter the password for MASTER NLIMS(Used by Master NLIMS for any other action e.g status update to Local NLIMS): " MASTER_NLIMS_PASSWORD
cat <<EOL > config/settings.yml
local_nlims:
default:
main:
master_nlims:
default:
main: $MASTER_NLIMS_PASSWORD
emr:
mahis:
EOL
echo "Configuration complete! bin/system_config.rb has been updated."
echo "Running system_config.rb..."
bundle exec rails r bin/system_config.rb
}
read -p "Is the setup for local site and not CHSU(Master)? (yes/no): " IS_LOCAL_NLIMS
if [[ "${IS_LOCAL_NLIMS,,}" == "yes" || "${IS_LOCAL_NLIMS,,}" == "y" ]]; then
main
else
master_setup
fi