Implement full OpenVPN lifecycle: provisioning, removal, IP allocation, and SSH integration
This commit is contained in:
@@ -113,4 +113,28 @@ public class OpenVpnService {
|
|||||||
public boolean isDryRun() {
|
public boolean isDryRun() {
|
||||||
return properties.isProvisionDryRun();
|
return properties.isProvisionDryRun();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String buildRemoveCommand(String clientName, String lanSubnet) {
|
||||||
|
validateShellSafe(clientName);
|
||||||
|
validateShellSafe(lanSubnet);
|
||||||
|
|
||||||
|
return properties.getToolsPath()
|
||||||
|
+ "/remove-client.sh "
|
||||||
|
+ clientName + " "
|
||||||
|
+ lanSubnet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SshCommandResult removeClient(String clientName, String lanSubnet) {
|
||||||
|
String command = buildRemoveCommand(clientName, lanSubnet);
|
||||||
|
|
||||||
|
if (properties.isProvisionDryRun()) {
|
||||||
|
return new SshCommandResult(
|
||||||
|
0,
|
||||||
|
"DRY RUN ONLY. Would execute: " + command,
|
||||||
|
""
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return sshService.executeOnConfiguredVps(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -45,13 +45,6 @@ public class RouterController {
|
|||||||
return service.findById(id);
|
return service.findById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/{id}/status")
|
|
||||||
public Router updateStatus(
|
|
||||||
@PathVariable UUID id,
|
|
||||||
@Valid @RequestBody UpdateRouterStatusRequest request
|
|
||||||
) {
|
|
||||||
return service.updateStatus(id, request);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void delete(@PathVariable UUID id) {
|
public void delete(@PathVariable UUID id) {
|
||||||
@@ -122,21 +115,32 @@ public class RouterController {
|
|||||||
var deployment = deploymentService.startDeployment(router, DeploymentAction.REMOVE);
|
var deployment = deploymentService.startDeployment(router, DeploymentAction.REMOVE);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
service.forceStatus(id, RouterStatus.PROVISIONING); // or REMOVING if you want later
|
service.forceStatus(id, RouterStatus.REMOVING);
|
||||||
|
|
||||||
var result = sshService.executeOnConfiguredVps(
|
var allocation = ipAllocationService.findByRouterId(id);
|
||||||
"echo 'Removing router: " + router.getName() + "' && whoami && hostname"
|
|
||||||
|
var result = openVpnService.removeClient(
|
||||||
|
allocation.getClientName(),
|
||||||
|
allocation.getLanSubnet()
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.exitCode() != 0) {
|
if (result.exitCode() != 0) {
|
||||||
throw new IllegalStateException(result.stderr());
|
throw new IllegalStateException(
|
||||||
|
"Remove failed. stdout: " + result.stdout()
|
||||||
|
+ " stderr: " + result.stderr()
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
var finished = deploymentService.finishSuccess(
|
var finished = deploymentService.finishSuccess(
|
||||||
deployment,
|
deployment,
|
||||||
result.stdout()
|
result.stdout()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (openVpnService.isDryRun()) {
|
||||||
|
service.forceStatus(id, RouterStatus.PROVISIONED);
|
||||||
|
} else {
|
||||||
service.forceStatus(id, RouterStatus.REMOVED);
|
service.forceStatus(id, RouterStatus.REMOVED);
|
||||||
|
}
|
||||||
|
|
||||||
return DeploymentResponse.from(finished);
|
return DeploymentResponse.from(finished);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user