-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
install.sh
executable file
·296 lines (260 loc) · 8.46 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
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
#!/bin/bash
# --- Function definition --- #
# Display the usage of the script
diplayUsage() {
echo 'Usage: ./install.sh'
echo ' MANDATORY PARAMETERS'
echo ' none'
echo ' OPTIONNAL PARAMETERS'
echo ' -n'
echo ' --no-interaction'
echo ' Run the installation in no interaction mode.'
echo ' --database-host'
echo ' Host of the database. Can be an ipv4 address or an URL. Mandatory if --no-interaction is selected.'
echo ' --database-port'
echo ' Port of the database. Only numbers are allowed. Mandatory if --no-interaction is selected.'
echo ' --database-name'
echo ' Name of the database. Mandatory if --no-interaction is selected.'
echo ' --database-user'
echo ' User to connect to the database. Mandatory if --no-interaction is selected.'
echo ' --database-password'
echo ' Password to connect to the database. Mandatory if --no-interaction is selected.'
echo ' -u'
echo ' --admin-username'
echo ' Username of the Structura administrator. Mandatory if --no-interaction is selected.'
echo ' -p'
echo ' --admin-password'
echo ' Password of the Structura administrator. Mandatory if --no-interaction is selected.'
echo ' -h'
echo ' --help'
echo ' Display this manual.'
}
# --- Check if admin usename or password start with a - and throw an error if so --- #
check_admin_login_data() {
echo 'Checking if admin login is starting with a hyphen'
if [[ $admin_username == -* ]];
then
echo '[ERROR] This tool does not support currently admin username starting with an hyphen.'
echo 'Exiting the script'
exit 2
fi
if [[ $admin_password == -* ]];
then
echo '[ERROR] This tool does not support currently admin password starting with an hyphen.'
echo 'Exiting the script'
exit 2
fi
}
# Ask user input of missing arguments
ask_user_input() {
echo
echo 'Please enter the following information:'
if [ -z "$admin_username" ]
then
read -p 'Structura administrator username : ' admin_username
fi
if [ -z "$admin_password" ]
then
read -sp 'Structura administrator password : ' admin_password
# Since the input is hidden for security reasons, we need to write to the new line
echo
fi
if [ -z "$database_host" ]
then
# Ask and read database_host with default value 127.0.0.1
read -p 'Database host [127.0.0.1] : ' database_host
database_host=${database_host:-'127.0.0.1'}
fi
if [ -z "$database_port" ]
then
# Ask and read database_port with default value 3306
read -p 'Database port [3306] : ' database_port
database_port=${database_port:-3306}
fi
if [ -z "$database_user" ]
then
# Ask and read database_user, no default value
read -p 'Database user : ' database_user
fi
if [ -z "$database_password" ]
then
# Ask and read database_password with no default value
read -sp 'Database password : ' database_password
# Since the input is hidden for security reasons, we need to write to the new line
echo
fi
if [ -z "$database_name" ]
then
# Ask and read database_name with default value erp-asso-test
read -p 'Database name [structura] : ' database_name
database_name=${database_name:-structura}
fi
}
# Create a .env.local file
setup_env_file() {
env_file_name=".env.local"
# Creation the file
touch $env_file_name
# Content is writen in the file
echo "###> doctrine/doctrine-bundle ###" >> $env_file_name
echo "DATABASE_URL=$database_url" >> $env_file_name
echo "###< doctrine/doctrine-bundle ###" >> $env_file_name
echo "" >> $env_file_name
echo "###> symfony/swiftmailer-bundle ###" >> $env_file_name
echo "MAILER_URL=null://localhost" >> $env_file_name
echo "###< symfony/swiftmailer-bundle ###" >> $env_file_name
}
# --- Parsing arguments --- #
for i in "$@"
do
case $i in
-h*)
help=1
;;
--help*)
help=1
;;
-n*)
no_interaction=1
;;
--no-interaction*)
no_interaction=1
;;
-u=*)
admin_username="${i#*=}"
shift # past argument=value
;;
--admin-username=*)
admin_username="${i#*=}"
shift # past argument=value
;;
-p=*)
admin_password="${i#*=}"
shift # past argument=value
;;
--admin-password=*)
admin_password="${i#*=}"
shift # past argument=value
;;
--database-host=*)
database_host="${i#*=}"
shift # past argument=value
;;
--database-port=*)
database_port="${i#*=}"
shift # past argument=value
;;
--database-user=*)
database_user="${i#*=}"
shift # past argument=value
;;
--database-password=*)
database_password="${i#*=}"
shift # past argument=value
;;
--database-name=*)
database_name="${i#*=}"
shift # past argument=value
;;
*)
# unknown argument or option
;;
esac
done
# --- Checking if the user needs help --- #
if [ "$help" == 1 ]
then
diplayUsage
exit
fi
# --- No interaction mode --- #
if [ "$no_interaction" == 1 ]
then
if [ -z "$admin_username" ]
then
echo 'Missing administrator username argument.'
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$admin_password" ]
then
echo 'Missing administrator password argument.'
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$database_host" ]
then
echo "Argument database-host is mandatory."
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$database_port" ]
then
echo "Argument database-port is mandatory."
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$database_user" ]
then
echo "Argument database-user is mandatory."
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$database_password" ]
then
echo "Argument database-password is mandatory."
diplayUsage
echo 'Exiting the script'
exit 2
fi
if [ -z "$database_name" ]
then
echo "Argument database-name is mandatory."
diplayUsage
echo 'Exiting the script'
exit 2
fi
fi
# --- Setup .env file --- #
# --- No interaction mode --- #
if [ "$no_interaction" == 1 ]
then
# Concatening previous data to form the database connection string (also named url) for doctrine
database_url="mysql://${database_user}:${database_password}@${database_host}:${database_port}/${database_name}"
# Generating env file
setup_env_file
# --- Interactive mode --- #
else
# Asking user environment informations
ask_user_input
# Concatening previous data to form the database connection string (also named url) for doctrine
database_url="mysql://${database_user}:${database_password}@${database_host}:${database_port}/${database_name}"
# Generating env and php unit files
setup_env_file
fi
# --- Check admin data --- #
check_admin_login_data || exit
# --- Install using Composer --- #
echo 'Installing project dependencies...'
composer install || exit
# --- Create database --- #
bin/console doctrine:database:create || exit
# --- Create database schema --- #
bin/console doctrine:migration:migrate -n || exit
# --- Create admin account --- #
bin/console app:create-admin-account "${admin_username}" "${admin_password}" || exit
# --- Display warning message about Messages worker --- #
# --- Define font settings used to display messages --- #
defaultFontSettings='\033[0m'
orangeBackground='\e[48;5;202m'
bold='\033[1m'
echo ''
echo -e "${orangeBackground}${bold}[ WARNING ]${defaultFontSettings} This software needs the Symfony messenger deamon to run properly."
echo -e "${orangeBackground}${bold}[ WARNING ]${defaultFontSettings} You need to run it using: ${bold}bin/console messenger:consume${defaultFontSettings}"
echo -e "${orangeBackground}${bold}[ WARNING ]${defaultFontSettings} Make sure to relaunch it if you reboot your server."
exit