EC2Computer.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package hudson.plugins.ec2;
  2. import hudson.Util;
  3. import hudson.slaves.SlaveComputer;
  4. import java.io.IOException;
  5. import java.util.Collections;
  6. import org.kohsuke.stapler.HttpRedirect;
  7. import org.kohsuke.stapler.HttpResponse;
  8. import com.amazonaws.AmazonClientException;
  9. import com.amazonaws.services.ec2.AmazonEC2;
  10. import com.amazonaws.services.ec2.model.DescribeInstancesRequest;
  11. import com.amazonaws.services.ec2.model.GetConsoleOutputRequest;
  12. import com.amazonaws.services.ec2.model.Instance;
  13. /**
  14. * @author Kohsuke Kawaguchi
  15. */
  16. public class EC2Computer extends SlaveComputer {
  17. /**
  18. * Cached description of this EC2 instance. Lazily fetched.
  19. */
  20. private volatile Instance ec2InstanceDescription;
  21. public EC2Computer(EC2Slave slave) {
  22. super(slave);
  23. }
  24. @Override
  25. public EC2Slave getNode() {
  26. return (EC2Slave)super.getNode();
  27. }
  28. public String getInstanceId() {
  29. return getName();
  30. }
  31. /**
  32. * Gets the EC2 console output.
  33. */
  34. public String getConsoleOutput() throws AmazonClientException {
  35. AmazonEC2 ec2 = EC2Cloud.get().connect();
  36. GetConsoleOutputRequest request = new GetConsoleOutputRequest(getInstanceId());
  37. return ec2.getConsoleOutput(request).getOutput();
  38. }
  39. /**
  40. * Obtains the instance state description in EC2.
  41. *
  42. * <p>
  43. * This method returns a cached state, so it's not suitable to check {@link Instance#getState()}
  44. * and {@link Instance#getStateCode()} from the returned instance (but all the other fields are valid as it won't change.)
  45. *
  46. * The cache can be flushed using {@link #updateInstanceDescription()}
  47. */
  48. public Instance describeInstance() throws AmazonClientException {
  49. if(ec2InstanceDescription==null)
  50. ec2InstanceDescription = _describeInstance();
  51. return ec2InstanceDescription;
  52. }
  53. /**
  54. * This will flush any cached description held by {@link #describeInstance()}.
  55. */
  56. public Instance updateInstanceDescription() throws AmazonClientException {
  57. return ec2InstanceDescription = _describeInstance();
  58. }
  59. /**
  60. * Gets the current state of the instance.
  61. *
  62. * <p>
  63. * Unlike {@link #describeInstance()}, this method always return the current status by calling EC2.
  64. */
  65. public InstanceState getState() throws AmazonClientException {
  66. ec2InstanceDescription=_describeInstance();
  67. return InstanceState.find(ec2InstanceDescription.getState().getName());
  68. }
  69. /**
  70. * Number of milli-secs since the instance was started.
  71. */
  72. public long getUptime() throws AmazonClientException {
  73. return System.currentTimeMillis()-describeInstance().getLaunchTime().getTime();
  74. }
  75. /**
  76. * Returns uptime in the human readable form.
  77. */
  78. public String getUptimeString() throws AmazonClientException {
  79. return Util.getTimeSpanString(getUptime());
  80. }
  81. private Instance _describeInstance() throws AmazonClientException {
  82. DescribeInstancesRequest request = new DescribeInstancesRequest();
  83. request.setInstanceIds(Collections.<String>singletonList(getNode().getInstanceId()));
  84. return EC2Cloud.get().connect().describeInstances(request).getReservations().get(0).getInstances().get(0);
  85. }
  86. /**
  87. * When the slave is deleted, terminate the instance.
  88. */
  89. @Override
  90. public HttpResponse doDoDelete() throws IOException {
  91. checkPermission(DELETE);
  92. if (getNode() != null)
  93. getNode().terminate();
  94. return new HttpRedirect("..");
  95. }
  96. /** What username to use to run root-like commands
  97. *
  98. */
  99. public String getRemoteAdmin() {
  100. return getNode().getRemoteAdmin();
  101. }
  102. public int getSshPort() {
  103. return getNode().getSshPort();
  104. }
  105. public String getRootCommandPrefix() {
  106. return getNode().getRootCommandPrefix();
  107. }
  108. }