Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optionally allow image resolution by name #68

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
68 changes: 63 additions & 5 deletions src/main/java/com/dubture/jenkins/digitalocean/DigitalOcean.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*
* Copyright (c) 2015 Rory Hunter ([email protected])
* 2016 Maxim Biro <[email protected]>
* 2017, 2021 Harald Sitter <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -25,6 +26,7 @@

package com.dubture.jenkins.digitalocean;

import java.text.MessageFormat;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -78,6 +80,12 @@ static List<Size> getAvailableSizes(String authToken) throws DigitalOceanExcepti
return availableSizes;
}

static enum ImageFilter
{
ALLIMAGES,
USERIMAGES
}

/**
* Fetches all available images. Unlike the other getAvailable* methods, this returns a map because the values
* are sorted by a key composed of their OS distribution and version, which is useful for display purposes. Backup
Expand All @@ -88,17 +96,24 @@ static List<Size> getAvailableSizes(String authToken) throws DigitalOceanExcepti
* @throws DigitalOceanException
* @throws RequestUnsuccessfulException
*/
static SortedMap<String,Image> getAvailableImages(String authToken) throws DigitalOceanException, RequestUnsuccessfulException {
static SortedMap<String,Image> getAvailableImages(String authToken, ImageFilter filter) throws DigitalOceanException, RequestUnsuccessfulException {
DigitalOceanClient client = new DigitalOceanClient(authToken);

SortedMap<String,Image> availableImages = new TreeMap<>(ignoringCase());

Images images;
Images images = null;
int page = 0;

do {
page += 1;
images = client.getAvailableImages(page, Integer.MAX_VALUE);
switch (filter) {
case ALLIMAGES:
images = client.getAvailableImages(page, Integer.MAX_VALUE);
break;
case USERIMAGES:
images = client.getUserImages(page, Integer.MAX_VALUE);
break;
}
for (Image image : images.getImages()) {
String prefix = getPrefix(image);
final String name = prefix + image.getDistribution() + " " + image.getName();
Expand All @@ -116,6 +131,14 @@ static SortedMap<String,Image> getAvailableImages(String authToken) throws Digit
return availableImages;
}

static SortedMap<String,Image> getAvailableImages(String authToken) throws DigitalOceanException, RequestUnsuccessfulException {
return getAvailableImages(authToken, ImageFilter.ALLIMAGES);
}

static SortedMap<String,Image> getAvailableUserImages(String authToken) throws DigitalOceanException, RequestUnsuccessfulException {
return getAvailableImages(authToken, ImageFilter.USERIMAGES);
}

private static String getPrefix(Image image) {

if (image.getType() == ImageType.BACKUP) {
Expand Down Expand Up @@ -252,6 +275,30 @@ static List<Droplet> getDroplets(String authToken) throws DigitalOceanException,
return availableDroplets;
}

static Image getMatchingNamedImage(String authToken, String imageName) throws DigitalOceanException, RequestUnsuccessfulException {
List<Image> matchingImages = new ArrayList<Image>();

final SortedMap<String, Image> images = getAvailableUserImages(authToken);
for (Image image : images.values()) {
if (imageName.equals(image.getName())) {
matchingImages.add(image);
}
}

Collections.sort(matchingImages, new Comparator<Image>() {
@Override
public int compare(Image left, Image right) {
return left.getCreatedDate().compareTo(right.getCreatedDate());
}
});

if (matchingImages.size() < 1) {
throw new RuntimeException(MessageFormat.format("Failed to resolve image name '{0}'", imageName));
}

return matchingImages.get(0);
}

/**
* Fetches information for the specified droplet.
* @param authToken the API authentication token to use
Expand All @@ -265,8 +312,19 @@ static Droplet getDroplet(String authToken, Integer dropletId) throws DigitalOce
return new DigitalOceanClient(authToken).getDropletInfo(dropletId);
}

static Image newImage(String idOrSlug) {
Image image = new Image(idOrSlug);
static Image newImage(String authToken, String idOrSlugOrName, Boolean imageByName) throws DigitalOceanException, RequestUnsuccessfulException {
if (imageByName) {
return getMatchingNamedImage(authToken, idOrSlugOrName);
}

Image image;
try {
image = new Image(Integer.parseInt(idOrSlugOrName));
}
catch (NumberFormatException e) {
image = new Image(idOrSlugOrName);
}

return image;
}

Expand Down
23 changes: 15 additions & 8 deletions src/main/java/com/dubture/jenkins/digitalocean/SlaveTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Copyright (c) 2014 [email protected]
* 2016 Maxim Biro <[email protected]>
* 2017 Harald Sitter <[email protected]>
* 2017, 2021 Harald Sitter <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -93,6 +93,8 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {

private final Boolean labellessJobsAllowed;

private final Boolean imageByName;

/**
* The Image to be used for the droplet.
*/
Expand Down Expand Up @@ -155,17 +157,19 @@ public class SlaveTemplate implements Describable<SlaveTemplate> {
* @param tags the droplet tags
* @param userData user data for DigitalOcean to apply when building the agent
* @param initScript setup script to configure the agent
* @param imageByName whether to resolve the image by name rather than id
*/
@DataBoundConstructor
public SlaveTemplate(String name, String imageId, String sizeId, String regionId, String username, String workspacePath,
Integer sshPort, Boolean setupPrivateNetworking, String idleTerminationInMinutes, String numExecutors, String labelString,
Boolean labellessJobsAllowed, String instanceCap, Boolean installMonitoring, String tags,
String userData, String initScript) {
String userData, String initScript, Boolean imageByName) {

LOGGER.log(Level.INFO, "Creating SlaveTemplate with imageId = {0}, sizeId = {1}, regionId = {2}",
new Object[] { imageId, sizeId, regionId});

this.name = name;
this.imageByName = imageByName;
this.imageId = imageId;
this.sizeId = sizeId;
this.regionId = regionId;
Expand Down Expand Up @@ -257,7 +261,7 @@ public Slave provision(ProvisioningActivity.Id provisioningId,
droplet.setName(dropletName);
droplet.setSize(sizeId);
droplet.setRegion(new Region(regionId));
droplet.setImage(DigitalOcean.newImage(imageId));
droplet.setImage(DigitalOcean.newImage(authToken, imageId, imageByName));
droplet.setKeys(Arrays.asList(new Key(sshKeyId)));
droplet.setInstallMonitoring(installMonitoringAgent);
droplet.setEnablePrivateNetworking(
Expand Down Expand Up @@ -442,22 +446,21 @@ public ListBoxModel doFillSizeIdItems(@RelativePath("..") @QueryParameter String
return model;
}

public ListBoxModel doFillImageIdItems(@RelativePath("..") @QueryParameter String authTokenCredentialId) throws Exception {

public ListBoxModel doFillImageIdItems(@RelativePath("..") @QueryParameter String authTokenCredentialId, @QueryParameter Boolean imageByName) throws Exception {
ListBoxModel model = new ListBoxModel();
String authToken = DigitalOceanCloud.getAuthTokenFromCredentialId(authTokenCredentialId);
final String authToken = DigitalOceanCloud.getAuthTokenFromCredentialId(authTokenCredentialId);
if (StringUtils.isBlank(authToken)) {
return model;
}

SortedMap<String, Image> availableImages = DigitalOcean.getAvailableImages(DigitalOceanCloud.getAuthTokenFromCredentialId(authTokenCredentialId));
final SortedMap<String, Image> availableImages = imageByName ? DigitalOcean.getAvailableUserImages(authToken) : DigitalOcean.getAvailableImages(authToken);

for (Map.Entry<String, Image> entry : availableImages.entrySet()) {
final Image image = entry.getValue();

// For non-snapshots, use the image ID instead of the slug (which isn't available anyway)
// so that we can build images based upon backups.
final String value = DigitalOcean.getImageIdentifier(image);
final String value = imageByName ? image.getName() : DigitalOcean.getImageIdentifier(image);

model.add(entry.getKey(), value);
}
Expand Down Expand Up @@ -515,6 +518,10 @@ public Set<LabelAtom> getLabelSet() {
return labelSet;
}

public Boolean getImageByName() {
return imageByName;
}

public String getImageId() {
return imageId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
~
~ Copyright (c) 2014 [email protected]
~ 2016 Maxim Biro <[email protected]>
~ 2021 Harald Sitter <[email protected]>
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -31,6 +32,10 @@
<f:textbox/>
</f:entry>

<f:entry title="Resolve user image by name rather than ID or slug" field="imageByName">
<f:checkbox/>
</f:entry>

<f:entry title="Image" field="imageId">
<f:select />
</f:entry>
Expand All @@ -54,7 +59,7 @@
<f:entry title="SSH port" field="sshPort">
<f:textbox default="22" />
</f:entry>

<f:entry title="Setup Private Networking" field="setupPrivateNetworking">
<f:checkbox/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
~ The MIT License (MIT)
~
~ Copyright (c) 2021 Harald Sitter <[email protected]>
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy
~ of this software and associated documentation files (the "Software"), to deal
~ in the Software without restriction, including without limitation the rights
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
~ copies of the Software, and to permit persons to whom the Software is
~ furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in
~ all copies or substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
~ THE SOFTWARE.
-->

<div>
Always resolves the actually used image through its name. Usually images are either resolved through their slug
or their unique id. The only reason to opt into resolution by name is when you recreate snapshots using the same
name to force updates into the cloud nodes. If multiple images are present the newest is used.

<p>This option may only be used with user images, not base images provided by DigitalOcean!</p>
</div>