summaryrefslogblamecommitdiffstats
path: root/satellit_upgrader/pack-update.sh
blob: 37b96c1f139d01443464933505a2623c3558da26 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                 






                                                                                                        
                                
                                                 

                                                      
                                           
                                        
                                          
                                                 
 
                   


                                                                       












                                 




















                                                                                      












                                               

                               

                                     
                                                 


                                                                              
            





                                                                                    


          


                              
                               
                  
                            


                                                                                                                     
                                                                                                                                 
                                               

                 
                               
  



                                          
                                        
                                      
                                     
 
                       
                                      

                                                   
                                            

                                            
                                                   

                                                      






                                                   
 



                                                                
 






                                                                                                       
                                                
                                                                             
             
 
#!/bin/bash

declare -rg SELFPID=$$
perror () {
	if [ -n "$IGNORE_ERRORS" ]; then
		echo "[ERROR] $@"
		return 0
	fi
	echo "[FATAL] $@"
	[ "$$" != "$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 -rg TGZ_SLXADMIN="files/slx-admin.tar.gz"
declare -rg TGZ_DOZMOD="files/dozmod.tar.gz"
declare -rg TGZ_TASKMANAGER="files/taskmanager.tar.gz"
declare -rg TGZ_TFTP="files/tftpdir.tar.gz"
declare -rg TGZ_IPXE="files/ipxe.tar.gz"
declare -rg TGZ_LDADP="files/ldadp.tar.gz"
declare -rg TGZ_DNBD3="files/dnbd3-server.tar.gz"

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

LEAN=
while true; do
	case "$1" in
		--lean|-l)
			LEAN=true
			;;
		*)
			break
			;;
	esac
	shift
done

# 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."

addpayload () {
	echo -n "Includes $2: "
	local FILEVAR=$1
	local FILENAME=${!FILEVAR}
	if [ -e "${FILENAME}" ]; then
		echo "yes"
		patchtgz "$FILEVAR" "$FILENAME"
	else
		echo "no"
		patchtgz "$FILEVAR"
	fi
}

addinstallfile () {
	echo -n "Includes $1: "
	local file=$1
	local path=$(dirname "$file")
	if [ ! -d "../satellit_installer" ]; then
		echo "NO! (satellit_installer not found)"
	elif [ ! -e "../satellit_installer/static_files/$file" ]; then
		echo "NO! (file not found in satellit_installer/static_files)"
	else
		mkdir -p "files/$path" || echo -n "(mkdir failed) "
		if cp "../satellit_installer/static_files/$file" "files/$file"; then
			echo "yes"
		else
			echo "...no! (copy failed)"
		fi
	fi
}

# Replace variables
# slxadmin version
echo -n "Includes SLX-Admin: "
if [ -e "$TGZ_SLXADMIN" ]; then
	echo "yes"
	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%/$(date '+%y-%m-%d %H:%M')/" "$UPDATER" || perror "could not patch slxadmin footer in updater"
	patchtgz "TGZ_SLXADMIN" "$TGZ_SLXADMIN"
else
	echo "no"
	patchtgz "TGZ_SLXADMIN"
fi

addpayload "TGZ_DOZMOD" "Dozmod server"
addpayload "TGZ_TASKMANAGER" "Taskmanager"
addpayload "TGZ_TFTP" "TFTP/PxeLinux data"
addpayload "TGZ_IPXE" "iPXE source code"
addpayload "TGZ_LDADP" "ldap/ad proxy"
addpayload "TGZ_DNBD3" "dnbd3-server"

if [ -z "$LEAN" ]; then
	addinstallfile "lighttpd.conf"
	addinstallfile "lighttpd-auto-ssl.sh"
	addinstallfile "lighttpd-include-conf-d.sh"
	addinstallfile "slxadmin-config.php"
	addinstallfile "slxadmin-cronscript"
	addinstallfile "slxadmin-crontab"
	addinstallfile "patch_lighttpd_phpchildren"

	# Not quite ideal, watch out for similar names
	addinstallfile "dnbd3/dnbd3-server.service"
	addinstallfile "dnbd3/is-enabled"
	addinstallfile "dnbd3/server.conf"
	addinstallfile "dnbd3/rpc.acl"

	addinstallfile "tftpd/tftpd-hpa.service"
	addinstallfile "tftpd/tftpd-hpa"

	# TODO: Unify more
	source ../satellit_installer/includes/10-sudo_config.inc
	sudo_config files/tm-sudo-config
fi

# 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"