Skip to content

Commit

Permalink
Merge pull request #18 from yyuu/master
Browse files Browse the repository at this point in the history
JENKINS-8617 cannot customize security group to launch slaves into
  • Loading branch information
francisu committed May 22, 2012
2 parents d96e049 + b4c4ba3 commit 543777c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 13 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Expand Up @@ -4,8 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.399</version>
<!-- <version>1.451</version>-->
<version>1.462</version>
</parent>

<artifactId>ec2</artifactId>
Expand Down
39 changes: 31 additions & 8 deletions src/main/java/hudson/plugins/ec2/SlaveTemplate.java
Expand Up @@ -15,7 +15,9 @@

import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {
public final String ami;
public final String description;
public final String zone;
public final String securityGroups;
public final String remoteFS;
public final String sshPort;
public final InstanceType type;
Expand All @@ -61,11 +64,13 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {


private transient /*almost final*/ Set<LabelAtom> labelSet;
private transient /*almost final*/ Set<String> securityGroupSet;

@DataBoundConstructor
public SlaveTemplate(String ami, String zone, String remoteFS, String sshPort, InstanceType type, String labelString, String description, String initScript, String userData, String numExecutors, String remoteAdmin, String rootCommandPrefix, String jvmopts, boolean stopOnTerminate) {
public SlaveTemplate(String ami, String zone, String securityGroups, String remoteFS, String sshPort, InstanceType type, String labelString, String description, String initScript, String userData, String numExecutors, String remoteAdmin, String rootCommandPrefix, String jvmopts, boolean stopOnTerminate) {
this.ami = ami;
this.zone = zone;
this.securityGroups = securityGroups;
this.remoteFS = remoteFS;
this.sshPort = sshPort;
this.type = type;
Expand Down Expand Up @@ -97,6 +102,22 @@ String getZone() {
return zone;
}

public String getSecurityGroupString() {
return securityGroups;
}

public Set<String> getSecurityGroupSet() {
return securityGroupSet;
}

public Set<String> parseSecurityGroups() {
if (securityGroups == null || "".equals(securityGroups.trim())) {
return Collections.emptySet();
} else {
return new HashSet<String>(Arrays.asList(securityGroups.split("\\s*,\\s*")));
}
}

public int getNumExecutors() {
try {
return Integer.parseInt(numExecutors);
Expand All @@ -121,9 +142,9 @@ public String getRootCommandPrefix() {
}

public Set getLabelSet(){
return labelSet;
return labelSet;
}

/**
* Does this contain the given label?
*
Expand Down Expand Up @@ -156,6 +177,7 @@ public EC2Slave provision(TaskListener listener) throws AmazonClientException, I
request.setUserData(userData);
request.setKeyName(keyPair.getKeyName());
request.setInstanceType(type.toString());
request.setSecurityGroups(securityGroupSet);
Instance inst = ec2.runInstances(request).getReservation().getInstances().get(0);
return newSlave(inst);
} catch (FormException e) {
Expand Down Expand Up @@ -191,6 +213,7 @@ public EC2Slave attach(String instanceId, TaskListener listener) throws AmazonCl
*/
protected Object readResolve() {
labelSet = Label.parse(labels);
securityGroupSet = parseSecurityGroups();
return this;
}

Expand All @@ -201,7 +224,7 @@ public Descriptor<SlaveTemplate> getDescriptor() {
@Extension
public static final class DescriptorImpl extends Descriptor<SlaveTemplate> {
@Override
public String getDisplayName() {
public String getDisplayName() {
return null;
}

Expand Down Expand Up @@ -247,10 +270,10 @@ public FormValidation doValidateAmi(
}

public ListBoxModel doFillZoneItems(@QueryParameter String accessId,
@QueryParameter String secretKey, @QueryParameter String region) throws IOException,
ServletException {
return EC2Slave.fillZoneItems(accessId, secretKey, region);
}
@QueryParameter String secretKey, @QueryParameter String region) throws IOException,
ServletException {
return EC2Slave.fillZoneItems(accessId, secretKey, region);
}

}
}
Expand Up @@ -38,6 +38,9 @@ THE SOFTWARE.
<!-- <f:select/>-->
<f:textbox/>
</f:entry>
<f:entry title="${%Security groups}" field="securityGroups">
<f:textbox/>
</f:entry>
<f:entry title="${%Remote FS root}" field="remoteFS">
<f:textbox />
</f:entry>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/hudson/plugins/ec2/SlaveTemplateTest.java
Expand Up @@ -24,7 +24,7 @@ protected void tearDown() throws Exception {

public void testConfigRoundtrip() throws Exception {
String ami = "ami1";
SlaveTemplate orig = new SlaveTemplate(ami, EC2Slave.TEST_ZONE, "foo", "22", InstanceType.M1Large, "ttt", "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", false);
SlaveTemplate orig = new SlaveTemplate(ami, EC2Slave.TEST_ZONE, "default", "foo", "22", InstanceType.M1Large, "ttt", "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", false);
List<SlaveTemplate> templates = new ArrayList<SlaveTemplate>();
templates.add(orig);
AmazonEC2Cloud ac = new AmazonEC2Cloud( "abc", "def", "us-east-1", "ghi", "3", templates);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/hudson/plugins/ec2/TemplateLabelsTest.java
Expand Up @@ -19,7 +19,7 @@ public class TemplateLabelsTest extends HudsonTestCase{
@Override
public void setUp() throws Exception{
super.setUp();
SlaveTemplate template = new SlaveTemplate("ami", "foo", "zone", "22", InstanceType.M1Large, LABEL1 + " " + LABEL2, "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", true);
SlaveTemplate template = new SlaveTemplate("ami", "foo", "default", "zone", "22", InstanceType.M1Large, LABEL1 + " " + LABEL2, "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", true);
List<SlaveTemplate> templates = new ArrayList<SlaveTemplate>();
templates.add(template);
ac = new AmazonEC2Cloud("us-east-1", "abc", "def", "ghi", "3", templates);
Expand All @@ -41,7 +41,7 @@ public void testLabelExpression() throws Exception{
}

public void testEmptyLabel() throws Exception{
SlaveTemplate temp = new SlaveTemplate("ami", "foo", "zone", "22", InstanceType.M1Large, "", "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", true);
SlaveTemplate temp = new SlaveTemplate("ami", "foo", "default", "zone", "22", InstanceType.M1Large, "", "foo ami", "bar", "aaa", "10", "rrr", "fff", "-Xmx1g", true);
List<SlaveTemplate> templates = new ArrayList<SlaveTemplate>();
templates.add(temp);
ac = new AmazonEC2Cloud("us-east-1", "abc", "def", "ghi", "3", templates);
Expand Down

2 comments on commit 543777c

@francisu
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to back out the change to make the Jenkins version 1.462; it should stay at 1.399 I think unless there is some other change where we need to depend on a higher version of Jenkins. Do you agree? (If you want to make a pull request to do that, I would appreciate it).

@frankbille
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with that. There should be a very good reason for upgrading because we cut off all the older versions of Jenkins/Hudson

Please sign in to comment.