1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
// Copyright (C) 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

mod config;
#[cfg(test)]
mod tests;

pub use config::{AddServiceOptions, InstallNodeServiceCtxBuilder, UpgradeOptions};

use crate::{config::create_owned_dir, service::ServiceControl, VerbosityLevel};
use color_eyre::{
    eyre::{eyre, OptionExt},
    Help, Result,
};
use colored::Colorize;
use libp2p::multiaddr::Protocol;
use semver::Version;
use sn_node_rpc_client::{RpcActions, RpcClient};
use sn_protocol::node_registry::{Node, NodeRegistry, NodeStatus};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

pub enum UpgradeResult {
    Forced(String, String),
    NotRequired,
    Upgraded(String, String),
    Error(String),
}

// macOS seems to require this delay to be in seconds rather than milliseconds.
const RPC_START_UP_DELAY_MS: u64 = 3000;

/// Install safenode as a service.
///
/// This only defines the service; it does not start it.
///
/// There are several arguments that probably seem like they could be handled within the function,
/// but they enable more controlled unit testing.
pub async fn add(
    options: AddServiceOptions,
    node_registry: &mut NodeRegistry,
    service_control: &dyn ServiceControl,
    verbosity: VerbosityLevel,
) -> Result<()> {
    if options.genesis {
        if let Some(count) = options.count {
            if count > 1 {
                return Err(eyre!("A genesis node can only be added as a single node"));
            }
        }

        let genesis_node = node_registry.nodes.iter().find(|n| n.genesis);
        if genesis_node.is_some() {
            return Err(eyre!("A genesis node already exists"));
        }
    }

    if options.count.is_some() && options.node_port.is_some() {
        let count = options.count.unwrap();
        if count > 1 {
            return Err(eyre!(
                "Custom node port can only be used when adding a single service"
            ));
        }
    }

    let safenode_file_name = options
        .safenode_bin_path
        .file_name()
        .ok_or_else(|| eyre!("Could not get filename from the safenode download path"))?
        .to_string_lossy()
        .to_string();

    //  store the bootstrap peers and the provided env variable.
    {
        let mut should_save = false;
        let new_bootstrap_peers: Vec<_> = options
            .bootstrap_peers
            .iter()
            .filter(|peer| !node_registry.bootstrap_peers.contains(peer))
            .collect();
        if !new_bootstrap_peers.is_empty() {
            node_registry
                .bootstrap_peers
                .extend(new_bootstrap_peers.into_iter().cloned());
            should_save = true;
        }

        if options.env_variables.is_some() {
            node_registry.environment_variables = options.env_variables.clone();
            should_save = true;
        }

        if should_save {
            node_registry.save()?;
        }
    }

    let mut added_service_data = vec![];
    let mut failed_service_data = vec![];

    let current_node_count = node_registry.nodes.len() as u16;
    let target_node_count = current_node_count + options.count.unwrap_or(1);

    let mut node_number = current_node_count + 1;
    while node_number <= target_node_count {
        let rpc_free_port = service_control.get_available_port()?;
        let rpc_socket_addr = if let Some(addr) = options.rpc_address {
            SocketAddr::new(IpAddr::V4(addr), rpc_free_port)
        } else {
            SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), rpc_free_port)
        };

        let service_name = format!("safenode{node_number}");
        let service_data_dir_path = options.service_data_dir_path.join(service_name.clone());
        let service_safenode_path = service_data_dir_path.join(safenode_file_name.clone());
        let service_log_dir_path = options.service_log_dir_path.join(service_name.clone());

        create_owned_dir(service_data_dir_path.clone(), &options.user)?;
        create_owned_dir(service_log_dir_path.clone(), &options.user)?;

        std::fs::copy(
            options.safenode_bin_path.clone(),
            service_safenode_path.clone(),
        )?;
        let install_ctx = InstallNodeServiceCtxBuilder {
            bootstrap_peers: options.bootstrap_peers.clone(),
            data_dir_path: service_data_dir_path.clone(),
            env_variables: options.env_variables.clone(),
            genesis: options.genesis,
            local: options.local,
            log_dir_path: service_log_dir_path.clone(),
            name: service_name.clone(),
            node_port: options.node_port,
            rpc_socket_addr,
            safenode_path: service_safenode_path.clone(),
            service_user: options.user.clone(),
        }
        .build()?;

        match service_control.install(install_ctx) {
            Ok(()) => {
                added_service_data.push((
                    service_name.clone(),
                    service_safenode_path.to_string_lossy().into_owned(),
                    service_data_dir_path.to_string_lossy().into_owned(),
                    service_log_dir_path.to_string_lossy().into_owned(),
                    rpc_socket_addr,
                ));

                node_registry.nodes.push(Node {
                    genesis: options.genesis,
                    local: options.local,
                    service_name,
                    user: options.user.clone(),
                    number: node_number,
                    rpc_socket_addr,
                    version: options.version.clone(),
                    status: NodeStatus::Added,
                    listen_addr: None,
                    pid: None,
                    peer_id: None,
                    log_dir_path: service_log_dir_path.clone(),
                    data_dir_path: service_data_dir_path.clone(),
                    safenode_path: service_safenode_path,
                    connected_peers: None,
                });
                // We save the node registry for each service because it's possible any number of
                // services could fail to be added.
                node_registry.save()?;
            }
            Err(e) => {
                failed_service_data.push((service_name.clone(), e.to_string()));
            }
        }

        node_number += 1;
    }

    std::fs::remove_file(options.safenode_bin_path)?;

    if !added_service_data.is_empty() {
        println!("Services Added:");
        for install in added_service_data.iter() {
            println!(" {} {}", "✓".green(), install.0);
            if verbosity != VerbosityLevel::Minimal {
                println!("    - Safenode path: {}", install.1);
                println!("    - Data path: {}", install.2);
                println!("    - Log path: {}", install.3);
                println!("    - RPC port: {}", install.4);
            }
        }
        println!("[!] Note: newly added services have not been started");
    }

    if !failed_service_data.is_empty() {
        println!("Failed to add {} service(s):", failed_service_data.len());
        for failed in failed_service_data.iter() {
            println!("{} {}: {}", "✕".red(), failed.0, failed.1);
        }
        return Err(eyre!("Failed to add one or more services")
            .suggestion("However, any services that were successfully added will be usable."));
    }

    Ok(())
}

