OneEC2Computer.java 4.1 KB

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