-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·169 lines (142 loc) · 4.81 KB
/
common.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
#!/usr/bin/env bash
set -Eeuo pipefail
guard_bash_error () {
set -Eeuo pipefail
}
# Log levels
INFO=0
WARN=1
ERROR=2
FATAL=3
DEBUG=4
DEFAULT_LOG_LEVEL=${ERROR}
my_exit () {
echo "EXIT: - [HOST:$(hostname)]: - $(date +"%Y-%m-%d %H:%M:%S") - $1"
exit "$2"
}
msg () {
if [ "$1" -le ${DEFAULT_LOG_LEVEL} ]; then
echo "[HOST:$(hostname)]: - $(date +"%Y-%m-%d %H:%M:%S") - $2"
fi
}
info () {
msg ${INFO} "INFO: - $1"
}
warn () {
msg ${WARN} "WARNING: - $1"
}
error () {
msg ${ERROR} "ERROR: - $1"
}
fatal () {
msg ${FATAL} "FATAL: - $1"
}
debug () {
msg ${DEBUG} "DEBUG: - $1"
}
begin_banner () {
info "$1 - $2 phase - begin"
}
done_banner () {
info "$1 - $2 phase - done"
}
### turn path within script into absolute path
### must pass the calling string of the script as the first parameter
### e.g., ./path_to_script/script.sh
### or, /root/path_to_script/script.sh
### return the absolute path to the script with "echo" command
turn_to_absolute_path () {
local SCRIPT_ABS_PATH_RAW
SCRIPT_ABS_PATH_RAW="$(dirname "$1")"
# turn SCRIPT_ABS_PATH into absolute path
case ${SCRIPT_ABS_PATH_RAW} in
/*) echo "${SCRIPT_ABS_PATH_RAW}" ;;
\.\.*) echo "$PWD/${SCRIPT_ABS_PATH_RAW}" ;;
\.*) echo "$PWD/${SCRIPT_ABS_PATH_RAW}" ;;
*) echo "$PWD" ;;
esac
}
### change CD to up to the project root directory
### must pass the absolute path to the script as the first parameter
change_CD_to_project_root () {
cd "$1"
local up_level=..
local my_loop=10 # guard not to loop forever
until [ -f "${up_level}/cook.sh" ] && [ ${my_loop} -gt 0 ]
do
up_level=${up_level}/..
my_loop=$((my_loop - 1))
done
if [ ${my_loop} -eq 0 ]; then
my_exit "Too many level up within the searching for DevOps directory,abort." 1
fi
cd "$1/${up_level}"
}
### check OS and distribution
### return the OS distribution and ID with "echo" command
check_dist_or_OS () {
local MY_THE_DISTRIBUTION_ID=""
local MY_THE_DISTRIBUTION_VERSION=""
if [ -e /etc/os-release ]; then
MY_THE_DISTRIBUTION_ID=$(grep -w "ID" /etc/os-release |awk -F"=" '{print $NF}'|sed 's/"//g')
if [ "${MY_THE_DISTRIBUTION_ID}" == "ubuntu" ]; then
MY_THE_DISTRIBUTION_VERSION=$(grep -w "VERSION_ID" /etc/os-release |awk -F"=" '{print $NF}'|sed 's/"//g')
else
MY_THE_DISTRIBUTION_VERSION=$(grep -w "VERSION_ID" /etc/os-release |awk -F"=" '{print $NF}'|awk -F"." '{print $1}'|sed 's/"//g')
fi
echo "${MY_THE_DISTRIBUTION_ID} ${MY_THE_DISTRIBUTION_VERSION}"
else
if type uname > /dev/null 2>&1; then
MY_THE_DISTRIBUTION_ID=$(uname -s)
MY_THE_DISTRIBUTION_VERSION=$(uname -r)
echo "${MY_THE_DISTRIBUTION_ID} ${MY_THE_DISTRIBUTION_VERSION}"
else
echo ""
fi
fi
}
### guard that the caller of the script must be root or has sudo right
guard_root_or_sudo () {
if [[ $EUID -gt 0 ]] && ! sudo echo >/dev/null 2>&1; then
return 1
else
return 0
fi
}
### init script with check if root or sudo
init_with_root_or_sudo () {
guard_bash_error
if ! guard_root_or_sudo; then
my_exit "You must be root or you must be sudoer to prepare the env for CI/CD." 1
fi
SCRIPT_ABS_PATH=$(turn_to_absolute_path "$0")
# change_CD_to_project_root ${SCRIPT_ABS_PATH}
THE_DISTRIBUTION_ID_VERSION=$(check_dist_or_OS)
THE_DISTRIBUTION_ID=$(echo "${THE_DISTRIBUTION_ID_VERSION}"|awk '{print $1}')
THE_DISTRIBUTION_VERSION=$(echo "${THE_DISTRIBUTION_ID_VERSION}"|awk '{print $2}')
}
### init script without check if root or sudo
init_without_root_or_sudo () {
guard_bash_error
SCRIPT_ABS_PATH=$(turn_to_absolute_path "$0")
# change_CD_to_project_root ${SCRIPT_ABS_PATH}
THE_DISTRIBUTION_ID_VERSION=$(check_dist_or_OS)
THE_DISTRIBUTION_ID=$(echo "${THE_DISTRIBUTION_ID_VERSION}"|awk '{print $1}')
THE_DISTRIBUTION_VERSION=$(echo "${THE_DISTRIBUTION_ID_VERSION}"|awk '{print $2}')
}
get_last_stable_nix_channel () {
local MY_CHANNEL_NAME_REGEX=""
case ${THE_DISTRIBUTION_ID} in
debian|ubuntu|rhel|centos) MY_CHANNEL_NAME_REGEX='s/.*\(nixos-[0-9][0-9].[0-9][0-9]\).*/\1/p' ;;
Darwin) MY_CHANNEL_NAME_REGEX='s/.*\(nixpkgs-[0-9][0-9].[0-9][0-9]-darwin\).*/\1/p' ;;
*) ;;
esac
local MY_LAST_NIX_STABLE_CHANNEL
MY_LAST_NIX_STABLE_CHANNEL=$(git ls-remote --heads https://github.com/NixOS/nixpkgs | awk '{print $NF}' | awk -F"/" '{print $NF}' | grep -v "\-unstable" | grep -v "\-small" | sed -n "${MY_CHANNEL_NAME_REGEX}" | sort | tail -1)
echo "${MY_LAST_NIX_STABLE_CHANNEL}"
}
switch_to_last_stable_nix_channel () {
nix-channel --remove nixpkgs
nix-channel --add "https://nixos.org/channels/$(get_last_stable_nix_channel)" nixpkgs
nix-channel --update
}