-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmigrate_validate.sh
executable file
·282 lines (255 loc) · 8.07 KB
/
migrate_validate.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
#!/bin/bash
check_length(){
len_status=1
str_length=${#1}
if [[ $str_length -ge 3 && $str_length -le 63 ]]; then
len_status=0
return $len_status;
else
return $len_status;
fi
}
check_ip()
{
local ip=$2
ip_stat=1
ip_pass=0
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,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 ]]
ip_stat=$?
if [[ ! $ip_stat == 0 ]]; then
echo "Error - Invalid value for $key"; fail=1
ip_pass=0
fi
else
echo "Error - Invalid value for $key"; fail=1
fi
}
check_base_dir(){
if [[ ! "$2" = /* ]] || [[ ! -d $2 ]]; then
echo "Error - $1 Please enter the absolute path or make sure the directory is present."; fail=1
fi
}
check_sys_user(){
result=`who | head -1 | awk '{print $1}'`
if [[ `egrep -i ^$2: /etc/passwd ; echo $?` != 0 && $result != $2 ]]; then
echo "Error - Please check the system_user_name."; fail=1
fi
}
check_db_naming(){
check_length $2
if [[ $? == 0 ]]; then
if [[ ! $2 =~ ^[A-Za-z_]*[^_0-9\$\@\#\%\*\-\^\?]$ ]]; then
echo "Error - Naming convention is not correct. Please change the value of $1."; fail=1
fi
else
echo "Error - Length of the value $1 is not correct. Provide the length between 3 and 63."; fail=1
fi
}
check_storage_type(){
if [[ $mode_of_installation == "localhost" ]]; then
if [[ ! $2 == "local" ]]; then
echo "Error - Please provide storage type as local for localhost installation"; fail=1
fi
fi
if [[ $mode_of_installation == "public" ]]; then
if ! [[ $2 == "s3" || $2 == "local" ]]; then
echo "Error - Please enter either s3 or local for $1"; fail=1
fi
fi
}
check_mode_of_installation(){
if ! [[ $2 == "localhost" || $2 == "public" ]]; then
echo "Error - Please enter either localhost or public for $1"; fail=1
fi
}
check_aws_key(){
aws_key_status=0
export AWS_ACCESS_KEY_ID=$1
export AWS_SECRET_ACCESS_KEY=$2
aws s3api list-buckets > /dev/null 2>&1
if [ ! $? -eq 0 ]; then echo "Error - Invalid aws access or secret keys"; fail=1
aws_key_status=1
fi
}
check_s3_bucket(){
if [[ $aws_key_status == 0 ]]; then
bucketstatus=`aws s3api head-bucket --bucket "${2}" 2>&1`
if [ ! $? == 0 ]
then
echo "Error - [ $1 : $2 ] Bucket not owned or not found. Please change the bucket name in aws_s3_config.yml"; fail=1
fi
fi
}
check_aws_default_region(){
region_len=${#2}
if [[ $region_len -ge 9 ]] && [[ $region_len -le 15 ]]; then
curl https://s3.$2.amazonaws.com > /dev/null 2>&1
if [[ ! $? == 0 ]]; then
echo "Error - There is a problem reaching the aws default region. Please check the $1 value." ; fail=1
fi
fi
}
check_directory(){
if [[ ! "$2" = /* ]] || [[ ! -d $2 ]]; then
echo "Error - $1 Please enter the absolute path or make sure the directory is present."; fail=1
fi
dir_owner=`stat -c '%U' $2`
#if ! [[ $dir_owner == $system_user_name ]]; then
# echo "Error - $1 directory owner not matchiing."; fail=1
#fi
if ! [[ -r "$2" ]]; then
echo "Error - '$1' please give read permission to directory."; fail=1
fi
if ! [[ -w "$2" ]]; then
echo "Error - '$1' please give write permission to directory."; fail=1
fi
if ! [[ -x "$2" ]]; then
echo "Error - '$1' please give execute permission to directory."; fail=1
fi
if ! [[ "$2" = */ ]]; then
echo "Error - $1 Please make sure the absolute path values should end with '/'"; fail=1
fi
}
get_config_values(){
key=$1
vals[$key]=$(awk ''/^$key:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
}
bold=$(tput bold)
normal=$(tput sgr0)
fail=0
if [[ ! $# -eq 0 ]]; then
core_install=$1
else
core_install="NA"
fi
bold=$(tput bold)
normal=$(tput sgr0)
fail=0
if [[ ! $# -eq 0 ]]; then
core_install=$1
else
core_install="NA"
fi
echo -e "\e[0;33m${bold}Validating the config file...${normal}"
# An array of mandatory values
declare -a arr=("installation_host_ip" "system_user_name" "base_dir" "db_user" "db_name" "storage_type" "mode_of_installation" "s3_access_key" "s3_secret_key" "aws_default_region" "s3_output_bucket" "output_directory")
# Create and empty array which will store the key and value pair from config file
declare -A vals
installation_host_ip=$(awk ''/^installation_host_ip:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
system_user_name=$(awk ''/^system_user_name:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
base_dir=$(awk ''/^base_dir:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
mode_of_installation=$(awk ''/^mode_of_installation:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
storage_type=$(awk ''/^storage_type:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
aws_access_key=$(awk ''/^s3_access_key:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
aws_secret_key=$(awk ''/^s3_secret_key:' /{ if ($2 !~ /#.*/) {print $2}}' migrate_config.yml)
# Iterate the array and retrieve values for mandatory fields from config file
for i in ${arr[@]}
do
get_config_values $i
done
for i in ${arr[@]}
do
key=$i
value=${vals[$key]}
case $key in
installation_host_ip)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_ip $key $value
fi
;;
system_user_name)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_sys_user $key $value
fi
;;
base_dir)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_base_dir $key $value
fi
;;
db_user)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_db_naming $key $value
fi
;;
db_name)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_db_naming $key $value
fi
;;
storage_type)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_storage_type $key $value
fi
;;
mode_of_installation)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_mode_of_installation $key $value
fi
;;
s3_access_key)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
fi
;;
s3_secret_key)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_aws_key $aws_access_key $aws_secret_key
fi
;;
aws_default_region)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check. Recommended value is ap-south-1"; fail=1
else
check_aws_default_region
fi
;;
s3_output_bucket)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_s3_bucket $key $value
fi
;;
output_directory)
if [[ $value == "" ]]; then
echo "Error - in $key. Unable to get the value. Please check."; fail=1
else
check_directory $key $value
fi
;;
*)
if [[ $value == "" ]]; then
echo -e "\e[0;31m${bold}Error - Value for $key cannot be empty. Please fill this value${normal}"; fail=1
fi
;;
esac
done
if [[ $fail -eq 1 ]]; then
echo -e "\e[0;34m${bold}Config file has errors. Please rectify the issues and restart the upgradation${normal}"
exit 1
else
echo -e "\e[0;32m${bold}Config file successfully validated${normal}"
fi