forked from JasonGiedymin/container-automation-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.sh
239 lines (204 loc) · 3.93 KB
/
core.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
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
#!/bin/bash
#
# container-automation-lib
# ------------------------
# author: Jason Giedymin
# license: Apache v2
# repo: https://github.com/Amuxbit/container-automation-lib
# version: 1.0.3
#
TMPDIR=${TMPDIR:-/tmp}
LIB_VERBOSE=$([[ $1 == true ]] && echo true || echo false);
LIB_TMP_DIR=$TMPDIR/automation-lib
# Verbose messages
function verbose() {
local cmd=$1
[[ $LIB_VERBOSE == true ]] && $cmd || return 0;
}
function debug() {
printf "\n ---> [$@]\n"
}
# TODO: add color
function infoMsg() {
printf "\n [$@]\n"
}
# TODO: add color
function errorMsg() {
printf "\n [$@]\n"
}
#
# Returns the checksum (via shasum) to stdout.
# Works with directories and files.
#
function checksum() {
local file_dir=$1
if [ ! -e $file_dir ]; then
error "Cannot find specified location $file_dir"
return 1
else
if [ -d $file_dir ]; then
tar c $file_dir | shasum -p | awk '{print $1}'
else
shasum -p $file_dir | awk '{print $1}'
fi
fi;
}
#
# Run a list of functions (map)
#
function run() {
local cmdList=("$@")
for cmd in "${cmdList[@]}"; do
debug "$cmd"
if [ $(type -t $cmd) == "function" ]; then
if $cmd ; then
echo "[$cmd] exited successfully."
else
local rc=$?
echo "Function [$cmd] failed with return code [$rc]"
return $rc;
fi;
else
echo "$cmd not a function!"
return 1
fi;
done;
return 0;
}
#
# Run a single command for each entry of a user supplied list.
# I.e. the command being cpanm, and entries being libraries
#
function runCmd() {
local cmd=$1
shift
local list=("$@")
for entry in "${list[@]}" ; do
$cmd $entry
local rc=$?
if [ $rc -gt 0 ]; then
echo "Function [$cmd] failed with return code [$rc]"
return $rc;
else
echo "[$cmd - $entry] exited successfully."
fi
done
return 0;
}
#
# Run a command in a loop feeding each entry as an arg
#
function runCmdShell() {
local cmd=$1
shift
local list=("$@")
for entry in "${list[@]}" ; do
# echo "Running: $cmd $entry"
shell "$cmd $entry"
local rc=$?
if [ $rc -gt 0 ]; then
echo "Function [$cmd] failed with return code [$rc]"
return $rc;
else
echo "[$cmd - $entry] exited successfully."
fi
done
return 0;
}
#
# My version of trap.
#
function catch() {
local rc=$?
if [ $rc -gt 0 ]; then
echo "Script ran into errors, see above."
exit $rc
fi;
}
function cancel() {
echo "Script canceled by user!"
exit 2;
}
function setTrap() {
trap cancel INT
}
#
# cpanm package installer
#
function runCpanm() {
local packages=("$@")
local cmd="cpanm --notest"
runCmd "$cmd" "${packages[@]}"
}
#
# cpanm package installer from a login shell
#
function runCpanmShell() {
local packages=("$@")
local cmd="cpanm --notest"
runCmdShell "$cmd" "${packages[@]}"
}
#
# Ubuntu apt-get updater, takes a list of packages to install.
#
function updateApt() {
sudo apt-get update -y
}
#
# Ubuntu apt-get installer, takes a list of packages to install.
#
function runApt() {
local packages=("$@")
local cmd="sudo apt-get install -y"
runCmd "$cmd" "${packages[@]}"
}
function cleanApt() {
sudo apt-get clean
}
#
# Runs a command in a login shell
# shell(command)
function shell() {
local command=$1
$SHELL -ilc "$command"
}
#
# Example
# Update ubuntu
# -------------
# aptUpdate() {
# update() {
# sudo apt-get update -y
# }
# install() {
# local packages=("curl" "libssl-dev")
# runApt ${packages[@]}
# }
# local commands=(update install)
# run "${commands[@]}"
# }
#
# Info
# -----
#
info() {
echo "User: [$(whoami)]"
# pushd $USER_HOME
echo "Working directory [$(pwd)]"
echo "Environment: $(env)"
}
#
# Library Init
# ------------
#
init() {
banner() {
verbose "echo Verbose mode: enabled"
}
prep() {
mkdir -p $LIB_TMP_DIR
}
local commands=(banner prep)
run "${commands[@]}"
}
init