diff --git a/src/main/java/io/hainenber/jenkins/multipass/MultipassAgent.java b/src/main/java/io/hainenber/jenkins/multipass/MultipassAgent.java index 0637cb4..e7255ba 100644 --- a/src/main/java/io/hainenber/jenkins/multipass/MultipassAgent.java +++ b/src/main/java/io/hainenber/jenkins/multipass/MultipassAgent.java @@ -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; @@ -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; /** diff --git a/src/main/java/io/hainenber/jenkins/multipass/MultipassCloud.java b/src/main/java/io/hainenber/jenkins/multipass/MultipassCloud.java index d351de2..8348d84 100644 --- a/src/main/java/io/hainenber/jenkins/multipass/MultipassCloud.java +++ b/src/main/java/io/hainenber/jenkins/multipass/MultipassCloud.java @@ -149,13 +149,12 @@ public synchronized Collection 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 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; @@ -164,7 +163,7 @@ public synchronized Collection provision(CloudState return null; } }); - nodeList.add(new NodeProvisioner.PlannedNode(displayName, nodeResolver, 1)); + nodeList.add(new NodeProvisioner.PlannedNode(instanceName, nodeResolver, 1)); } lastProvisionTime = System.currentTimeMillis(); diff --git a/src/main/java/io/hainenber/jenkins/multipass/MultipassLauncher.java b/src/main/java/io/hainenber/jenkins/multipass/MultipassLauncher.java index 789952c..6bc33fe 100644 --- a/src/main/java/io/hainenber/jenkins/multipass/MultipassLauncher.java +++ b/src/main/java/io/hainenber/jenkins/multipass/MultipassLauncher.java @@ -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; @@ -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);