pub async fn start(
    node: &mut Node,
    service_control: &dyn ServiceControl,
    rpc_client: &dyn RpcActions,
    verbosity: VerbosityLevel,
) -> Result<()> {
    if let NodeStatus::Running = node.status {
        // The last time we checked the service was running, but it doesn't mean it's actually
        // running at this point in time. If it is running, we don't need to do anything. If it
        // stopped because of a fault, we will drop to the code below and attempt to start it
        // again.
        if service_control.is_service_process_running(node.pid.ok_or_eyre("The PID was not set")?) {
            println!("The {} service is already running", node.service_name);
            return Ok(());
        }
    }

    // At this point the service either hasn't been started for the first time or it has been
    // stopped. If it was stopped, it was either intentional or because it crashed.
    if verbosity != VerbosityLevel::Minimal {
        println!("Attempting to start {}...", node.service_name);
    }
    service_control.start(&node.service_name)?;

    // Give the node a little bit of time to start before initiating the node info query.
    service_control.wait(RPC_START_UP_DELAY_MS);
    let node_info = rpc_client.node_info().await?;
    let network_info = rpc_client.network_info().await?;
    node.listen_addr = Some(
        network_info
            .listeners
            .into_iter()
            .map(|addr| addr.with(Protocol::P2p(node_info.peer_id)))
            .collect(),
    );
    node.pid = Some(node_info.pid);
    node.peer_id = Some(node_info.peer_id);
    node.status = NodeStatus::Running;

    println!("{} Started {} service", "✓".green(), node.service_name);
    if verbosity != VerbosityLevel::Minimal {
        println!("  - Peer ID: {}", node_info.peer_id);
        println!("  - Logs: {}", node_info.log_path.to_string_lossy());
    }

    Ok(())
}

