From b67827f8649bc5f365adcb10cb7b4d64741a1ec8 Mon Sep 17 00:00:00 2001 From: Andrea Turli Date: Wed, 14 Dec 2011 14:05:05 +0000 Subject: [PATCH] issue 384: first implementation of install_guest_additions Statement + Test --- .../domain/InstallGuestAdditions.java | 49 ++++++++++++++++ .../jclouds/virtualbox/domain/Statements.java | 3 +- .../functions/exportIpAddressFromVmNamed.sh | 2 +- .../functions/installGuestAdditions.sh | 14 +++++ .../domain/ExportIpAddressForVMNamedTest.java | 1 - .../GetIPAddressFromGuestAdditionsTest.java | 58 ------------------- .../domain/InstallGuestAdditionsTest.java | 23 ++++++++ .../test_export_ip_address_from_vm_named.sh | 2 +- .../resources/test_install_guest_additions.sh | 31 ++++++++++ 9 files changed, 121 insertions(+), 62 deletions(-) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/InstallGuestAdditions.java create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/installGuestAdditions.sh delete mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_install_guest_additions.sh diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/InstallGuestAdditions.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/InstallGuestAdditions.java new file mode 100644 index 0000000000..fac25bf9c7 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/InstallGuestAdditions.java @@ -0,0 +1,49 @@ +package org.jclouds.virtualbox.domain; + +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 java.util.Collections; + +import org.jclouds.scriptbuilder.ScriptBuilder; +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.Statement; + +public class InstallGuestAdditions implements Statement { + + private final String vboxVersion; + private final String mountPoint; + + public InstallGuestAdditions(String vboxVersion) { + this(vboxVersion, "/mnt"); + } + + public InstallGuestAdditions(String vboxVersion, String mountPoint) { + this.vboxVersion = checkNotNull(vboxVersion, "vboxVersion"); + this.mountPoint = checkNotNull(mountPoint, "mountPoint"); + } + + @Override + public Iterable functionDependencies(OsFamily family) { + return Collections.emptyList(); + } + + @Override + public String render(OsFamily family) { + checkNotNull(family, "family"); + if (family == OsFamily.WINDOWS) + throw new UnsupportedOperationException("windows not yet implemented"); + + String vboxGuestAdditionsIso = "VBoxGuestAdditions_" + vboxVersion + ".iso"; + ScriptBuilder scriptBuilder = new ScriptBuilder() + .addStatement(exec("{cd} {fs}tmp")) + .addStatement(exec("wget http://download.virtualbox.org/virtualbox/" + vboxVersion + "/" + vboxGuestAdditionsIso)) + .addStatement(exec(String.format("mount -o loop %s %s", vboxGuestAdditionsIso, mountPoint))) + .addStatement(call("installGuestAdditions")) + .addStatement(exec(String.format("sh %s%s", mountPoint, "/VBoxLinuxAdditions.run"))) + .addStatement(exec(String.format("umount %s", mountPoint))); + return scriptBuilder.render(family); + } + +} diff --git a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java index 2e8d0da0ee..944975afd0 100644 --- a/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java @@ -18,6 +18,7 @@ */ package org.jclouds.virtualbox.domain; +import org.jclouds.scriptbuilder.domain.AppendFile; import org.jclouds.scriptbuilder.domain.Call; import org.jclouds.scriptbuilder.domain.Statement; @@ -37,4 +38,4 @@ public class Statements { public static Statement exportIpAddressFromVmNamed(String vmName) { return new Call("exportIpAddressFromVmNamed", vmName); } - } \ No newline at end of file + } diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh b/sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh index f123b89faf..a845e6ff3c 100644 --- a/sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh +++ b/sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh @@ -1,7 +1,7 @@ function exportIpAddressFromVmNamed { unset FOUND_IP_ADDRESS; [ $# -eq 1 ] || { - abort "installGuestAdditions requires virtual machine name parameter" + abort "exportIpAddressFromVmNamed requires virtual machine name parameter" return 1 } local VMNAME="$0"; shift diff --git a/sandbox-apis/virtualbox/src/main/resources/functions/installGuestAdditions.sh b/sandbox-apis/virtualbox/src/main/resources/functions/installGuestAdditions.sh new file mode 100644 index 0000000000..e1bd00e9a4 --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/resources/functions/installGuestAdditions.sh @@ -0,0 +1,14 @@ +function installGuestAdditions { + unset OSNAME; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift + if [ $OSNAME = 'Ubuntu' ] + then + echo "OS Name is Ubuntu" + `apt-get install build-essential module-assistant && m-a prepare -i` + fi + return 0 +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java index 2c1bc074ab..47a9115691 100644 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java @@ -47,5 +47,4 @@ public class ExportIpAddressForVMNamedTest { assertEquals(exportIpAddressForVMNamedBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources .getResource("test_export_ip_address_from_vm_named." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); } - } \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java deleted file mode 100644 index 05b8c9184c..0000000000 --- a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/GetIPAddressFromGuestAdditionsTest.java +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Licensed to jclouds, Inc. (jclouds) under one or more - * contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. jclouds licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.jclouds.virtualbox.domain; - -import static org.jclouds.scriptbuilder.domain.Statements.interpret; -import static org.jclouds.virtualbox.domain.Statements.getIpAddress; -import static org.testng.Assert.assertEquals; - -import java.io.IOException; - -import org.jclouds.scriptbuilder.ScriptBuilder; -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.ShellToken; -import org.testng.annotations.Test; - -import com.google.common.base.Charsets; -import com.google.common.io.CharStreams; -import com.google.common.io.Resources; - -/** - * @author Andrea Turli - */ -@Test(groups = "unit") -public class GetIPAddressFromGuestAdditionsTest { - - ScriptBuilder getIpAddressBuilder = new ScriptBuilder() - .addStatement(getIpAddress("{args}")) - .addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}")); - - public void testUNIX() throws IOException { - assertEquals(getIpAddressBuilder.render(OsFamily.UNIX), CharStreams.toString(Resources.newReaderSupplier(Resources - .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); - } - - // TODO - /* - public void testWINDOWS() throws IOException { - assertEquals(getIpAddressBuilder.render(OsFamily.WINDOWS), CharStreams.toString(Resources.newReaderSupplier(Resources - .getResource("test_get_ip_address." + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8))); - } - */ -} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java new file mode 100644 index 0000000000..065d5aa096 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/InstallGuestAdditionsTest.java @@ -0,0 +1,23 @@ +package org.jclouds.virtualbox.domain; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; + +import org.jclouds.scriptbuilder.domain.OsFamily; +import org.jclouds.scriptbuilder.domain.ShellToken; +import org.testng.annotations.Test; + +import com.google.common.base.Charsets; +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_install_guest_additions." + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); + } +} \ No newline at end of file diff --git a/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh b/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh index 624d8c3228..33076117e4 100644 --- a/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh +++ b/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh @@ -10,7 +10,7 @@ function abort { function exportIpAddressFromVmNamed { unset FOUND_IP_ADDRESS; [ $# -eq 1 ] || { - abort "installGuestAdditions requires virtual machine name parameter" + abort "exportIpAddressFromVmNamed requires virtual machine name parameter" return 1 } local VMNAME="$0"; shift diff --git a/sandbox-apis/virtualbox/src/test/resources/test_install_guest_additions.sh b/sandbox-apis/virtualbox/src/test/resources/test_install_guest_additions.sh new file mode 100644 index 0000000000..e4f4172807 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/resources/test_install_guest_additions.sh @@ -0,0 +1,31 @@ +#!/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 installGuestAdditions { + unset OSNAME; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local OSNAME=`lsb_release -d -s | cut -d ' ' -f 1`; shift + if [ $OSNAME = 'Ubuntu' ] + then + echo "OS Name is Ubuntu" + `apt-get install build-essential module-assistant && m-a prepare -i` + fi + return 0 +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +cd /tmp +wget http://download.virtualbox.org/virtualbox/4.1.6/VBoxGuestAdditions_4.1.6.iso +mount -o loop VBoxGuestAdditions_4.1.6.iso /mnt +installGuestAdditions || exit 1 +sh /mnt/VBoxLinuxAdditions.run +umount /mnt +exit 0