From 481421569c300a7b17889e2d923a8f82b5e6183e Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 14 Feb 2012 02:00:57 +0100 Subject: [PATCH] refactored virtualbox scripts to use new scriptbuilder functions --- .../statements/InstallGuestAdditions.java | 46 ++--- .../virtualbox/statements/Statements.java | 4 +- .../domain/InstallGuestAdditionsTest.java | 27 ++- .../statements/GuestAdditionsInstaller.java | 1 - .../test_guest_additions_installer.sh | 24 +-- .../test_guest_additions_installer_init.sh | 191 ++++++++++++++++++ 6 files changed, 231 insertions(+), 62 deletions(-) create mode 100644 labs/virtualbox/src/test/resources/test_guest_additions_installer_init.sh diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java index 8ba07609ea..8d26a1f18b 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java @@ -22,34 +22,31 @@ package org.jclouds.virtualbox.statements; import static com.google.common.base.Preconditions.checkNotNull; import static org.jclouds.scriptbuilder.domain.Statements.call; import static org.jclouds.scriptbuilder.domain.Statements.exec; +import static org.jclouds.scriptbuilder.domain.Statements.saveHttpResponseTo; import java.net.URI; -import java.util.Collections; -import org.jclouds.scriptbuilder.ScriptBuilder; import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.SaveHttpResponseTo; -import org.jclouds.scriptbuilder.domain.Statement; +import org.jclouds.scriptbuilder.domain.StatementList; -import com.google.common.collect.ImmutableMultimap; - -public class InstallGuestAdditions implements Statement { - - private final String vboxVersion; - private final String mountPoint; +public class InstallGuestAdditions extends StatementList { public InstallGuestAdditions(String vboxVersion) { - this(vboxVersion, "/mnt"); + this(vboxVersion, "/mnt", "VBoxGuestAdditions_" + vboxVersion + ".iso"); } - public InstallGuestAdditions(String vboxVersion, String mountPoint) { - this.vboxVersion = checkNotNull(vboxVersion, "vboxVersion"); - this.mountPoint = checkNotNull(mountPoint, "mountPoint"); + public InstallGuestAdditions(String vboxVersion, String mountPoint, String vboxGuestAdditionsIso) { + this(URI.create("http://download.virtualbox.org/virtualbox/" + vboxVersion + "/" + vboxGuestAdditionsIso), + mountPoint, vboxGuestAdditionsIso); } - @Override - public Iterable functionDependencies(OsFamily family) { - return Collections.emptyList(); + public InstallGuestAdditions(URI download, String mountPoint, String vboxGuestAdditionsIso) { + super(call("setupPublicCurl"), // + saveHttpResponseTo(download, "{tmp}{fs}", vboxGuestAdditionsIso),// + exec(String.format("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint)), + call("installModuleAssistantIfNeeded"), // + exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run")), // + exec(String.format("umount %s", mountPoint))); } @Override @@ -57,19 +54,6 @@ public class InstallGuestAdditions implements Statement { checkNotNull(family, "family"); if (family == OsFamily.WINDOWS) throw new UnsupportedOperationException("windows not yet implemented"); - - String vboxGuestAdditionsIso = "VBoxGuestAdditions_" + vboxVersion + ".iso"; - ScriptBuilder scriptBuilder = new ScriptBuilder() - .addStatement( - new SaveHttpResponseTo("{tmp}{fs}", vboxGuestAdditionsIso, "GET", URI - .create("http://download.virtualbox.org/virtualbox/" + vboxVersion + "/" - + vboxGuestAdditionsIso), ImmutableMultimap. of())) - .addStatement(exec(String.format("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint))) - .addStatement(call("installModuleAssistantIfNeeded")) - .addStatement(exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run"))) - .addStatement(exec(String.format("umount %s", mountPoint))); - - return scriptBuilder.render(family); + return super.render(family); } - } diff --git a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java index 6698c8800e..5bfdb1c079 100644 --- a/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java +++ b/labs/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java @@ -18,7 +18,7 @@ */ package org.jclouds.virtualbox.statements; -import org.jclouds.scriptbuilder.domain.Call; +import org.jclouds.scriptbuilder.ScriptBuilder; import org.jclouds.scriptbuilder.domain.Statement; /** @@ -35,6 +35,6 @@ public class Statements { * - the vm name */ public static Statement exportIpAddressFromVmNamed(String vmName) { - return new Call("exportIpAddressFromVmNamed", vmName); + return ScriptBuilder.call("exportIpAddressFromVmNamed", vmName); } } diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java index 68df010720..d989deae58 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java @@ -22,21 +22,36 @@ import static org.testng.Assert.assertEquals; import java.io.IOException; +import org.jclouds.scriptbuilder.InitBuilder; import org.jclouds.scriptbuilder.domain.OsFamily; import org.jclouds.scriptbuilder.domain.ShellToken; +import org.jclouds.scriptbuilder.domain.Statement; import org.jclouds.virtualbox.statements.InstallGuestAdditions; import org.testng.annotations.Test; import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableSet; import com.google.common.io.CharStreams; import com.google.common.io.Resources; @Test(groups = "unit") public class InstallGuestAdditionsTest { - @Test - public void testUnix() throws IOException { - InstallGuestAdditions statement = new InstallGuestAdditions("4.1.6"); - assertEquals(statement.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources - .getResource("test_guest_additions_installer." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); - } + @Test + public void testUnixByItself() throws IOException { + InstallGuestAdditions statement = new InstallGuestAdditions("4.1.6"); + assertEquals(statement.render(OsFamily.UNIX), + CharStreams.toString(Resources.newReaderSupplier( + Resources.getResource("test_guest_additions_installer." + ShellToken.SH.to(OsFamily.UNIX)), + Charsets.UTF_8))); + } + + @Test + public void testUnixInInitScript() throws IOException { + Statement statement = new InitBuilder("install_guest_additions", ImmutableSet. of(), + ImmutableSet. of(new InstallGuestAdditions("4.1.6"))); + + assertEquals(statement.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier( + Resources.getResource("test_guest_additions_installer_init." + ShellToken.SH.to(OsFamily.UNIX)), + Charsets.UTF_8))); + } } \ No newline at end of file diff --git a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/GuestAdditionsInstaller.java b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/GuestAdditionsInstaller.java index 77e37c309c..3b0605d3cd 100644 --- a/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/GuestAdditionsInstaller.java +++ b/labs/virtualbox/src/test/java/org/jclouds/virtualbox/statements/GuestAdditionsInstaller.java @@ -1,6 +1,5 @@ package org.jclouds.virtualbox.statements; -import static com.google.common.base.Preconditions.checkNotNull; import static org.jclouds.compute.options.RunScriptOptions.Builder.runAsRoot; import java.util.concurrent.ExecutionException; diff --git a/labs/virtualbox/src/test/resources/test_guest_additions_installer.sh b/labs/virtualbox/src/test/resources/test_guest_additions_installer.sh index 6d6e7bd1a8..493a1df018 100644 --- a/labs/virtualbox/src/test/resources/test_guest_additions_installer.sh +++ b/labs/virtualbox/src/test/resources/test_guest_additions_installer.sh @@ -1,26 +1,6 @@ -#!/bin/bash -set +u -shopt -s xpg_echo -shopt -s expand_aliases -unset PATH JAVA_HOME LD_LIBRARY_PATH -function abort { - echo "aborting: $@" 1>&2 - exit 1 -} -function installModuleAssistantIfNeeded { - unset OSNAME; - local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift - if [ $OSNAME = 'Ubuntu' ] - then - echo "OS is Ubuntu" - apt-get -f -y -qq --force-yes install build-essential module-assistant; - m-a prepare -i - fi -} -export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +setupPublicCurl || return 1 (mkdir -p /tmp/ && cd /tmp/ && [ ! -f VBoxGuestAdditions_4.1.6.iso ] && curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -C - -X GET http://download.virtualbox.org/virtualbox/4.1.6/VBoxGuestAdditions_4.1.6.iso >VBoxGuestAdditions_4.1.6.iso) mount -o loop /tmp/VBoxGuestAdditions_4.1.6.iso /mnt -installModuleAssistantIfNeeded || exit 1 +installModuleAssistantIfNeeded || return 1 /mnt/VBoxLinuxAdditions.run umount /mnt -exit 0 diff --git a/labs/virtualbox/src/test/resources/test_guest_additions_installer_init.sh b/labs/virtualbox/src/test/resources/test_guest_additions_installer_init.sh new file mode 100644 index 0000000000..a27e2c27ab --- /dev/null +++ b/labs/virtualbox/src/test/resources/test_guest_additions_installer_init.sh @@ -0,0 +1,191 @@ +#!/bin/bash +set +u +shopt -s xpg_echo +shopt -s expand_aliases +unset PATH JAVA_HOME LD_LIBRARY_PATH +function abort { + echo "aborting: $@" 1>&2 + exit 1 +} +function default { + export INSTANCE_NAME="install_guest_additions" +export INSTANCE_HOME="$HOME/instances/install_guest_additions" +export LOG_DIR="$HOME/instances/install_guest_additions" + return 0 +} +function install_guest_additions { + return 0 +} +function findPid { + unset FOUND_PID; + [ $# -eq 1 ] || { + abort "findPid requires a parameter of pattern to match" + return 1 + } + local PATTERN="$1"; shift + local _FOUND=`ps auxwww|grep "$PATTERN"|grep -v " $0"|grep -v grep|grep -v $$|awk '{print $2}'` + [ -n "$_FOUND" ] && { + export FOUND_PID=$_FOUND + return 0 + } || { + return 1 + } +} +function forget { + unset FOUND_PID; + [ $# -eq 3 ] || { + abort "forget requires parameters INSTANCE_NAME SCRIPT LOG_DIR" + return 1 + } + local INSTANCE_NAME="$1"; shift + local SCRIPT="$1"; shift + local LOG_DIR="$1"; shift + mkdir -p $LOG_DIR + findPid $INSTANCE_NAME + [ -n "$FOUND_PID" -a -f $LOG_DIR/stdout.log ] && { + echo $INSTANCE_NAME already running pid [$FOUND_PID] + return 1; + } || { + nohup $SCRIPT >$LOG_DIR/stdout.log 2>$LOG_DIR/stderr.log & + RETURN=$? + # this is generally followed by findPid, so we shouldn't exit + # immediately as the proc may not have registered in ps, yet + test $RETURN && sleep 1 + return $RETURN; + } +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +case $1 in +init) + default || exit 1 + install_guest_additions || exit 1 + mkdir -p $INSTANCE_HOME + + # create runscript header + cat > $INSTANCE_HOME/install_guest_additions.sh <&2 + exit 1 +} +alias apt-get-install="apt-get install -f -y -qq --force-yes" +alias apt-get-upgrade="(apt-get update -qq&&apt-get upgrade -y -qq)" + +function ensure_cmd_or_install_package_apt(){ + local cmd=\$1 + local pkg=\$2 + + hash \$cmd 2>/dev/null || apt-get-install \$pkg || ( apt-get-upgrade && apt-get-install \$pkg ) +} + +function ensure_cmd_or_install_package_yum(){ + local cmd=\$1 + local pkg=\$2 + hash \$cmd 2>/dev/null || yum --nogpgcheck -y ensure \$pkg +} + +function ensure_netutils_apt() { + ensure_cmd_or_install_package_apt nslookup dnsutils + ensure_cmd_or_install_package_apt curl curl +} + +function ensure_netutils_yum() { + ensure_cmd_or_install_package_yum nslookup bind-utils + ensure_cmd_or_install_package_yum curl curl +} + +# most network services require that the hostname is in +# the /etc/hosts file, or they won't operate +function ensure_hostname_in_hosts() { + egrep -q `hostname` /etc/hosts || awk -v hostname=`hostname` 'END { print \$1" "hostname }' /proc/net/arp >> /etc/hosts +} + +# download locations for many services are at public dns +function ensure_can_resolve_public_dns() { + nslookup yahoo.com > /dev/null || echo nameserver 208.67.222.222 >> /etc/resolv.conf +} + +function setupPublicCurl() { + ensure_hostname_in_hosts + if hash apt-get 2>/dev/null; then + ensure_netutils_apt + elif hash yum 2>/dev/null; then + ensure_netutils_yum + else + abort "we only support apt-get and yum right now... please contribute!" + return 1 + fi + ensure_can_resolve_public_dns + return 0 +} +function installModuleAssistantIfNeeded { + unset OSNAME; + local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift + if [ \$OSNAME = 'Ubuntu' ] + then + echo "OS is Ubuntu" + apt-get -f -y -qq --force-yes install build-essential module-assistant; + m-a prepare -i + fi +} +END_OF_SCRIPT + + # add desired commands from the user + cat >> $INSTANCE_HOME/install_guest_additions.sh <<'END_OF_SCRIPT' +cd $INSTANCE_HOME +setupPublicCurl || exit 1 +(mkdir -p /tmp/ && cd /tmp/ && [ ! -f VBoxGuestAdditions_4.1.6.iso ] && curl -q -s -S -L --connect-timeout 10 --max-time 600 --retry 20 -C - -X GET http://download.virtualbox.org/virtualbox/4.1.6/VBoxGuestAdditions_4.1.6.iso >VBoxGuestAdditions_4.1.6.iso) +mount -o loop /tmp/VBoxGuestAdditions_4.1.6.iso /mnt +installModuleAssistantIfNeeded || exit 1 +/mnt/VBoxLinuxAdditions.run +umount /mnt + +END_OF_SCRIPT + + # add runscript footer + cat >> $INSTANCE_HOME/install_guest_additions.sh <<'END_OF_SCRIPT' +exit 0 +END_OF_SCRIPT + + chmod u+x $INSTANCE_HOME/install_guest_additions.sh + ;; +status) + default || exit 1 + findPid $INSTANCE_NAME || exit 1 + echo [$FOUND_PID] + ;; +stop) + default || exit 1 + findPid $INSTANCE_NAME || exit 1 + [ -n "$FOUND_PID" ] && { + echo stopping $FOUND_PID + kill -9 $FOUND_PID + } + ;; +start) + default || exit 1 + forget $INSTANCE_NAME $INSTANCE_HOME/$INSTANCE_NAME.sh $LOG_DIR || exit 1 + ;; +tail) + default || exit 1 + tail $LOG_DIR/stdout.log + ;; +tailerr) + default || exit 1 + tail $LOG_DIR/stderr.log + ;; +run) + default || exit 1 + $INSTANCE_HOME/$INSTANCE_NAME.sh + ;; +esac +exit 0