pub async fn stop(node: &mut Node, service_control: &dyn ServiceControl) -> Result<()> {
    match node.status {
        NodeStatus::Added => {
            println!(
                "Service {} has not been started since it was installed",
                node.service_name
            );
            Ok(())
        }
        NodeStatus::Removed => Err(eyre!("Service {} has been removed", node.service_name)),
        NodeStatus::Running => {
            let pid = node.pid.ok_or_eyre("The PID was not set")?;
            if service_control.is_service_process_running(pid) {
                println!("Attempting to stop {}...", node.service_name);
                service_control.stop(&node.service_name)?;
                println!(
                    "{} Service {} with PID {} was stopped",
                    "✓".green(),
                    node.service_name,
                    pid
                );
            } else {
                println!(
                    "{} Service {} was already stopped",
                    "✓".green(),
                    node.service_name
                );
            }
            node.pid = None;
            node.status = NodeStatus::Stopped;
            node.connected_peers = None;
            Ok(())
        }
        NodeStatus::Stopped => {
            println!(
                "{} Service {} was already stopped",
                "✓".green(),
                node.service_name
            );
            Ok(())
        }
    }
}

pub async fn status(
    node_registry: &mut NodeRegistry,
    service_control: &dyn ServiceControl,
    detailed_view: bool,
    output_json: bool,
    fail: bool,
) -> Result<()> {
    // Again confirm that services which are marked running are still actually running.
    // If they aren't we'll mark them as stopped.
    for node in &mut node_registry.nodes {
        let rpc_client = RpcClient::from_socket_addr(node.rpc_socket_addr);
        if let NodeStatus::Running = node.status {
            if let Some(pid) = node.pid {
                // First we can try the PID we have now. If there is still a process running with
                // that PID, we know the node is still running.
                if service_control.is_service_process_running(pid) {
                    match rpc_client.network_info().await {
                        Ok(info) => {
                            node.connected_peers = Some(info.connected_peers);
                        }
                        Err(_) => {
                            node.connected_peers = None;
                        }
                    }
                } else {
                    // The process with the PID we had has died at some point. However, if the
                    // service has been configured to restart on failures, it's possible that a new
                    // process has been launched and hence we would have a new PID. We can use the
                    // RPC service to try and retrieve it.
                    match rpc_client.node_info().await {
                        Ok(info) => {
                            node.pid = Some(info.pid);
                        }
                        Err(_) => {
                            // Finally, if there was an error communicating with the RPC client, we
                            // can assume that this node is actually stopped.
                            node.status = NodeStatus::Stopped;
                            node.pid = None;
                        }
                    }
                    match rpc_client.network_info().await {
                        Ok(info) => {
                            node.connected_peers = Some(info.connected_peers);
                        }
                        Err(_) => {
                            node.connected_peers = None;
                        }
                    }
                }
            }
        }
    }

    if output_json {
        let json = serde_json::to_string(&node_registry.nodes)?;
        println!("{json}");
    } else if detailed_view {
        for node in &node_registry.nodes {
            let service_status = format!("{} - {}", node.service_name, format_status(&node.status));
            let banner = "=".repeat(service_status.len());
            println!("{}", banner);
            println!("{service_status}");
            println!("{}", banner);
            println!("Version: {}", node.version);
            println!(
                "Peer ID: {}",
                node.peer_id.map_or("-".to_string(), |p| p.to_string())
            );
            println!("RPC Socket: {}", node.rpc_socket_addr);
            println!("Listen Addresses: {:?}", node.listen_addr);
            println!(
                "PID: {}",
                node.pid.map_or("-".to_string(), |p| p.to_string())
            );
            println!("Data path: {}", node.data_dir_path.to_string_lossy());
            println!("Log path: {}", node.log_dir_path.to_string_lossy());
            println!("Bin path: {}", node.safenode_path.to_string_lossy());
            println!(
                "Connected peers: {}",
                node.connected_peers
                    .as_ref()
                    .map_or("-".to_string(), |p| p.len().to_string())
            );
            println!();
        }
    } else {
        println!(
            "{:<18} {:<52} {:<7} {:>15}",
            "Service Name", "Peer ID", "Status", "Connected Peers"
        );
        let nodes = node_registry
            .nodes
            .iter()
            .filter(|n| n.status != NodeStatus::Removed)
            .collect::<Vec<&Node>>();
        for node in nodes {
            let peer_id = node.peer_id.map_or("-".to_string(), |p| p.to_string());
            let connected_peers = node
                .connected_peers
                .clone()
                .map_or("-".to_string(), |p| p.len().to_string());
            println!(
                "{:<18} {:<52} {:<7} {:>15}",
                node.service_name,
                peer_id,
                format_status(&node.status),
                connected_peers
            );
        }
    }

    if fail
        && node_registry
            .nodes
            .iter()
            .any(|n| n.status != NodeStatus::Running)
    {
        return Err(eyre!("One or more nodes are not in a running state"));
    }

    Ok(())
}

