aboutsummaryrefslogtreecommitdiff
path: root/src/utils/deploy.rs
blob: 59217dfe7944c02fdd66b16d3d129d1f4c72bd1b (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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// SPDX-FileCopyrightText: 2020 Serokell <https://serokell.io/>
//
// SPDX-License-Identifier: MPL-2.0

use std::borrow::Cow;
use tokio::process::Command;

fn build_activate_command(
    activate_path_str: String,
    sudo: &Option<String>,
    profile_path: &str,
    closure: &str,
    bootstrap_cmd: &Option<String>,
    auto_rollback: bool,
    temp_path: &Cow<str>,
    confirm_timeout: u16,
    magic_rollback: bool,
) -> String {
    let mut self_activate_command = format!(
        "{} '{}' '{}' --temp-path {} --confirm-timeout {}",
        activate_path_str, profile_path, closure, temp_path, confirm_timeout
    );

    if magic_rollback {
        self_activate_command = format!("{} --magic-rollback", self_activate_command);
    }

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

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

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

    self_activate_command
}

#[test]
fn test_activation_command_builder() {
    let activate_path_str = "/blah/bin/activate".to_string();
    let sudo = Some("sudo -u test".to_string());
    let profile_path = "/blah/profiles/test";
    let closure = "/blah/etc";
    let bootstrap_cmd = None;
    let auto_rollback = true;
    let temp_path = &"/tmp/deploy-rs".into();
    let confirm_timeout = 30;
    let magic_rollback = true;

    assert_eq!(
        build_activate_command(
            activate_path_str,
            &sudo,
            profile_path,
            closure,
            &bootstrap_cmd,
            auto_rollback,
            temp_path,
            confirm_timeout,
            magic_rollback
        ),
        "sudo -u test /blah/bin/activate '/blah/profiles/test' '/blah/etc' --temp-path /tmp/deploy-rs --confirm-timeout 30 --magic-rollback --auto-rollback"
            .to_string(),
    );
}

pub async fn deploy_profile(
    deploy_data: &super::DeployData<'_>,
    deploy_defs: &super::DeployDefs<'_>,
) -> Result<(), Box<dyn std::error::Error>> {
    info!(
        "Activating profile `{}` for node `{}`",
        deploy_data.profile_name, deploy_data.node_name
    );

    let activate_path_str = super::deploy_path_to_activate_path_str(&deploy_defs.current_exe)?;

    let temp_path: Cow<str> = match &deploy_data.merged_settings.temp_path {
        Some(x) => x.into(),
        None => "/tmp/deploy-rs".into(),
    };

    let confirm_timeout = deploy_data.merged_settings.confirm_timeout.unwrap_or(30);

    let magic_rollback = deploy_data.merged_settings.magic_rollback.unwrap_or(false);

    let auto_rollback = deploy_data.merged_settings.auto_rollback.unwrap_or(true);

    let self_activate_command = build_activate_command(
        activate_path_str,
        &deploy_defs.sudo,
        &deploy_defs.profile_path,
        &deploy_data.profile.profile_settings.path,
        &deploy_data.profile.profile_settings.bootstrap,
        auto_rollback,
        &temp_path,
        confirm_timeout,
        magic_rollback,
    );

    debug!("Constructed activation command: {}", self_activate_command);

    let hostname = match deploy_data.cmd_overrides.hostname {
        Some(ref x) => x,
        None => &deploy_data.node.node_settings.hostname,
    };

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

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

    let ssh_exit_status = ssh_command.arg(self_activate_command).status().await?;

    if !ssh_exit_status.success() {
        good_panic!("Activation over SSH failed");
    }

    info!("Success activating!");

    if magic_rollback {
        info!("Attempting to confirm activation");

        let mut c = Command::new("ssh");
        let mut ssh_confirm_command = c.arg(format!("ssh://{}@{}", deploy_defs.ssh_user, hostname));

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

        let lock_hash = &deploy_data.profile.profile_settings.path[11 /* /nix/store/ */ ..];
        let lock_path = format!("{}/activating-{}", temp_path, lock_hash);

        let mut confirm_command = format!("rm {}", lock_path);
        if let Some(sudo_cmd) = &deploy_defs.sudo {
            confirm_command = format!("{} {}", sudo_cmd, confirm_command);
        }

        debug!(
            "Attempting to run command to confirm deployment: {}",
            confirm_command
        );

        let ssh_exit_status = ssh_confirm_command.arg(confirm_command).status().await?;

        if !ssh_exit_status.success() {
            good_panic!(
                "Failed to confirm deployment, the node will roll back in <{} seconds",
                confirm_timeout
            );
        }

        info!("Deployment confirmed.");
    }

    Ok(())
}