Add dry-run provisioning command generation

This commit is contained in:
litoral05
2026-05-05 12:29:33 +01:00
parent c09aff2fcb
commit 6f70cbfe67
4 changed files with 42 additions and 9 deletions
@@ -3,6 +3,8 @@ package com.litoralregas.openvpn.router;
import com.litoralregas.openvpn.deployment.DeploymentAction;
import com.litoralregas.openvpn.deployment.DeploymentResponse;
import com.litoralregas.openvpn.deployment.DeploymentService;
import com.litoralregas.openvpn.openvpn.IpAllocationService;
import com.litoralregas.openvpn.openvpn.OpenVpnService;
import com.litoralregas.openvpn.ssh.SshService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
@@ -17,11 +19,15 @@ public class RouterController {
private final RouterService service;
private final DeploymentService deploymentService;
private final SshService sshService;
private final IpAllocationService ipAllocationService;
private final OpenVpnService openVpnService;
public RouterController(RouterService service, DeploymentService deploymentService, SshService sshService) {
public RouterController(RouterService service, DeploymentService deploymentService, SshService sshService, IpAllocationService ipAllocationService, OpenVpnService openVpnService) {
this.service = service;
this.deploymentService = deploymentService;
this.sshService = sshService;
this.ipAllocationService = ipAllocationService;
this.openVpnService = openVpnService;
}
@GetMapping
@@ -61,20 +67,22 @@ public class RouterController {
try {
service.forceStatus(id, RouterStatus.PROVISIONING);
var result = sshService.executeOnConfiguredVps(
"echo 'Provisioning router: " + router.getName() + "' && whoami && hostname"
);
var allocation = ipAllocationService.findByRouterId(id);
if (result.exitCode() != 0) {
throw new IllegalStateException(result.stderr());
}
String command = openVpnService.buildProvisionCommand(
allocation.getClientName(),
allocation.getLanSubnet(),
allocation.getVpnIp()
);
var finishedDeployment = deploymentService.finishSuccess(
deployment,
result.stdout()
"DRY RUN ONLY. Would execute: " + command
);
service.forceStatus(id, RouterStatus.REMOVING);
// Keep this as PROVISIONING? No — dry run succeeded but real provision did not happen.
// So we should NOT mark as PROVISIONED yet.
service.forceStatus(id, RouterStatus.PENDING);
return DeploymentResponse.from(finishedDeployment);
} catch (Exception exception) {