-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_script.sh
More file actions
executable file
·290 lines (248 loc) · 6.22 KB
/
build_script.sh
File metadata and controls
executable file
·290 lines (248 loc) · 6.22 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
281
282
283
284
285
#!/bin/bash
# Get PORD_SCRIPT
# GET AUT_SCRIPT
# SIZE_OF
REGION="usa"
ERROR_STATUS=0
SUCCESS_STATUS=1
UAT_SCRIPT_DIR=""
PRODUCT_SCRIPT_DIR=""
#*********************************
# Check if a file exists or not.#
#********************************
function file_exist(){
args="$#"
# check if parameter is provided or not
if (( $args < 1 ))
then
return "${ERROR_STATUS}"
fi
local filename="$1"
# check if the file exists
if [ -e "$filename" ]
then
return "${SUCCESS_STATUS}"
fi
return "${ERROR_STATUS}"
}
#****************************************
# @brief
# the function will get the basename of the file
# @param filename
# @author Obaro I. Johnson
#****************************************
function get_bash_file_basename(){
local filename="$1"
# check if the filename is set or not
if [ ! -z "$filename" ]
then
# trim out the extension .sh from the from of string
filename="${filename%%.sh}"
# trim out other rest of the parts until the last '/'
filename="${filename##*/}"
echo "$filename"
return "${SUCCESS_STATUS}"
fi
return "${ERROR_STATUS}"
}
#***********************************
# @brief
# The function take two parameters filename and the an associate array for out.
# @param filename takes the path to the region_map_file
# @param arr an associate array for output
# @return 0 if the function failed otherwise 1#
#************************************
function parse_region_map_from_file(){
nParams="$#"
if (( $nParams < 2 ))
then
echo "@error: expecting 2 parameter , but $nParams provided"
return "${ERROR_STATUS}" && exit
fi
local filename="$1"
declare -n region_scripts=$2
#check if the file exists or not
file_exist "$filename"
local status=$?
if (($status == $SUCCESS_STATUS))
then
# file exist , read and parse it
# read file data
# we want leading space to be trim , therefore remove IFS=
while read -r line
do
if [ ! -z "$line" ]
then
#split into two array
arr=(`echo $line | sed 's/ /\n/g'`)
region="${arr[0]}"
scripts="${arr[1]}"
region_scripts["$region"]="$scripts"
fi
done < "$filename"
return "${SUCCESS_STATUS}"
else
return "${ERROR_STATUS}"
fi
}
#Function check if the is presented.
function does_region_exist(){
local -n regions0="$1"
local region="$2"
for key in "${!regions0[@]}"
do
if [ "$key" == "$region" ]
then
return "${SUCCESS_STATUS}"
fi
done
return "${ERROR_STATUS}"
}
function parse_region_scripts(){
local nargs="$#"
if ((nargs <= 1))
then
return "${ERROR_STATUS}"
fi
local scripts="$1"
local index=0
declare -n local_scripts_array="$2"
if [ ! -z $scripts ]
then
scripts=$(echo $scripts | tr "^" ",")
while read -d ',' part
do
local_scripts_array["$index"]="$part"
index=$(($index + 1))
done <<< "$scripts"
if [ ! -z "$part" ]
then
local_scripts_array["$index"]="$part"
fi
fi
return "${SUCCESS_STATUS}"
}
function build_region_script(){
nargs="$#"
if (( nargs < 1 ))
then
return "${ERROR_STATUS}"
fi
filename="$1"
local failed="${ERROR_STATUS}"
if [ -e "$filename" ]
then
# build the script with the current steps as usual.
echo "$filename"
else
echo "$filename does not exist"
failed="${SUCCESS_STATUS}"
fi
if (($failed == $SUCCESS_STATUS))
then
return "${ERROR_STATUS}"
fi
return "${SUCCESS_STATUS}"
}
function build_region_scripts(){
nargs=$#
if (( nargs < 2))
then
return "${ERROR_STATUS}"
fi
local filename="$1"
local regionname="$2"
declare -A regions;
parse_region_map_from_file "$filename" regions
status=$?
if (($status == $SUCCESS_STATUS))
then
does_region_exist regions "$regionname"
status=$?
if (($status == $SUCCESS_STATUS))
then
scripts="${regions[$regionname]}"
#create an output script_array that contains the prod, and the uat scripts in order of inputs
declare -a scripts_array
parse_region_scripts "$scripts" scripts_array
for script in "${scripts_array[@]}"
do
# check if the files exists or not
# build aut scripts
if [[ "$script" == *"prod"* ]]
then
path="$PRODUCT_SCRIPT_DIR/$script"
build_region_script "$path"
else
path="$UAT_SCRIPT_DIR/$script"
build_region_script "$path"
fi
done
fi
fi
}
#*******************************************************
# Author Obaro I. Johnson
# Load regions from the region file
# format of region line
# region aut_file,...^prod_file,...
# @return it return a strings of region lines seperated with space
# region1=autfile,...^prod_file,... region2=autfile,...^prod_file,...
#*******************************************************
function load_regions_from_file(){
local nargs="$#"
if(($nargs < 1)); then
return "$ERROR_STATUS"
fi
local filename="$1"
file_exist $filename
status=$?
if(($status < 1)); then
return $status
fi
local -a region_lines
local index=0;
while read -r line
do
if [ -z "$line" ] ; then
continue
fi
line=$(echo "$line" | tr ' ' '=')
region_lines["$index"]="$line";
index=$(($index + 1))
done < "$filename"
results=$( echo "${region_lines[@]}")
results=${results##'\n'}
results=${results%%'\n'}
echo $results
return $SUCCESS_STATUS
}
function does_region_exist(){
nargs=$#
if((nargs < 2)); then
return $ERROR_STATUS
fi
local region="$1"
local regions=($(echo "$2"))
for line in ${regions[@]}
do
lines=($(echo $line | tr '=' ' '))
reg="${lines[0]}"
if [[ $reg == $region ]]; then
return $SUCCESS_STATUS
fi
done
return "${ERROR_STATUS}"
}
filename="./releaseconfig/regions.map"
#build_region_scripts "${filename}" "$REGION"
regions=$(load_regions_from_file $filename)
#can=ir.sh,ir2.sh^ir_prod.sh,ir2_prod.sh
does_region_exist 'usa' "$regions"
status=$?
if(($status <= 0)); then
echo "Failed" && exit 1
fi
echo "Passed"
filename="./releaseconfig/regions.map"
build_region_scripts "${filename}" "$REGION"