-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelpers.sh
136 lines (124 loc) · 4.24 KB
/
helpers.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
function info() {
echo "Info: $@"
}
function fail() {
echo "Failed: $@"
exit 1
}
function test_variable() {
NAME="$1"
test -z "${!NAME}" && fail "Variable $NAME not set" || true
}
function get_init_value() {
FILE="$1"
VALUE="$2"
test -f "$FILE" || fail "Not such file or directory '$FILE'"
cat "$FILE" |awk -v search="^${VALUE}.*" -F '=' '{ if (match ($1, search)) { gsub(/ /, "", $0); gsub(/\r/, "", $0); print $2 } }' | tail -n1
}
function servicemanager() {
service="$1"
action="$2"
test -z $service && fail "service must be given"
test -z $action && fail "action must be given"
export SUDO=""
[ "$(whoami)" != "root" ] && export SUDO="sudo"
# Systemctl
which systemctl > /dev/null 2>&1 && $SUDO systemctl "$action" "$service" && return 0
# Init.d
test -f /etc/init.d/$service && $SUDO /etc/init.d/$service "$action" && return 0
# service
which service > /dev/null 2>&1 && $SUDO service "$service" "$action" && return 0
}
function backup_files() {
sourcedir=$1
destdir=$2
test -z "$sourcedir" && fail "Backup source dir cannot be empty"
test -z "$destdir" && fail "Backup destination dir cannot be empty"
test -d "$sourcedir" || fail "Backup source dir does not exist"
sourcedir="${sourcedir%/}"
destdir="${destdir%/}"
info "Backupping $sourcedir to ${destdir}/"
rsync -aHAX --exclude backups/ --exclude export/ --delete "$sourcedir" "${destdir}/"
}
function restore_file() {
path="$1"
old_base="$2"
new_base="$3"
test -z "$old_base" && fail "Old base must be defined"
test -z "$new_base" && fail "Old base must be defined"
old="${old_base}/${path}"
new="${new_base}/${path}"
test -f "$old" || fail "Old file '$old' must exist"
test -f "$new" && diff "$old" "$new" && true
# Backup distributed version and copy old configuration
if [ -f "$new" ]
then
mv "$new" "${new}.dist"
fi
cp "$old" "$new"
true
}
function latest_version_url() {
if [ -z "$1" ]
then
echo "Error: Invalid number of options in latest_version_url()" 1>&2
exit 1
fi
software="$(echo $1 | tr '[:upper:]' '[:lower:]')"
wget -qO- "https://my.atlassian.com/download/feeds/current/${software}.json" |sed -e 's/downloads(//' -e 's/)$//' |python -m json.tool |awk -F '"' '/tar.gz/ {print $4}' |sort -V |tail -n 1
}
function latest_version() {
if [ -z "$1" ]
then
echo "Error: Invalid number of options in lastest_version()" 1>&2
exit 1
fi
software="$(echo $1 | tr '[:upper:]' '[:lower:]')"
wget -qO- "https://my.atlassian.com/download/feeds/current/${software}.json" |sed -e 's/downloads(//' -e 's/)$//' |python -m json.tool |awk -F '"' '/version/ {print $4}' |sort -V |tail -n 1
}
function get_version_url() {
if [ -z "$1" ] || [ -z "$2" ]
then
echo "Error: Invalid number of options in get_version_url()" 1>&2
exit 1
fi
software="$(echo $1 | tr '[:upper:]' '[:lower:]')"
version="$(echo $2 | sed 's#\.#\\.#g')"
URL=$(wget -qO- "https://my.atlassian.com/download/feeds/archived/${software}.json" |sed -e 's/downloads(//' -e 's/)$//' |python -m json.tool |awk -F '"' '/'${version}'.tar.gz/ {print $4}')
test -z "$URL" && URL=$(wget -qO- "https://my.atlassian.com/download/feeds/current/${software}.json" |sed -e 's/downloads(//' -e 's/)$//' |python -m json.tool |awk -F '"' '/'${version}'.tar.gz/ {print $4}')
echo $URL
}
# http://stackoverflow.com/questions/4023830/bash-how-compare-two-strings-in-version-format
vercomp () {
# Returns 0 if version numbers are equal
# Returns 1 if version number 1 is greater than version number 2
# Returns 2 if version number 1 is smaller than version number 2
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}