/
EC2ComputerLauncher.java
61 lines (53 loc) · 2.14 KB
/
EC2ComputerLauncher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package hudson.plugins.ec2;
import com.xerox.amazonws.ec2.EC2Exception;
import com.xerox.amazonws.ec2.ReservationDescription.Instance;
import hudson.model.TaskListener;
import hudson.slaves.ComputerLauncher;
import hudson.slaves.SlaveComputer;
import java.io.IOException;
import java.io.PrintStream;
import org.jets3t.service.S3ServiceException;
/**
* {@link ComputerLauncher} for EC2 that waits for the instance to really come up before proceeding to
* the real user-specified {@link ComputerLauncher}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class EC2ComputerLauncher extends ComputerLauncher {
@Override
public void launch(SlaveComputer _computer, TaskListener listener) {
try {
EC2Computer computer = (EC2Computer)_computer;
PrintStream logger = listener.getLogger();
OUTER:
while(true) {
switch (computer.getState()) {
case PENDING:
Thread.sleep(5000); // check every 5 secs
continue OUTER;
case RUNNING:
break OUTER;
case SHUTTING_DOWN:
case TERMINATED:
// abort
logger.println("The instance "+computer.getInstanceId()+" appears to be shut down. Aborting launch.");
return;
}
}
launch(computer, logger, computer.describeInstance());
} catch (EC2Exception e) {
e.printStackTrace(listener.error(e.getMessage()));
} catch (IOException e) {
e.printStackTrace(listener.error(e.getMessage()));
} catch (InterruptedException e) {
e.printStackTrace(listener.error(e.getMessage()));
} catch (S3ServiceException e) {
e.printStackTrace(listener.error(e.getMessage()));
}
}
/**
* Stage 2 of the launch. Called after the EC2 instance comes up.
*/
protected abstract void launch(EC2Computer computer, PrintStream logger, Instance inst)
throws EC2Exception, IOException, InterruptedException, S3ServiceException;
}