-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlxc-to-go-template.sh
executable file
·285 lines (258 loc) · 7.98 KB
/
lxc-to-go-template.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/bin/sh
### LICENSE - (BSD 2-Clause) // ###
#
# Copyright (c) 2015, Daniel Plominski (Plominski IT Consulting)
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice, this
# list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
### // LICENSE - (BSD 2-Clause) ###
### ### ### PLITC // ### ### ###
### stage0 // ###
DEBIAN=$(grep -s "ID=" /etc/os-release | egrep -v "VERSION" | sed 's/ID=//g')
DEBVERSION=$(grep -s "VERSION_ID" /etc/os-release | sed 's/VERSION_ID=//g' | sed 's/"//g')
DEBTESTVERSION=$(grep -s "PRETTY_NAME" /etc/os-release | awk '{print $3}' | sed 's/"//g' | grep -c "stretch/sid")
MYNAME=$(whoami)
PRG="$0"
##/ need this for relative symlinks
while [ -h "$PRG" ] ;
do
PRG=$(readlink "$PRG")
done
DIR=$(dirname "$PRG")
#
ADIR="$PWD"
#// function: run script as root
checkrootuser()
{
if [ "$(id -u)" != "0" ]; then
echo "[ERROR] This script must be run as root" 1>&2
exit 1
fi
}
#// function: check debian based distributions
checkdebiandistribution()
{
if [ "$DEBVERSION" = "7" ]; then
: # dummy
else
if [ "$DEBVERSION" = "8" ]; then
: # dummy
else
if [ "$DEBTESTVERSION" = "1" ]; then
: # dummy
else
if [ "$DEBIAN" = "linuxmint" ]; then
: # dummy
else
if [ "$DEBIAN" = "ubuntu" ]; then
: # dummy
else
if [ "$DEBIAN" = "devuan" ]; then
: # dummy
else
echo "[ERROR] We currently only support: Debian 7,8,9 (testing) / Linux Mint Debian Edition (LMDE 2 Betsy) / Ubuntu Desktop 15.10+ and Devuan"
exit 1
fi
fi
fi
fi
fi
fi
}
#// FUNCTION: check state (Version 1.0)
checkhard() {
if [ $? -eq 0 ]
then
echo "[$(printf "\033[1;32m OK \033[0m\n")] '"$@"'"
else
echo "[$(printf "\033[1;31mFAILED\033[0m\n")] '"$@"'"
sleep 1
exit 1
fi
}
#// FUNCTION: check state without exit (Version 1.0)
checksoft() {
if [ $? -eq 0 ]
then
echo "[$(printf "\033[1;32m OK \033[0m\n")] '"$@"'"
else
echo "[$(printf "\033[1;33mFAILED\033[0m\n")] '"$@"'"
sleep 1
fi
}
#// FUNCTION: check state hidden (Version 1.0)
checkhiddenhard() {
if [ $? -eq 0 ]
then
return 0
else
#/return 1
checkhard "$@"
return 1
fi
}
#// FUNCTION: check state hidden without exit (Version 1.0)
checkhiddensoft() {
if [ $? -eq 0 ]
then
return 0
else
#/return 1
checksoft "$@"
return 1
fi
}
### // stage0 ###
### stage1 // ###
if [ "$DEBIAN" = "debian" -o "$DEBIAN" = "linuxmint" -o "$DEBIAN" = "ubuntu" -o "$DEBIAN" = "devuan" ]
then
: # dummy
else
echo "[ERROR] Plattform = unknown"
exit 1
fi
### stage2 // ###
checkrootuser
checkdebiandistribution
### // stage2 ###
#
### stage3 // ###
#
CHECKLXCINSTALL=$(/usr/bin/which lxc-checkconfig)
if [ -z "$CHECKLXCINSTALL" ]; then
echo "" # dummy
printf "\033[1;31mLXC 'managed' doesn't run, execute the 'bootstrap' command at first\033[0m\n"
exit 1
fi
checkhard lxc-to-go environment - stage 1
DIALOG=$(/usr/bin/which dialog)
if [ -z "$DIALOG" ]; then
echo "<--- --- --->"
echo "need dialog"
echo "<--- --- --->"
apt-get update
apt-get -y install dialog
echo "<--- --- --->"
fi
checkhard lxc-to-go environment - stage 2
#
### stage4 // ###
#
### ### ### ### ### ### ### ### ###
CHECKBRIDGE1=$(ifconfig | grep -c "vswitch0")
if [ "$CHECKBRIDGE1" = "0" ]; then
### ### ### ### ### ### ### ### ###
echo "" # printf
printf "\033[1;31mCan't find the Bridge Zones, execute the 'bootstrap' command at first\033[0m\n"
exit 1
### ### ### ### ### ### ### ### ###
fi
checkhard lxc-to-go environment - stage 3
CHECKLXCCONTAINER=$(lxc-ls | tr ' ' '\n' | egrep -c "managed|deb7template|deb8template")
if [ "$CHECKLXCCONTAINER" = "3" ]; then
: # dummy
else
### ### ### ### ### ### ### ### ###
echo "" # printf
printf "\033[1;31mCan't find all nessessary default lxc container, delete the 'managed|deb7template|deb8template' lxc and execute the 'bootstrap' command again\033[0m\n"
exit 1
### ### ### ### ### ### ### ### ###
fi
checkhard lxc-to-go environment - stage 4
### TEMPLATE // ###
LISTTEMPLATEFILE1="/etc/lxc-to-go/tmp/choose_templ1.tmp"
LISTTEMPLATEFILE2="/etc/lxc-to-go/tmp/choose_templ2.tmp"
LISTTEMPLATEFILE3="/etc/lxc-to-go/tmp/choose_templ3.tmp"
LISTTEMPLATEFILE4="/etc/lxc-to-go/tmp/choose_templ4.tmp"
LISTTEMPLATEFILE5="/etc/lxc-to-go/tmp/choose_templ5.tmp"
#/ls -tr "$DIR"/hooks/templates/ > "$LISTTEMPLATEFILE1"
ls -r "$DIR"/hooks/templates/ > "$LISTTEMPLATEFILE1"
nl "$LISTTEMPLATEFILE1" | sed 's/ //g' > "$LISTTEMPLATEFILE2"
/bin/sed 's/$/ off/' "$LISTTEMPLATEFILE2" > "$LISTTEMPLATEFILE3"
checkhard lxc-to-go template - stage 1
dialog --radiolist "Choose one template:" 45 80 60 --file "$LISTTEMPLATEFILE3" 2>"$LISTTEMPLATEFILE4"
list1=$?
case $list1 in
0)
echo "" # dummy
echo "" # dummy
awk 'NR==FNR {h[$1] = $2; next} {print $1,$2,h[$1]}' "$LISTTEMPLATEFILE3" "$LISTTEMPLATEFILE4" | awk '{print $2}' | sed 's/"//g' > "$LISTTEMPLATEFILE5"
GETTEMPLATE=$(cat "$LISTTEMPLATEFILE5")
if [ -z "$GETTEMPLATE" ]
then
echo "[ERROR] nothing selected"
exit 1
fi
#/ cp -prf "$DIR"/hooks/templates/"$GETTEMPLATE" "$DIR"/hooks/hook_provisioning.sh
cp -prf "$DIR"/hooks/templates/"$GETTEMPLATE" /etc/lxc-to-go/hook_provisioning.sh
: # dummy
;;
1)
echo "" # dummy
echo "" # dummy
exit 0
;;
255)
echo "" # dummy
echo "" # dummy
echo "[ESC] key pressed."
exit 0
;;
esac
checkhiddenhard lxc-to-go template - stage 2
### // TEMPLATE ###
### TEMPLATE BUILD STATUS // ###
TEMPLATESTATUS1=$(cat "$DIR"/hooks/template-status | sed -n '/OK/,/WARNING/p' | grep -sw "$GETTEMPLATE")
TEMPLATESTATUS2=$(cat "$DIR"/hooks/template-status | sed -n '/WARNING/,/FAIL/p' | grep -sw "$GETTEMPLATE")
TEMPLATESTATUS3=$(cat "$DIR"/hooks/template-status | sed -n '/FAIL/,//p' | grep -sw "$GETTEMPLATE")
if [ "$GETTEMPLATE" = "$TEMPLATESTATUS1" ]; then
echo "" # dummy
echo "STATUS: $(printf "\033[1;32mBuilding process should be OK\033[0m\n")"
echo "(continuous integration tests based on: https://travis-ci.org/plitc/lxc-to-go)"
fi
if [ "$GETTEMPLATE" = "$TEMPLATESTATUS2" ]; then
echo "" # dummy
echo "STATUS: $(printf "\033[1;33mBuilding process could be with warnings\033[0m\n")"
echo "(please read the github.com/plitc/lxc-to-go README)"
fi
if [ "$GETTEMPLATE" = "$TEMPLATESTATUS3" ]; then
echo "" # dummy
echo "STATUS: $(printf "\033[1;31mBuilding process could be faulty\033[0m\n")"
echo "(please read the github.com/plitc/lxc-to-go README)"
fi
### // TEMPLATE BUILD STATUS ###
### ### ###
echo "" # dummy
echo "" # dummy
printf "\033[1;32mlxc-to-go template selection finished.\033[0m\n"
### ### ###
### ### ### ### ### ### ### ### ###
#
### // stage4 ###
#
### // stage3 ###
#
### // stage2 ###
exit 0
### ### ### PLITC ### ### ###
# EOF