// already running instance
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
DescribeInstancesResult describeInstancesResult = ec2client.describeInstances(describeInstancesRequest.withInstanceIds(ins_id));
if (describeInstancesResult.getReservations().size() == 0 || describeInstancesResult.getReservations().get(0).getInstances().size() == 0) {
throw new GfacException("Instance not found:" + ins_id, FaultCode.InvalidRequest);
}
this.instance = describeInstancesResult.getReservations().get(0).getInstances().get(0);
// check instance keypair
if (this.instance.getKeyName() == null || !this.instance.getKeyName().equals(KEY_PAIR_NAME))
throw new GfacException("Keypair for instance:" + ins_id + " is not valid", FaultCode.InvalidRequest);
}
//send out instance id
invocationContext.getExecutionContext().getNotificationService().sendResourceMappingNotifications(this.instance.getPublicDnsName(), "EC2 Instance " + this.instance.getInstanceId() + " is running with public name " + this.instance.getPublicDnsName(), this.instance.getInstanceId());
/*
* Make sure port 22 is connectable
*/
for (GroupIdentifier g : this.instance.getSecurityGroups()) {
IpPermission ip = new IpPermission();
ip.setIpProtocol("tcp");
ip.setFromPort(22);
ip.setToPort(22);
AuthorizeSecurityGroupIngressRequest r = new AuthorizeSecurityGroupIngressRequest();
r = r.withIpPermissions(ip.withIpRanges("0.0.0.0/0"));
r.setGroupId(g.getGroupId());
try {
ec2client.authorizeSecurityGroupIngress(r);
} catch (AmazonServiceException as) {
/*
* If exception is from duplicate room, ignore it.
*/
if (!as.getErrorCode().equals("InvalidPermission.Duplicate"))
throw as;
}
}
} catch (Exception e) {
// TODO throw out
e.printStackTrace();
log.error(e.getMessage(), e);
throw new GfacException(e, FaultCode.InvalidRequest);
}
//set Host location
model.setHost(this.instance.getPublicDnsName());
/*
* Make directory
*/
SSHClient ssh = new SSHClient();
try {
ssh.loadKnownHosts();
ssh.connect(this.instance.getPublicDnsName());
ssh.authPublickey(privateKeyFilePath);
final Session session = ssh.startSession();
try {
StringBuilder command = new StringBuilder();
command.append("mkdir -p ");
command.append(model.getTmpDir());
command.append(" | ");
command.append("mkdir -p ");
command.append(model.getWorkingDir());
command.append(" | ");
command.append("mkdir -p ");
command.append(model.getInputDataDir());
command.append(" | ");
command.append("mkdir -p ");
command.append(model.getOutputDataDir());
Command cmd = session.exec(command.toString());
cmd.join(5, TimeUnit.SECONDS);
} catch (Exception e) {
throw e;
} finally {
try {
session.close();
} catch (Exception e) {
}
}
} catch (Exception e) {
throw new GfacException(e.getMessage(), e);
} finally {
try {
ssh.disconnect();
} catch (Exception e) {
}