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

use super::PrefabResult;
use super::deploy::{Deploy, Undeploy};

mod address;
pub use address::Address;


#[derive(Clone, Debug)]
pub struct Host {
    address: Address,
}


impl Host {
    pub fn new(address: Address) -> Self {
        Self {
            address: address,
        }
    }
    
    
    pub async fn deploy(&self, deployment: &impl Deploy) -> PrefabResult {
        
        self.print_header(deployment.description());
        
        let result = deployment.deploy(self).await;
        
        result
    }
    
    
    pub async fn undeploy(&self, undeployment: &impl Undeploy) -> PrefabResult {
        
        self.print_header(undeployment.description());
        
        let result = undeployment.undeploy(self).await;
        
        result
    }
    
    
    pub fn is_localhost(&self) -> bool {
        self.address.is_loopback()
    }
    
    
    fn print_header(&self, description: String) {
        println!();
        println!("==== [{}] {} ====", self.address, description);
    }
    
    
    pub(super) async fn session(&self) -> openssh::Session {
        openssh::Session::connect(&self.address.to_string(), openssh::KnownHosts::Strict).await.expect("Failed to connect.")
    }
}


impl std::fmt::Display for Host {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}", self.address)
    }
}