Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes JENKINS-28815
  • Loading branch information
felfert committed Oct 3, 2015
1 parent c1dd46a commit d636f09
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 27 deletions.
Expand Up @@ -219,19 +219,23 @@ static ComputeServiceContext ctx(String providerName, String identity, String cr
.buildView(ComputeServiceContext.class);
}

public ComputeService newCompute() {
Properties overrides = new Properties();
if (!Strings.isNullOrEmpty(this.endPointUrl)) {
overrides.setProperty(Constants.PROPERTY_ENDPOINT, this.endPointUrl);
}
if (scriptTimeout > 0) {
overrides.setProperty(ComputeServiceProperties.TIMEOUT_SCRIPT_COMPLETE, String.valueOf(scriptTimeout));
}
if (startTimeout > 0) {
overrides.setProperty(ComputeServiceProperties.TIMEOUT_NODE_RUNNING, String.valueOf(startTimeout));
}
return ctx(this.providerName, this.identity, Secret.toString(credential), overrides, this.zones).getComputeService();
}

public ComputeService getCompute() {
if (this.compute == null) {
Properties overrides = new Properties();
if (!Strings.isNullOrEmpty(this.endPointUrl)) {
overrides.setProperty(Constants.PROPERTY_ENDPOINT, this.endPointUrl);
}
if (scriptTimeout > 0) {
overrides.setProperty(ComputeServiceProperties.TIMEOUT_SCRIPT_COMPLETE, String.valueOf(scriptTimeout));
}
if (startTimeout > 0) {
overrides.setProperty(ComputeServiceProperties.TIMEOUT_NODE_RUNNING, String.valueOf(startTimeout));
}
this.compute = ctx(this.providerName, this.identity, Secret.toString(credential), overrides, this.zones).getComputeService();
this.compute = newCompute();
}
return compute;
}
Expand Down Expand Up @@ -321,7 +325,7 @@ public JCloudsSlaveTemplate getTemplate(Label label) {
return null;
}

JCloudsSlave doProvisionFromTemplate(final JCloudsSlaveTemplate t) throws IOException {
JCloudsSlave doProvisionFromTemplate(final JCloudsSlaveTemplate t) throws IOException {
final StringWriter sw = new StringWriter();
final StreamTaskListener listener = new StreamTaskListener(sw);
JCloudsSlave node = t.provisionSlave(listener);
Expand Down Expand Up @@ -393,11 +397,11 @@ public String getDisplayName() {
public FormValidation doTestConnection(@QueryParameter String providerName, @QueryParameter String identity, @QueryParameter String credential,
@QueryParameter String cloudGlobalKeyId, @QueryParameter String endPointUrl, @QueryParameter String zones) throws IOException {
if (identity == null)
return FormValidation.error("Invalid (AccessId).");
return FormValidation.error("Invalid (AccessId).");
if (credential == null)
return FormValidation.error("Invalid credential (secret key).");
return FormValidation.error("Invalid credential (secret key).");
if (null == Util.fixEmptyAndTrim(cloudGlobalKeyId)) {
return FormValidation.error("Cloud RSA key is not specified.");
return FormValidation.error("Cloud RSA key is not specified.");
}

// Remove empty text/whitespace from the fields.
Expand Down
Expand Up @@ -17,6 +17,8 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.codec.binary.Base64;

Expand Down Expand Up @@ -310,8 +312,23 @@ public NodeMetadata get() {
LOGGER.info("Setting image id to " + imageId);
templateBuilder.imageId(imageId);
} else if (!Strings.isNullOrEmpty(imageNameRegex)) {
LOGGER.info("Setting image name regex to " + imageNameRegex);
templateBuilder.imageNameMatches(imageNameRegex);
LOGGER.info("Resolving image name regex " + imageNameRegex);
// We do NOT use templateBuilder.imageNameMatches(imageNameRegex),
// because the corresponding image id gets cached for a LOOONG time
// and we do not want that. Therefore we always search for images
// ourselves and then use the Id of a found image. To work around
// caching, we need to do this using a freshly instantiated
// ComputeService.
// See: https://issues.apache.org/jira/browse/JCLOUDS-570
// and: https://issues.apache.org/jira/browse/JCLOUDS-512
// for some insight.
for (Image i : getCloud().newCompute().listImages()) {
if (i.getName().matches(imageNameRegex)) {
LOGGER.info("Setting image id to " + i.getId());
templateBuilder.imageId(i.getId());
break;
}
}
} else {
if (!Strings.isNullOrEmpty(osFamily)) {
LOGGER.info("Setting osFamily to " + osFamily);
Expand Down Expand Up @@ -560,25 +577,38 @@ public FormValidation doValidateImageNameRegex(@QueryParameter String providerNa
return computeContextValidationResult;
}
if (Strings.isNullOrEmpty(imageNameRegex)) {
return FormValidation.error("Image Name Regex shouldn't be empty");
return FormValidation.error("Image Name Regex should not be empty.");
}

// Remove empty text/whitespace from the fields.
imageNameRegex = Util.fixEmptyAndTrim(imageNameRegex);

try {
final Set<? extends Image> images = listImages(providerName, identity, Secret.fromString(credential).getPlainText(), endPointUrl, zones);
if (images != null) {
for (final Image image : images) {
if (image.getName().matches(imageNameRegex)) {
return FormValidation.ok("Image Name Regex is valid.");
int matchcount = 0;
Pattern p = Pattern.compile(imageNameRegex);
try {
final Set<? extends Image> images = listImages(providerName, identity, Secret.fromString(credential).getPlainText(), endPointUrl, zones);
if (images != null) {
for (final Image image : images) {
if (p.matcher(image.getName()).matches()) {
matchcount++;
}
}
} else {
return FormValidation.ok("No images available to check against.");
}
} catch (Exception ex) {
return FormValidation.error("Unable to check the image name regex, please check if the credentials you provided are correct.", ex);
}
} catch (Exception ex) {
return FormValidation.error("Unable to check the image name regex, " + "please check if the credentials you provided are correct.", ex);
if (1 == matchcount) {
return FormValidation.ok("Image name regex matches exactly one image.");
}
if (1 < matchcount) {
return FormValidation.error("Ambiguous image name regex matches multiple images, please check the value and try again.");
}
} catch (PatternSyntaxException ex) {
return FormValidation.error("Invalid image name regex syntax.");
}
return FormValidation.error("Invalid Image Name Regex, please check the value and try again.");
return FormValidation.error("Image name regex does not match any image, please check the value and try again.");
}

private FormValidation validateComputeContextParameters(@QueryParameter String providerName, @QueryParameter String identity,
Expand Down

0 comments on commit d636f09

Please sign in to comment.