summaryrefslogtreecommitdiffstats
path: root/satellit_upgrader/pack-update.sh
blob: 6c6904ab79a9518a85322869949f205fbafffc7d (plain) (blame)
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
#!/bin/bash

declare -rg SELFPID=$$
perror () {
	if [ -n "$IGNORE_ERRORS" ]; then
		echo "${RED}[ERROR]${RESET} $@"
		return 0
	fi
	echo "${RED}[FATAL]${RESET} $@"
	[ "$$" != "$SELFPID" ] && kill "$SELFPID"
	exit 1
}

patchtgz () {
	[ -z "$1" ] && perror "patchtgz call failure"
	local VARNAME="$1"
	local VALUE="$2"
	sed -i "s,%${VARNAME}%,${VALUE},g" "$UPDATER" || perror "could not patchtgz $VARNAME in updater"
}

declare -rg UPDATER="updater.sh"

# Declare all the archives we need with a couple of expected contents,
# so we can detect goof-ups right when packing the update

declare -rg TGZ_SLXADMIN="files/slx-admin.tar.gz"
declare -rg FILES_SLXADMIN="
	modules-available/
	index.php
	config.php.example
	Mustache/
"

declare -rg TGZ_DOZMOD="files/dozmod.tar.gz"
declare -rg FILES_DOZMOD="
	dmsd.jar
	dmsd.sh
"

declare -rg TGZ_TASKMANAGER="files/taskmanager.tar.gz"
declare -rg FILES_TASKMANAGER="
	plugins/bwlp.jar
	taskmanager.jar
	data/
	scripts/
"

declare -rg TGZ_TFTP="files/tftpdir.tar.gz"
declare -rg FILES_TFTP="
	ipxelinux.0
	openslx.png
	pxechain.com
"

declare -rg TGZ_IPXE="files/ipxe.tar.gz"
declare -rg FILES_IPXE="
	pxelinux.0
	src/
	.git/
"

declare -rg TGZ_LDADP="files/ldadp.tar.gz"
declare -rg LIST_LDADP="
	Makefile
	.localversion
	version.in.h
	ldadp.c
"

declare -rg TGZ_DNBD3="files/dnbd3.tar.gz"
declare -rg FILES_DNBD3="
	dnbd3.*source/CMakeLists.txt
	dnbd3.*source/src/
	dnbd3.*source/inc/dnbd3/version.h
"

checkfiles () {
tar tf "$1" | awk -v strings="$2" '
BEGIN {
	numStrings = split(strings,tmp)
	for (i in tmp) strs[tmp[i]]
}
numStrings == 0 { exit 0 }
{
	for (str in strs) {
		if ( $0 ~ str ) {
			delete strs[str]
			numStrings--
		}
	}
}
END { exit (numStrings ? 1 : 0) }
'
}

RED=
GREEN=
RESET=
if [ -t 1 ]; then
	RED=$( echo -en '\033[1;31m' )
	GREEN=$( echo -en '\033[1;32m' )
	RESET=$( echo -en '\033[0m' )
fi
readonly RED GREEN RESET

# Prepare installer
cp "updater.template.sh" "$UPDATER" || perror "could not copy template"
chmod +x "$UPDATER"

VERSION=
while true; do
	case "$1" in
		--version)
			shift
			VERSION="$1"
			;;
		*)
			break
			;;
	esac
	shift
done

[ -z "$VERSION" ] && perror "No --version given"
[[ "$VERSION" =~ ^v[0-9]\.[0-9]+[a-z]?$ ]] || echo "${RED}Warn${RESET}: Version format is not vX.X(y)"

# Prepare source directory
declare -rg SRCDIR=$1
if [ -z "$SRCDIR" ]; then
	echo "Usage: $0 <srcdir>"
	echo "Where <srcdir> is the directory you put all the file to pack as payload"
	exit 1
fi

if [ "x$(stat -c "%d:%i" "$SRCDIR")" != "x$(stat -c "%d:%i" files)" ]; then
	rm -rf "files"
	[ -d "files" ] && perror "files still exists..."
	cp -r "$SRCDIR" "files"
