Skip to content

Commit

Permalink
Add support to attach volumes to server
Browse files Browse the repository at this point in the history
Fixes #71

Signed-off-by: Richard Kosegi <richard.kosegi@gmail.com>
  • Loading branch information
rkosegi committed Aug 17, 2023
1 parent 69136a8 commit e3d68a8
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ These additional attributes can be specified, but are not required:
- `Only public networking will be allocated` - public IP address will be allocated to the server
- `Configure both private and public networking`

- `Automount volumes` - Auto-mount volumes after attach.

- `Volume IDs to attach` - Volume IDs which should be attached to the Server at the creation time. Volumes must be in the same Location.
Note that volumes can be mounted into **single server** at the time.

### Scripted configuration using Groovy

```groovy
Expand Down Expand Up @@ -197,11 +202,15 @@ jenkins:
remoteFs: /var/lib/jenkins
location: fsn1
image: name=jenkins
mode: EXCLUSIVE
network: subsystem=cd
labelStr: java
numExecutors: 3
placementGroup: "1000656"
connectivity: "public-only"
automountVolumes: true
volumeIds:
- 12345678
connector:
root:
sshCredentialsId: 'ssh-private-key'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cloud.dnation.hetznerclient.GetPlacementGroupByIdResponse;
import cloud.dnation.hetznerclient.GetPlacementGroupsResponse;
import cloud.dnation.hetznerclient.GetServerTypesResponse;
import cloud.dnation.hetznerclient.GetVolumeByIdResponse;
import cloud.dnation.hetznerclient.HetznerApi;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
Expand All @@ -34,6 +35,8 @@
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;

@Slf4j
public class ConfigurationValidator {
/**
Expand Down Expand Up @@ -205,6 +208,27 @@ static ValidationResult verifyLocation(String location, String credentialsId) {
}, credentialsId);
}

static ValidationResult verifyVolume(String volume, String credentialsId) {
return validateWithClient(api -> {

Check warning on line 212 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 212 is not covered by tests
if (!Helper.isPossiblyLong(volume)) {

Check warning on line 213 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 213 is only partially covered, 2 branches are missing
return new ValidationResult(false, String.format("not a valid volume ID: %s", volume));

Check warning on line 214 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 214 is not covered by tests
}
final GetVolumeByIdResponse result = api.getVolumeById(Long.parseLong(volume)).execute().body();

Check warning on line 216 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 216 is not covered by tests
if (result == null) {

Check warning on line 217 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 217 is only partially covered, 2 branches are missing
return new ValidationResult(false, String.format("Volume %s not found", volume));

Check warning on line 218 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 218 is not covered by tests
}else {
return new ValidationResult(true, String.format("%s: %s",

Check warning on line 220 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 220 is not covered by tests
result.getVolume().getName(), result.getVolume().getFormat()));

Check warning on line 221 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 221 is not covered by tests
}
}, credentialsId);
}

static ValidationResult verifyVolumes(String volumeIds, String credentialId) {
log.info("volumeIds: {}", volumeIds);

Check warning on line 227 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 227 is not covered by tests
return Arrays.stream(volumeIds.split(",")).map(volId -> verifyVolume(volId, credentialId))

Check warning on line 228 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 228 is not covered by tests
.filter(res -> !res.isSuccess()).findFirst().orElse(ValidationResult.OK);

Check warning on line 229 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 229 is only partially covered, 2 branches are missing
}

public static FormValidation doCheckPositiveInt(String value, String name) {
if (Ints.tryParse(value) == null) {
return FormValidation.error(name + " must be positive integer : " + value);
Expand All @@ -226,7 +250,7 @@ private interface ValidationAction {

@Data
static class ValidationResult {
static final ValidationResult OK = new ValidationResult(true, null);
static final ValidationResult OK = new ValidationResult(true, "OK");

Check warning on line 253 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/ConfigurationValidator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 253 is not covered by tests
private final boolean success;
private final String message;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Base64;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -105,6 +106,10 @@ public static boolean isPossiblyLong(String str) {
}
}

public static List<Long> idList(String str) {
return Arrays.stream(str.split(",")).map(Long::parseLong).collect(Collectors.toList());

Check warning on line 110 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/Helper.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 110 is not covered by tests
}

public static <T, E> List<E> getPayload(@Nonnull Response<T> response, @Nonnull Function<T, List<E>> mapper) {
final T body = response.body();
if (body == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ public HetznerServerInfo createServer(HetznerServerAgent agent) {
}

final CreateServerRequest createServerRequest = new CreateServerRequest();
if (agent.getTemplate().isAutomountVolumes()) {

Check warning on line 254 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloudResourceManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 254 is only partially covered, 2 branches are missing
createServerRequest.setAutomount(true);

Check warning on line 255 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloudResourceManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 255 is not covered by tests
}
if (!Strings.isNullOrEmpty(agent.getTemplate().getVolumeIds())) {

Check warning on line 257 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloudResourceManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 257 is only partially covered, 2 branches are missing
createServerRequest.setVolumes(Helper.idList(agent.getTemplate().getVolumeIds()));

Check warning on line 258 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerCloudResourceManager.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 258 is not covered by tests
}
final ConnectivityType ct = agent.getTemplate().getConnectivity().getType();
if (ct == ConnectivityType.BOTH || ct == ConnectivityType.PRIVATE) {
if (!Strings.isNullOrEmpty(agent.getTemplate().getNetwork())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static cloud.dnation.jenkins.plugins.hetzner.ConfigurationValidator.verifyNetwork;
import static cloud.dnation.jenkins.plugins.hetzner.ConfigurationValidator.verifyPlacementGroup;
import static cloud.dnation.jenkins.plugins.hetzner.ConfigurationValidator.verifyServerType;
import static cloud.dnation.jenkins.plugins.hetzner.ConfigurationValidator.verifyVolumes;
import static cloud.dnation.jenkins.plugins.hetzner.Helper.getStringOrDefault;
import static cloud.dnation.jenkins.plugins.hetzner.HetznerConstants.DEFAULT_REMOTE_FS;

Expand Down Expand Up @@ -130,6 +131,14 @@ public class HetznerServerTemplate extends AbstractDescribableImpl<HetznerServer
@Setter(onMethod = @__({@DataBoundSetter}))
private AbstractConnectivity connectivity;

@Getter
@Setter(onMethod = @__({@DataBoundSetter}))

Check warning on line 135 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerServerTemplate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 135 is not covered by tests
private boolean automountVolumes;

@Getter
@Setter(onMethod = @__({@DataBoundSetter}))

Check warning on line 139 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerServerTemplate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 139 is not covered by tests
private String volumeIds;

@DataBoundConstructor
public HetznerServerTemplate(String name, String labelStr, String image,
String location, String serverType) {
Expand Down Expand Up @@ -168,6 +177,9 @@ protected Object readResolve() {
if (userData == null) {
userData = "";
}
if (volumeIds == null) {
volumeIds = "";
}
return this;
}

Expand Down Expand Up @@ -234,6 +246,13 @@ public FormValidation doVerifyServerType(@QueryParameter String serverType,
return verifyServerType(serverType, credentialsId).toFormValidation();
}

@Restricted(NoExternalUse.class)
@RequirePOST
public FormValidation doVerifyVolumes(@QueryParameter String volumeIds,
@QueryParameter String credentialsId) {
return verifyVolumes(volumeIds, credentialsId).toFormValidation();

Check warning on line 253 in src/main/java/cloud/dnation/jenkins/plugins/hetzner/HetznerServerTemplate.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 253 is not covered by tests
}

@Restricted(NoExternalUse.class)
@RequirePOST
public FormValidation doCheckImage(@QueryParameter String image) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@
<f:dropdownDescriptorSelector field="primaryIp" title="Primary IP" />

<f:dropdownDescriptorSelector field="connectivity" title="${%Connectivity}" />

<f:entry title="Automount volumes" field="automountVolumes" default="false">
<f:checkbox />
</f:entry>
<f:entry title="${%Volume IDs to attach}" field="volumeIds">
<f:textbox />
<f:validateButton title="Verify volumes" progress="${%Testing...}" method="verifyVolumes"
with="volumeIds,credentialsId"/>
</f:entry>
</f:advanced>
<f:advanced title="${%Other}" align="left">
<f:entry title="${%User data}" field="userData">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
Copyright 2023 https://dnation.cloud
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div>
Auto-mount Volumes after attach.
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!--
Copyright 2023 https://dnation.cloud
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<div>
Volume IDs which should be attached to the Server at the creation time. Volumes must be in the same Location.
Note that volumes can be mounted into <strong>single server</strong> at the time.
</div>

0 comments on commit e3d68a8

Please sign in to comment.