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
use std::io::{stdout, Write};

use color_eyre::owo_colors::OwoColorize;
use config::configs;
use cubist_config::{network::NetworkProfile, secret::SecretUrl, util::OrBug};
use cubist_util::proc::{kill, SIGTERM};
use error::{Error, Result};
use futures::future::TryJoinAll;
use progress::ServerPb;
use provider::Server;
use tokio::{select, try_join};

use crate::{
    historical::HistoricalMetadata,
    progress::{DownloadPb, ExtractPb, ServerMpb},
};

pub mod config;
pub mod error;
mod historical;
mod progress;
pub mod provider;
mod proxy;
pub mod resource;
mod tracing;

trait UrlExt {
    fn is_loopback(&self) -> Result<bool>;
}

impl UrlExt for SecretUrl {
    fn is_loopback(&self) -> Result<bool> {
        let url = self.expose_url()?;
        // The port number is irrelevant, since we're just doing a dns lookup.
        match url.socket_addrs(|| None).or_bug("No socket_addrs").get(0) {
            Some(it) => Ok(it.ip().is_loopback()),
            None => Err(Error::CannotResolveHostname(
                url.host_str().unwrap_or_default().to_string(),
            )),
        }
    }
}

pub fn to_uri(url: &url::Url) -> hyper::Uri {
    // Safety: valid URLs are valid URIs so this unwrap is safe
    url.as_str().parse().expect("URL must be URI")
}

pub async fn start(profile: &NetworkProfile) -> Result<Vec<Box<dyn Server>>> {
    // Validate the config
    let providers = configs(profile)
        .map(|c| c.provider().map(|p| (c, p)))
        .collect::<Result<Vec<_>>>()?;

    // Collect all missing dependencies
    let mut downloadables = vec![];
    for (_c, p) in providers.iter() {
        downloadables.append(&mut p.preflight()?);
    }

    // Download those dependencies (serially)
    for d in downloadables {
        if d.exists().await.is_ok() {
            continue;
        }

        println!("{} {}", "Installing".bold().green(), d.name());
        stdout().flush().unwrap();

        // download
        let pb = DownloadPb::new();
        let bytes = d.download(Some(&pb)).await?;
        pb.finished();

        // extract
        let pb = ExtractPb::new();
        d.extract(&bytes, Some(&pb)).await?;
        pb.finished();

        // verify checksums
        d.exists().await?;
    }

    println!("{}", "Launching chains".bold().green());

    // Start up the servers serialy (since they might need to find
    // open ports and avoid clashing with each other)
    let mut servers = vec![];
    let max_name_len = providers
        .iter()
        .map(|(c, _)| c.name().len())
        .max()
        .unwrap_or(0);
    let mut mpb = ServerMpb::new(max_name_len as u8);
    let mut hist = HistoricalMetadata::load();
    for (c, p) in providers {
        let pb = mpb.add(
            c.common().url.to_string(),
            c.name().to_owned(),
            hist.get_server_bootstrap_duration(p.name())
                .unwrap_or_else(|| p.bootstrap_eta()),
        );
        select! {
            _ = pb.auto_update() => {},
            server = p.start() => {
                let server = server?;
                // kill server process immediately if interrupted
                if let Some(pid) = server.pid() {
                    tokio::spawn(async move {
                        tokio::signal::ctrl_c().await.unwrap();
                        kill(pid, SIGTERM).await.unwrap();
                    });
                }
                servers.push((p, server, pb));
            },
        }
    }

    // Wait for all of them to become available
    servers
        .iter_mut()
        .map(|(_p, s, pb)| until_initialized(s, pb))
        .collect::<TryJoinAll<_>>()
        .await?;
    println!("{}", "All servers available".bold().green());

    // Update historical metadata and return servers
    let mut result = vec![];
    for (p, s, pb) in servers {
        result.push(s);
        hist.set_server_bootstrap_duration(p.name(), &pb.duration())
    }
    hist.save()?;
    Ok(result)
}

async fn until_initialized(s: &mut Box<dyn Server>, pb: &ServerPb) -> Result<()> {
    let init = async {
        s.available().await?;
        s.initialize().await
    };
    try_join!(pb.auto_update(), async {
        let res = init.await;
        pb.finished(res.is_ok());
        res
    })?;
    Ok(())
}