From 735bf558d7322efe4960b7c8ab2a55ee2d1ce847 Mon Sep 17 00:00:00 2001 From: andreaturli Date: Mon, 12 Dec 2011 21:54:11 +0000 Subject: [PATCH] issue 384: change getIpAddress to exportIpAddressFromVmName, removed windows test TODO --- .../jclouds/virtualbox/domain/Statements.java | 40 +++++++++++++++ .../functions/exportIpAddressFromVmNamed.sh | 16 ++++++ .../domain/ExportIpAddressForVMNamedTest.java | 51 +++++++++++++++++++ .../test_export_ip_address_from_vm_named.sh | 29 +++++++++++ 4 files changed, 136 insertions(+) create mode 100644 sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java create mode 100644 sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh create mode 100644 sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java create mode 100644 sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh 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 new file mode 100644 index 0000000000..2e8d0da0ee --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/java/org/jclouds/virtualbox/domain/Statements.java @@ -0,0 +1,40 @@ +/** + * 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 org.jclouds.scriptbuilder.domain.Call; +import org.jclouds.scriptbuilder.domain.Statement; + +/** + * VboxManage statements used in shell scripts . + * + * @author Andrea Turli + */ +public class Statements { + + /** + * Extract the IP address from the specified vm and stores the IP address into the variable {@code FOUND_IP_ADDRESS} if successful. + * + * @param vmname + * - the vm name + */ + 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 new file mode 100644 index 0000000000..f123b89faf --- /dev/null +++ b/sandbox-apis/virtualbox/src/main/resources/functions/exportIpAddressFromVmNamed.sh @@ -0,0 +1,16 @@ +function exportIpAddressFromVmNamed { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} 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 new file mode 100644 index 0000000000..2c1bc074ab --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/java/org/jclouds/virtualbox/domain/ExportIpAddressForVMNamedTest.java @@ -0,0 +1,51 @@ +/** + * 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.exportIpAddressFromVmNamed; +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 ExportIpAddressForVMNamedTest { + + ScriptBuilder exportIpAddressForVMNamedBuilder = new ScriptBuilder() + .addStatement(exportIpAddressFromVmNamed("{args}")) + .addStatement(interpret("echo {varl}FOUND_IP_ADDRESS{varr}{lf}")); + + public void testUNIX() throws IOException { + 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/resources/test_export_ip_address_from_vm_named.sh b/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh new file mode 100644 index 0000000000..624d8c3228 --- /dev/null +++ b/sandbox-apis/virtualbox/src/test/resources/test_export_ip_address_from_vm_named.sh @@ -0,0 +1,29 @@ +#!/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 exportIpAddressFromVmNamed { + unset FOUND_IP_ADDRESS; + [ $# -eq 1 ] || { + abort "installGuestAdditions requires virtual machine name parameter" + return 1 + } + local VMNAME="$0"; shift + local _FOUND=`vboxmanage guestproperty enumerate "$VMNAME" --patterns "/VirtualBox/GuestInfo/Net/0/V4/IP" | awk '{ print $4 }' | cut -c 1-14` + [ -n "$_FOUND" ] && { + export FOUND_IP_ADDRESS=$_FOUND + echo [$FOUND_IP_ADDRESS] + return 0 + } || { + return 1 + } +} +export PATH=/usr/ucb/bin:/bin:/sbin:/usr/bin:/usr/sbin +exportIpAddressFromVmNamed $@ || exit 1 +echo $FOUND_IP_ADDRESS +exit 0