Skip to content

Commit

Permalink
fix(cloud): rectify issue of created Multipass VMs not getting attach…
Browse files Browse the repository at this point in the history
…ed as Computer abstraction between shutdown/restarts.
  • Loading branch information
hainenber committed Dec 1, 2024
1 parent 7f555f6 commit 84f0790
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.hainenber.jenkins.multipass.sdk.MultipassClient;
import jakarta.annotation.Nonnull;
import java.io.IOException;
import java.io.Serial;
import java.util.Objects;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
Expand All @@ -17,6 +18,8 @@
public class MultipassAgent extends AbstractCloudSlave {
private static final Logger LOGGER = LoggerFactory.getLogger(MultipassAgent.class);
private final transient MultipassCloud cloud;

@Serial
private static final long serialVersionUID = 2553788927582449937L;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,12 @@ public synchronized Collection<NodeProvisioner.PlannedNode> provision(CloudState

// Initializing builder nodes and add to list of provisioned instances.
for (int i = 0; i < numInstancesToLaunch; i++) {
final String suffix = RandomStringUtils.randomAlphabetic(4);
final String displayName = String.format("%s-multipass", suffix);
final String instanceName = String.format("%s-%s", getName(), RandomStringUtils.randomAlphanumeric(4));
final MultipassCloud cloud = this;
final Future<Node> nodeResolver = Computer.threadPoolForRemoting.submit(() -> {
MultipassLauncher launcher = new MultipassLauncher(cloud);
try {
MultipassAgent agent = new MultipassAgent(cloud, displayName, launcher);
MultipassAgent agent = new MultipassAgent(cloud, instanceName, launcher);
agent.setLabelString(cloud.getLabel());
jenkinsController().addNode(agent);
return agent;
Expand All @@ -164,7 +163,7 @@ public synchronized Collection<NodeProvisioner.PlannedNode> provision(CloudState
return null;
}
});
nodeList.add(new NodeProvisioner.PlannedNode(displayName, nodeResolver, 1));
nodeList.add(new NodeProvisioner.PlannedNode(instanceName, nodeResolver, 1));
}

lastProvisionTime = System.currentTimeMillis();
Expand Down
30 changes: 16 additions & 14 deletions src/main/java/io/hainenber/jenkins/multipass/MultipassLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -85,15 +81,21 @@ protected void launchScript(MultipassComputer computer, TaskListener listener) t

synchronized (this) {
try {
var instanceName = String.format("%s-%s", cloud.getName(), RandomStringUtils.randomAlphanumeric(4));
cloud.getMultipassClient()
.createInstance(
instanceName,
cloud.getCloudInitConfig(),
cloud.getCpu(),
cloud.getMemory(),
cloud.getDisk(),
cloud.getDistroAlias());
var instanceName = computer.getDisplayName();
var multipassClient = cloud.getMultipassClient();

// Only create new Multipass VM when there's no VM with matching name identifier.
// If there's matched one, the launcher will launch its Computer abstraction.
var existingInstance = cloud.getMultipassClient().getInstance(instanceName);
if (existingInstance.isEmpty()) {
multipassClient.createInstance(
instanceName,
cloud.getCloudInitConfig(),
cloud.getCpu(),
cloud.getMemory(),
cloud.getDisk(),
cloud.getDistroAlias());
}

// Establish SSH connection between controller and agent.
var instance = cloud.getMultipassClient().getInstance(instanceName);
Expand Down

0 comments on commit 84f0790

Please sign in to comment.