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 -> {
if (!Helper.isPossiblyLong(volume)) {
return new ValidationResult(false, String.format("not a valid volume ID: %s", volume));
}
final GetVolumeByIdResponse result = api.getVolumeById(Long.parseLong(volume)).execute().body();
if (result == null) {
return new ValidationResult(false, String.format("Volume %s not found", volume));
}else {
return new ValidationResult(true, String.format("%s: %s",
result.getVolume().getName(), result.getVolume().getFormat()));
}
}, credentialsId);
}

static ValidationResult verifyVolumes(String volumeIds, String credentialId) {
log.info("volumeIds: {}", volumeIds);
return Arrays.stream(volumeIds.split(",")).map(volId -> verifyVolume(volId, credentialId))
.filter(res -> !res.isSuccess()).findFirst().orElse(ValidationResult.OK);
}

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");
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());
}

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()) {
createServerRequest.setAutomount(true);
}
if (!Strings.isNullOrEmpty(agent.getTemplate().getVolumeIds())) {
createServerRequest.setVolumes(Helper.idList(agent.getTemplate().getVolumeIds()));
}
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}))
private boolean automountVolumes;

@Getter
@Setter(onMethod = @__({@DataBoundSetter}))
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();
}

@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.