Configure VPS SSH connection from environment variables
This commit is contained in:
@@ -27,7 +27,7 @@
|
||||
<url/>
|
||||
</scm>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<java.version>17</java.version>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
package com.litoralregas.openvpn;
|
||||
|
||||
import com.litoralregas.openvpn.ssh.VpsSshProperties;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableConfigurationProperties(VpsSshProperties.class)
|
||||
public class LrOpenvpnBackendApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LrOpenvpnBackendApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ 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.ssh.SshService;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -15,10 +16,12 @@ public class RouterController {
|
||||
|
||||
private final RouterService service;
|
||||
private final DeploymentService deploymentService;
|
||||
private final SshService sshService;
|
||||
|
||||
public RouterController(RouterService service, DeploymentService deploymentService) {
|
||||
public RouterController(RouterService service, DeploymentService deploymentService, SshService sshService) {
|
||||
this.service = service;
|
||||
this.deploymentService = deploymentService;
|
||||
this.sshService = sshService;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
@@ -58,24 +61,22 @@ public class RouterController {
|
||||
try {
|
||||
service.forceStatus(id, RouterStatus.PROVISIONING);
|
||||
|
||||
// Simulated provisioning for now.
|
||||
var finishedDeployment = deploymentService.finishSuccess(
|
||||
deployment,
|
||||
"Provision completed successfully"
|
||||
);
|
||||
var result = sshService.executeOnConfiguredVps("whoami && hostname");
|
||||
|
||||
service.forceStatus(id, RouterStatus.PROVISIONED);
|
||||
|
||||
return DeploymentResponse.from(finishedDeployment);
|
||||
} catch (Exception exception) {
|
||||
var failedDeployment = deploymentService.finishFailure(
|
||||
deployment,
|
||||
exception.getMessage()
|
||||
);
|
||||
if (result.exitCode() == 0) {
|
||||
var finished = deploymentService.finishSuccess(deployment, result.stdout());
|
||||
service.forceStatus(id, RouterStatus.PROVISIONED);
|
||||
return DeploymentResponse.from(finished);
|
||||
} else {
|
||||
var failed = deploymentService.finishFailure(deployment, result.stderr());
|
||||
service.forceStatus(id, RouterStatus.FAILED);
|
||||
return DeploymentResponse.from(failed);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
var failed = deploymentService.finishFailure(deployment, e.getMessage());
|
||||
service.forceStatus(id, RouterStatus.FAILED);
|
||||
|
||||
return DeploymentResponse.from(failedDeployment);
|
||||
return DeploymentResponse.from(failed);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,20 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
@Service
|
||||
public class SshService {
|
||||
private final VpsSshProperties properties;
|
||||
public SshService(VpsSshProperties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public SshCommandResult executeOnConfiguredVps(String command) {
|
||||
return execute(
|
||||
properties.getHost(),
|
||||
properties.getPort(),
|
||||
properties.getUsername(),
|
||||
properties.getPassword(),
|
||||
command
|
||||
);
|
||||
}
|
||||
|
||||
public SshCommandResult execute(
|
||||
String host,
|
||||
|
||||
@@ -13,13 +13,7 @@ public class SshTestController {
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public SshCommandResult test(@RequestBody SshTestRequest request) {
|
||||
return sshService.execute(
|
||||
request.host(),
|
||||
request.port(),
|
||||
request.username(),
|
||||
request.password(),
|
||||
"whoami && hostname"
|
||||
);
|
||||
public SshCommandResult test() {
|
||||
return sshService.executeOnConfiguredVps("whoami && hostname");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.litoralregas.openvpn.ssh;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties(prefix = "lr.vps.ssh")
|
||||
public class VpsSshProperties {
|
||||
|
||||
private String host;
|
||||
private int port = 22;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
}
|
||||
@@ -22,4 +22,12 @@ management:
|
||||
endpoints:
|
||||
web:
|
||||
exposure:
|
||||
include: health,info
|
||||
include: health,info
|
||||
|
||||
lr:
|
||||
vps:
|
||||
ssh:
|
||||
host: ${LR_VPS_SSH_HOST}
|
||||
port: ${LR_VPS_SSH_PORT:22}
|
||||
username: ${LR_VPS_SSH_USER}
|
||||
password: ${LR_VPS_SSH_PASSWORD}
|
||||
Reference in New Issue
Block a user