-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.sh
177 lines (153 loc) · 2.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
#!/bin/bash
#
# container-automation-lib
# ------------------------
# author: Jason Giedymin
# license: Apache v2
# repo: https://github.com/Amuxbit/container-automation-lib
# version: 1.0.1
#
function debug() {
printf "\n ---> [$@]\n"
}
#
# Run a list of functions (map)
#
function run() {
local cmdList=("$@")
for cmd in "${cmdList[@]}" ; do
debug "$cmd"
$cmd
local rc=$?
if [ $rc -gt 0 ]; then
echo "Function [$cmd] failed with return code [$rc]"
return $rc;
else
echo "[$cmd] exited successfully."
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
# echo "Running $cmd"
$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)"
}