aboutsummaryrefslogtreecommitdiff
path: root/src/utils/deploy.rs
blob: 42bd0b4b734e781a453fd3c35e0fddfba795bac2 (plain)
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
use super::data;

use tokio::process::Command;

pub async fn deploy_profile(
    profile: &data::Profile,
    profile_name: &str,
    node: &data::Node,
    node_name: &str,
    merged_settings: &data::GenericSettings,
    deploy_data: &super::DeployData<'_>,
    auto_rollback: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    info!(
        "Activating profile `{}` for node `{}`",
        profile_name, node_name
    );

    let mut self_activate_command = format!(
        "{} '{}' '{}'",
        deploy_data
            .current_exe
            .as_path()
            .parent()
            .unwrap()
            .to_str()
            .unwrap()
            .to_owned()
            + "/activate",
        deploy_data.profile_path,
        profile.profile_settings.path,
    );

    if let Some(sudo_cmd) = &deploy_data.sudo {
        self_activate_command = format!("{} {}", sudo_cmd, self_activate_command);
    }

    if let Some(ref bootstrap_cmd) = profile.profile_settings.bootstrap {
        self_activate_command = format!(
            "{} --bootstrap-cmd '{}'",
            self_activate_command, bootstrap_cmd
        );
    }

    if let Some(ref activate_cmd) = profile.profile_settings.activate {
        self_activate_command = format!(
            "{} --activate-cmd '{}'",
            self_activate_command, activate_cmd
        );
    }

    if auto_rollback {
        self_activate_command = format!("{} --auto-rollback", self_activate_command);
    }

    let mut c = Command::new("ssh");
    let mut ssh_command = c.arg(format!(
        "ssh://{}@{}",
        deploy_data.ssh_user, node.node_settings.hostname
    ));

    for ssh_opt in &merged_settings.ssh_opts {
        ssh_command = ssh_command.arg(ssh_opt);
    }

    ssh_command.arg(self_activate_command).spawn()?.await?;

    Ok(())
}