Skip to content

Commit a2ff7d4

Browse files
committed
refactor: restructure SSH module into dedicated submodules
- Convert src/command_wrappers/ssh.rs file into folder structure - Create separate submodules for SSH components: - credentials.rs: SshCredentials struct and implementations - connection.rs: SshConnection struct and implementations - client.rs: SshClient struct and implementations + tests - mod.rs: SshError enum and module exports - Move SshCredentials and SshConnection from src/config/ssh.rs to command_wrappers - Update all imports throughout codebase to use new module paths - Maintain backwards compatibility via config module re-exports - Update documentation examples to reference new module paths This improves code organization by grouping SSH-related types together and creating focused, single-responsibility modules for better maintainability.
1 parent a8385c6 commit a2ff7d4

File tree

9 files changed

+105
-90
lines changed

9 files changed

+105
-90
lines changed

src/actions/cloud_init.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tracing::info;
33

44
use crate::actions::{RemoteAction, RemoteActionError};
55
use crate::command_wrappers::ssh::SshClient;
6-
use crate::config::ssh::SshConnection;
6+
use crate::command_wrappers::ssh::SshConnection;
77

88
/// Action that checks if cloud-init has completed successfully on the server
99
pub struct CloudInitValidator {

src/actions/docker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tracing::{info, warn};
33

44
use crate::actions::{RemoteAction, RemoteActionError};
55
use crate::command_wrappers::ssh::SshClient;
6-
use crate::config::ssh::SshConnection;
6+
use crate::command_wrappers::ssh::SshConnection;
77

88
/// Action that validates Docker installation and daemon status on the server
99
pub struct DockerValidator {

src/actions/docker_compose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use tracing::{info, warn};
33

44
use crate::actions::{RemoteAction, RemoteActionError};
55
use crate::command_wrappers::ssh::SshClient;
6-
use crate::config::ssh::SshConnection;
6+
use crate::command_wrappers::ssh::SshConnection;
77

88
/// Action that validates Docker Compose installation and basic functionality on the server
99
pub struct DockerComposeValidator {
Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
use std::time::Duration;
2-
use thiserror::Error;
32

43
use tracing::info;
54

65
use crate::command::{CommandError, CommandExecutor};
7-
use crate::config::ssh::SshConnection;
86

9-
/// Errors that can occur during SSH operations
10-
#[derive(Error, Debug)]
11-
pub enum SshError {
12-
/// SSH connectivity could not be established within the timeout period
13-
#[error("SSH connectivity to '{host_ip}' could not be established after {attempts} attempts ({timeout_seconds} seconds)")]
14-
ConnectivityTimeout {
15-
host_ip: String,
16-
attempts: u32,
17-
timeout_seconds: u32,
18-
},
19-
20-
/// Underlying command execution failed
21-
#[error("SSH command execution failed: {source}")]
22-
CommandFailed {
23-
#[source]
24-
source: CommandError,
25-
},
26-
}
7+
use super::{SshConnection, SshError};
278

289
/// A specialized SSH client with predefined security settings
2910
///
@@ -246,8 +227,8 @@ impl SshClient {
246227

247228
#[cfg(test)]
248229
mod tests {
230+
use super::super::SshCredentials;
249231
use super::*;
250-
use crate::config::ssh::SshCredentials;
251232
use std::net::{IpAddr, Ipv4Addr};
252233
use std::path::PathBuf;
253234

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use std::net::IpAddr;
2+
use std::path::PathBuf;
3+
4+
use super::SshCredentials;
5+
6+
/// SSH connection configuration for a specific remote instance.
7+
///
8+
/// Contains both the SSH credentials and the target host IP address,
9+
/// representing everything needed to establish an SSH connection.
10+
#[derive(Clone)]
11+
pub struct SshConnection {
12+
/// SSH authentication credentials.
13+
pub credentials: SshCredentials,
14+
15+
/// IP address of the target host for SSH connections.
16+
///
17+
/// This is the IP address of the remote instance that the SSH client
18+
/// will connect to.
19+
pub host_ip: IpAddr,
20+
}
21+
22+
impl SshConnection {
23+
/// Creates a new SSH connection configuration with the provided parameters.
24+
///
25+
/// ```rust
26+
/// # use std::net::{IpAddr, Ipv4Addr};
27+
/// # use std::path::PathBuf;
28+
/// # use torrust_tracker_deploy::command_wrappers::ssh::{SshCredentials, SshConnection};
29+
/// let credentials = SshCredentials::new(
30+
/// PathBuf::from("/home/user/.ssh/deploy_key"),
31+
/// PathBuf::from("/home/user/.ssh/deploy_key.pub"),
32+
/// "ubuntu".to_string(),
33+
/// );
34+
/// let connection = SshConnection::new(
35+
/// credentials,
36+
/// IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)),
37+
/// );
38+
/// ```
39+
#[must_use]
40+
pub fn new(credentials: SshCredentials, host_ip: IpAddr) -> Self {
41+
Self {
42+
credentials,
43+
host_ip,
44+
}
45+
}
46+
47+
/// Access the SSH private key path.
48+
#[must_use]
49+
pub fn ssh_priv_key_path(&self) -> &PathBuf {
50+
&self.credentials.ssh_priv_key_path
51+
}
52+
53+
/// Access the SSH public key path.
54+
#[must_use]
55+
pub fn ssh_pub_key_path(&self) -> &PathBuf {
56+
&self.credentials.ssh_pub_key_path
57+
}
58+
59+
/// Access the SSH username.
60+
#[must_use]
61+
pub fn ssh_username(&self) -> &str {
62+
&self.credentials.ssh_username
63+
}
64+
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::net::IpAddr;
22
use std::path::PathBuf;
33

4+
use super::SshConnection;
5+
46
/// SSH credentials for remote instance authentication.
57
///
68
/// Contains the static SSH authentication information that is known
@@ -33,7 +35,7 @@ impl SshCredentials {
3335
///
3436
/// ```rust
3537
/// # use std::path::PathBuf;
36-
/// # use torrust_tracker_deploy::config::ssh::SshCredentials;
38+
/// # use torrust_tracker_deploy::command_wrappers::ssh::SshCredentials;
3739
/// let credentials = SshCredentials::new(
3840
/// PathBuf::from("/home/user/.ssh/deploy_key"),
3941
/// PathBuf::from("/home/user/.ssh/deploy_key.pub"),
@@ -65,63 +67,3 @@ impl SshCredentials {
6567
}
6668
}
6769
}
68-
69-
/// SSH connection configuration for a specific remote instance.
70-
///
71-
/// Contains both the SSH credentials and the target host IP address,
72-
/// representing everything needed to establish an SSH connection.
73-
#[derive(Clone)]
74-
pub struct SshConnection {
75-
/// SSH authentication credentials.
76-
pub credentials: SshCredentials,
77-
78-
/// IP address of the target host for SSH connections.
79-
///
80-
/// This is the IP address of the remote instance that the SSH client
81-
/// will connect to.
82-
pub host_ip: IpAddr,
83-
}
84-
85-
impl SshConnection {
86-
/// Creates a new SSH connection configuration with the provided parameters.
87-
///
88-
/// ```rust
89-
/// # use std::net::{IpAddr, Ipv4Addr};
90-
/// # use std::path::PathBuf;
91-
/// # use torrust_tracker_deploy::config::ssh::{SshCredentials, SshConnection};
92-
/// let credentials = SshCredentials::new(
93-
/// PathBuf::from("/home/user/.ssh/deploy_key"),
94-
/// PathBuf::from("/home/user/.ssh/deploy_key.pub"),
95-
/// "ubuntu".to_string(),
96-
/// );
97-
/// let connection = SshConnection::new(
98-
/// credentials,
99-
/// IpAddr::V4(Ipv4Addr::new(192, 168, 1, 100)),
100-
/// );
101-
/// ```
102-
#[must_use]
103-
pub fn new(credentials: SshCredentials, host_ip: IpAddr) -> Self {
104-
Self {
105-
credentials,
106-
host_ip,
107-
}
108-
}
109-
110-
/// Access the SSH private key path.
111-
#[must_use]
112-
pub fn ssh_priv_key_path(&self) -> &PathBuf {
113-
&self.credentials.ssh_priv_key_path
114-
}
115-
116-
/// Access the SSH public key path.
117-
#[must_use]
118-
pub fn ssh_pub_key_path(&self) -> &PathBuf {
119-
&self.credentials.ssh_pub_key_path
120-
}
121-
122-
/// Access the SSH username.
123-
#[must_use]
124-
pub fn ssh_username(&self) -> &str {
125-
&self.credentials.ssh_username
126-
}
127-
}

src/command_wrappers/ssh/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
pub mod client;
2+
pub mod connection;
3+
pub mod credentials;
4+
5+
pub use client::SshClient;
6+
pub use connection::SshConnection;
7+
pub use credentials::SshCredentials;
8+
9+
use thiserror::Error;
10+
11+
use crate::command::CommandError;
12+
13+
/// Errors that can occur during SSH operations
14+
#[derive(Error, Debug)]
15+
pub enum SshError {
16+
/// SSH connectivity could not be established within the timeout period
17+
#[error("SSH connectivity to '{host_ip}' could not be established after {attempts} attempts ({timeout_seconds} seconds)")]
18+
ConnectivityTimeout {
19+
host_ip: String,
20+
attempts: u32,
21+
timeout_seconds: u32,
22+
},
23+
24+
/// Underlying command execution failed
25+
#[error("SSH command execution failed: {source}")]
26+
CommandFailed {
27+
#[source]
28+
source: CommandError,
29+
},
30+
}

src/config/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::path::PathBuf;
22

3-
pub mod ssh;
4-
pub use ssh::{SshConnection, SshCredentials};
3+
pub use crate::command_wrappers::ssh::{SshConnection, SshCredentials};
54

65
/// Configuration parameters for deployment environments.
76
///

src/steps/infrastructure/wait_ssh_connectivity.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use tracing::info;
22

3-
use crate::command_wrappers::ssh::{SshClient, SshError};
4-
use crate::config::SshConnection;
3+
use crate::command_wrappers::ssh::{SshClient, SshConnection, SshError};
54

65
/// Step that waits for SSH connectivity to be established on a remote host
76
pub struct WaitForSSHConnectivityStep {
@@ -54,7 +53,7 @@ impl WaitForSSHConnectivityStep {
5453
mod tests {
5554
use std::net::{IpAddr, Ipv4Addr};
5655

57-
use crate::config::ssh::SshCredentials;
56+
use crate::command_wrappers::ssh::SshCredentials;
5857

5958
use super::*;
6059

0 commit comments

Comments
 (0)