This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions
297 lines (216 loc) · 6.55 KB
/
functions
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
# -*- mode: sh -*-
#
# Copyright (c) 2016 - Present Jeong Han Lee
# Copyright (c) 2016 - Present European Spallation Source ERIC
#
# The program is free software: you can redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of the
# License, or any newer version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see https://www.gnu.org/licenses/gpl-2.0.txt
#
# Author : Jeong Han Lee
# Email : jeonghan.lee@gmail.com
# Date : Thursday, March 30 13:55:51 CEST 2017
# Version : 0.0.5
ROOT_UID=0
E_NOTROOT=101
EXIST=1
NON_EXIST=0
function pushd() { builtin pushd "$@" > /dev/null; }
function popd() { builtin popd "$@" > /dev/null; }
function __ini_func() { printf "\n>>>> You are entering in : %s\n" "${1}"; }
function __end_func() { printf "\n<<<< You are leaving from : %s\n" "${1}"; }
function __checkstr() {
if [ -z "$1" ]; then
printf "%s : input variable is not defined \n" "${FUNCNAME[*]}"
exit 1;
fi
}
function getHostname() {
local hostname_cmd="$(hostname)";
}
# Generic : git_clone
# 1.0.3 Tuesday, November 8 18:13:44 CET 2016
#
# Required Global Variable
# - SC_LOGDATE : Input
function git_clone() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local git_src_dir=$1;
local git_src_url=$2;
local git_src_name=$3;
local tag_name=$4;
__checkstr ${SC_LOGDATE};
if [[ ! -d ${git_src_dir} ]]; then
printf "No git source repository in the expected location %s\n" "${git_src_dir}";
else
printf "Old git source repository in the expected location %s\n" "${git_src_dir}";
printf "The old one is renamed to %s_%s\n" "${git_src_dir}" "${SC_LOGDATE}";
mv ${git_src_dir} ${git_src_dir}_${SC_LOGDATE}
fi
# Always fresh cloning ..... in order to workaround any local
# modification in the repository, which was cloned before.
#
# we need the recursive option in order to build a web based viewer for Archappl
if [ -z "$tag_name" ]; then
git clone --recursive "${git_src_url}/${git_src_name}" "${git_src_dir}";
else
git clone --recursive -b "${tag_name}" --single-branch --depth 1 "${git_src_url}/${git_src_name}" "${git_src_dir}";
fi
__end_func ${func_name};
}
# Generic : git_selection
#
# 1.0.4 : Saturday, December 31 17:14:42 CET 2016
# only for archappl
#
# Require Global vairable
# - SC_SELECTED_GIT_SRC : Output
#
function git_selection() {
local func_name=${FUNCNAME[*]}; __ini_func ${func_name};
local git_ckoutcmd=""
local checked_git_src=""
declare -i index=0
declare -i master_index=0
declare -i list_size=0
declare -i selected_one=0
declare -a git_src_list=()
local n_tags=${1};
# no set n_tags, set default 10
if [[ ${n_tags} -eq 0 ]]; then
n_tags=20;
fi
git_src_list+=("master")
git_src_list+=($(git tag -l | xargs -I@ git log --format=format:"%ai @%n" -1 @ | sort -r | head -n${n_tags} | awk '{print $4}'))
for tag in "${git_src_list[@]}"
do
printf "%2s: git src %34s\n" "$index" "$tag"
let "index = $index + 1"
done
echo -n "Select master or one of tags which can be built, followed by [ENTER]:"
# don't wait for 3 characters
# read -e -n 2 line
read -e line
# convert a string to an integer?
# do I need this?
# selected_one=${line/.*}
# Without selection number, type [ENTER], 0 is selected as default.
#
selected_one=${line}
let "list_size = ${#git_src_list[@]} - 1"
if [[ "$selected_one" -gt "$list_size" ]]; then
printf "\n>>> Please select one number smaller than %s\n" "${list_size}"
exit 1;
fi
if [[ "$selected_one" -lt 0 ]]; then
printf "\n>>> Please select one number larger than 0\n"
exit 1;
fi
SC_SELECTED_GIT_SRC="$(tr -d ' ' <<< ${git_src_list[line]})"
printf "\n>>> Selected %34s --- \n" "${SC_SELECTED_GIT_SRC}"
echo ""
if [ "$selected_one" -ne "$master_index" ]; then
git_ckoutcmd="git checkout tags/${SC_SELECTED_GIT_SRC}"
$git_ckoutcmd
checked_git_src="$(git describe --exact-match --tags)"
checked_git_src="$(tr -d ' ' <<< ${checked_git_src})"
printf "\n>>> Selected : %s --- \n>>> Checkout : %s --- \n" "${SC_SELECTED_GIT_SRC}" "${checked_git_src}"
if [ "${SC_SELECTED_GIT_SRC}" != "${checked_git_src}" ]; then
echo "Something is not right, please check your git reposiotry"
exit 1
fi
else
git_ckoutcmd="git checkout ${SC_SELECTED_GIT_SRC}"
$git_ckoutcmd
fi
git submodule update --init --recursive
__end_func ${func_name}
}
function checkIfRoot() {
if [[ $(id -u) -ne "$ROOT_UID" ]] ; then
echo "Please run it as root or with sudo" ;
exit $E_NOTROOT ;
fi
}
function checkIfArchappl() {
local pid_list=()
local no_archappl_running=0
IFS=' ' read -r -a pid_list <<< $(/sbin/pidof jsvc.exec);
# echo "${pid_list[@]}"
# echo "${#pid_list[@]}"
if [[ "${#pid_list[@]}" -ne "$no_archappl_running" ]] ; then
printf "Total %d processes are founded now.\n" "${#pid_list[@]}"
printf "Please stop the running archiver appliance first!\n"
exit;
fi
}
function checkIfDir() {
local dir=$1
local result=""
if [ ! -d "$dir" ]; then
result=$NON_EXIST
# doesn't exist
else
result=$EXIST
# exist
fi
echo "${result}"
}
function checkIfFile() {
local file=$1
local result=""
if [ ! -e "$file" ]; then
result=$NON_EXIST
# doesn't exist
else
result=$EXIST
# exist
fi
echo "${result}"
}
function checkIfVar() {
local var=$1
local result=""
if [ -z "$var" ]; then
result=$NON_EXIST
# doesn't exist
else
result=$EXIST
# exist
fi
echo "${result}"
}
# This function is really dangerous
# use it at your risk.
function removeAllFiles() {
local dir=$1
if [ -z "$dir" ]; then
printf "%s should be defined fisrt\n";
exit
fi
if [ ! -d "$dir" ]; then
echo "No $dir"
exit
else
dir1=/tmp/dummpy_$RANDOM
mkdir -p $dir1
rsync -a --delete $dir1 $dir
rm -rf $dir1
rm -rf $dir
fi
}
function command_exists() {
command -v "$1" >/dev/null 2>&1
}
function get_ip_address() {
ip -4 route get 8.8.8.8 | awk {'print $7'} | tr -d '\n'
}