8
8
import hudson .model .Slave ;
9
9
import hudson .plugins .ec2 .ssh .EC2UnixLauncher ;
10
10
import hudson .slaves .NodeProperty ;
11
+ import hudson .util .ListBoxModel ;
11
12
12
13
import java .io .IOException ;
13
14
import java .util .Collections ;
14
15
import java .util .List ;
15
16
import java .util .logging .Level ;
16
17
import java .util .logging .Logger ;
17
18
19
+ import javax .servlet .ServletException ;
20
+
21
+ import org .apache .commons .lang .StringUtils ;
18
22
import org .kohsuke .stapler .DataBoundConstructor ;
23
+ import org .kohsuke .stapler .QueryParameter ;
19
24
20
25
import com .amazonaws .AmazonClientException ;
21
26
import com .amazonaws .services .ec2 .AmazonEC2 ;
27
+ import com .amazonaws .services .ec2 .model .AvailabilityZone ;
28
+ import com .amazonaws .services .ec2 .model .DescribeAvailabilityZonesResult ;
22
29
import com .amazonaws .services .ec2 .model .InstanceType ;
23
30
import com .amazonaws .services .ec2 .model .TerminateInstancesRequest ;
24
31
@@ -31,6 +38,7 @@ public final class EC2Slave extends Slave {
31
38
/**
32
39
* Comes from {@link SlaveTemplate#initScript}.
33
40
*/
41
+ public final String zone ;
34
42
public final String initScript ;
35
43
public final String remoteAdmin ; // e.g. 'ubuntu'
36
44
public final String rootCommandPrefix ; // e.g. 'sudo'
@@ -41,13 +49,16 @@ public final class EC2Slave extends Slave {
41
49
*/
42
50
private final int sshPort ;
43
51
44
- public EC2Slave (String instanceId , String description , String remoteFS , int sshPort , int numExecutors , String labelString , String initScript , String remoteAdmin , String rootCommandPrefix , String jvmopts ) throws FormException , IOException {
45
- this (instanceId , description , remoteFS , sshPort , numExecutors , Mode .NORMAL , labelString , initScript , Collections .<NodeProperty <?>>emptyList (), remoteAdmin , rootCommandPrefix , jvmopts );
52
+ public static final String TEST_ZONE = "testZone" ;
53
+
54
+ public EC2Slave (String instanceId , String description , String zone , String remoteFS , int sshPort , int numExecutors , String labelString , String initScript , String remoteAdmin , String rootCommandPrefix , String jvmopts ) throws FormException , IOException {
55
+ this (instanceId , description , zone , remoteFS , sshPort , numExecutors , Mode .NORMAL , labelString , initScript , Collections .<NodeProperty <?>>emptyList (), remoteAdmin , rootCommandPrefix , jvmopts );
46
56
}
47
57
48
58
@ DataBoundConstructor
49
- public EC2Slave (String instanceId , String description , String remoteFS , int sshPort , int numExecutors , Mode mode , String labelString , String initScript , List <? extends NodeProperty <?>> nodeProperties , String remoteAdmin , String rootCommandPrefix , String jvmopts ) throws FormException , IOException {
59
+ public EC2Slave (String instanceId , String description , String zone , String remoteFS , int sshPort , int numExecutors , Mode mode , String labelString , String initScript , List <? extends NodeProperty <?>> nodeProperties , String remoteAdmin , String rootCommandPrefix , String jvmopts ) throws FormException , IOException {
50
60
super (instanceId , description , remoteFS , numExecutors , mode , labelString , new EC2UnixLauncher (), new EC2RetentionStrategy (), nodeProperties );
61
+ this .zone = zone ;
51
62
this .initScript = initScript ;
52
63
this .remoteAdmin = remoteAdmin ;
53
64
this .rootCommandPrefix = rootCommandPrefix ;
@@ -59,7 +70,7 @@ public EC2Slave(String instanceId, String description, String remoteFS, int sshP
59
70
* Constructor for debugging.
60
71
*/
61
72
public EC2Slave (String instanceId ) throws FormException , IOException {
62
- this (instanceId ,"debug" ,"/tmp/hudson" , 22 , 1 , Mode .NORMAL , "debug" , "" , Collections .<NodeProperty <?>>emptyList (), null , null , null );
73
+ this (instanceId ,"debug" ,"zone" , " /tmp/hudson" , 22 , 1 , Mode .NORMAL , "debug" , "" , Collections .<NodeProperty <?>>emptyList (), null , null , null );
63
74
}
64
75
65
76
/**
@@ -111,6 +122,10 @@ public void terminate() {
111
122
}
112
123
}
113
124
125
+ String getZone () {
126
+ return zone ;
127
+ }
128
+
114
129
String getRemoteAdmin () {
115
130
if (remoteAdmin == null || remoteAdmin .length () == 0 )
116
131
return "root" ;
@@ -131,6 +146,29 @@ public int getSshPort() {
131
146
return sshPort !=0 ? sshPort : 22 ;
132
147
}
133
148
149
+ public static ListBoxModel fillZoneItems (String accessId ,
150
+ String secretKey , String region ) throws IOException ,
151
+ ServletException {
152
+ ListBoxModel model = new ListBoxModel ();
153
+ if (AmazonEC2Cloud .testMode ) {
154
+ model .add (TEST_ZONE );
155
+ return model ;
156
+ }
157
+
158
+ if (!StringUtils .isEmpty (accessId ) && !StringUtils .isEmpty (secretKey ) && !StringUtils .isEmpty (region )) {
159
+ AmazonEC2 client = AmazonEC2Cloud .connect (accessId , secretKey , AmazonEC2Cloud .getEc2EndpointUrl (region ));
160
+ DescribeAvailabilityZonesResult zones = client .describeAvailabilityZones ();
161
+ List <AvailabilityZone > zoneList = zones .getAvailabilityZones ();
162
+ model .add ("<not specified>" , "" );
163
+ for (AvailabilityZone z : zoneList ) {
164
+ model .add (z .getZoneName (), z .getZoneName ());
165
+ }
166
+ }
167
+ return model ;
168
+ }
169
+
170
+
171
+
134
172
@ Extension
135
173
public static final class DescriptorImpl extends SlaveDescriptor {
136
174
@ Override
@@ -142,6 +180,12 @@ public String getDisplayName() {
142
180
public boolean isInstantiable () {
143
181
return false ;
144
182
}
183
+
184
+ public ListBoxModel doFillZoneItems (@ QueryParameter String accessId ,
185
+ @ QueryParameter String secretKey , @ QueryParameter String region ) throws IOException ,
186
+ ServletException {
187
+ return fillZoneItems (accessId , secretKey , region );
188
+ }
145
189
}
146
190
147
191
private static final Logger LOGGER = Logger .getLogger (EC2Slave .class .getName ());
0 commit comments