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