Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ private Object readResolve() {
lf = LauncherFactory.JNLP.JNLP;
}

BootSource.Image bs = imageId == null ? null : new BootSource.Image(imageId);
BootSource.Image bs = imageId == null ? null : new BootSource.Image(imageId, null, false);
slaveOptions = SlaveOptions.builder().bootSource(bs).hardwareId(hardwareId).numExecutors(Integer.getInteger(numExecutors)).jvmOptions(jvmOptions).userDataId(userDataId)
.fsRoot(fsRoot).retentionTime(overrideRetentionTime).keyPairName(keyPairName).networkId(networkId).securityGroups(securityGroups)
.launcherFactory(lf).availabilityZone(availabilityZone).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public SlaveOptions(

private Object readResolve() {
if (bootSource == null && imageId != null) {
bootSource = new BootSource.Image(imageId);
bootSource = new BootSource.Image(imageId, null, false);
}
imageId = null;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.openstack4j.model.compute.builder.ServerCreateBuilder;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -132,16 +133,22 @@ public List<String> getAuthFieldsOffsets() {
return Arrays.asList("../..", "../../..");
}

protected ListBoxModel makeListBoxModelOfAllNames(String existingValue, String endPointUrl, boolean ignoreSsl, String credentialsId, String zone) {
protected ListBoxModel makeListBoxModelOfAllNames(String existingValue, String endPointUrl, boolean ignoreSsl, String credentialsId, String zone, String nameFilterRegexp) {
ListBoxModel m = new ListBoxModel();
final String valueOrEmpty = Util.fixNull(existingValue);
final String filter = Util.fixEmptyAndTrim(nameFilterRegexp);
OpenstackCredential openstackCredential = OpenstackCredentials.getCredential(credentialsId);
m.add(new ListBoxModel.Option("None specified", "", valueOrEmpty.isEmpty()));
try {
if (haveAuthDetails(endPointUrl, openstackCredential, zone)) {
final Openstack openstack = Openstack.Factory.get(endPointUrl, ignoreSsl, openstackCredential, zone);
final List<String> values = listAllNames(openstack);
for (String value : values) {
if (filter != null) {
if (!value.matches(filter)){
continue;
}
}
final String displayText = value;
m.add(displayText, value);
}
Expand Down Expand Up @@ -216,28 +223,60 @@ public static class Image extends BootSource {
private static final String OPENSTACK_BOOTSOURCE_IMAGE_ID_KEY = "jenkins-boot-image-id";

protected final @Nonnull String name;
protected final @Nullable String nameFilterRegexp;
protected final boolean overrideNotFoundImage;

protected String imageId;

@DataBoundConstructor
public Image(@Nonnull String name) {
public Image(@Nonnull String name, @Nullable String nameFilterRegexp, boolean overrideNotFoundImage) {
Objects.requireNonNull(name, "Image name missing");
this.name = name;
this.nameFilterRegexp = Util.fixEmptyAndTrim(nameFilterRegexp);
this.overrideNotFoundImage = overrideNotFoundImage;
}

@Nonnull
public String getName() {
return name;
}

public String getNameFilterRegexp() {
return nameFilterRegexp;
}

public boolean getOverrideNotFoundImage() {
return overrideNotFoundImage;
}

@Override
public void setServerBootSource(
@Nonnull ServerCreateBuilder builder, @Nonnull Openstack os
) throws JCloudsCloud.ProvisioningFailedException {
super.setServerBootSource(builder, os);
final List<String> matchingIds = getDescriptor().findMatchingIds(os, name);
final String id = selectIdFromListAndLogProblems(matchingIds, name, "Images");
List<String> matchingIds = getDescriptor().findMatchingIds(os, name);
if (matchingIds.size() == 0 && overrideNotFoundImage) {
String overrideName = name;
final List<String> values = getDescriptor().listAllNames(os);
for (String value : values) {
if (nameFilterRegexp != null) {
if (value.matches(nameFilterRegexp)){
overrideName = value;
LOGGER.warning("Image " + name + "not found, overriding with " + overrideName);
break;
}
}
}
matchingIds = getDescriptor().findMatchingIds(os, overrideName);
imageId = selectIdFromListAndLogProblems(matchingIds, overrideName, "Images");

} else {
imageId = selectIdFromListAndLogProblems(matchingIds, name, "Images");

}
builder.image(imageId);
builder.addMetadataItem(OPENSTACK_BOOTSOURCE_IMAGE_ID_KEY, imageId);

builder.image(id);
builder.addMetadataItem(OPENSTACK_BOOTSOURCE_IMAGE_ID_KEY, id);
}

@Override
Expand Down Expand Up @@ -287,8 +326,9 @@ public ListBoxModel doFillNameItems(@QueryParameter String name,
@QueryParameter String endPointUrl,
@QueryParameter boolean ignoreSsl,
@QueryParameter String credentialsId,
@QueryParameter String zone) {
return makeListBoxModelOfAllNames(name, endPointUrl, ignoreSsl, credentialsId, zone);
@QueryParameter String zone,
@QueryParameter String nameFilterRegexp) {
return makeListBoxModelOfAllNames(name, endPointUrl, ignoreSsl, credentialsId, zone, nameFilterRegexp);
}

@Restricted(DoNotUse.class)
Expand Down Expand Up @@ -319,28 +359,26 @@ public int getVolumeSize() {
}

@DataBoundConstructor
public VolumeFromImage(@Nonnull String name, int volumeSize) {
super(name);
public VolumeFromImage(@Nonnull String name, int volumeSize, @Nullable String nameFilterRegexp, boolean overrideNotFoundImage) {
super(name, nameFilterRegexp, overrideNotFoundImage);
if (volumeSize <= 0) throw new IllegalArgumentException("Volume size must be positive, got " + volumeSize);
this.volumeSize = volumeSize;
}

@Override
public void setServerBootSource(@Nonnull ServerCreateBuilder builder, @Nonnull Openstack os) throws JCloudsCloud.ProvisioningFailedException {
super.setServerBootSource(builder, os);
final List<String> matchingIds = getDescriptor().findMatchingIds(os, name);
final String id = selectIdFromListAndLogProblems(matchingIds, name, "Images");

final BlockDeviceMappingBuilder volumeBuilder = Builders.blockDeviceMapping()
.sourceType(BDMSourceType.IMAGE)
.destinationType(BDMDestType.VOLUME)
.uuid(id)
.uuid(imageId)
.volumeSize(volumeSize)
.deleteOnTermination(true)
.bootIndex(0)
;
builder.blockDevice(volumeBuilder.build());
builder.addMetadataItem(OPENSTACK_BOOTSOURCE_VOLUME_FROM_IMAGE_ID_KEY, id);
builder.addMetadataItem(OPENSTACK_BOOTSOURCE_VOLUME_FROM_IMAGE_ID_KEY, imageId);
}

@Override
Expand Down Expand Up @@ -380,11 +418,15 @@ public static final class VolumeSnapshot extends BootSource {
private static final String OPENSTACK_BOOTSOURCE_VOLUMESNAPSHOT_DESC_KEY = "jenkins-boot-volumesnapshot-description";

private final @Nonnull String name;
protected final @Nullable String nameFilterRegexp;
protected final boolean overrideNotFoundImage;

@DataBoundConstructor
public VolumeSnapshot(@Nonnull String name) {
public VolumeSnapshot(@Nonnull String name, @Nullable String nameFilterRegexp, boolean overrideNotFoundImage) {
Objects.requireNonNull(name, "Volume snapshot name missing");
this.name = name;
this.nameFilterRegexp = Util.fixEmptyAndTrim(nameFilterRegexp);
this.overrideNotFoundImage = overrideNotFoundImage;
}

@Nonnull
Expand All @@ -395,8 +437,25 @@ public String getName() {
@Override
public void setServerBootSource(@Nonnull ServerCreateBuilder builder, @Nonnull Openstack os) {
super.setServerBootSource(builder, os);
final List<String> matchingIds = getDescriptor().findMatchingIds(os, name);
final String id = selectIdFromListAndLogProblems(matchingIds, name, "VolumeSnapshots");
List<String> matchingIds = getDescriptor().findMatchingIds(os, name);
final String id;
if (matchingIds.size() == 0 && overrideNotFoundImage) {
String overrideName = name;
final List<String> values = getDescriptor().listAllNames(os);
for (String value : values) {
if (nameFilterRegexp != null) {
if (value.matches(nameFilterRegexp)){
overrideName = value;
LOGGER.warning("Volume snapshot " + name + "not found, overriding with " + overrideName);
break;
}
}
}
matchingIds = getDescriptor().findMatchingIds(os, overrideName);
id = selectIdFromListAndLogProblems(matchingIds, overrideName, "VolumeSnapshots");
} else {
id = selectIdFromListAndLogProblems(matchingIds, name, "VolumeSnapshots");
}
String volumeSnapshotDescriptionOrNull = null;
try {
volumeSnapshotDescriptionOrNull = os.getVolumeSnapshotDescription(id);
Expand Down Expand Up @@ -499,8 +558,8 @@ public List<String> listAllNames(Openstack openstack) {
@Restricted(DoNotUse.class)
@OsAuthDescriptor.InjectOsAuth
@RequirePOST
public ListBoxModel doFillNameItems(@QueryParameter String name, @QueryParameter String endPointUrl, @QueryParameter boolean ignoreSsl, @QueryParameter String credentialsId, @QueryParameter String zone) {
return makeListBoxModelOfAllNames(name, endPointUrl, ignoreSsl, credentialsId, zone);
public ListBoxModel doFillNameItems(@QueryParameter String name, @QueryParameter String endPointUrl, @QueryParameter boolean ignoreSsl, @QueryParameter String credentialsId, @QueryParameter String zone, @QueryParameter String nameFilterRegexp) {
return makeListBoxModelOfAllNames(name, endPointUrl, ignoreSsl, credentialsId, zone, nameFilterRegexp);
}

@Restricted(DoNotUse.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
<f:entry title="Name" field="name">
<f:select checkMethod="post"/>
</f:entry>
<f:entry title="Name filter Regexp" field="nameFilterRegexp">
<f:textbox checkMethod="post"/>
</f:entry>
<f:entry title="Use first available, if selected not found" field="overrideNotFoundImage">
<f:checkbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Filter to list available image names based on RegExp.
<br/>
The name list will be populated with the names of images that matches provided RegExp.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Override selected name, if not found or no longer available on server.
<br/>
Note: If the filter RegExp is ambiguous, i.e. there are multiple images matches filter regexp, then first matched will be used.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@
<f:entry title="Volume Size in GB" field="volumeSize">
<f:number clazz="required positive-number"/>
</f:entry>
<f:entry title="Name filter Regexp" field="nameFilterRegexp">
<f:textbox checkMethod="post"/>
</f:entry>
<f:entry title="Use first available, if selected not found" field="overrideNotFoundImage">
<f:checkbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Filter to list available image names based on RegExp.
<br/>
The name list will be populated with the names of images that matches provided RegExp.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Override selected name, if not found or no longer available on server.
<br/>
Note: If the filter RegExp is ambiguous, i.e. there are multiple images matches filter regexp, then first matched will be used.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@
<f:entry title="Name" field="name">
<f:select checkMethod="post"/>
</f:entry>
<f:entry title="Name filter Regexp" field="nameFilterRegexp">
<f:textbox checkMethod="post"/>
</f:entry>
<f:entry title="Use first available, if selected not found" field="overrideNotFoundImage">
<f:checkbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Filter to list available image names based on RegExp.
<br/>
The name list will be populated with the names of images that matches provided RegExp.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
Override selected name, if not found or no longer available on server.
<br/>
Note: If the filter RegExp is ambiguous, i.e. there are multiple images matches filter regexp, then first matched will be used.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static SlaveOptions dummySlaveOptions() {
dummyUserData("dummyUserDataId");
}
return new SlaveOptions(
new BootSource.VolumeSnapshot("id"), "hw", "nw1,mw2", "dummyUserDataId", 1, 2, "pool", "sg", "az", 1, null, 10,
new BootSource.VolumeSnapshot("id", null, false), "hw", "nw1,mw2", "dummyUserDataId", 1, 2, "pool", "sg", "az", 1, null, 10,
"jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, mkListOfNodeProperties(1, 2), 1, null
);
}
Expand Down Expand Up @@ -173,7 +173,7 @@ public SlaveOptions defaultSlaveOptions() {

// Use some real-looking values preserving defaults to make sure plugin works with them
return JCloudsCloud.DescriptorImpl.getDefaultOptions().getBuilder()
.bootSource(new BootSource.Image("dummyImageId"))
.bootSource(new BootSource.Image("dummyImageId", null, false))
.hardwareId("dummyHardwareId")
.networkId("dummyNetworkId")
.userDataId("dummyUserDataId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public void presentUIDefaults() throws Exception {
String openstackAuth = j.dummyCredentials();

JCloudsSlaveTemplate template = new JCloudsSlaveTemplate("template", "label", new SlaveOptions(
new BootSource.Image("iid"), "hw", "nw", "ud", 1, 0, "public", "sg", "az", 2, "kp", 3, "jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, null, 4, false
new BootSource.Image("iid", null, false), "hw", "nw", "ud", 1, 0, "public", "sg", "az", 2, "kp", 3, "jvmo", "fsRoot", LauncherFactory.JNLP.JNLP, null, 4, false
));
JCloudsCloud cloud = new JCloudsCloud("openstack", "endPointUrl", false,"zone", new SlaveOptions(
new BootSource.VolumeSnapshot("vsid"), "HW", "NW", "UD", 6, 4, null, "SG", "AZ", 7, "KP", 8, "JVMO", "FSrOOT", new LauncherFactory.SSH("cid"), null, 9, false
new BootSource.VolumeSnapshot("vsid", null, false), "HW", "NW", "UD", 6, 4, null, "SG", "AZ", 7, "KP", 8, "JVMO", "FSrOOT", new LauncherFactory.SSH("cid"), null, 9, false
), Collections.singletonList(template),openstackAuth);
j.jenkins.clouds.add(cloud);

Expand Down
Loading