-
Notifications
You must be signed in to change notification settings - Fork 4
/
allas-lib
131 lines (108 loc) · 3.43 KB
/
allas-lib
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
# OS spesific tar_extra_options
add_os_tar_extra_options () {
if [ $(uname -s) == 'Darwin' ]; then
tar_extra_options="$tar_extra_options --exclude *.DS_Store"
fi
}
# local file checksum
checksum_tmpdir_tmpfile () {
if [ $(uname -s) == 'Darwin' ]; then
sum=$(md5 -r ${tmp_dir}/$tmp_file | awk '{print $1}')
else
sum=$(md5sum ${tmp_dir}/$tmp_file | awk '{print $1}')
fi
echo "$sum"
}
# make secure temp dir and set the global tmp_dir to it
function make_temp_dir {
if [ $(uname -s) == 'Darwin' ]; then
tmp_dir=$(mktemp -d -t allas-cli-tools)
else
tmp_dir=$(mktemp -d -p ${tmp_root} allas-cli-tools-XXXXXX)
fi
}
# remove tmp_dir if it exists
function clean_temp_dir {
if [ -n "${tmp_dir}" -a -d "${tmp_dir}" ]; then
rm -rf ${tmp_dir}
fi
}
check_swift_connection () {
test=$(rclone about ${storage_server}: 2> /dev/null | wc -l)
if [[ $test -lt 1 ]]
then
if [[ -n "$OS_PASSWORD" ]]; then
if [[ $silent -eq 0 ]] ; then
echo "Updating token"
fi
source $allas_conf_path --user $user -k $OS_PROJECT_NAME --mode $mode -f
fi
#test=$(swift stat 2> /dev/null | grep -c "Account:")
test=$(rclone about ${storage_server}: 2> /dev/null | wc -l)
if [[ $test -lt 1 ]]
then
echo "No connection to ${storage_name}!"
echo "Please try setting up the connection again."
exit 1
else
echo "swift connection updated"
fi
else
echo "swift connection OK"
fi
}
remove_slash_from_ends(){
path_string=$1
if [[ ${path_string:(-1)} == "/" ]]; then
path_string=${path_string%/}
fi
if [[ ${path_string:0:1} == "/" ]]; then
tlen=${#path_string}
path_string=${path_string:1:tlen}
fi
echo $path_string
}
# This function returns the difference between the sum of segment sizes and
# and the expected size. This is needed for allas-health-check
check_segments(){
local segment_server="$1"
local segments_bucket="${2}_segments"
local check_object=$3
local segsizes=($(rclone ls ${segment_server}:${segments_bucket}/${check_object}/ | grep -v "slo/" | sed s/"^ "/""/ | tr "/" " " | awk '{ a = a + $1}END{ print a" "$3 }'))
if [[ ${#segsizes[@]} -eq 2 ]]; then
local ssizesum=${segsizes[0]}
local ssizeval=${segsizes[1]}
(( check_value = ssizesum - ssizeval ))
echo $check_value
else
#No segments found. Assuming OK
echo 0
fi
}
check_segment_sizes(){
local segment_server="$1"
local segments_bucket="${2}_segments"
local check_object=$3
local segsizes=($(rclone ls ${segment_server}:${segments_bucket}/${check_object}/ | tr "/" " " | awk '{ a = a + $1}END{ print a" "$3 }'))
if [[ ${#segsizes[@]} -eq 2 ]]; then
if [[ ${segsizes[0]} -eq ${segsizes[1]} ]]; then
echo "OK"
else
if [[ ${segsizes[0]} -lt ${segsizes[1]} ]]; then
local m="ERROR.
Total size of segments ${segsizes[0]} is less that original file size ${segsizes[1]}.
Some segments are missing?"
echo $m
fi
if [[ ${segsizes[0]} -gt ${segsizes[1]} ]]; then
local m="ERROR. Total size of segments ${segsizes[0]} is more than original file size ${segsizes[1]}.
Check the segments bucket ${segments_bucket} for object ${check_object}."
echo $m
fi
fi
else
#No segments found. Assuming OK
echo "OK"
fi
}
abspath() { old=`pwd`;new=$(dirname "$1");if [ "$new" != "." ]; then cd $new; fi;file=`pwd`/$(basename "$1");cd $old;echo $file; }