summaryrefslogtreecommitdiffstats
path: root/openslx
blob: e4039dd387f2a8d9c7f7c791cd31fc6211c1d25a (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
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
#!/bin/bash
# -----------------------------------------------------------------------------
#
# Copyright (c) 2014 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your suggestions, praise, or complaints to feedback@openslx.org
#
# General information about OpenSLX can be found at http://openslx.org/
# -----------------------------------------------------------------------------
#
#				OpenSLX server tool for packing up files from remote machines
#
# -----------------------------------------------------------------------------

SELF="$(readlink -f $0)"
ROOT_DIR="$(dirname "${SELF}")"
OPENSLX_PID="$$"

qnd_exit() {
	unset_quiet
	kill "$OPENSLX_PID"
	[ $# -ge 1 ] && kill "$1"
	exit 1
}

# source all helper unit files that are found under helper/*.inc
# or remote/includes/*.inc
# a helper unit may contain a function called "__init" which will be called
# after all helpers have been sourced.
for HELPER in "$ROOT_DIR"/helper/*.inc "$ROOT_DIR"/server/includes/*.inc; do
	SHORT=${HELPER%.inc}
	SHORT="_init_${SHORT##*/}"
	. <(sed "s/^__init/$SHORT/" "$HELPER") && continue
	unset_quiet
	echo "Could not source $HELPER"
	qnd_exit
done
# called below, after initial_checks
init_helpers () {
	# in case the helers have init functions, call them now
	for HELPER in "$ROOT_DIR"/helper/*.inc "$ROOT_DIR"/server/includes/*.inc; do
		SHORT=${HELPER%.inc}
		SHORT="_init_${SHORT##*/}"
		type -t "$SHORT" | grep -q '^function$' && "$SHORT"
	done
}


banner () {
#                             (        
#               (             )\    )  
#  (   `  )    ))\  (     (  ((_)( /(  
#  )\  /(/(   /((_) )\ )  )\  _  )\()) 
# ((_)((_)_\ (_))  _(_/( ((_)| |((_)\  
#/ _ \| '_ \)/ -_)| ' \))(_-<| |\ \ /  
#\___/| .__/ \___||_||_| /__/|_|/_\_\  
#     |_|                              
# Aus der Kategorie: Man kanns auch übertreiben
	echo -e "\033[38;5;202m\t                             (        "
	echo -e "\033[38;5;202m\t               (             )\\    )  "
	echo -e "\033[38;5;208m\t  (   \`  )    ))\\  (     (  ((_)( /(  "
	echo -e "\033[38;5;208m\t  )\\  /(/(   /((_) )\\ )  )\\  \033[38;5;220m_\033[38;5;208m  )\\()) "
	echo -e "\033[38;5;208m\t ((\033[38;5;220m_\033[38;5;208m)((\033[38;5;220m_\033[38;5;208m)\033[38;5;220m_\033[38;5;208m\\ (\033[38;5;220m_\033[38;5;208m))  \033[38;5;220m_\033[38;5;208m(\033[38;5;220m_\033[38;5;208m/( ((\033[38;5;220m_\033[38;5;208m)\033[38;5;220m| |\033[38;5;208m((\033[38;5;220m_\033[38;5;208m)\\  "
	echo -e "\033[38;5;220m\t/ _ \\| '_ \\ \033[38;5;208m)\033[38;5;220m/ -_)| ' \\ \033[38;5;208m))\033[38;5;220m(_-<| |\\ \\ /  "
	echo -e "\033[38;5;226m\t\\___/| .__/ \\___||_||_| /__/|_|/_\\_\\  "
	echo -e "\033[38;5;226m\t     | |                              "
	echo -e "\033[38;5;220m\t     | |            ** OpenSLX Project // 2013 **"
	echo -e "\033[38;5;226m\t     |_|               http://lab.openslx.org/"
	echo -e "\033[00m"
}

print_usage() {
	echo -e ""
	echo -e "Toolkit for packing mini linux generated by mltk"
	echo -e "Usage: $(basename ${SELF}) <remotehost> [-s] [<target>] [-e cpio|sqfs] [-d] [-c] [-k config]"
	echo -e ""
	echo -e "  Options:"
	echo -e "     -d            activates debug output for the task"
	echo -e "     -e type       export target as 'type'. can be 'cpio' (simple initramfs) or 'sqfs' (squashfs)"
	echo -e "     -s            sync 'builds' from remote host"
	echo -e "     -c            clean target in server/local_builds/ and corresponding files under boot/"
	echo -e ""
	echo -e "  Examples:"
	echo -e "     1.2.3.4 -s"
	echo -e "          (sync all builds from remote system 1.2.3.4)"
	echo -e "     1.2.3.4 stage31 -e cpio"
	echo -e "          (packs stage31 as initram-fs (cpio) from remote system 1.2.3.4)"
	echo -e "     1.2.3.4 stage32 -e sqfs"
	echo -e "          (packs stage32 as squashfs from remote system 1.2.3.4)"
	echo -e "     1.2.3.4 stage31 -c"
	echo -e "          (clean stage31 build under server/local_builds and initramfs under server/boot)"
	echo -e "     1.2.3.4 addons-eexam -e sqfs -s"
	echo -e "          (pack eexam-addons as squashfs, sync from remote before packing)"
	echo -e "     1.2.3.4 -k <configname>"
	echo -e "          (generate config file server/boot/<remotehost>/configs/<configname>/config.tgz)"
	echo -e ""
	echo -e " NOTE: Use 'local' as the remote host if you're running on the same machine as mltk."
	echo -e "       (for testing only, not recommended for actual use)"
	echo -e ""
}


