@@ -67,9 +67,7 @@ impl Command for ConnectCommand {
67
67
if self . is_test_connection {
68
68
// If the bridge is part of the mapper, the bridge config file won't exist
69
69
// 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) {
73
71
return match self . check_connection ( config) {
74
72
Ok ( DeviceStatus :: AlreadyExists ) => {
75
73
let cloud = bridge_config. cloud_name ;
@@ -103,6 +101,12 @@ impl Command for ConnectCommand {
103
101
Err ( err) => return Err ( err. into ( ) ) ,
104
102
}
105
103
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
+
106
110
match self . check_connection ( config) {
107
111
Ok ( DeviceStatus :: AlreadyExists ) => {
108
112
println ! ( "Connection check is successful.\n " ) ;
@@ -115,16 +119,10 @@ impl Command for ConnectCommand {
115
119
}
116
120
}
117
121
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 ( ) ;
128
126
}
129
127
130
128
if let Cloud :: C8y = self . cloud {
@@ -165,7 +163,20 @@ impl ConnectCommand {
165
163
. join ( TEDGE_BRIDGE_CONF_DIR_PATH )
166
164
. join ( br_config. config_file . clone ( ) ) ;
167
165
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
+ }
169
180
}
170
181
}
171
182
@@ -445,7 +456,9 @@ fn new_bridge(
445
456
device_type : & str ,
446
457
) -> Result < ( ) , ConnectError > {
447
458
if bridge_config. bridge_location == BridgeLocation :: BuiltIn {
459
+ println ! ( "Deleting mosquitto bridge configuration in favour of built-in bridge" ) ;
448
460
clean_up ( config_location, bridge_config) ?;
461
+ restart_mosquitto ( bridge_config, service_manager, config_location) ?;
449
462
return Ok ( ( ) ) ;
450
463
}
451
464
println ! ( "Checking if {} is available.\n " , service_manager. name( ) ) ;
@@ -512,6 +525,27 @@ fn restart_mosquitto(
512
525
config_location : & TEdgeConfigLocation ,
513
526
) -> Result < ( ) , ConnectError > {
514
527
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
+
515
549
if let Err ( err) = service_manager. restart_service ( SystemService :: Mosquitto ) {
516
550
clean_up ( config_location, bridge_config) ?;
517
551
return Err ( err. into ( ) ) ;
0 commit comments