cookutils rev 318

Add cookiso (Automate and log ISO build)
author Christophe Lincoln <pankso@slitaz.org>
date Thu Mar 15 03:01:16 2012 +0100 (2012-03-15)
parents 9109770afce4
children 830286d5eae4
files cookiso
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cookiso	Thu Mar 15 03:01:16 2012 +0100
     1.3 @@ -0,0 +1,271 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# Cookiso utility - Build official ISO's in a chroot environment.
     1.7 +# The goal is ti have a tool well integrated to cookutils but who
     1.8 +# can run on it's own and to automate official SliTaz ISO creation.
     1.9 +#
    1.10 +
    1.11 +[ -f "/etc/slitaz/cook.conf" ] && . /etc/slitaz/cook.conf
    1.12 +[ -f "cook.conf" ] && . ./cook.conf
    1.13 +
    1.14 +# --> cook.conf
    1.15 +# SSH/RSA configuration to upload on a server.
    1.16 +SSH_CMD="dbclient -i /root/.ssh/id_rsa.dropbear"
    1.17 +SSH_ISO="/var/www/slitaz/mirror/iso"
    1.18 +SSH_HOST="slitaz@mirror.slitaz.org"
    1.19 +SSH_PLUS="ifconfig eth0"
    1.20 +#BWLIMIT="--bwlimit=40"
    1.21 +
    1.22 +# Cookiso DB files.
    1.23 +cache="$CACHE/cookiso"
    1.24 +repo="$SLITAZ/flavors"
    1.25 +iso="$SLITAZ/iso"
    1.26 +activity="$cache/activity"
    1.27 +command="$cache/command"
    1.28 +rollog="$cache/rolling.log"
    1.29 +commits="$cache/commits"
    1.30 +
    1.31 +# Parse cmdline options.
    1.32 +for opt in "$@"
    1.33 +do
    1.34 +	case "$opt" in
    1.35 +		--pkgdb)
    1.36 +			cook pkgdb --flavors ;;
    1.37 +		--push)
    1.38 +			echo "TODO: Uplaod iso's to mirror"
    1.39 +			exit 0 ;;
    1.40 +		--flavors=*)
    1.41 +			flavors=${opt#--flavors=} ;;
    1.42 +		--version=*)
    1.43 +			version=${opt#--version=} ;;
    1.44 +	esac
    1.45 +done
    1.46 +
    1.47 +# Default to rolling, or: cookiso [cmd] --version=stable
    1.48 +case "$version" in
    1.49 +	stable)
    1.50 +		string=$(cat /etc/slitaz-release) ;;
    1.51 +	cooking)
    1.52 +		string=cooking ;;
    1.53 +	*)
    1.54 +		version=cooking
    1.55 +		string=rolling ;;
    1.56 +esac
    1.57 +
    1.58 +# Running command
    1.59 +[ -d "$cache" ] && echo "$@" > $command
    1.60 +trap 'rm -f $command && exit 1' INT TERM
    1.61 +
    1.62 +#
    1.63 +# Functions
    1.64 +#
    1.65 +
    1.66 +usage() {
    1.67 +	cat << EOT
    1.68 +
    1.69 +$(echo -e "\033[1mUsage:\033[0m") cookiso [command] [--option]
    1.70 +
    1.71 +$(echo -e "\033[1mCommands:\033[0m")
    1.72 +  usage         Display this short usage.
    1.73 +  setup         Setup Cookiso build environment.
    1.74 +  push          Manually push ISO to a server via SSH.
    1.75 +  gen           Generate specified flavors.
    1.76 +  4in1          Generate all 4in1 flavors.
    1.77 +  rolling       Build the rollings ISO's if any changes.
    1.78 +
    1.79 +$(echo -e "\033[1mOptions:\033[0m")
    1.80 +  --pkgdb       Generate packages DB before building ISO.
    1.81 +  --push        Upload freshly generated ISO to a server.
    1.82 +  --flavors=    List of flavors to generate with 'gen' command.
    1.83 +  --version=    Specify SliTaz version: [rolling|cooking|stable]
    1.84 +
    1.85 +EOT
    1.86 +}
    1.87 +
    1.88 +spider() {
    1.89 +	echo '  //  \\'
    1.90 +	echo ' _\\()//_'
    1.91 +	echo '/ //  \\ \\'
    1.92 +	echo ' | \__/ |'
    1.93 +}
    1.94 +
    1.95 +# Check for one a some flavors on cmdline
    1.96 +flavors_list() {
    1.97 +	if [ "$flavors" == "all" ]; then
    1.98 +		flavors=$(ls $SLITAZ/flavors)
    1.99 +	fi
   1.100 +	if [ ! "$flavors" ]; then
   1.101 +		echo "No flavor specified on cmdline. Use: --flavors="
   1.102 +		rm -f $command && exit 0
   1.103 +	fi
   1.104 +}
   1.105 +
   1.106 +# Log activities, we want first letter capitalized.
   1.107 +log() {
   1.108 +	grep ^[A-Z] | \
   1.109 +		sed s"#^[A-Z]\([^']*\)#$(date '+%Y-%m-%d %H:%M') : \0#" >> $activity
   1.110 +}
   1.111 +
   1.112 +log_bot() {
   1.113 +	sed '/^.\//'d | sed '/^.hg/'d | tee -a $rollog
   1.114 +}
   1.115 +
   1.116 +# Generate requested flavors.
   1.117 +gen_flavors() {
   1.118 +	cd $SLITAZ/flavors && hg pull -u
   1.119 +	mkdir -p $cache && cd $cache
   1.120 +	rm -rf *.flavor *.list *.conf
   1.121 +	for flavor in $flavors
   1.122 +	do
   1.123 +		if [ "$flavor" != "core-4in1" ]; then
   1.124 +			name="slitaz-$string-$flavor"
   1.125 +		else
   1.126 +			name="slitaz-$string"
   1.127 +		fi
   1.128 +		log=$iso/$name.log
   1.129 +		rm -f $log && touch $log
   1.130 +		echo "Building $string <a href='?distro=$string-$flavor'>$flavor</a>" | log
   1.131 +		echo "Cookiso started: $(date '+%Y-%m-%d %H:%M')" | tee -a $log
   1.132 +		tazlito pack-flavor $flavor | tee -a $log
   1.133 +		tazlito get-flavor $flavor | tee -a $log
   1.134 +		script -c "echo -e '\n' | tazlito gen-distro" -a $log
   1.135 +		# Rename ISO and md5
   1.136 +		mv -f $SLITAZ/distro/slitaz-$flavor.iso $iso/$name.iso
   1.137 +		cd $iso && md5sum $name.iso > $name.md5
   1.138 +		echo "Cookiso ended: $(date '+%Y-%m-%d %H:%M')" | tee -a $log
   1.139 +	done && echo ""
   1.140 +}
   1.141 +
   1.142 +# Push an ISO to a server.
   1.143 +push_iso() {
   1.144 +	export DROPBEAR_PASSWORD=none
   1.145 +	for flavor in $flavors
   1.146 +	do
   1.147 +		rsync $BWLIMIT -vtP -e "$SSH_CMD" \
   1.148 +			$iso/slitaz-$string-$flavor.* \
   1.149 +			${SSH_HOST}:$SSH_ISO/$string $SSH_PLUS
   1.150 +	done
   1.151 +}
   1.152 +
   1.153 +#
   1.154 +# Commands
   1.155 +#
   1.156 +
   1.157 +case "$1" in
   1.158 +	setup)
   1.159 +		# Setup Hg repo and dirs
   1.160 +		echo -e "\nSetting up Cookiso environment..."
   1.161 +		cd $SLITAZ
   1.162 +		if [ ! -d "flavors" ]; then
   1.163 +			case $version in
   1.164 +				cooking|rolling)
   1.165 +					hg clone http://hg.slitaz.org/flavors ;;
   1.166 +				stable)
   1.167 +					hg clone http://hg.slitaz.org/flavors-stable flavors ;;
   1.168 +			esac
   1.169 +		fi
   1.170 +		# Needed packages
   1.171 +		for pkg in mercurial tazlito rsync dropbear
   1.172 +		do
   1.173 +			[ -d "/var/lib/tazpkg/installed/$pkg" ] || tazpkg -gi $pkg
   1.174 +		done
   1.175 +		echo "Creating directories and files..."
   1.176 +		mkdir -p $cache $iso
   1.177 +		touch $activity
   1.178 +		sed -i s'/^WORK_DIR=.*/WORK_DIR="\/home\/slitaz"/' \
   1.179 +			/etc/tazlito/tazlito.conf
   1.180 +		echo ""
   1.181 +		echo "Flavors files : $SLITAZ/flavors"
   1.182 +		echo "Cache files   : $cache"
   1.183 +		echo "ISO images    : $iso"
   1.184 +		echo "" ;;
   1.185 +	push)
   1.186 +		# Manually upload an ISO to a server.
   1.187 +		flavors_cook
   1.188 +		push_iso ;;
   1.189 +	gen)
   1.190 +		# Build one or more flavor.
   1.191 +		flavors_list
   1.192 +		echo -e "\nGenerating flavors:\n$flavors"
   1.193 +		gen_flavors ;;
   1.194 +	4in1)
   1.195 +		echo -e "\nGenerating 4in1 distro's..."
   1.196 +		flavors="base justx gtkonly core core-4in1"
   1.197 +		gen_flavors ;;
   1.198 +	rolling)
   1.199 +		#
   1.200 +		# Official SliTaz rolling release flavors are automaticaly build.
   1.201 +		#
   1.202 +		# Check if packages list was modified or if any commit have been
   1.203 +		# done in one the rolling flavors and rebuild ISO's if needed.
   1.204 +		#
   1.205 +		pkgs=$SLITAZ/packages/packages.md5
   1.206 +		last=$cache/packages.md5
   1.207 +		diff=$cache/packages.diff
   1.208 +		cook="preinit justx core-4in1"
   1.209 +		
   1.210 +		# Log stuff
   1.211 +		rm -f $rollog && touch $rollog
   1.212 +		rm -f $commit $commits.tmp && touch $commits.tmp
   1.213 +		echo "Rolling tracking for changes" | log
   1.214 +		echo "Cookiso rolling started: $(date '+%Y-%m-%d %H:%M')" | log_bot
   1.215 +		
   1.216 +		# Packages changes
   1.217 +		[ -f "$last" ] || cp -f $pkgs $cache
   1.218 +		diff $last $pkgs > $diff 
   1.219 +		if [ -s "$diff" ]; then
   1.220 +			echo "Found new or rebuilded packages" | log_bot
   1.221 +			cat $diff >> $rollog
   1.222 +			#
   1.223 +			# TODO: Check new pkg and see if it's part of one of the rolling
   1.224 +			# flavors, if not we have nothing to build.
   1.225 +			#
   1.226 +			for flavor in $cook
   1.227 +			do
   1.228 +				echo "$flavor" >> $commits.tmp
   1.229 +				echo "New packages for : $flavor" | log_bot
   1.230 +			done
   1.231 +		else
   1.232 +			echo "No changes found in packages MD5 sum" | log_bot
   1.233 +			echo "" > $commits.tmp
   1.234 +		fi
   1.235 +		cp -f $pkgs $cache
   1.236 +		
   1.237 +		# Hg changes
   1.238 +		cd $repo || exit 1
   1.239 +		cur=$(hg head --template '{rev}\n')
   1.240 +		echo "Updating wok : $repo (rev $cur)" | log_bot 
   1.241 +		cd $repo && hg pull -u | log_bot 
   1.242 +		new=$(hg head --template '{rev}\n') 
   1.243 +		cur=$(($cur + 1))
   1.244 +		for rev in $(seq $cur $new)
   1.245 +		do
   1.246 +			for file in $(hg log --rev=$rev --template "{files}")
   1.247 +			do
   1.248 +				flavor=$(echo $file | cut -d "/" -f 1)
   1.249 +				desc=$(hg log --rev=$rev --template "{desc}" $file)
   1.250 +				echo "Commited flavor  : $flavor - $desc" | log_bot
   1.251 +				# Build only rolling flavor
   1.252 +				if echo "$cook" | fgrep -q $flavor; then
   1.253 +					echo $flavor >> $commits.tmp
   1.254 +				fi
   1.255 +			done
   1.256 +		done
   1.257 +		
   1.258 +		# Keep previous commit and discard duplicate lines
   1.259 +		cat $commits.tmp | sed /"^$"/d > $commits.new
   1.260 +		uniq $commits.new > $commits && rm $commits.*
   1.261 +		nb=$(cat $commits | wc -l)
   1.262 +		echo "Flavors to cook  : $nb" | log_bot
   1.263 +		flavors=$(cat $commits) 
   1.264 +		gen_flavors 
   1.265 +		;;
   1.266 +	spider)
   1.267 +		# SliTaz Easter egg command :-)
   1.268 +		spider ;;
   1.269 +	*)
   1.270 +		usage ;;
   1.271 +esac
   1.272 +
   1.273 +rm -f $command
   1.274 +exit 0