fi

if [ ! -d "$SRCDIR" ]; then
	echo "Source dir '$SRCDIR' not found!"
fi

#declare -rg TMPDIR=$(mktemp -d)
#[ -d "$TMPDIR" ] || perror "TMPDIR fail."

downpayload () {
	local FILEVAR="TGZ_$1"
	FILEVAR="${!FILEVAR}"
	if [ -s "$FILEVAR" ]; then
		echo "${RED}Not${RESET} downloading $FILEVAR as it already exists in source dir..."
		return
	fi
	echo "Fetching $1..."
	wget --quiet -O "${FILEVAR}" "$2" || perror "Could not download $2"
}

addpayload () {
	echo -n "Includes $2: "
	local FILEVAR="TGZ_$1"
	local LISTVAR="FILES_$1"
	local FILENAME=${!FILEVAR}
	local LISTFILES=${!LISTVAR}
	if [ -e "${FILENAME}" ]; then
		if [ -n "$LISTFILES" ] && ! checkfiles "$FILENAME" "$LISTFILES"; then
			echo "${RED}no      !!! BAD FORMAT !!!${RESET}"
			patchtgz "$FILEVAR"
			return
		fi
		echo "${GREEN}yes${RESET}"
		patchtgz "$FILEVAR" "$FILENAME"
	else
		echo "${RED}no${RESET}"
		patchtgz "$FILEVAR"
	fi
}

urlbase="http://132.230.4.17/install"

downpayload "TFTP" "${urlbase}/openslx_tftp.tar.gz"
downpayload "SLXADMIN" "${urlbase}/slx-admin.tar.gz"
downpayload "DOZMOD" "${urlbase}/dozmod.tar.gz"
downpayload "TASKMANAGER" "${urlbase}/taskmanager.tar.gz"

# Replace variables
# slxadmin version
echo -n "Includes SLX-Admin: "
if [ -e "$TGZ_SLXADMIN" ]; then
	echo "${GREEN}yes${RESET}"
	VERS=$(date +%Y%j%H)
	[ -n "$VERS" ] || perror "Could not extract slx-admin version!"
	echo "Version: $VERS"
	sed -i "s/%TARGET_WEBIF_VERSION%/${VERS}/" "$UPDATER" || perror "could not patch slxadmin version in updater"
	sed -i "s/%SLXADMIN_FOOTER%/${VERSION}/" "$UPDATER" || perror "could not patch slxadmin footer in updater"
	patchtgz "TGZ_SLXADMIN" "$TGZ_SLXADMIN"
else
	echo "${RED}no${RESET}"
	patchtgz "TGZ_SLXADMIN"
fi

addpayload "DOZMOD" "Dozmod server"
addpayload "TASKMANAGER" "Taskmanager"
addpayload "TFTP" "TFTP/PxeLinux data"
addpayload "IPXE" "iPXE source code"
addpayload "LDADP" "ldap/ad proxy source code"
addpayload "DNBD3" "dnbd3-server source code"

# Add complete static_files directory structure
find ../satellit_installer/static_files -mindepth 1 -maxdepth 1 -type d \
	-exec cp -a {} files/ \; \
	|| perror "Could not copy static_files dir structure"

# Add simple dnbd3-server Makefile we need for older Debian 9 sats
cp dnbd3-Makefile files/ || perror "Could not include dnbd3 Makefile"

# Last patch: Payload offset
# Calc payload offset, which is tricky as the size changes as we patch
SIZE=$(stat -c %s "$UPDATER")
SIZE=$(( ( $SIZE / 1024 ) * 1024 + 1024 ))
sed -i "s/%PAYLOAD_OFFSET%/${SIZE}/" "$UPDATER" || perror "could not patch payload variable in updater"
# Truncate, append payload
truncate --size="$SIZE" "$UPDATER" || perror "Could not truncate updater to $SIZE bytes"
echo "Appending tar payload to final updater..."
tar ckz files/* >> "$UPDATER" || perror "Could not append payload to updater"
echo "..done"