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
// 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.

use super::is_running_as_root;
use crate::{
    add_services::{add_daemon, config::AddDaemonServiceOptions},
    config,
    helpers::{download_and_extract_release, get_bin_version},
    ServiceManager, VerbosityLevel,
};
use color_eyre::{eyre::eyre, Result};
use sn_releases::{ReleaseType, SafeReleaseRepoActions};
use sn_service_management::{
    control::{ServiceControl, ServiceController},
    DaemonService, NodeRegistry,
};
use std::{net::Ipv4Addr, path::PathBuf};

pub async fn add(
    address: Ipv4Addr,
    env_variables: Option<Vec<(String, String)>>,
    port: u16,
    src_path: Option<PathBuf>,
    url: Option<String>,
    version: Option<String>,
    verbosity: VerbosityLevel,
) -> Result<()> {
    if !is_running_as_root() {
        return Err(eyre!("The add command must run as the root user"));
    }

    if verbosity != VerbosityLevel::Minimal {
        println!("=================================================");
        println!("              Add Daemon Service                 ");
        println!("=================================================");
    }

    let service_user = "safe";
    let service_manager = ServiceController {};
    service_manager.create_service_user(service_user)?;

    let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?;
    let release_repo = <dyn SafeReleaseRepoActions>::default_config();

    let (daemon_src_bin_path, version) = if let Some(path) = src_path {
        let version = get_bin_version(&path)?;
        (path, version)
    } else {
        download_and_extract_release(
            ReleaseType::SafenodeManagerDaemon,
            url.clone(),
            version,
            &*release_repo,
        )
        .await?
    };

    // At the moment we don't have the option to provide a user for running the service. Since
    // `safenodemand` requires manipulation of services, the user running it must either be root or
    // have root access. For now we will just use the `root` user. The user option gets ignored on
    // Windows anyway, so there shouldn't be a cross-platform issue here.
    add_daemon(
        AddDaemonServiceOptions {
            address,
            env_variables,
            daemon_install_bin_path: config::get_daemon_install_path(),
            daemon_src_bin_path,
            port,
            user: "root".to_string(),
            version,
        },
        &mut node_registry,
        &ServiceController {},
    )?;
    Ok(())
}

pub async fn start(verbosity: VerbosityLevel) -> Result<()> {
    if !is_running_as_root() {
        return Err(eyre!("The start command must run as the root user"));
    }

    let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?;
    if let Some(daemon) = &mut node_registry.daemon {
        if verbosity != VerbosityLevel::Minimal {
            println!("=================================================");
            println!("             Start Daemon Service                ");
            println!("=================================================");
        }

        let service = DaemonService::new(daemon, Box::new(ServiceController {}));
        let mut service_manager =
            ServiceManager::new(service, Box::new(ServiceController {}), verbosity);
        service_manager.start().await?;

        println!(
            "Endpoint: {}",
            service_manager
                .service
                .service_data
                .endpoint
                .map_or("-".to_string(), |e| e.to_string())
        );

        node_registry.save()?;
        return Ok(());
    }

    Err(eyre!("The daemon service has not been added yet"))
}

pub async fn stop(verbosity: VerbosityLevel) -> Result<()> {
    if !is_running_as_root() {
        return Err(eyre!("The stop command must run as the root user"));
    }

    let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?;
    if let Some(daemon) = &mut node_registry.daemon {
        if verbosity != VerbosityLevel::Minimal {
            println!("=================================================");
            println!("             Stop Daemon Service                 ");
            println!("=================================================");
        }

        let service = DaemonService::new(daemon, Box::new(ServiceController {}));
        let mut service_manager =
            ServiceManager::new(service, Box::new(ServiceController {}), verbosity);
        service_manager.stop().await?;

        node_registry.save()?;

        return Ok(());
    }

    Err(eyre!("The daemon service has not been added yet"))
}