pub async fn remove(
    node: &mut Node,
    service_control: &dyn ServiceControl,
    keep_directories: bool,
) -> Result<()> {
    if let NodeStatus::Running = node.status {
        if service_control.is_service_process_running(
            node.pid
                .ok_or_else(|| eyre!("The PID should be set before the node is removed"))?,
        ) {
            return Err(eyre!("A running node cannot be removed")
                .suggestion("Stop the node first then try again"));
        } else {
            // If the node wasn't actually running, we should give the user an opportunity to
            // check why it may have failed before removing everything.
            node.pid = None;
            node.status = NodeStatus::Stopped;
            return Err(
                eyre!("This node was marked as running but it had actually stopped")
                    .suggestion("You may want to check the logs for errors before removing it")
                    .suggestion("To remove the node, run the command again."),
            );
        }
    }

    service_control.uninstall(&node.service_name)?;

    if !keep_directories {
        std::fs::remove_dir_all(&node.data_dir_path)?;
        std::fs::remove_dir_all(&node.log_dir_path)?;
    }

    node.status = NodeStatus::Removed;

    println!("{} Service {} was removed", "✓".green(), node.service_name);

    Ok(())
}

pub async fn upgrade(
    options: UpgradeOptions,
    node: &mut Node,
    service_control: &dyn ServiceControl,
    rpc_client: &dyn RpcActions,
) -> Result<UpgradeResult> {
    let current_version = Version::parse(&node.version)?;
    if !options.force
        && (current_version == options.target_version || options.target_version < current_version)
    {
        return Ok(UpgradeResult::NotRequired);
    }

    stop(node, service_control).await?;
    std::fs::copy(options.target_safenode_path, &node.safenode_path)?;

    // Install the service again to make sure we re-use the same node port.
    // Windows requires that the service be uninstalled first.
    service_control.uninstall(&node.service_name.clone())?;
    let install_ctx = InstallNodeServiceCtxBuilder {
        local: node.local,
        data_dir_path: node.data_dir_path.clone(),
        genesis: node.genesis,
        name: node.service_name.clone(),
        node_port: node.get_safenode_port(),
        bootstrap_peers: options.bootstrap_peers,
        rpc_socket_addr: node.rpc_socket_addr,
        log_dir_path: node.log_dir_path.clone(),
        safenode_path: node.safenode_path.clone(),
        service_user: node.user.clone(),
        env_variables: options.env_variables.clone(),
    }
    .build()?;
    service_control.install(install_ctx)?;

    if options.start_node {
        start(node, service_control, rpc_client, VerbosityLevel::Normal).await?;
    }
    node.version = options.target_version.to_string();

    match options.force {
        true => Ok(UpgradeResult::Forced(
            current_version.to_string(),
            options.target_version.to_string(),
        )),
        false => Ok(UpgradeResult::Upgraded(
            current_version.to_string(),
            options.target_version.to_string(),
        )),
    }
}

fn format_status(status: &NodeStatus) -> String {
    match status {
        NodeStatus::Running => "RUNNING".green().to_string(),
        NodeStatus::Stopped => "STOPPED".red().to_string(),
        NodeStatus::Added => "ADDED".yellow().to_string(),
        NodeStatus::Removed => "REMOVED".red().to_string(),
    }
}