support_kit/deployments/
deployment_command.rs

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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use clap::Parser;

use crate::SupportControl;

use super::DeploymentContext;

#[derive(Clone, Copy, Parser, Debug, PartialEq)]
#[clap(rename_all = "kebab-case")]
pub enum DeploymentCommand {
    Install,
    Setup,
    Build,
    Start,
    List,
    Login,
    Pull,
    Push,
    Restart,
}

impl DeploymentCommand {
    #[tracing::instrument(skip(self, controller), level = "trace")]
    pub async fn exec_remote(&self, controller: &SupportControl) -> crate::Result<()> {
        Ok(exec_remote_container_op(&controller, *self).await?)
    }

    #[tracing::instrument(skip(self, controller), level = "trace")]
    pub async fn exec_local(&self, controller: &SupportControl) -> crate::Result<()> {
        Ok(exec_local_container_op(&controller, self).await?)
    }
}

#[tracing::instrument(skip(controller, command), level = "trace")]
pub async fn exec_local_container_op(
    controller: &SupportControl,
    command: &DeploymentCommand,
) -> crate::Result<()> {
    let deployment_context = DeploymentContext::from(controller);
    tracing::trace!(command = ?command, "executing local container operation");
    match command {
        DeploymentCommand::Build => {
            for image in deployment_context.images {
                image.build()?.run()?;
            }
        }
        DeploymentCommand::Install => {
            deployment_context.get_install_script()?.run()?;
            deployment_context.install_docker()?.run()?;
        }
        DeploymentCommand::List => deployment_context.list_containers()?.run()?,
        DeploymentCommand::Login => deployment_context.login()?.run()?,
        DeploymentCommand::Pull => {
            for image in deployment_context.images {
                image.pull()?.run()?;
            }
        }
        DeploymentCommand::Push => {
            for image in deployment_context.images {
                image.push()?.run()?;
            }
        }
        DeploymentCommand::Restart => {
            let path = deployment_context.emit_config()?;
            for image in deployment_context.images {
                image.pull()?.run()?;
                image.kill_all()?.run()?;
                image.start(path.clone())?.run()?;
            }
        }
        DeploymentCommand::Setup => {
            deployment_context.setup_cert_volume()?.run()?;

            for image in deployment_context.images {
                image.setup_config_volume()?.run()?;
            }
        }
        DeploymentCommand::Start => {
            let path = deployment_context.emit_config()?;
            for image in deployment_context.images {
                image.start(path.clone())?.run()?;
            }
        }
    }

    Ok(())
}

#[tracing::instrument(skip(controller, command), level = "trace")]
pub async fn exec_remote_container_op(
    controller: &SupportControl,
    command: DeploymentCommand,
) -> crate::Result<()> {
    let deployment_context = DeploymentContext::from(controller);
    tracing::trace!(command = ?command, "executing remote container operation");
    match command {
        DeploymentCommand::Build => {
            for image in deployment_context.images {
                controller
                    .on_remotes()
                    .commands(image.build()?)
                    .call()
                    .await?;
            }
        }
        DeploymentCommand::Install => {
            controller
                .on_remotes()
                .commands(bon::vec![
                    deployment_context.get_install_script()?,
                    deployment_context.install_docker()?
                ])
                .call()
                .await?;
        }
        DeploymentCommand::List => {
            controller
                .on_remotes()
                .commands(deployment_context.list_containers()?)
                .call()
                .await?;
        }
        DeploymentCommand::Login => {
            controller
                .on_remotes()
                .commands(deployment_context.login()?)
                .call()
                .await?;
        }
        DeploymentCommand::Pull => {
            for image in deployment_context.images {
                controller
                    .on_remotes()
                    .commands(image.pull()?)
                    .call()
                    .await?;
            }
        }
        DeploymentCommand::Push => {}
        DeploymentCommand::Restart => {
            let path = deployment_context.emit_config()?;
            let from_path = path.to_string_lossy();
            let to_path = format!("{name}.json", name = controller.config.name());

            for host in deployment_context.hosts {
                host.send_file(&from_path, &to_path)?.run()?;
            }

            for image in deployment_context.images {
                controller
                    .on_remotes()
                    .commands(bon::vec![
                        image.pull()?,
                        image.kill_all()?,
                        image.start(to_path.clone())?
                    ])
                    .call()
                    .await?;
            }
        }
        DeploymentCommand::Setup => {
            let certs_volume = deployment_context.setup_cert_volume()?;

            for image in deployment_context.images {
                controller
                    .on_remotes()
                    .commands(bon::vec![
                        certs_volume.clone(),
                        image.setup_config_volume()?
                    ])
                    .call()
                    .await?;
            }
        }
        DeploymentCommand::Start => {
            let path = deployment_context.emit_config()?;

            let from_path = path.to_string_lossy();
            let to_path = format!("{name}.container.json", name = controller.config.name());

            for host in deployment_context.hosts {
                host.send_file(&from_path, &to_path)?.run()?;
            }

            for image_controller in deployment_context.images {
                controller
                    .on_remotes()
                    .commands(image_controller.start(to_path.clone())?)
                    .call()
                    .await?;
            }
        }
    }

    Ok(())
}