#!/bin/bash
# codename : viviane
# Virtualized 
# Integrated 
# Verification of
# Installer with 
# Automated 
# Networked
# Eyeballs 
#
# adapted from http://kashyapc.wordpress.com/2011/08/18/unattended-guest-install-with-a-local-kickstart/

#VERSION="cauldron"
#DIRECTORY="/pub/linux/Mageia/distrib/$VERSION"
VERSION="2010.2"
DIRECTORY="/pub/linux/MandrivaLinux/official/$VERSION"

SERVER="distrib-coffee.ipsl.jussieu.fr"
TREE="http://$SERVER$DIRECTORY"

function auto_install_test() {
    ARCH=$1
    PACKAGE=$2
    NAME=viviane_${ARCH}_${PACKAGE}_$(date +%Y_%m_%d)
    TMP_DIR=$(mktemp -d /tmp/viviane_XXXXX)
    chmod go+rx $TMP_DIR
    date >> /tmp/viviane.log
    echo "test of $NAME started" >> /tmp/viviane.log


       

    # based on http://www.zarb.org/~guillomovitch/installation_automatique.html
    # user that is created must be in dialout, to be able to send the message 
    # to the host on ttyS0 ( since consolekit do not handle this so far )
    cat > $TMP_DIR/auto_inst.cfg.pl <<EOF
#!/usr/bin/perl -cw

\$o = {
       'timezone' => {
		       'ntp' => undef,
		       'timezone' => 'Europe/Paris',
		       'UTC' => 1
		     },
       'default_packages' => [
                                'urpmi',
                                'basesystem',
                                'man-pages',
                                'info',
                                'curl',
                                'dhcp-client',
                                'iputils',
                                'autologin',
                                '$PACKAGE',
			     ],

       'users' => [
		    {
		      'icon' => 'default',
		      'realname' => 'Myrddin Wyllt',
		      'uid' => undef,
		      'groups' => ['dialout'],
		      'name' => 'merlin',
		      'shell' => '/bin/bash',
		      'gid' => undef
		    }
		  ],
       'locale' => {
		     'country' => 'us',
		     'lang' => 'en_US',
		     'langs' => {
				  'en_US' => 1
				},
		     'utf8' => 1
		   },
       'net' => {
		  'resolv' => {
				'DOMAINNAME' => 'test',
				'dnsServer' => '192.168.122.1',
				'DOMAINNAME2' => undef,
				'dnsServer2' => undef,
				'DOMAINNAME3' => undef,
				'dnsServer3' => undef
			      },
		  'network' => {
				 'NETWORKING' => 'yes',
				 'GATEWAY' => '192.168.122.1',
				 'FORWARD_IPV4' => 'false',
                 'HOSTNAME' => '$NAME.test',
			       },
		  'ethernet' => {},
		  'ifcfg' => {
			       'eth0' => {
					   'BROADCAST' => '',
					   'isUp' => 1,
					   'BOOTPROTO' => 'dhcp',
					   'isPtp' => '',
					   'NETWORK' => '',
					   'HWADDR' => undef,
					   'DEVICE' => 'eth0',
					   'METRIC' => 10
					 }
			     },
		  'net_interface' => 'eth0',
		  'type' => 'ethernet',
		  'PROFILE' => 'default'
		},
       'authentication' => {
			     'shadow' => 1,
			     'blowfish' => 1
			   },
       'partitions' => [
			 {
			   'fs_type' => 'ext4',
			   'mntpoint' => '/',
			   'size' => 7700000
			 },
			 {
			   'fs_type' => 'swap',
			   'mntpoint' => 'swap',
			   'size' => 256000
			 },
		       ],
       'partitioning' => {
			   'auto_allocate' => 1,
			   'clearall' => 1,
			   'eraseBadPartitions' => 1
			 },
       'superuser' => {
			'password' => 'root',
			'realname' => 'root',
			'uid' => '0',
			'shell' => '/bin/bash',
			'home' => '/root',
			'gid' => '0'
		      },
       'security' => 1,
       'interactiveSteps' => [
#			       'doPartitionDisks',
#			       'formatPartitions'
			     ],
       'autoExitInstall' => 1,
       'keyboard' => {
		       'GRP_TOGGLE' => '',
		       'KEYBOARD' => 'us'
		     },
       'postInstall' => q(
cat > /usr/local/bin/viviane_notify.sh <<SCRIPT
#!/bin/bash
echo "install_ok" >> /dev/ttyS0
SCRIPT

chmod ugo+rx /usr/local/bin/viviane_notify.sh

cat > /etc/xdg/autostart/viviane_notify.desktop <<DESKTOP
[Desktop Entry]
Exec=/usr/local/bin/viviane_notify.sh
Name=Viviane notification 
Terminal=false
Type=Application
StartupNotify=false
NoDisplay=true
X-KDE-autostart-after=panel
DESKTOP

cat > /etc/sysconfig/autologin <<AUTO
USER=merlin
AUTOLOGIN=yes
AUTO

        ),
        'rpmsrate_flags_chosen' => {
            },

        'compssListLevel' => 3,
        'firewall_ports' => undef,
        'keep_unrequested_dependencies' => 0,
     };
EOF

    # check the file, in case someone modify it
    perl -cw $TMP_DIR/auto_inst.cfg.pl 
    if [[ $? -ne 0 ]] ; then 
       echo "Incorrect auto_inst.cfg.pl file" 
       exit
    fi
    # needed to let qemu write here
    touch $TMP_DIR/log
    setfacl -m u:qemu:rw $TMP_DIR/log
    
    # --os-type and --os-variant are likely needed to find the 
    # proper layout on the mirror, do not modify it
    # required a patched version of virtinst 
    # ( https://bugzilla.redhat.com/show_bug.cgi?id=733121 )
    # Also, the documentation about auto installation is wrong on mandriva 
    # wiki, since short form "net" is not the same as "netw"

    virt-install --connect=qemu:///system \
        --network=network=default \
        --initrd-inject=$TMP_DIR/auto_inst.cfg.pl \
        --extra-args="text kickstart=/auto_inst.cfg.pl automatic=met:http,network:dhcp,ser:$SERVER,dir:$DIRECTORY/$ARCH,hostname:$NAME.test" \
        --name=${NAME} \
        --disk /var/lib/libvirt/images/${NAME}.img,size=8 \
        --ram 1024 \
        --vcpus=1 \
        --check-cpu \
        --location=$TREE/$ARCH \
        --serial=file,path=$TMP_DIR/log \
        --hvm \
        --accelerate \
        --os-type=linux \
        --os-variant=mandriva2010 \
        --wait=30 \
        --noautoconsole \
        --quiet

    # let the system do the first boot 
    sleep 120 

    if grep -q install_ok $TMP_DIR/log ; then
        virsh --connect qemu:///system destroy $NAME
        virsh --connect qemu:///system undefine $NAME
        rm -Rf $TMP_DIR
    else 
        # FIXME requires a recent version of libvirt for screenshot
        # virsh --connect qemu:///system screenshot $NAME /tmp/$NAME.png 
        # virsh --connect qemu:///system suspend $NAME
        # FIXME better message ( with url of the screenshot )
        #echo "test of $NAME failed" | mail foo  -s "automated install test report" 
        date >> /tmp/viviane.log
        echo "test of $NAME ( $TMP_DIR ) failed" >> /tmp/viviane.log
        cat $TMP_DIR/log >> /tmp/viviane.log
    fi 


}

auto_install_test i586 task-kde4

#for a in i586 x86_64; do 
#    for p in task-kde4 task-gnome; do
#        auto_install_test $a $p
#    done
#done
