Skip to content

Commit c471aa8

Browse files
committed
Ensure tedge reconnect c8y migrates configuration successfully for
built-in bridge Signed-off-by: James Rhodes <jarhodes314@gmail.com>
1 parent 3c91d10 commit c471aa8

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

crates/core/tedge/src/cli/certificate/create.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl CreateCertCmd {
5757

5858
let cert = KeyCertPair::new_selfsigned_certificate(config, &self.id, key_kind)?;
5959

60+
// TODO cope with broker user being tedge
6061
// Creating files with permission 644 owned by the MQTT broker
6162
let mut cert_file =
6263
create_new_file(&self.cert_path, crate::BROKER_USER, crate::BROKER_GROUP)

crates/core/tedge/src/cli/connect/command.rs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ impl Command for ConnectCommand {
6767
if self.is_test_connection {
6868
// If the bridge is part of the mapper, the bridge config file won't exist
6969
// TODO tidy me up once mosquitto is no longer required for bridge
70-
if bridge_config.bridge_location == BridgeLocation::BuiltIn
71-
|| self.check_if_bridge_exists(&bridge_config)
72-
{
70+
if self.check_if_bridge_exists(&bridge_config) {
7371
return match self.check_connection(config) {
7472
Ok(DeviceStatus::AlreadyExists) => {
7573
let cloud = bridge_config.cloud_name;
@@ -103,6 +101,12 @@ impl Command for ConnectCommand {
103101
Err(err) => return Err(err.into()),
104102
}
105103

104+
if bridge_config.use_mapper && bridge_config.bridge_location == BridgeLocation::BuiltIn {
105+
// If the bridge is built in, the mapper needs to be running with the new configuration
106+
// to be connected
107+
self.start_mapper();
108+
}
109+
106110
match self.check_connection(config) {
107111
Ok(DeviceStatus::AlreadyExists) => {
108112
println!("Connection check is successful.\n");
@@ -115,16 +119,10 @@ impl Command for ConnectCommand {
115119
}
116120
}
117121

118-
if bridge_config.use_mapper {
119-
println!("Checking if tedge-mapper is installed.\n");
120-
121-
if which("tedge-mapper").is_err() {
122-
println!("Warning: tedge-mapper is not installed.\n");
123-
} else {
124-
self.service_manager
125-
.as_ref()
126-
.start_and_enable_service(self.cloud.mapper_service(), std::io::stdout());
127-
}
122+
if bridge_config.use_mapper && bridge_config.bridge_location == BridgeLocation::Mosquitto {
123+
// If the bridge is in mosquitto, the mapper should only start once the cloud connection
124+
// is verified
125+
self.start_mapper();
128126
}
129127

130128
if let Cloud::C8y = self.cloud {
@@ -165,7 +163,20 @@ impl ConnectCommand {
165163
.join(TEDGE_BRIDGE_CONF_DIR_PATH)
166164
.join(br_config.config_file.clone());
167165

168-
Path::new(&bridge_conf_path).exists()
166+
br_config.bridge_location == BridgeLocation::BuiltIn
167+
|| Path::new(&bridge_conf_path).exists()
168+
}
169+
170+
fn start_mapper(&self) {
171+
println!("Checking if tedge-mapper is installed.\n");
172+
173+
if which("tedge-mapper").is_err() {
174+
println!("Warning: tedge-mapper is not installed.\n");
175+
} else {
176+
self.service_manager
177+
.as_ref()
178+
.start_and_enable_service(self.cloud.mapper_service(), std::io::stdout());
179+
}
169180
}
170181
}
171182

@@ -445,7 +456,9 @@ fn new_bridge(
445456
device_type: &str,
446457
) -> Result<(), ConnectError> {
447458
if bridge_config.bridge_location == BridgeLocation::BuiltIn {
459+
println!("Deleting mosquitto bridge configuration in favour of built-in bridge");
448460
clean_up(config_location, bridge_config)?;
461+
restart_mosquitto(bridge_config, service_manager, config_location)?;
449462
return Ok(());
450463
}
451464
println!("Checking if {} is available.\n", service_manager.name());
@@ -512,6 +525,27 @@ fn restart_mosquitto(
512525
config_location: &TEdgeConfigLocation,
513526
) -> Result<(), ConnectError> {
514527
println!("Restarting mosquitto service.\n");
528+
529+
if let Err(err) = service_manager.stop_service(SystemService::Mosquitto) {
530+
clean_up(config_location, bridge_config)?;
531+
return Err(err.into());
532+
}
533+
534+
let (user, group) = match bridge_config.bridge_location {
535+
BridgeLocation::BuiltIn => ("tedge", "tedge"),
536+
BridgeLocation::Mosquitto => (crate::BROKER_USER, crate::BROKER_GROUP),
537+
};
538+
// Ignore errors - This was the behavior with the now deprecated user manager.
539+
// - When `tedge cert create` is not run as root, a certificate is created but owned by the user running the command.
540+
// - A better approach could be to remove this `chown` and run the command as mosquitto.
541+
for path in [
542+
&bridge_config.bridge_certfile,
543+
&bridge_config.bridge_keyfile,
544+
] {
545+
// TODO maybe ignore errors here
546+
tedge_utils::file::change_user_and_group(dbg!(path.as_ref()), user, group).unwrap();
547+
}
548+
515549
if let Err(err) = service_manager.restart_service(SystemService::Mosquitto) {
516550
clean_up(config_location, bridge_config)?;
517551
return Err(err.into());

0 commit comments

Comments
 (0)