summaryrefslogblamecommitdiffstats
path: root/openslx
blob: e4039dd387f2a8d9c7f7c791cd31fc6211c1d25a (plain) (tree)
1
2
3
4


                                                                               
                                   

























                                                                                             






                                                                            



                                       








                                                                                    




































                                                                                                                                                                                                                                                                                                                                      
                                 
                                                                        
                                              
                                                                                           
                                              
                                                                                  
                                         
                                                                                                           
                                                      
                                                                                            
                                              










































                                                                                                                        
                                                                                         









































































                                                                                                                               





                                                                                           























                                                                                                                                    
                                                                                                             














                                                                                    
            





                                                 
#!/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