ant_node_manager/cmd/
faucet.rs

1// Copyright (C) 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9use crate::{
10    config::{self, is_running_as_root},
11    print_banner, ServiceManager, VerbosityLevel,
12};
13use ant_bootstrap::InitialPeersConfig;
14use ant_service_management::{control::ServiceController, FaucetService, NodeRegistry};
15use color_eyre::{eyre::eyre, Result};
16use std::path::PathBuf;
17
18pub async fn add(
19    _env_variables: Option<Vec<(String, String)>>,
20    _log_dir_path: Option<PathBuf>,
21    _peers_args: InitialPeersConfig,
22    _src_path: Option<PathBuf>,
23    _url: Option<String>,
24    _version: Option<String>,
25    _verbosity: VerbosityLevel,
26) -> Result<()> {
27    // TODO: The whole subcommand for the auditor should be removed when we have some time.
28    panic!("The faucet service is no longer supported");
29}
30
31pub async fn start(verbosity: VerbosityLevel) -> Result<()> {
32    if !is_running_as_root() {
33        error!("The faucet start command must run as the root user");
34        return Err(eyre!("The start command must run as the root user"));
35    }
36
37    let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?;
38    if let Some(faucet) = &mut node_registry.faucet {
39        if verbosity != VerbosityLevel::Minimal {
40            print_banner("Start Faucet Service");
41        }
42        info!("Starting faucet service");
43
44        let service = FaucetService::new(faucet, Box::new(ServiceController {}));
45        let mut service_manager = ServiceManager::new(
46            service,
47            Box::new(ServiceController {}),
48            VerbosityLevel::Normal,
49        );
50        service_manager.start().await?;
51
52        node_registry.save()?;
53        return Ok(());
54    }
55
56    error!("The faucet service has not been added yet");
57    Err(eyre!("The faucet service has not been added yet"))
58}
59
60pub async fn stop(verbosity: VerbosityLevel) -> Result<()> {
61    if !is_running_as_root() {
62        error!("The faucet stop command must run as the root user");
63        return Err(eyre!("The stop command must run as the root user"));
64    }
65
66    let mut node_registry = NodeRegistry::load(&config::get_node_registry_path()?)?;
67    if let Some(faucet) = &mut node_registry.faucet {
68        if verbosity != VerbosityLevel::Minimal {
69            print_banner("Stop Faucet Service");
70        }
71        info!("Stopping faucet service");
72
73        let service = FaucetService::new(faucet, Box::new(ServiceController {}));
74        let mut service_manager =
75            ServiceManager::new(service, Box::new(ServiceController {}), verbosity);
76        service_manager.stop().await?;
77
78        node_registry.save()?;
79
80        return Ok(());
81    }
82
83    error!("The faucet service has not been added yet");
84    Err(eyre!("The faucet service has not been added yet"))
85}
86
87pub async fn upgrade(
88    _do_not_start: bool,
89    _force: bool,
90    _provided_env_variables: Option<Vec<(String, String)>>,
91    _url: Option<String>,
92    _version: Option<String>,
93    _verbosity: VerbosityLevel,
94) -> Result<()> {
95    // TODO: The whole subcommand for the auditor should be removed when we have some time.
96    panic!("The faucet service is no longer supported");
97}