Skip to content

Commit b991e4d

Browse files
committed
feat: add configurable SSH port to Environment struct
- Add ssh_port: u16 field to Environment<S> struct after ssh_credentials - Update Environment::new constructor to accept ssh_port parameter (3 args) - Add ssh_port() getter method for accessing the field - Make DEFAULT_SSH_PORT constant public and re-export from ssh module - Update all three E2E binaries to import and use DEFAULT_SSH_PORT - Update render_ansible_templates in provision.rs to accept ssh_port - Update container start method to accept ssh_port parameter - Update all Environment::new calls throughout codebase with ssh_port - Fix all doc tests to include ssh_port parameter This change enables configurable SSH port across the deployment system while maintaining backward compatibility with default port 22.
1 parent 64c0b25 commit b991e4d

28 files changed

+120
-75
lines changed

src/application/commands/provision.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ impl ProvisionCommand {
284284
let instance_ip = instance_info.ip_address;
285285

286286
let current_step = ProvisionStep::RenderAnsibleTemplates;
287-
self.render_ansible_templates(ssh_credentials, instance_ip)
287+
let ssh_port = environment.ssh_port();
288+
self.render_ansible_templates(ssh_credentials, instance_ip, ssh_port)
288289
.await
289290
.map_err(|e| (e, current_step))?;
290291

@@ -363,8 +364,9 @@ impl ProvisionCommand {
363364
&self,
364365
ssh_credentials: &SshCredentials,
365366
instance_ip: IpAddr,
367+
ssh_port: u16,
366368
) -> Result<(), ProvisionCommandError> {
367-
let socket_addr = std::net::SocketAddr::new(instance_ip, 22); // Default SSH port for VMs
369+
let socket_addr = std::net::SocketAddr::new(instance_ip, ssh_port);
368370
RenderAnsibleTemplatesStep::new(
369371
Arc::clone(&self.ansible_template_renderer),
370372
ssh_credentials.clone(),

src/bin/e2e_config_tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ use torrust_tracker_deploy::e2e::tasks::{
7575
run_configuration_validation::run_configuration_validation,
7676
};
7777
use torrust_tracker_deploy::logging::{self, LogFormat};
78-
use torrust_tracker_deploy::shared::{ssh::SshCredentials, Username};
78+
use torrust_tracker_deploy::shared::{
79+
ssh::{SshCredentials, DEFAULT_SSH_PORT},
80+
Username,
81+
};
7982

8083
#[derive(Parser)]
8184
#[command(name = "e2e-config-tests")]
@@ -137,7 +140,8 @@ pub async fn main() -> Result<()> {
137140
ssh_user.clone(),
138141
);
139142

140-
let environment = Environment::new(env_name, ssh_credentials);
143+
let ssh_port = DEFAULT_SSH_PORT;
144+
let environment = Environment::new(env_name, ssh_credentials, ssh_port);
141145

142146
// Create and initialize TestContext
143147
let test_context =

src/bin/e2e_provision_tests.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ use torrust_tracker_deploy::e2e::tasks::virtual_machine::{
6060
run_provision_command::run_provision_command,
6161
};
6262
use torrust_tracker_deploy::logging::{self, LogFormat};
63-
use torrust_tracker_deploy::shared::{ssh::SshCredentials, Username};
63+
use torrust_tracker_deploy::shared::{
64+
ssh::{SshCredentials, DEFAULT_SSH_PORT},
65+
Username,
66+
};
6467

6568
#[derive(Parser)]
6669
#[command(name = "e2e-provision-tests")]
@@ -131,7 +134,8 @@ pub async fn main() -> Result<()> {
131134
ssh_user.clone(),
132135
);
133136

134-
let environment = Environment::new(environment_name, ssh_credentials);
137+
let ssh_port = DEFAULT_SSH_PORT;
138+
let environment = Environment::new(environment_name, ssh_credentials, ssh_port);
135139

136140
let test_context =
137141
TestContext::from_environment(cli.keep, environment, TestContextType::VirtualMachine)?

src/bin/e2e_tests_full.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ use torrust_tracker_deploy::e2e::tasks::{
7272
},
7373
};
7474
use torrust_tracker_deploy::logging::{self, LogFormat};
75-
use torrust_tracker_deploy::shared::{ssh::SshCredentials, Username};
75+
use torrust_tracker_deploy::shared::{
76+
ssh::{SshCredentials, DEFAULT_SSH_PORT},
77+
Username,
78+
};
7679

7780
#[derive(Parser)]
7881
#[command(name = "e2e-tests")]
@@ -138,7 +141,8 @@ pub async fn main() -> Result<()> {
138141
ssh_user.clone(),
139142
);
140143

141-
let environment = Environment::new(environment_name, ssh_credentials);
144+
let ssh_port = DEFAULT_SSH_PORT;
145+
let environment = Environment::new(environment_name, ssh_credentials, ssh_port);
142146

143147
let test_context =
144148
TestContext::from_environment(cli.keep, environment, TestContextType::VirtualMachine)?

src/domain/environment/mod.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! PathBuf::from("fixtures/testing_rsa.pub"),
3636
//! ssh_username,
3737
//! );
38-
//! let environment = Environment::new(env_name, ssh_credentials);
38+
//! let environment = Environment::new(env_name, ssh_credentials, 22);
3939
//!
4040
//! // Environment automatically generates paths
4141
//! assert_eq!(*environment.data_dir(), PathBuf::from("data/e2e-config"));
@@ -120,6 +120,9 @@ pub struct Environment<S = Created> {
120120
/// SSH credentials for connecting to instances in this environment
121121
ssh_credentials: SshCredentials,
122122

123+
/// SSH port for connecting to instances in this environment
124+
ssh_port: u16,
125+
123126
/// Build directory for this environment (auto-generated)
124127
build_dir: PathBuf,
125128

@@ -144,6 +147,7 @@ impl Environment {
144147
///
145148
/// * `name` - The validated environment name
146149
/// * `ssh_credentials` - SSH credentials for connecting to instances
150+
/// * `ssh_port` - SSH port for connecting to instances
147151
///
148152
/// # Returns
149153
///
@@ -164,7 +168,8 @@ impl Environment {
164168
/// PathBuf::from("keys/prod_rsa.pub"),
165169
/// ssh_username,
166170
/// );
167-
/// let environment = Environment::new(env_name, ssh_credentials);
171+
/// let ssh_port = 22;
172+
/// let environment = Environment::new(env_name, ssh_credentials, ssh_port);
168173
///
169174
/// assert_eq!(environment.instance_name().as_str(), "torrust-tracker-vm-production");
170175
/// assert_eq!(*environment.data_dir(), PathBuf::from("data/production"));
@@ -178,7 +183,11 @@ impl Environment {
178183
/// This function does not panic. All instance name generation is guaranteed
179184
/// to succeed for valid environment names.
180185
#[must_use]
181-
pub fn new(name: EnvironmentName, ssh_credentials: SshCredentials) -> Environment<Created> {
186+
pub fn new(
187+
name: EnvironmentName,
188+
ssh_credentials: SshCredentials,
189+
ssh_port: u16,
190+
) -> Environment<Created> {
182191
let env_str = name.as_str();
183192

184193
// Generate instance name: torrust-tracker-vm-{env_name}
@@ -200,6 +209,7 @@ impl Environment {
200209
instance_name,
201210
profile_name,
202211
ssh_credentials,
212+
ssh_port,
203213
build_dir,
204214
data_dir,
205215
instance_ip: None,
@@ -245,6 +255,7 @@ impl<S> Environment<S> {
245255
instance_name: self.instance_name,
246256
profile_name: self.profile_name,
247257
ssh_credentials: self.ssh_credentials,
258+
ssh_port: self.ssh_port,
248259
build_dir: self.build_dir,
249260
data_dir: self.data_dir,
250261
instance_ip: self.instance_ip,
@@ -298,6 +309,12 @@ impl<S> Environment<S> {
298309
&self.ssh_credentials
299310
}
300311

312+
/// Returns the SSH port for this environment
313+
#[must_use]
314+
pub fn ssh_port(&self) -> u16 {
315+
self.ssh_port
316+
}
317+
301318
/// Returns the SSH username for this environment
302319
#[must_use]
303320
pub fn ssh_username(&self) -> &Username {
@@ -353,7 +370,7 @@ impl<S> Environment<S> {
353370
/// PathBuf::from("keys/test_rsa.pub"),
354371
/// ssh_username,
355372
/// );
356-
/// let environment = Environment::new(env_name, ssh_credentials);
373+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
357374
///
358375
/// // Before provisioning
359376
/// assert_eq!(environment.instance_ip(), None);
@@ -398,7 +415,7 @@ impl<S> Environment<S> {
398415
/// PathBuf::from("keys/prod_rsa.pub"),
399416
/// ssh_username,
400417
/// );
401-
/// let environment = Environment::new(env_name, ssh_credentials);
418+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
402419
///
403420
/// // Set IP after provisioning
404421
/// let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 42));
@@ -433,7 +450,7 @@ impl<S> Environment<S> {
433450
/// PathBuf::from("keys/staging_rsa.pub"),
434451
/// ssh_username,
435452
/// );
436-
/// let environment = Environment::new(env_name, ssh_credentials);
453+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
437454
///
438455
/// assert_eq!(
439456
/// environment.templates_dir(),
@@ -466,7 +483,7 @@ impl<S> Environment<S> {
466483
/// PathBuf::from("keys/prod_rsa.pub"),
467484
/// ssh_username,
468485
/// );
469-
/// let environment = Environment::new(env_name, ssh_credentials);
486+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
470487
///
471488
/// assert_eq!(
472489
/// environment.traces_dir(),
@@ -496,7 +513,7 @@ impl<S> Environment<S> {
496513
/// PathBuf::from("keys/dev_rsa.pub"),
497514
/// ssh_username,
498515
/// );
499-
/// let environment = Environment::new(env_name, ssh_credentials);
516+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
500517
///
501518
/// assert_eq!(
502519
/// environment.ansible_build_dir(),
@@ -526,7 +543,7 @@ impl<S> Environment<S> {
526543
/// PathBuf::from("keys/test_rsa.pub"),
527544
/// ssh_username,
528545
/// );
529-
/// let environment = Environment::new(env_name, ssh_credentials);
546+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
530547
///
531548
/// assert_eq!(
532549
/// environment.tofu_build_dir(),
@@ -556,7 +573,7 @@ impl<S> Environment<S> {
556573
/// PathBuf::from("keys/integration_rsa.pub"),
557574
/// ssh_username,
558575
/// );
559-
/// let environment = Environment::new(env_name, ssh_credentials);
576+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
560577
///
561578
/// assert_eq!(
562579
/// environment.ansible_templates_dir(),
@@ -586,7 +603,7 @@ impl<S> Environment<S> {
586603
/// PathBuf::from("keys/load-test-rsa.pub"),
587604
/// ssh_username,
588605
/// );
589-
/// let environment = Environment::new(env_name, ssh_credentials);
606+
/// let environment = Environment::new(env_name, ssh_credentials, 22);
590607
///
591608
/// assert_eq!(
592609
/// environment.tofu_templates_dir(),
@@ -688,7 +705,9 @@ mod tests {
688705
ssh_username,
689706
);
690707

691-
Environment::new(env_name, ssh_credentials)
708+
let ssh_port = 22;
709+
710+
Environment::new(env_name, ssh_credentials, ssh_port)
692711
}
693712

694713
/// Builds an Environment and returns the `TempDir`
@@ -707,7 +726,8 @@ mod tests {
707726
ssh_username,
708727
);
709728

710-
let environment = Environment::new(env_name, ssh_credentials);
729+
let ssh_port = 22;
730+
let environment = Environment::new(env_name, ssh_credentials, ssh_port);
711731
(environment, self.temp_dir)
712732
}
713733

@@ -737,6 +757,7 @@ mod tests {
737757
instance_name,
738758
profile_name,
739759
ssh_credentials,
760+
ssh_port: 22,
740761
data_dir: data_dir.clone(),
741762
build_dir: build_dir.clone(),
742763
instance_ip: None,

src/domain/environment/state/configure_failed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ mod tests {
164164
fn create_test_environment_configure_failed() -> Environment<ConfigureFailed> {
165165
let name = EnvironmentName::new("test-env".to_string()).unwrap();
166166
let ssh_creds = create_test_ssh_credentials();
167-
Environment::new(name, ssh_creds)
167+
Environment::new(name, ssh_creds, 22)
168168
.start_provisioning()
169169
.provisioned()
170170
.start_configuring()

src/domain/environment/state/configured.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mod tests {
9090
fn create_test_environment_configured() -> Environment<Configured> {
9191
let name = EnvironmentName::new("test-env".to_string()).unwrap();
9292
let ssh_creds = create_test_ssh_credentials();
93-
Environment::new(name, ssh_creds)
93+
Environment::new(name, ssh_creds, 22)
9494
.start_provisioning()
9595
.provisioned()
9696
.start_configuring()
@@ -129,7 +129,7 @@ mod tests {
129129
PathBuf::from("test_key.pub"),
130130
ssh_username,
131131
);
132-
Environment::new(env_name, ssh_credentials)
132+
Environment::new(env_name, ssh_credentials, 22)
133133
.start_provisioning()
134134
.provisioned()
135135
.start_configuring()

src/domain/environment/state/configuring.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod tests {
107107
fn create_test_environment_configuring() -> Environment<Configuring> {
108108
let name = EnvironmentName::new("test-env".to_string()).unwrap();
109109
let ssh_creds = create_test_ssh_credentials();
110-
Environment::new(name, ssh_creds)
110+
Environment::new(name, ssh_creds, 22)
111111
.start_provisioning()
112112
.provisioned()
113113
.start_configuring()
@@ -145,7 +145,7 @@ mod tests {
145145
PathBuf::from("test_key.pub"),
146146
ssh_username,
147147
);
148-
Environment::new(env_name, ssh_credentials)
148+
Environment::new(env_name, ssh_credentials, 22)
149149
.start_provisioning()
150150
.provisioned()
151151
.start_configuring()

src/domain/environment/state/created.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ mod tests {
112112
fn create_test_environment_created() -> Environment<Created> {
113113
let name = EnvironmentName::new("test-env".to_string()).unwrap();
114114
let ssh_creds = create_test_ssh_credentials();
115-
Environment::new(name, ssh_creds)
115+
Environment::new(name, ssh_creds, 22)
116116
}
117117

118118
#[test]
@@ -176,7 +176,7 @@ mod tests {
176176
fn create_test_environment_created() -> Environment<Created> {
177177
let name = EnvironmentName::new("test-env".to_string()).unwrap();
178178
let ssh_creds = create_test_ssh_credentials();
179-
Environment::new(name, ssh_creds)
179+
Environment::new(name, ssh_creds, 22)
180180
}
181181

182182
#[test]

src/domain/environment/state/destroyed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ mod tests {
7777
fn create_test_environment_destroyed() -> Environment<Destroyed> {
7878
let name = EnvironmentName::new("test-env".to_string()).unwrap();
7979
let ssh_creds = create_test_ssh_credentials();
80-
Environment::new(name, ssh_creds).destroy()
80+
Environment::new(name, ssh_creds, 22).destroy()
8181
}
8282

8383
#[test]

0 commit comments

Comments
 (0)