Xenserver 5.5 Api Cheat Sheet Page 3

ADVERTISEMENT

Start machine and wait for IP address
// This code requires XenServer Tools to be installed on the VM
newVM.start(connection, false /* don't start paused */, false /*don't force*/);
List<InetAddress> ipAddresses =
new
LinkedList<InetAddress>();
while(true) {
try
{
ipAddresses= getIpAddresses(newVM);
break;
}
catch
(com.xensource.xenapi.Types.HandleInvalid e) {
Thread.sleep(5000);
}
}
// returns a list of IP addresses of the virtual machine
// requires the VM to have XenServer Tools installed
List<InetAddress> getIpAddresses(VM machine) {
List<InetAddress> ipAddresses =
new
LinkedList<InetAddress>();
VMGuestMetrics metrics = machine.getGuestMetrics(getConnection());
Map<String,String> networks = metrics.getNetworks(getConnection());
for (String key : networks.keySet()) {
InetAddress ipAddress = InetAddress.getByName(networks.get(key));
ipAddresses.add(ipAddress);
}
return
ipAddresses;
}
Start parameterized script, after network interface is properly loaded
// This code uses the ANT org.apache.tools.ant.taskdefs.optional.ssh.SSHExec
// task to execute remote shell commands over SSH
String result = ""
while
(!result.contains("ping")) {
result = runCommand(ipAddress,5000,"echo ping");
}
result = runCommand(ipAddress,5000,"~/startSomething.sh "+param1+" "+param2);
private String runCommand(String ipAddress, long timeoutMilliseconds,
String command) {
final File output = File.createTempFile("sshCommand", ".txt");
try
{
final SSHExec task =
new
SSHExec();
task.setOutput(output);
// ssh related parameters
task.setFailonerror(false);
task.setCommand(command);
task.setHost(ipAddress);
task.setTrust(true);
task.setUsername(this.username);
task.setPassword(this.password);
task.setTimeout(timeoutMilliseconds);
task.execute();
String response = readFileAsString(output);
return
response;
}
finally
{ output.delete(); }
}

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go
Page of 4