http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/IMediumPredicates.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/IMediumPredicates.java b/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/IMediumPredicates.java deleted file mode 100644 index 7cee923..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/IMediumPredicates.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.predicates; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Predicates.equalTo; -import static com.google.common.collect.Iterables.any; - -import org.virtualbox_4_2.DeviceType; -import org.virtualbox_4_2.IMedium; - -import com.google.common.base.Predicate; - -public class IMediumPredicates { - public static class DeviceTypeEquals implements Predicate<IMedium> { - private final DeviceType deviceType; - - public DeviceTypeEquals(DeviceType deviceType) { - this.deviceType = checkNotNull(deviceType, "deviceType"); - } - - @Override - public boolean apply(IMedium arg0) { - return deviceType.equals(arg0.getDeviceType()); - } - - @Override - public String toString() { - return "deviceTypeEquals(" + deviceType + ")"; - } - } - - public static Predicate<IMedium> deviceTypeEquals(DeviceType deviceType) { - return new DeviceTypeEquals(deviceType); - } - - public static enum HasParent implements Predicate<IMedium> { - INSTANCE; - - @Override - public boolean apply(IMedium arg0) { - return arg0.getParent() != null; - } - - @Override - public String toString() { - return "hasParent()"; - } - } - - public static Predicate<IMedium> hasParent() { - return HasParent.INSTANCE; - } - - public static class MachineIdsContain implements Predicate<IMedium> { - private final String id; - - public MachineIdsContain(String id) { - this.id = checkNotNull(id, "id"); - } - - @Override - public boolean apply(IMedium arg0) { - return any(arg0.getMachineIds(), equalTo(id)); - } - - @Override - public String toString() { - return "machineIdsContain(" + id + ")"; - } - } - - public static Predicate<IMedium> machineIdsContain(String id) { - return new MachineIdsContain(id); - } - -}
http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/RetryIfSocketNotYetOpen.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/RetryIfSocketNotYetOpen.java b/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/RetryIfSocketNotYetOpen.java deleted file mode 100644 index 73ad969..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/RetryIfSocketNotYetOpen.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.predicates; - -import static org.jclouds.util.Predicates2.retry; - -import java.util.concurrent.TimeUnit; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Named; - -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.compute.reference.ComputeServiceConstants.Timeouts; -import org.jclouds.logging.Logger; -import org.jclouds.predicates.SocketOpen; - -import com.google.common.base.Predicate; -import com.google.common.net.HostAndPort; - -/** - * - * - * Not singleton as seconds are mutable - */ -public class RetryIfSocketNotYetOpen implements Predicate<HostAndPort> { - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - private Logger logger = Logger.NULL; - private final SocketOpen socketTester; - private long timeoutValue; - private TimeUnit timeoutUnits; - - - public RetryIfSocketNotYetOpen(SocketOpen socketTester, Logger logger, long timeoutValue, TimeUnit timeoutUnits) { - this.socketTester = socketTester; - this.logger = logger; - this.timeoutValue = timeoutValue; - this.timeoutUnits = timeoutUnits; - } - - public RetryIfSocketNotYetOpen(SocketOpen socketTester, Logger logger) { - this(socketTester, logger, 0, TimeUnit.MILLISECONDS); - } - - @Inject - public RetryIfSocketNotYetOpen(SocketOpen socketTester, Timeouts timeouts) { - this(socketTester, Logger.NULL, timeouts.portOpen, TimeUnit.MILLISECONDS); - } - - public RetryIfSocketNotYetOpen milliseconds(long milliseconds) { - this.timeoutValue = milliseconds; - this.timeoutUnits = TimeUnit.MILLISECONDS; - return this; - } - - public RetryIfSocketNotYetOpen seconds(long seconds) { - this.timeoutValue = seconds; - this.timeoutUnits = TimeUnit.SECONDS; - return this; - } - - @Override - public String toString() { - return "retryIfSocketNotYetOpen(" + timeoutValue + " " + timeoutUnits + ")"; - } - - @Override - public boolean apply(HostAndPort socket) { - logger.debug(">> blocking on socket %s for %d %s", socket, timeoutValue, timeoutUnits); - // Specify a retry period of 1s, expressed in the same time units. - long period = timeoutUnits.convert(1, TimeUnit.SECONDS); - boolean passed = retry(socketTester, timeoutValue, period, timeoutUnits).apply(socket); - if (passed) - logger.debug("<< socket %s opened", socket); - else - logger.warn("<< socket %s didn't open after %d %s", socket, timeoutValue, timeoutUnits); - return passed; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshAvailable.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshAvailable.java b/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshAvailable.java deleted file mode 100644 index 54546bf..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshAvailable.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.predicates; - -import static org.jclouds.compute.options.RunScriptOptions.Builder.wrapInInitScript; - -import javax.annotation.Resource; -import javax.inject.Named; - -import org.jclouds.compute.ComputeServiceContext; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.logging.Logger; -import org.jclouds.ssh.SshException; - -import com.google.common.base.Predicate; - -public class SshAvailable implements Predicate<String> { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final ComputeServiceContext context; - - public SshAvailable(ComputeServiceContext context) { - this.context = context; - } - - @Override - public boolean apply(String nodeId) { - boolean sshDaemonIsRunning = false; - try { - if (context.getComputeService() - .runScriptOnNode(nodeId, "id", wrapInInitScript(false).runAsRoot(false)) - .getExitStatus() == 0) { - logger.debug("Got response from ssh daemon running on %s", nodeId); - sshDaemonIsRunning = true; - } - } catch (SshException e) { - logger.debug("No response from ssh daemon running on %s", nodeId); - return sshDaemonIsRunning; - } - return sshDaemonIsRunning; - } - - @Override - public String toString() { - return "sshAvailable()"; - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshResponds.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshResponds.java b/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshResponds.java deleted file mode 100644 index 9ab3dba..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/predicates/SshResponds.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.predicates; - -import javax.annotation.Resource; - -import org.jclouds.logging.Logger; -import org.jclouds.ssh.SshClient; -import org.jclouds.ssh.SshException; - -import com.google.common.base.Predicate; - -public class SshResponds implements Predicate<SshClient> { - @Resource - protected Logger logger = Logger.NULL; - - @Override - public boolean apply(SshClient client) { - - try { - client.connect(); - if (client.exec("id").getExitStatus() == 0) { - return true; - } - } catch (SshException e) { - logger.trace("No response from ssh daemon connecting to %s: %s", client, e.getMessage()); - } finally { - if (client != null) { - client.disconnect(); - } - } - return false; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/settings/KeyboardScancodes.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/settings/KeyboardScancodes.java b/virtualbox/src/main/java/org/jclouds/virtualbox/settings/KeyboardScancodes.java deleted file mode 100644 index 7c650ef..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/settings/KeyboardScancodes.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.settings; - -import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.Multimap; - -/** - * - * @see <a href="http://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html" /> - */ -public class KeyboardScancodes { - - public static final Multimap<String, Integer> NORMAL_KEYBOARD_BUTTON_MAP_LIST = ImmutableMultimap - .<String, Integer> builder() - - .putAll("1", 0x02, 0x82) - .putAll("2", 0x03, 0x83) - .putAll("3", 0x04, 0x84) - .putAll("4", 0x05, 0x85) - .putAll("5", 0x06, 0x86) - .putAll("6", 0x07, 0x87) - .putAll("7", 0x08, 0x88) - .putAll("8", 0x09, 0x89) - .putAll("9", 0x0a, 0x8a) - .putAll("0", 0x0b, 0x8b) - - .putAll("-", 0x0c, 0x8c) - .putAll("=", 0x0d, 0x8d) - .putAll("Tab", 0x0f, 0x8f) - .putAll("q", 0x10, 0x90) - .putAll("w", 0x11, 0x91) - .putAll("e", 0x12, 0x92) - .putAll("r", 0x13, 0x93) - .putAll("t", 0x14, 0x94) - .putAll("y", 0x15, 0x95) - .putAll("u", 0x16, 0x96) - .putAll("i", 0x17, 0x97) - .putAll("o", 0x18, 0x98) - .putAll("p", 0x19, 0x99) - - .putAll("Q", 0x2a, 0x10, 0xaa) - .putAll("W", 0x2a, 0x11, 0xaa) - .putAll("E", 0x2a, 0x12, 0xaa) - .putAll("R", 0x2a, 0x13, 0xaa) - .putAll("T", 0x2a, 0x14, 0xaa) - .putAll("Y", 0x2a, 0x15, 0xaa) - .putAll("U", 0x2a, 0x16, 0xaa) - .putAll("I", 0x2a, 0x17, 0xaa) - .putAll("O", 0x2a, 0x18, 0xaa) - .putAll("P", 0x2a, 0x19, 0xaa) - - .putAll("a", 0x1e, 0x9e) - .putAll("s", 0x1f, 0x9f) - .putAll("d", 0x20, 0xa0) - .putAll("f", 0x21, 0xa1) - .putAll("g", 0x22, 0xa2) - .putAll("h", 0x23, 0xa3) - .putAll("j", 0x24, 0xa4) - .putAll("k", 0x25, 0xa5) - .putAll("l", 0x26, 0xa6) - - .putAll("A", 0x2a, 0x1e, 0xaa, 0x9e) - .putAll("S", 0x2a, 0x1f, 0xaa, 0x9f) - .putAll("D", 0x2a, 0x20, 0xaa, 0xa0) - .putAll("F", 0x2a, 0x21, 0xaa, 0xa1) - .putAll("G", 0x2a, 0x22, 0xaa, 0xa2) - .putAll("H", 0x2a, 0x23, 0xaa, 0xa3) - .putAll("J", 0x2a, 0x24, 0xaa, 0xa4) - .putAll("K", 0x2a, 0x25, 0xaa, 0xa5) - .putAll("L", 0x2a, 0x26, 0xaa, 0xa6) - - .putAll(") ", 0x27, 0xa7) - .putAll("\"", 0x2a, 0x28, 0xaa, 0xa8) - .putAll("\"", 0x28, 0xa8) - .putAll("\\", 0x2b, 0xab) - .putAll("|", 0x2a, 0x2b, 0xaa, 0x8b) - .putAll("[", 0x1a, 0x9a) - .putAll("", 0x1b, 0x9b) - .putAll("<", 0x2a, 0x33, 0xaa, 0xb3) - .putAll(">", 0x2a, 0x34, 0xaa, 0xb4) - .putAll("$", 0x2a, 0x05, 0xaa, 0x85) - .putAll("+", 0x2a, 0x0d, 0xaa, 0x8d) - - .putAll("z", 0x2c, 0xac) - .putAll("x", 0x2d, 0xad) - .putAll("c", 0x2e, 0xae) - .putAll("v", 0x2f, 0xaf) - .putAll("b", 0x30, 0xb0) - .putAll("n", 0x31, 0xb1) - .putAll("m", 0x32, 0xb2) - .putAll("Z", 0x2a, 0x2c, 0xaa, 0xac) - .putAll("X", 0x2a, 0x2d, 0xaa, 0xad) - .putAll("C", 0x2a, 0x2e, 0xaa, 0xae) - .putAll("V", 0x2a, 0x2f, 0xaa, 0xaf) - .putAll("B", 0x2a, 0x30, 0xaa, 0xb0) - .putAll("N", 0x2a, 0x31, 0xaa, 0xb1) - .putAll("M", 0x2a, 0x32, 0xaa, 0xb2) - - .putAll(",", 0x33, 0xb3) - .putAll(".", 0x34, 0xb4) - .putAll("/", 0x35, 0xb5) - .putAll(":", 0x2a, 0x27, 0xaa, 0xa7) - .putAll("%", 0x2a, 0x06, 0xaa, 0x86) - .putAll("_", 0x2a, 0x0c, 0xaa, 0x8c) - .putAll("&", 0x2a, 0x08, 0xaa, 0x88) - .putAll("(", 0x2a, 0x0a, 0xaa, 0x8a) - .putAll(")", 0x2a, 0x0b, 0xaa, 0x8b) - .putAll("#", 0x2a, 0x04, 0xaa, 0x85) - - .build(); - - - public static final Multimap<String, Integer> SPECIAL_KEYBOARD_BUTTON_MAP_LIST = ImmutableMultimap - .<String, Integer> builder() - - .putAll("<Enter>", 0x1c, 0x9c) - .putAll("<Backspace>", 0x0e, 0x8e) - .putAll("<Spacebar>", 0x39, 0xb9) - .putAll("<Return>", 0x1c, 0x9c) - .putAll("<Esc>", 0x01, 0x81) - .putAll("<Tab>", 0x0f, 0x8f) - .putAll("<KillX>", 0x1d, 0x38, 0x0e) - - .putAll("<Up>", 0x48, 0xc8) - .putAll("<Down>", 0x50, 0xd0) - .putAll("<PageUp>", 0x49, 0xc9) - .putAll("<PageDown>", 0x51, 0xd1) - .putAll("<End>", 0x4f, 0xcf) - .putAll("<Insert>", 0x52, 0xd2) - .putAll("<Delete>", 0x53, 0xd3) - .putAll("<Left>", 0x4b, 0xcb) - .putAll("<Right>", 0x4d, 0xcd) - .putAll("<Home>", 0x47, 0xc7) - - .putAll("<F1>", 0x3b) - .putAll("<F2>", 0x3c) - .putAll("<F3>", 0x3d) - .putAll("<F4>", 0x3e) - .putAll("<F5>", 0x3f) - .putAll("<F6>", 0x40) - .putAll("<F7>", 0x41) - .putAll("<F8>", 0x42) - .putAll("<F9>", 0x43) - .putAll("<F10>", 0x44) - .build(); - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java deleted file mode 100644 index b304f0d..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/DeleteGShadowLock.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; - -import com.google.common.collect.ImmutableList; - -/** - * Deletes /etc/gshadow.lock. see https://bugs.launchpad.net/ubuntu/+source/qemu/+bug/732864. - */ -public class DeleteGShadowLock implements Statement { - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return "rm -f /etc/passwd.lock /etc/group.lock /etc/gshadow.lock"; - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java deleted file mode 100644 index 421419b..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/EnableNetworkInterface.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; -import static org.jclouds.scriptbuilder.domain.Statements.exec; - -import java.util.List; - -import com.google.common.collect.Lists; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.scriptbuilder.domain.StatementList; -import org.jclouds.virtualbox.domain.NetworkInterfaceCard; - -/** - * Up the network interface chosen - */ -public class EnableNetworkInterface implements Statement { - - private final StatementList statements; - - public EnableNetworkInterface(NetworkInterfaceCard networkInterfaceCard) { - int slot = (int) networkInterfaceCard.getSlot(); - String iface = null; - switch (slot) { - case 0: - iface = "eth0"; - break; - case 1: - iface = "eth1"; - break; - case 2: - iface = "eth2"; - break; - case 3: - iface = "eth3"; - break; - default: - throw new IllegalArgumentException("slot must be 0,1,2,3 (was: " + slot + ")"); - } - this.statements = new StatementList(getStatements(iface)); - } - - private List<Statement> getStatements(String iface) { - List<Statement> statements = Lists.newArrayList(); - statements.add(exec(String.format("echo auto %s >> /etc/network/interfaces", iface))); - statements.add(exec(String.format("echo iface %s inet dhcp >> /etc/network/interfaces", iface))); - statements.add(exec("/etc/init.d/networking restart")); - return statements; - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return statements.functionDependencies(family); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return statements.render(family); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/GetIPAddressFromMAC.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/GetIPAddressFromMAC.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/GetIPAddressFromMAC.java deleted file mode 100644 index 7f9f463..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/GetIPAddressFromMAC.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.Map; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.scriptbuilder.util.Utils; -import org.jclouds.virtualbox.functions.MacAddressToBSD; - -import com.google.common.base.Joiner; -import com.google.common.base.Splitter; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -public class GetIPAddressFromMAC implements Statement { - - public static final Map<OsFamily, String> OS_TO_ARP = ImmutableMap - .of(OsFamily.UNIX, - "MAC={macAddress} && [[ `uname -s` = \"Darwin\" ]] && MAC={macAddressBsd}\n arp -an | grep $MAC\n", - OsFamily.WINDOWS, "set MAC={macAddress} arp -a | Findstr %MAC%"); - - private String macAddress; - private String macAddressBsd; - - public GetIPAddressFromMAC(String macAddress) { - this(Joiner.on(":").join(Splitter.fixedLength(2).split(macAddress)).toLowerCase(), - MacAddressToBSD.INSTANCE.apply(Joiner.on(":").join(Splitter.fixedLength(2).split(macAddress)).toLowerCase())); - } - - public GetIPAddressFromMAC(String macAddress, String macAddressBsd) { - checkNotNull(macAddress, "macAddress"); - checkArgument(macAddress.length() == 17); - this.macAddress = macAddress; - checkNotNull(macAddressBsd, "macAddressBsd"); - this.macAddressBsd = macAddressBsd; - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - StringBuilder arp = new StringBuilder(); - arp.append(Utils.replaceTokens(OS_TO_ARP.get(family), ImmutableMap.of( - "macAddress", macAddress, "macAddressBsd", macAddressBsd))); - return arp.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java deleted file mode 100644 index 3c2a1e3..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/InstallGuestAdditions.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.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.List; - -import javax.annotation.Resource; -import javax.inject.Named; - -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.logging.Logger; -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.scriptbuilder.domain.StatementList; -import org.jclouds.virtualbox.domain.IsoImage; -import org.jclouds.virtualbox.domain.StorageController; -import org.jclouds.virtualbox.domain.VmSpec; - -import com.google.common.base.Predicate; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - -/** - * Mounts the DVD with guest additions that was downloaded and attached as removable storage. If no - * guest additions is attached to the vmspec then it is downloaded. - */ -public class InstallGuestAdditions implements Statement { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - private final StatementList statements; - - public InstallGuestAdditions(VmSpec vmSpecification, String vboxVersion) { - this.statements = new StatementList(getStatements(vmSpecification, vboxVersion)); - } - - private List<Statement> getStatements(VmSpec vmSpecification, String vboxVersion) { - List<Statement> statements = Lists.newArrayList(); - statements.add(call("installModuleAssistantIfNeeded")); - String mountPoint = "/mnt"; - if (Iterables.tryFind(vmSpecification.getControllers(), new Predicate<StorageController>() { - @Override - public boolean apply(StorageController input) { - if (!input.getIsoImages().isEmpty()) { - for (IsoImage iso : input.getIsoImages()) { - if (iso.getSourcePath().contains("VBoxGuestAdditions_")) { - return true; - } - } - } - return false; - } - }).isPresent()) { - statements.add(exec("mount -t iso9660 /dev/cdrom1 " + mountPoint)); - } else { - String vboxGuestAdditionsIso = "VBoxGuestAdditions_" + vboxVersion + ".iso"; - URI download = URI.create("http://download.virtualbox.org/virtualbox/" + vboxVersion + "/" - + vboxGuestAdditionsIso); - statements.add(call("setupPublicCurl")); - statements.add(saveHttpResponseTo(download, "{tmp}{fs}", vboxGuestAdditionsIso));// - statements.add(exec(String.format("mount -o loop {tmp}{fs}%s %s", vboxGuestAdditionsIso, mountPoint))); - } - statements.add(exec(String.format("%s%s", mountPoint, "/VBoxLinuxAdditions.run --nox11"))); - return statements; - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return statements.render(family); - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return statements.functionDependencies(family); - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Md5.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Md5.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Md5.java deleted file mode 100644 index 36e30f2..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Md5.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; - -import com.google.common.collect.ImmutableList; - -public class Md5 implements Statement { - - private final String filePath; - - public Md5(String filePath) { - this.filePath = checkNotNull(filePath, "filePath"); - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return String.format("command -v md5sum >/dev/null 2>&1 && md5sum %s | awk '{print $1}' " - + "|| command -v md5 >/dev/null 2>&1 && md5 %s | awk '{ print $4 }'", filePath, filePath); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/PasswordlessSudo.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/PasswordlessSudo.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/PasswordlessSudo.java deleted file mode 100644 index 0078730..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/PasswordlessSudo.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; - -import com.google.common.collect.ImmutableList; - -/** - * Assign to user the passwordless sudo rights - */ -public class PasswordlessSudo implements Statement { - - private final String user; - - public PasswordlessSudo(String user) { - this.user = user; - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return String.format("touch /etc/sudoers.d/passwordless && echo \"%s ALL = NOPASSWD: ALL\" > /etc/sudoers.d/passwordless && chmod 0440 /etc/sudoers.d/passwordless", user); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/ScanNetworkWithPing.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/ScanNetworkWithPing.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/ScanNetworkWithPing.java deleted file mode 100644 index 68f6ed9..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/ScanNetworkWithPing.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.Map; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.scriptbuilder.util.Utils; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - -public class ScanNetworkWithPing implements Statement { - - public static final Map<OsFamily, String> OS_TO_PING = ImmutableMap - .of(OsFamily.UNIX, - "for i in {1..254} ; do ping -c 1 -t 1 {network}.$i & done", - OsFamily.WINDOWS, "TODO"); - - private String network; - - public ScanNetworkWithPing(String network) { - this.network = checkNotNull(network, "network"); - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - network = network.substring(0, network.lastIndexOf(".")); - StringBuilder arp = new StringBuilder(); - arp.append(Utils.replaceTokens(OS_TO_PING.get(family), ImmutableMap.of("network", network))); - return arp.toString(); - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetHostname.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetHostname.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetHostname.java deleted file mode 100644 index eae7006..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetHostname.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; - -import com.google.common.collect.ImmutableList; - -/** - * Set hostname - */ -public class SetHostname implements Statement { - - private final String publicIpAddress; - - public SetHostname(String publicIpAddress) { - this.publicIpAddress = checkNotNull(publicIpAddress, "publicIpAddress"); - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return String.format("sudo hostname %s", publicIpAddress); - } - -} - http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java deleted file mode 100644 index 4353fe5..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/SetIpAddress.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import static com.google.common.base.Preconditions.checkNotNull; - -import org.jclouds.scriptbuilder.domain.OsFamily; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.virtualbox.domain.NetworkInterfaceCard; - -import com.google.common.collect.ImmutableList; - -/** - * Sets the ipaddress using ssh. Used for host-only networking - */ -public class SetIpAddress implements Statement { - - private String script; - - public SetIpAddress(NetworkInterfaceCard networkInterfaceCard) { - String ipAddress = networkInterfaceCard.getNetworkAdapter().getStaticIp(); - checkNotNull(ipAddress, "ip address"); - int slot = (int) networkInterfaceCard.getSlot(); - String iface = null; - switch (slot) { - case 0: - iface = "eth0"; - break; - case 1: - iface = "eth1"; - break; - case 2: - iface = "eth2"; - break; - case 3: - iface = "eth3"; - break; - default: - throw new IllegalArgumentException("slot must be 0,1,2,3 (was: " + slot + ")"); - } - script = String.format("ifconfig %s %s;", iface, ipAddress); - } - - @Override - public Iterable<String> functionDependencies(OsFamily family) { - return ImmutableList.of(); - } - - @Override - public String render(OsFamily family) { - if (checkNotNull(family, "family") == OsFamily.WINDOWS) - throw new UnsupportedOperationException("windows not yet implemented"); - return script; - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java b/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java deleted file mode 100644 index 6f27be6..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/statements/Statements.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.statements; - -import org.jclouds.scriptbuilder.ScriptBuilder; -import org.jclouds.scriptbuilder.domain.Statement; - -/** - * VboxManage statements used in shell scripts . - */ -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 ScriptBuilder.call("exportIpAddressFromVmNamed", vmName); - } - } http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/util/IMediumAttachments.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/util/IMediumAttachments.java b/virtualbox/src/main/java/org/jclouds/virtualbox/util/IMediumAttachments.java deleted file mode 100644 index d9a2652..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/util/IMediumAttachments.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.util; - -import org.virtualbox_4_2.IMedium; -import org.virtualbox_4_2.IMediumAttachment; - -import com.google.common.base.Function; - -public class IMediumAttachments { - static enum ToMedium implements Function<IMediumAttachment, IMedium> { - INSTANCE; - @Override - public IMedium apply(IMediumAttachment arg0) { - return arg0.getMedium(); - } - - @Override - public String toString() { - return "toMedium()"; - } - }; - - public static Function<IMediumAttachment, IMedium> toMedium() { - return ToMedium.INSTANCE; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java b/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java deleted file mode 100644 index c1f2623..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineController.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.util; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static java.util.concurrent.TimeUnit.SECONDS; -import static org.jclouds.util.Predicates2.retry; - -import java.util.List; - -import javax.annotation.Resource; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.logging.Logger; -import org.jclouds.virtualbox.domain.ExecutionType; -import org.jclouds.virtualbox.functions.LaunchMachineIfNotAlreadyRunning; -import org.virtualbox_4_2.AdditionsFacilityStatus; -import org.virtualbox_4_2.AdditionsFacilityType; -import org.virtualbox_4_2.AdditionsRunLevelType; -import org.virtualbox_4_2.IAdditionsFacility; -import org.virtualbox_4_2.IMachine; -import org.virtualbox_4_2.IProgress; -import org.virtualbox_4_2.ISession; -import org.virtualbox_4_2.IVirtualBox; -import org.virtualbox_4_2.LockType; -import org.virtualbox_4_2.MachineState; -import org.virtualbox_4_2.VirtualBoxManager; - -import com.google.common.base.Function; -import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import com.google.common.base.Strings; -import com.google.common.base.Supplier; -import com.google.common.collect.Iterables; -import com.google.inject.Inject; - -/** - * Utilities to manage VirtualBox machine life cycle. - */ - -@Singleton -public class MachineController { - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final Supplier<VirtualBoxManager> manager; - private final MachineUtils machineUtils; - private final ExecutionType executionType; - - @Inject - public MachineController(Supplier<VirtualBoxManager> manager, MachineUtils machineUtils, ExecutionType executionType) { - this.manager = manager; - this.machineUtils = machineUtils; - this.executionType = executionType; - } - - public ISession ensureMachineIsLaunched(String vmName) { - ISession session = null; - IMachine machine = manager.get().getVBox().findMachine(vmName); - while (!machine.getState().equals(MachineState.Running)) { - try { - session = machineUtils.applyForMachine(vmName, new LaunchMachineIfNotAlreadyRunning(manager.get(), - executionType, "")); - } catch (RuntimeException e) { - if (e.getMessage().contains( - "org.virtualbox_4_2.VBoxException: VirtualBox error: The given session is busy (0x80BB0007)")) { - throw e; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - - String guestAdditionsInstalled = machineUtils.sharedLockMachineAndApplyToSession(vmName, - new Function<ISession, String>() { - @Override - public String apply(ISession session) { - retry(new FacilitiesPredicate(session), 15, 3, SECONDS).apply(4); - String guestAdditionsInstalled = session.getConsole().getGuest().getAdditionsVersion(); - return guestAdditionsInstalled; - } - - }); - if (!Strings.nullToEmpty(guestAdditionsInstalled).isEmpty()) { - logger.debug("<< guest additions(%s) installed on vm(%s)", guestAdditionsInstalled, vmName); - waitVBoxServiceIsActive(vmName); - } else { - logger.debug("<< guest additions not available on(%s)", vmName); - } - return checkNotNull(session, "session"); - } - - public ISession ensureMachineHasPowerDown(String vmName) { - ISession session = machineUtils.sharedLockMachineAndApplyToSession(vmName, new Function<ISession, ISession>() { - @Override - public ISession apply(ISession session) { - IProgress powerdownIProgress = session.getConsole().powerDown(); - powerdownIProgress.waitForCompletion(-1); - return session; - } - }); - return checkNotNull(session, "session"); - } - - /** - * if machine supports ACPI it can be shutdown gently - not powerdown() - * http://askubuntu.com/questions/82015/shutting-down-ubuntu-server-running-in-headless-virtualbox - */ - public ISession ensureMachineIsShutdown(String vmName) { - ISession session = machineUtils.sharedLockMachineAndApplyToSession(vmName, new Function<ISession, ISession>() { - @Override - public ISession apply(ISession session) { - session.getConsole().powerButton(); - return session; - } - }); - checkState( - retry(new MachineStatePredicate(manager.get().getVBox(), vmName), 15, 3, SECONDS).apply( - MachineState.PoweredOff), "vm(%s) is not shutdown correctly", vmName); - return checkNotNull(session, "session"); - } - - public void ensureMachineIsPaused(String vmName) { - while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Paused)) { - try { - machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() { - @Override - public Void apply(ISession session) { - session.getConsole().pause(); - return null; - } - }); - } catch (RuntimeException e) { - // sometimes the machine might be powered of between the while - // test and the call to - // lockSessionOnMachineAndApply - if (e.getMessage().contains("Invalid machine state: Paused")) { - return; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - } - - public void ensureMachineIsResumed(String vmName) { - while (!manager.get().getVBox().findMachine(vmName).getState().equals(MachineState.Running)) { - try { - machineUtils.lockSessionOnMachineAndApply(vmName, LockType.Shared, new Function<ISession, Void>() { - @Override - public Void apply(ISession session) { - session.getConsole().resume(); - return null; - } - }); - } catch (RuntimeException e) { - // sometimes the machine might be powered of between the while - // test and the call to lockSessionOnMachineAndApply - if (e.getMessage().contains("Invalid machine state: Resumed")) { - return; - } else if (e.getMessage().contains("VirtualBox error: The object is not ready")) { - continue; - } else { - throw e; - } - } - } - } - - private void waitVBoxServiceIsActive(final String vmName) { - machineUtils.sharedLockMachineAndApplyToSession(vmName, new Function<ISession, Void>() { - - @Override - public Void apply(ISession session) { - checkState( - retry(new AdditionsStatusPredicate(session), 10, 2, SECONDS).apply(AdditionsRunLevelType.Userland), - "timed out waiting for additionsRunLevelType to be %s", AdditionsRunLevelType.Userland); - checkState(retry(new FacilitiesPredicate(session), 15, 3, SECONDS).apply(4), - "timed out waiting for 4 running facilities"); - Optional<IAdditionsFacility> vboxServiceFacility = Optional.absent(); - while (!vboxServiceFacility.isPresent()) { - List<IAdditionsFacility> facilities = session.getConsole().getGuest().getFacilities(); - vboxServiceFacility = Iterables.tryFind(facilities, new Predicate<IAdditionsFacility>() { - @Override - public boolean apply(IAdditionsFacility additionsFacility) { - return additionsFacility.getType().equals(AdditionsFacilityType.VBoxService) - && additionsFacility.getStatus().equals(AdditionsFacilityStatus.Active); - } - }); - } - logger.debug("<< virtualbox service ready on vm(%s)", vmName); - return null; - } - }); - } - - private static class AdditionsStatusPredicate implements Predicate<AdditionsRunLevelType> { - private final ISession session; - - AdditionsStatusPredicate(ISession session) { - this.session = session; - } - - @Override - public boolean apply(AdditionsRunLevelType input) { - return session.getConsole().getGuest().getAdditionsStatus(input); - } - } - - private static class FacilitiesPredicate implements Predicate<Integer> { - private final ISession session; - - FacilitiesPredicate(ISession session) { - this.session = session; - } - - @Override - public boolean apply(Integer input) { - return session.getConsole().getGuest().getFacilities().size() == input; - } - } - - private static class MachineStatePredicate implements Predicate<MachineState> { - private final IVirtualBox virtualBox; - private final String vmName; - - MachineStatePredicate(IVirtualBox virtualBox, String vmName) { - this.virtualBox = virtualBox; - this.vmName = vmName; - } - - @Override - public boolean apply(MachineState input) { - MachineState state = virtualBox.findMachine(vmName).getState(); - return state.equals(input); - } - } - -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineNameOrIdAndNicSlot.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineNameOrIdAndNicSlot.java b/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineNameOrIdAndNicSlot.java deleted file mode 100644 index 04a6abe..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineNameOrIdAndNicSlot.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.util; - -import static com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; - - -import com.google.common.base.Objects; -import com.google.common.base.Splitter; -import com.google.common.base.Strings; -import com.google.common.collect.Iterables; - - -/** - * An immutable representation of a MachineNameOrId and NIC port. - * - * <p>Example usage: - * <pre> - * MachineNameOrIdAndNicSlot mp = MachineNameOrIdAndNicSlot.fromString("myMachine:1"); - * hp.getMachineNameOrId(); // returns "myMachine" - * hp.getSlot(); // returns 1 - * hp.toString(); // returns "myMachine:1" - * </pre> - */ -public final class MachineNameOrIdAndNicSlot { - - private static final String SEPARATOR = ":"; - -/** IMachine name or id*/ - private final String machineNameOrId; - - /** Validated NIC slot number in the range [0..3] */ - private final long slot; - - private MachineNameOrIdAndNicSlot(String machineNameOrId, long slot) { - this.machineNameOrId = machineNameOrId; - this.slot = slot; - } - - public String getMachineNameOrId() { - return machineNameOrId; - } - - public boolean hasSlot() { - return slot >= 0; - } - - public long getSlot() { - checkState(hasSlot()); - return slot; - } - - public String getSlotText() { - checkState(hasSlot()); - return String.valueOf(slot); - } - - public static MachineNameOrIdAndNicSlot fromParts(String machineNameOrId, long slot) { - checkArgument(isValidSlot(slot)); - return new MachineNameOrIdAndNicSlot(checkNotNull(machineNameOrId, "machineNameOrId"), slot); - } - - public static MachineNameOrIdAndNicSlot fromString(String machineNameOrIdAndNicSlotString) { - Iterable<String> splittedString = Splitter.on(SEPARATOR).split(machineNameOrIdAndNicSlotString); - checkState(Iterables.size(splittedString) == 2); - String machineNameOrId = Strings.nullToEmpty(Iterables.get(splittedString, 0)); - String nicSlotString = Strings.nullToEmpty(Iterables.get(splittedString, 1)); - checkArgument(!nicSlotString.startsWith("+"), "Unparseable slot number: %s", nicSlotString); - try { - long slot = Long.parseLong(nicSlotString); - checkArgument(isValidSlot(slot), "Slot number out of range: %s", nicSlotString); - return new MachineNameOrIdAndNicSlot(machineNameOrId, slot); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("Unparseable slot number: " + nicSlotString); - } - } - - public MachineNameOrIdAndNicSlot withDefaultSlot(int defaultSlot) { - checkArgument(isValidSlot(defaultSlot)); - if (hasSlot() || slot == defaultSlot) { - return this; - } - return new MachineNameOrIdAndNicSlot(machineNameOrId, defaultSlot); - } - - @Override - public boolean equals(Object other) { - if (this == other) { - return true; - } - if (other instanceof MachineNameOrIdAndNicSlot) { - MachineNameOrIdAndNicSlot that = (MachineNameOrIdAndNicSlot) other; - return Objects.equal(this.machineNameOrId, that.machineNameOrId) - && this.slot == that.slot; - } - return false; - } - - @Override - public int hashCode() { - return Objects.hashCode(machineNameOrId, slot); - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("machineNameOrId", machineNameOrId) - .add("nicSlot", slot) - .toString(); - } - - private static boolean isValidSlot(long slot) { - return slot >= 0l && slot <= 3l; - } -} http://git-wip-us.apache.org/repos/asf/jclouds-labs/blob/abd0be21/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java ---------------------------------------------------------------------- diff --git a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java b/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java deleted file mode 100644 index f4bae13..0000000 --- a/virtualbox/src/main/java/org/jclouds/virtualbox/util/MachineUtils.java +++ /dev/null @@ -1,280 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF 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.util; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.Uninterruptibles; -import com.google.inject.Inject; -import org.jclouds.compute.callables.RunScriptOnNode; -import org.jclouds.compute.callables.RunScriptOnNode.Factory; -import org.jclouds.compute.domain.ExecResponse; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.options.RunScriptOptions; -import org.jclouds.compute.reference.ComputeServiceConstants; -import org.jclouds.logging.Logger; -import org.jclouds.scriptbuilder.domain.Statement; -import org.jclouds.util.Throwables2; -import org.virtualbox_4_2.IMachine; -import org.virtualbox_4_2.ISession; -import org.virtualbox_4_2.LockType; -import org.virtualbox_4_2.SessionState; -import org.virtualbox_4_2.VBoxException; -import org.virtualbox_4_2.VirtualBoxManager; - -import javax.annotation.Resource; -import javax.inject.Named; -import javax.inject.Singleton; -import java.util.concurrent.TimeUnit; - -import static com.google.common.base.Preconditions.checkNotNull; -import static com.google.common.base.Preconditions.checkState; -import static org.jclouds.util.Predicates2.retry; - -/** - * Utilities for executing functions on a VirtualBox machine. - */ - -@Singleton -public class MachineUtils { - public static final String IP_V4_ADDRESS_PATTERN = "^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." - + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." - + "([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"; - - @Resource - @Named(ComputeServiceConstants.COMPUTE_LOGGER) - protected Logger logger = Logger.NULL; - - private final Supplier<VirtualBoxManager> manager; - private final Factory scriptRunner; - - - @Inject - public MachineUtils(Supplier<VirtualBoxManager> manager, RunScriptOnNode.Factory scriptRunner) { - this.manager = manager; - this.scriptRunner = scriptRunner; - } - - public ListenableFuture<ExecResponse> runScriptOnNode(NodeMetadata metadata, Statement statement, - RunScriptOptions options) { - return scriptRunner.submit(metadata, statement, options); - } - - /** - * Locks the machine and executes the given function using the machine matching the given id. - * Since the machine is locked it is possible to perform some modifications to the IMachine. - * <p/> - * Unlocks the machine before returning. - * - * @param machineId - * the id of the machine - * @param function - * the function to execute - * @return the result from applying the function to the machine. - */ - public <T> T writeLockMachineAndApply(final String machineId, final Function<IMachine, T> function) { - return lockSessionOnMachineAndApply(machineId, LockType.Write, new Function<ISession, T>() { - - @Override - public T apply(ISession session) { - return function.apply(session.getMachine()); - } - - @Override - public String toString() { - return function.toString(); - } - - }); - } - - /** - * Locks the machine and executes the given function using the machine matching the given id. The - * machine is write locked and modifications to the session that reflect on the machine can be - * done safely. - * <p/> - * Unlocks the machine before returning. - * - * @param machineId - * the id of the machine - * @param function - * the function to execute - * @return the result from applying the function to the machine. - */ - public <T> T writeLockMachineAndApplyToSession(final String machineId, final Function<ISession, T> function) { - return lockSessionOnMachineAndApply(machineId, LockType.Write, function); - } - - /** - * Locks the machine and executes the given function using the machine matching the given id. The - * machine is read locked, which means that settings can be read safely (but not changed) by - * function. - * <p/> - * Unlocks the machine before returning. - * - * @param machineId - * the id of the machine - * @param function - * the function to execute - * @return the result from applying the function to the machine. - */ - public <T> T sharedLockMachineAndApply(final String machineId, final Function<IMachine, T> function) { - return lockSessionOnMachineAndApply(machineId, LockType.Shared, new Function<ISession, T>() { - - @Override - public T apply(ISession session) { - return function.apply(session.getMachine()); - } - - @Override - public String toString() { - return function.toString(); - } - - }); - } - - /** - * Locks the machine and executes the given function to the session using the machine matching - * the given id. The machine is read locked, which means that settings can be read safely (but - * not changed) by function. - * <p/> - * Unlocks the machine before returning. - * - * @param machineId - * the id of the machine - * @param function - * the function to execute - * @return the result from applying the function to the machine. - */ - public <T> T sharedLockMachineAndApplyToSession(final String machineId, final Function<ISession, T> function) { - return lockSessionOnMachineAndApply(machineId, LockType.Shared, function); - } - - /** - * Locks the machine and executes the given function using the current session. Since the machine - * is locked it is possible to perform some modifications to the IMachine. - * <p/> - * Unlocks the machine before returning. - * - * Tries to obtain a lock 15 times before giving up waiting 1 sec between tries. When no machine - * is found null is returned. - * - * @param type - * the kind of lock to use when initially locking the machine. - * @param machineId - * the id of the machine - * @param function - * the function to execute - * @return the result from applying the function to the session. - */ - protected <T> T lockSessionOnMachineAndApply(String machineId, LockType type, Function<ISession, T> function) { - int retries = 15; - ISession session = checkNotNull(lockSession(machineId, type, retries), "session"); - try { - return function.apply(session); - } catch (VBoxException e) { - throw new RuntimeException(String.format("error applying %s to %s with %s lock: %s", function, machineId, - type, e.getMessage()), e); - } finally { - // this is a workaround for shared lock type, where session state is not updated immediately - if (type == LockType.Shared) { - Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); - } - if (session.getState().equals(SessionState.Locked)) { - session.unlockMachine(); - } - if (!session.getState().equals(SessionState.Unlocked)) { - checkSessionIsUnlocked(session, 5, 3L, TimeUnit.SECONDS); - } - } - } - - private ISession lockSession(String machineId, LockType type, int retries) { - int count = 0; - IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); - ISession session; - while (true) { - try { - session = manager.get().getSessionObject(); - immutableMachine.lockMachine(session, type); - break; - } catch (VBoxException e) { - VBoxException vbex = Throwables2.getFirstThrowableOfType(e, VBoxException.class); - if (vbex != null && machineNotFoundException(vbex)) { - return null; - } - count++; - logger.debug("Could not lock machine (try %d of %d). Error: %s", count, retries, e.getMessage()); - if (count == retries) { - throw new RuntimeException(String.format("error locking %s with %s lock: %s", machineId, type, - e.getMessage()), e); - } - Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS); - } - } - checkState(session.getState().equals(SessionState.Locked)); - return checkNotNull(session, "session"); - } - - /** - * @param machineId - * @param function - * @return - */ - public <T> T applyForMachine(final String machineId, final Function<IMachine, T> function) { - final IMachine immutableMachine = manager.get().getVBox().findMachine(machineId); - return new Function<IMachine, T>() { - @Override - public T apply(IMachine machine) { - return function.apply(machine); - } - - @Override - public String toString() { - return function.toString(); - } - }.apply(immutableMachine); - } - - public static boolean machineNotFoundException(VBoxException e) { - return e.getMessage().contains("VirtualBox error: Could not find a registered machine named ") - || e.getMessage().contains("Could not find a registered machine with UUID {"); - } - - private void checkSessionIsUnlocked(ISession session, int attempts, long period, TimeUnit timeUnit) { - checkState( - retry(new SessionStatePredicate(session), attempts * period, period, timeUnit).apply(SessionState.Unlocked), - "timed out or number of retries(%s) reached waiting for session to be unlocked", attempts); - } - - private static class SessionStatePredicate implements Predicate<SessionState> { - private final ISession session; - - SessionStatePredicate(ISession session) { - this.session = session; - } - - @Override - public boolean apply(SessionState input) { - return session.getState().equals(input); - } - } - -}
