
org.jclouds.virtualbox.functions.TakeSnapshotIfNotAlreadyAttached Maven / Gradle / Ivy
/**
* 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.functions;
import org.jclouds.logging.Logger;
import org.virtualbox_4_1.IMachine;
import org.virtualbox_4_1.IProgress;
import org.virtualbox_4_1.ISession;
import org.virtualbox_4_1.ISnapshot;
import org.virtualbox_4_1.MachineState;
import org.virtualbox_4_1.VirtualBoxManager;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Throwables;
/**
* @author Andrea Turli
*/
public class TakeSnapshotIfNotAlreadyAttached implements Function {
private Supplier manager;
private String snapshotName;
private String snapshotDesc;
private Logger logger;
public TakeSnapshotIfNotAlreadyAttached(Supplier manager, String snapshotName,
String snapshotDesc, Logger logger) {
this.manager = manager;
this.snapshotName = snapshotName;
this.snapshotDesc = snapshotDesc;
this.logger = logger;
}
@Override
public ISnapshot apply(IMachine machine) {
// Snapshot a machine
ISession session = null;
ISnapshot snap = machine.getCurrentSnapshot();
if (snap == null) {
try {
session = manager.get().openMachineSession(machine);
logger.debug("No snapshot available taking new one: %s (description: %s) taken from %s", snapshotName,
snapshotDesc, machine.getName());
int retries = 10;
while (true) {
try {
// running machines need to be pause before a snapshot can be taken
// due to a vbox bug see https://www.virtualbox.org/ticket/9255
boolean paused = false;
if (machine.getState() == MachineState.Running) {
session.getConsole().pause();
paused = true;
}
IProgress progress = session.getConsole().takeSnapshot(snapshotName, snapshotDesc);
progress.waitForCompletion(-1);
if (paused) {
session.getConsole().resume();
}
snap = machine.getCurrentSnapshot();
logger.debug("Snapshot %s (description: %s) taken from %s", snapshotName, snapshotDesc,
machine.getName());
break;
} catch (Exception e) {
if (e.getMessage().contains("VirtualBox error: The object is not ready")
|| e.getMessage().contains("This machine does not have any snapshots")) {
retries--;
if (retries == 0) {
logger.error(e,
"Problem creating snapshot (too many retries) %s (description: %s) from machine %s",
snapshotName, snapshotDesc, machine.getName());
throw Throwables.propagate(e);
}
Thread.sleep(1000L);
continue;
}
logger.error(e, "Problem creating snapshot %s (description: %s) from machine %s", snapshotName,
snapshotDesc, machine.getName());
throw Throwables.propagate(e);
}
}
} catch (Exception e) {
Throwables.propagate(e);
} finally {
if (session != null) {
manager.get().closeMachineSession(session);
}
}
}
return snap;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy