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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::collections::HashMap;
use std::io::{BufRead, BufReader};
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::process::{Child, ChildStdout, Command};
use std::sync::{Arc, RwLock};

use anyhow::{bail, Context, Result};
use async_process::Stdio;
use serde::{Deserialize, Serialize};
use tempfile::TempDir;

use super::progress::ProgressTracker;

pub static TERRAFORM_ALPHABET: [char; 16] = [
    '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', 'a', 'b', 'c', 'd', 'e', 'f',
];

/// Keeps track of resources which may need to be cleaned up.
#[derive(Default)]
pub struct TerraformPool {
    counter: u32,
    active_applies: HashMap<u32, Arc<tokio::sync::RwLock<TerraformApply>>>,
}

impl TerraformPool {
    fn create_apply(
        &mut self,
        deployment_folder: TempDir,
    ) -> Result<(u32, Arc<tokio::sync::RwLock<TerraformApply>>)> {
        let next_counter = self.counter;
        self.counter += 1;

        let mut apply_command = Command::new("terraform");

        apply_command
            .current_dir(deployment_folder.path())
            .arg("apply")
            .arg("-auto-approve")
            .arg("-no-color")
            .arg("-parallelism=128");

        #[cfg(unix)]
        {
            apply_command.process_group(0);
        }

        let spawned_child = apply_command
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn `terraform`. Is it installed?")?;

        let spawned_id = spawned_child.id();

        let deployment = Arc::new(tokio::sync::RwLock::new(TerraformApply {
            child: Some((spawned_id, Arc::new(RwLock::new(spawned_child)))),
            deployment_folder: Some(deployment_folder),
        }));

        self.active_applies.insert(next_counter, deployment.clone());

        Ok((next_counter, deployment))
    }

    fn drop_apply(&mut self, counter: u32) {
        self.active_applies.remove(&counter);
    }
}

impl Drop for TerraformPool {
    fn drop(&mut self) {
        for (_, apply) in self.active_applies.drain() {
            debug_assert_eq!(Arc::strong_count(&apply), 1);
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct TerraformBatch {
    pub terraform: TerraformConfig,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub provider: HashMap<String, serde_json::Value>,
    #[serde(skip_serializing_if = "HashMap::is_empty")]
    pub data: HashMap<String, HashMap<String, serde_json::Value>>,
    pub resource: HashMap<String, HashMap<String, serde_json::Value>>,
    pub output: HashMap<String, TerraformOutput>,
}

impl Default for TerraformBatch {
    fn default() -> TerraformBatch {
        TerraformBatch {
            terraform: TerraformConfig {
                required_providers: HashMap::new(),
            },
            provider: HashMap::new(),
            data: HashMap::new(),
            resource: HashMap::new(),
            output: HashMap::new(),
        }
    }
}

impl TerraformBatch {
    pub async fn provision(self, pool: &mut TerraformPool) -> Result<TerraformResult> {
        // Hack to quiet false-positive `clippy::needless_pass_by_ref_mut` on latest nightlies.
        // TODO(mingwei): Remove this when it is no longer needed (current date 2023-08-30).
        // https://github.com/rust-lang/rust-clippy/issues/11380
        let pool = std::convert::identity(pool);

        if self.terraform.required_providers.is_empty()
            && self.resource.is_empty()
            && self.data.is_empty()
            && self.output.is_empty()
        {
            return Ok(TerraformResult {
                outputs: HashMap::new(),
                deployment_folder: None,
            });
        }

        ProgressTracker::with_group("terraform", Some(1), || async {
            let dothydro_folder = std::env::current_dir().unwrap().join(".hydro");
            std::fs::create_dir_all(&dothydro_folder).unwrap();
            let deployment_folder = tempfile::tempdir_in(dothydro_folder).unwrap();

            std::fs::write(
                deployment_folder.path().join("main.tf.json"),
                serde_json::to_string(&self).unwrap(),
            )
            .unwrap();

            if !Command::new("terraform")
                .current_dir(deployment_folder.path())
                .arg("init")
                .stdout(Stdio::null())
                .spawn()
                .context("Failed to spawn `terraform`. Is it installed?")?
                .wait()
                .context("Failed to launch terraform init command")?
                .success()
            {
                bail!("Failed to initialize terraform");
            }

            let (apply_id, apply) = pool.create_apply(deployment_folder)?;

            let output = ProgressTracker::with_group(
                "apply",
                Some(self.resource.values().map(|r| r.len()).sum()),
                || async { apply.write().await.output().await },
            )
            .await;
            pool.drop_apply(apply_id);
            output
        })
        .await
    }
}

struct TerraformApply {
    child: Option<(u32, Arc<RwLock<Child>>)>,
    deployment_folder: Option<TempDir>,
}

async fn display_apply_outputs(stdout: &mut ChildStdout) {
    let lines = BufReader::new(stdout).lines();
    let mut waiting_for_result = HashMap::new();

    for line in lines {
        if let Ok(line) = line {
            let mut split = line.split(':');
            if let Some(first) = split.next() {
                if first.chars().all(|c| c != ' ')
                    && split.next().is_some()
                    && split.next().is_none()
                {
                    if line.starts_with("Plan:")
                        || line.starts_with("Outputs:")
                        || line.contains(": Still creating...")
                        || line.contains(": Reading...")
                        || line.contains(": Still reading...")
                        || line.contains(": Read complete after")
                    {
                    } else if line.ends_with(": Creating...") {
                        let id = line.split(':').next().unwrap().trim().to_string();
                        let (channel_send, channel_recv) = tokio::sync::oneshot::channel();
                        waiting_for_result.insert(
                            id.to_string(),
                            (
                                channel_send,
                                tokio::task::spawn(ProgressTracker::leaf(id, async move {
                                    // `Err(RecvError)` means send side was dropped due to another error.
                                    // Ignore here to prevent spurious panic stack traces.
                                    let _result = channel_recv.await;
                                })),
                            ),
                        );
                    } else if line.contains(": Creation complete after") {
                        let id = line.split(':').next().unwrap().trim();
                        let (sender, to_await) = waiting_for_result.remove(id).unwrap();
                        let _ = sender.send(());
                        to_await.await.unwrap();
                    } else {
                        panic!("Unexpected from Terraform: {}", line);
                    }
                }
            }
        } else {
            break;
        }
    }
}

fn filter_terraform_logs(child: &mut Child) {
    let lines = BufReader::new(child.stdout.take().unwrap()).lines();
    for line in lines {
        if let Ok(line) = line {
            let mut split = line.split(':');
            if let Some(first) = split.next() {
                if first.chars().all(|c| c != ' ')
                    && split.next().is_some()
                    && split.next().is_none()
                {
                    println!("[terraform] {}", line);
                }
            }
        } else {
            break;
        }
    }
}

impl TerraformApply {
    async fn output(&mut self) -> Result<TerraformResult> {
        let (_, child) = self.child.as_ref().unwrap().clone();
        let mut stdout = child.write().unwrap().stdout.take().unwrap();
        let stderr = child.write().unwrap().stderr.take().unwrap();

        let status = tokio::task::spawn_blocking(move || {
            // it is okay for this thread to keep running even if the future is cancelled
            child.write().unwrap().wait().unwrap()
        });

        let display_apply = display_apply_outputs(&mut stdout);
        let stderr_loop = tokio::task::spawn_blocking(move || {
            let mut lines = BufReader::new(stderr).lines();
            while let Some(Ok(line)) = lines.next() {
                ProgressTracker::println(format!("[terraform] {}", line));
            }
        });

        let _ = futures::join!(display_apply, stderr_loop);

        let status = status.await;

        self.child = None;

        if !status.unwrap().success() {
            bail!("Terraform deployment failed, see `[terraform]` logs above.");
        }

        let mut output_command = Command::new("terraform");
        output_command
            .current_dir(self.deployment_folder.as_ref().unwrap().path())
            .arg("output")
            .arg("-json");

        #[cfg(unix)]
        {
            output_command.process_group(0);
        }

        let output = output_command
            .output()
            .context("Failed to read Terraform outputs")?;

        Ok(TerraformResult {
            outputs: serde_json::from_slice(&output.stdout).unwrap(),
            deployment_folder: self.deployment_folder.take(),
        })
    }
}

fn destroy_deployment(deployment_folder: TempDir) {
    println!(
        "Destroying terraform deployment at {}",
        deployment_folder.path().display()
    );

    let mut destroy_command = Command::new("terraform");
    destroy_command
        .current_dir(deployment_folder.path())
        .arg("destroy")
        .arg("-auto-approve")
        .arg("-no-color")
        .arg("-parallelism=128")
        .stdout(Stdio::piped());

    #[cfg(unix)]
    {
        destroy_command.process_group(0);
    }

    let mut destroy_child = destroy_command
        .spawn()
        .expect("Failed to spawn terraform destroy command");

    filter_terraform_logs(&mut destroy_child);

    if !destroy_child
        .wait()
        .expect("Failed to destroy terraform deployment")
        .success()
    {
        // prevent the folder from being deleted
        let _ = deployment_folder.into_path();
        eprintln!("WARNING: failed to destroy terraform deployment");
    }
}

impl Drop for TerraformApply {
    fn drop(&mut self) {
        if let Some((pid, child)) = self.child.take() {
            #[cfg(unix)]
            nix::sys::signal::kill(
                nix::unistd::Pid::from_raw(pid as i32),
                nix::sys::signal::Signal::SIGINT,
            )
            .unwrap();
            #[cfg(not(unix))]
            let _ = pid;

            let mut child_write = child.write().unwrap();
            if child_write.try_wait().unwrap().is_none() {
                println!("Waiting for Terraform apply to finish...");
                child_write.wait().unwrap();
            }
        }

        if let Some(deployment_folder) = self.deployment_folder.take() {
            destroy_deployment(deployment_folder);
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct TerraformConfig {
    pub required_providers: HashMap<String, TerraformProvider>,
}

#[derive(Serialize, Deserialize)]
pub struct TerraformProvider {
    pub source: String,
    pub version: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct TerraformOutput {
    pub value: String,
}

#[derive(Debug)]
pub struct TerraformResult {
    pub outputs: HashMap<String, TerraformOutput>,
    /// `None` if no deployment was performed
    pub deployment_folder: Option<TempDir>,
}

impl Drop for TerraformResult {
    fn drop(&mut self) {
        if let Some(deployment_folder) = self.deployment_folder.take() {
            destroy_deployment(deployment_folder);
        }
    }
}

#[derive(Serialize, Deserialize)]
pub struct TerraformResultOutput {
    value: String,
}