Skip to content
Permalink
Browse files
Unmount the volume before detaching.
  • Loading branch information
Everett Toews committed Nov 21, 2012
1 parent a9c4027 commit c4ae44746091c6790ad361f33c04899198a1677c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
@@ -60,6 +60,7 @@
*/
public class CreateVolumeAndAttach {
private static final String NAME = "jclouds-example";
private static final String PASSWORD = "sbmFPqaw5d43";
private static final String ZONE = "DFW";
private static final String DEVICE = "/dev/xvdd";

@@ -139,10 +140,13 @@ private NodeMetadata createServer() throws RunNodesException, TimeoutException {
Set<? extends NodeMetadata> nodes = compute.createNodesInGroup(NAME, 1, template);
NodeMetadata nodeMetadata = nodes.iterator().next();
String publicAddress = nodeMetadata.getPublicAddresses().iterator().next();

// We set the password to something we know so we can login in the DetachVolume example
nova.getApi().getServerApiForZone(ZONE).changeAdminPass(nodeMetadata.getProviderId(), PASSWORD);

System.out.println(" " + nodeMetadata);
System.out.println(" Login: ssh " + nodeMetadata.getCredentials().getUser() + "@" + publicAddress);
System.out.println(" Password: " + nodeMetadata.getCredentials().getPassword());
System.out.println(" Password: " + PASSWORD);

return nodeMetadata;
}
@@ -193,7 +197,9 @@ private void mountVolume(NodeMetadata node) {
.addStatement(exec("mount /dev/xvdd /mnt"))
.render(OsFamily.UNIX);

RunScriptOptions options = RunScriptOptions.Builder.blockOnComplete(true);
RunScriptOptions options = RunScriptOptions.Builder
.blockOnComplete(true)
.overrideLoginPassword(PASSWORD);

ExecResponse response = compute.runScriptOnNode(node.getId(), script, options);

@@ -18,13 +18,17 @@
*/
package org.jclouds.examples.rackspace.cloudblockstorage;

import static org.jclouds.scriptbuilder.domain.Statements.exec;

import java.util.Properties;
import java.util.concurrent.TimeoutException;

import org.jclouds.ContextBuilder;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.ComputeServiceContext;
import org.jclouds.compute.config.ComputeServiceProperties;
import org.jclouds.compute.domain.ExecResponse;
import org.jclouds.compute.options.RunScriptOptions;
import org.jclouds.openstack.cinder.v1.CinderApi;
import org.jclouds.openstack.cinder.v1.CinderApiMetadata;
import org.jclouds.openstack.cinder.v1.CinderAsyncApi;
@@ -35,11 +39,17 @@
import org.jclouds.openstack.nova.v2_0.NovaAsyncApi;
import org.jclouds.openstack.nova.v2_0.domain.Server;
import org.jclouds.openstack.nova.v2_0.domain.VolumeAttachment;
import org.jclouds.openstack.nova.v2_0.domain.zonescoped.ZoneAndId;
import org.jclouds.openstack.nova.v2_0.extensions.VolumeAttachmentApi;
import org.jclouds.openstack.nova.v2_0.features.ServerApi;
import org.jclouds.rest.RestContext;
import org.jclouds.scriptbuilder.ScriptBuilder;
import org.jclouds.scriptbuilder.domain.OsFamily;
import org.jclouds.sshj.config.SshjSshClientModule;

import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Module;

/**
* This example detaches the volume created in the CreateVolumeAndAttach example.
@@ -48,6 +58,8 @@
*/
public class DetachVolume {
private static final String NAME = "jclouds-example";
private static final String ROOT = "root";
private static final String PASSWORD = "sbmFPqaw5d43";
private static final String ZONE = "DFW";

private ComputeService compute;
@@ -71,6 +83,7 @@ public static void main(String[] args) {
try {
detachVolume.init(args);
VolumeAttachment volumeAttachment = detachVolume.getVolumeAttachment();
detachVolume.unmountVolume(volumeAttachment);
detachVolume.detachVolume(volumeAttachment);
}
catch (Exception e) {
@@ -94,8 +107,11 @@ private void init(String[] args) {
overrides.setProperty(ComputeServiceProperties.POLL_INITIAL_PERIOD, "20000");
overrides.setProperty(ComputeServiceProperties.POLL_MAX_PERIOD, "20000");

Iterable<Module> modules = ImmutableSet.<Module> of(new SshjSshClientModule());

ComputeServiceContext context = ContextBuilder.newBuilder(provider)
.credentials(username, apiKey)
.modules(modules)
.overrides(overrides)
.buildView(ComputeServiceContext.class);
compute = context.getComputeService();
@@ -128,6 +144,32 @@ private VolumeAttachment getVolumeAttachment() {
throw new RuntimeException(NAME + " not found. Run the CreateVolumeAndAttach example first.");
}

/**
* Make sure you've unmounted the volume first. Failure to do so could result in failure or data loss.
*/
private void unmountVolume(VolumeAttachment volumeAttachment) {
System.out.println("Unmount Volume");

String script = new ScriptBuilder()
.addStatement(exec("umount /mnt"))
.render(OsFamily.UNIX);

RunScriptOptions options = RunScriptOptions.Builder
.overrideLoginUser(ROOT)
.overrideLoginPassword(PASSWORD)
.blockOnComplete(true);

ZoneAndId zoneAndId = ZoneAndId.fromZoneAndId(ZONE, volumeAttachment.getServerId());
ExecResponse response = compute.runScriptOnNode(zoneAndId.slashEncode(), script, options);

if (response.getExitStatus() == 0) {
System.out.println(" Exit Status: " + response.getExitStatus());
}
else {
System.out.println(" Error: " + response.getOutput());
}
}

private void detachVolume(VolumeAttachment volumeAttachment) throws TimeoutException {
System.out.println("Detach Volume");

0 comments on commit c4ae447

Please sign in to comment.