lunes/node/
exec.rs

1use crate::node::NodeInstall;
2use std::process::Command;
3use trauma::Error;
4
5// #[cfg(target_os = "linux")]
6pub fn up() {
7    std::process::Command::new("clear").status().unwrap();
8    println!("🚀 Up Lunes Node");
9
10    match Command::new("systemctl")
11        .arg("start")
12        .arg("lunesnode.service")
13        .output()
14    {
15        Err(e) => panic!("Error starting Lunes Node, {:?}", e),
16        Ok(x) => x,
17    };
18    println!("✅ Done!");
19}
20
21// #[cfg(target_os = "linux")]
22pub fn down() {
23    std::process::Command::new("clear").status().unwrap();
24    println!("🚨 Down Lunes Node");
25
26    match Command::new("systemctl")
27        .arg("stop")
28        .arg("lunesnode.service")
29        .output()
30    {
31        Err(e) => panic!("Error stopping Lunes Node, {:?}", e),
32        Ok(x) => x,
33    };
34    println!("✅ Done!");
35}
36
37// #[cfg(target_os = "linux")]
38pub fn logs() {
39    std::process::Command::new("clear").status().unwrap();
40    println!("📊 Logs Lunes Node");
41
42    match Command::new("journalctl")
43        .arg("-fu")
44        .arg("lunesnode")
45        .output()
46    {
47        Err(e) => panic!("Error show Lunes Node Logs, {:?}", e),
48        Ok(x) => x,
49    };
50    println!("✅ Done!");
51}
52
53// #[cfg(target_os = "linux")]
54pub fn status() {
55    std::process::Command::new("clear").status().unwrap();
56    println!("🌡  Status Lunes Node");
57
58    match Command::new("systemctl")
59        .arg("status")
60        .arg("lunesnode.service")
61        .output()
62    {
63        Err(e) => panic!("Error read status of Lunes Node, {:?}", e),
64        Ok(x) => x,
65    };
66    println!("✅ Done!");
67}
68
69// #[cfg(target_os = "linux")]
70pub fn restart() {
71    std::process::Command::new("clear").status().unwrap();
72    std::process::Command::new("clear").status().unwrap();
73    println!("🔄 Restart Lunes Node");
74
75    match Command::new("systemctl")
76        .arg("restart")
77        .arg("lunesnode.service")
78        .output()
79    {
80        Err(e) => panic!("Error restarting Lunes Node, {:?}", e),
81        Ok(x) => x,
82    };
83    println!("✅ Done!");
84}
85
86pub fn version() {
87    println!("Comming Soon")
88}
89
90pub fn config() {
91    println!("Comming Soon")
92}
93
94async fn downloading(version: String) -> Result<(), Error> {
95    use std::path::PathBuf;
96    use trauma::{download::Download, downloader::DownloaderBuilder};
97
98    let url = format!(
99        "https://github.com/lunes-platform/lunes-node/releases/download/{}/lunesnode-{}.jar",
100        version, version
101    );
102    let downloads = vec![Download::try_from(url.as_str()).unwrap()];
103    let downloader = DownloaderBuilder::new()
104        .directory(PathBuf::from("."))
105        .build();
106    downloader.download(&downloads).await;
107    Ok(())
108}
109
110fn move_files_to_opt(args: NodeInstall) {
111    use super::utils::{mount_hocon, mount_service};
112    use std::fs;
113
114    mount_hocon(args.clone());
115    mount_service();
116
117    fs::create_dir_all("/opt/lunesnode").expect("failed in create dir /opt/lunesnode");
118    fs::copy("lunesnode-0.0.8.jar", "/opt/lunesnode/lunesnode-0.0.8.jar")
119        .expect("failed in move .jar");
120    fs::remove_file("lunesnode-0.0.8.jar").expect("failed in remove tmp .jar");
121}
122
123// #[cfg(target_os = "linux")]
124pub async fn install(args: NodeInstall) {
125    match args.version {
126        Some(ref v) => {
127            std::process::Command::new("clear").status().unwrap();
128            println!("⬇️ Downloading Lunes Node {}", v);
129            downloading(v.to_string()).await;
130            move_files_to_opt(args.clone());
131            println!("✅ Done!");
132            println!("🚀 Running `lunes node up` to start");
133        }
134        None => {
135            std::process::Command::new("clear").status().unwrap();
136            println!("⬇️  Downloading Lunes Node 0.0.8");
137            downloading(String::from("0.0.8")).await.expect("");
138            move_files_to_opt(args.clone());
139            println!("✅ Done!");
140            println!("🚀 Running `lunes node up` to start");
141        }
142    };
143}