check_devtools() {
	# Checking for needed development tools.
	local DEV_TOOLS="mksquashfs rsync"

	# DEV_CHECK marks missing dev-tools
	local DEV_TOOLS_MISSING=""
	for i in $DEV_TOOLS; do
		which "$i" 2>/dev/null 1>&2 || DEV_TOOLS_MISSING+="$i "
	done

	if [ -n "$DEV_TOOLS_MISSING" ]; then
		pinfo "You appear to be missing following development tools."
		pinfo "Missing tools are: $DEV_TOOLS_MISSING"
		pinfo "For $SYS_DISTRIBUTION you probably need to run:"
		case $SYS_DISTRIBUTION in
			ubuntu | debian)
				pinfo "apt-get install squashfs-tools rsync"
				pinfo "to install missing development tools."
				;;
			# FIXME: Don't know how precise this filter works so we might want to have a better matching ...
			opensuse)
				pinfo "zypper install squashfs rsync"
				pinfo "to install missing development tools."
				;;
			*)
				perror "No help for $SYS_DISTRIBUTION available."
				;;
		esac
		perror "Please install missing dependencies (see above) and try again!"

	fi
}

initial_checks() {
	if [ "x$(whoami)" != "xroot" ]; then
		perror "ERROR: You need to have root rights for proper rsync/mksquashfs."
		exit 1
	else
		banner
	fi

	# source the central script:
	# export_target
	#	- pack given target as a cpio or squashfs depending on
	#	  the export type:
	#	  	stage31 (cpio archive of the complete stage31-filesystem)
	#		stage32 (cpio archive containing the squashfs'ed stage32-filesystem)
	SERVER_EXPORT_TARGET="${ROOT_DIR}/server/export_target"

	[ ! -e "${SERVER_EXPORT_TARGET}" ] && perror "Missing script server/export_target. Exiting."
	return 0
}

read_params() {
	local SUBMODE=""

	REMOTE_IP="$1"
	shift

	TARGET=""
	if [[ $1 != -* ]]; then
		TARGET="$1"
		shift
	elif [[ "$1" != "-s" && "$1" != "-k" ]]; then
		perror "A target is required. None given."
	fi

	# handle rest of arguments
	while [ "$#" -gt "0" ]; do
		local PARAM="$1"
		shift

		# options to current target
		if [[ "$PARAM" == "-"* ]]; then
			case "$PARAM" in
				-k)
					# NOTE: check for validity of config is done in 'inital_checks' of server/export_target
					SERVER_CONFIG_TYPE="$1"
					SERVER_CONFIG="1"
					shift
					continue
					;;
				-s)
					[ "$REMOTE_IP" != "local" ] && SERVER_SYNC="1"
					continue
					;;
				-c)
					SUBMODE="CLEAN"
					;;
				-d)
					eval SERVER_DEBUG="1"
					continue
					;;
				-e)
					[ "$#" -lt "1" ] && perror "Missing argument to -e"
					case "$1" in
						cpio|stage31) # stage31 for compat
							SERVER_EXPORT_TYPE="cpio"
							;;
						sqfs|stage32|addons)
							SERVER_EXPORT_TYPE="sqfs"
							;;
						*)
							perror "Wrong type specified. Must be either 'cpio' or 'sqfs'"
							;;
					esac
					SERVER_EXPORT="1"
					shift
					continue
					;;
				-i)
					[ "$#" -lt "1" ] && perror "Missing argument to -i"
					IGNORE_TARGET="$1"
					shift
					continue
					;;
				*)
					pwarning "Unknown flag to target: $PARAM"
					print_usage
					exit 1
					;;
			esac
			eval SERVER_${SUBMODE}="1"
			continue
		fi

		pwarning "Unexpected '$PARAM'" && print_usage && exit 1
	done

	# exit if no command
	[[ "$SERVER_CLEAN" == 0 && "$SERVER_EXPORT" == 0 && "$SERVER_SYNC" == 0 && "$SERVER_CONFIG" == 0 ]] && print_usage && exit 1
	return 0
}

run() {
	if [[ "$SERVER_CLEAN" == 1 || "$SERVER_EXPORT" == 1 || "$SERVER_SYNC" == 1 || "$SERVER_CONFIG" == 1 ]]; then
		[[ "$SERVER_DEBUG" == 1 ]] && unset_quiet || set_quiet
		. "${SERVER_EXPORT_TARGET}" || perror "Cannot source ${SERVER_EXPORT_TARGET}"
		[[ "$SERVER_SYNC" == 1 ]] && sync_remote
		[[ "$SERVER_CLEAN" == 1 ]] && clean_target "$TARGET"
		[[ "$SERVER_EXPORT" == 1 ]] && export_target "$TARGET" "$SERVER_EXPORT_TYPE" "$IGNORE_TARGET"
		[[ "$SERVER_CONFIG" == 1 ]] && generate_config "$SERVER_CONFIG_TYPE"
		pinfo "Exiting normally"
	fi
	return 0
}

SERVER_DEBUG="0"
SERVER_EXPORT="0"
SERVER_CLEAN="0"
SERVER_EXPORT_TYPE=""
SERVER_SYNC="0"
SERVER_CONFIG="0"

initial_checks || perror "initial_checks failed."
read_params $@ || perror "read_params failed."
init_helpers

check_devtools || perror "check_devtools failed."

run || perror "run failed."
exit 0