difftreelog
feat fleet tf now executes terraform by itself
in: trunk
3 files changed
cmds/fleet/src/cmds/tf.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/tf.rs
+++ b/cmds/fleet/src/cmds/tf.rs
@@ -1,17 +1,23 @@
use std::{
collections::{BTreeMap, HashMap},
+ ffi::OsString,
path::PathBuf,
};
-use anyhow::{bail, Context, Result};
+use anyhow::{Context, Result};
use clap::Parser;
use fleet_base::host::Config;
use nix_eval::nix_go;
use serde::Deserialize;
use serde_json::Value;
-use tokio::{fs::copy, process::Command};
+use tempfile::NamedTempFile;
+use tokio::{
+ fs::{self, create_dir_all},
+ process::Command,
+};
+use tracing::debug;
-#[derive(Deserialize)]
+#[derive(Deserialize, Debug)]
pub struct TfData {
// Dummy
#[allow(dead_code)]
@@ -23,44 +29,56 @@
}
#[derive(Parser)]
-pub enum Tf {
- /// Generate fleet.tf.json file for running terraform.
- Generate,
- /// Fetch data from terraform to fleet.
- Refresh,
+pub struct Tf {
+ args: Vec<OsString>,
}
impl Tf {
pub async fn run(&self, config: &Config) -> Result<()> {
- match self {
- Tf::Generate => {
- let system = &config.local_system;
- let config = &config.config_field;
- let data: HashMap<String, PathBuf> = nix_go!(config.tf({ system })).build().await?;
- let data = &data["out"];
+ let dir = config.directory.join(".fleet/tf/default");
+ // TODO: consider postponing fleet init until this step, as it might be
+ // highly preferred to extract terraform configuration using multithreaded nix or
+ // lazy-trees nix. lazy-trees nix is very fast and perfect for this task.
+ {
+ debug!("generating terraform configs");
+ let system = &config.local_system;
+ let config = &config.config_field;
+ let data: HashMap<String, PathBuf> = nix_go!(config.tf({ system })).build().await?;
+ let data = &data["out"];
+ let data = fs::read(&data).await?;
+
+ create_dir_all(&dir).await?;
- copy(data, "fleet.tf.json").await?;
- }
- Tf::Refresh => {
- let cmd = Command::new("terraform").arg("refresh").status().await?;
- if !cmd.success() {
- bail!("terraform refresh failed")
- }
+ let tmp = NamedTempFile::new_in(&dir)?;
+ fs::write(tmp.path(), data).await?;
+ tmp.persist(dir.join("fleet.tf.json"))?;
+ }
- let data = Command::new("terraform")
- .arg("output")
- .arg("-json")
- .arg("fleet")
- .output()
- .await?;
- let tf_data: TfData = serde_json::from_slice(&data.stdout)
- .context("failed to parse terraform fleet output")?;
+ {
+ debug!("running terraform command");
+ Command::new("terraform")
+ .current_dir(&dir)
+ .args(&self.args)
+ .status()
+ .await?;
+ }
+ {
+ debug!("syncing terraform data");
+ let data = Command::new("terraform")
+ .current_dir(dir)
+ .arg("output")
+ .arg("-json")
+ .arg("fleet")
+ .output()
+ .await?;
+ let tf_data: TfData = serde_json::from_slice(&data.stdout)
+ .context("failed to parse terraform fleet output")?;
- let mut data = config.data();
- data.extra.insert(
- "terraformHosts".to_owned(),
- serde_json::to_value(tf_data.hosts).expect("should be valid extra"),
- );
- }
+ let mut data = config.data();
+ debug!("synchronized done = {tf_data:?}");
+ data.extra.insert(
+ "terraformHosts".to_owned(),
+ serde_json::to_value(tf_data.hosts).expect("should be valid extra"),
+ );
}
Ok(())
cmds/fleet/src/main.rsdiffbeforeafterboth1#![recursion_limit = "512"]2#![feature(try_blocks)]34pub(crate) mod cmds;5// pub(crate) mod command;6pub(crate) mod extra_args;78use std::{ffi::OsString, process::ExitCode};910use anyhow::{bail, Result};11use clap::{CommandFactory, Parser};12use cmds::{13 build_systems::{BuildSystems, Deploy},14 complete::Complete,15 info::Info,16 secrets::Secret,17 tf::Tf,18};19use fleet_base::{host::Config, opts::FleetOpts};20use futures::{future::LocalBoxFuture, stream::FuturesUnordered, TryStreamExt};21// use host::Config;22#[cfg(feature = "indicatif")]23use human_repr::HumanCount;24#[cfg(feature = "indicatif")]25use indicatif::{ProgressState, ProgressStyle};26use tracing::{error, info, info_span, Instrument};27#[cfg(feature = "indicatif")]28use tracing_indicatif::IndicatifLayer;29use tracing_subscriber::{prelude::*, EnvFilter};3031#[derive(Parser)]32struct Prefetch {}33impl Prefetch {34 async fn run(&self, config: &Config) -> Result<()> {35 let mut prefetch_dir = config.directory.to_path_buf();36 prefetch_dir.push("prefetch");37 if !prefetch_dir.is_dir() {38 info!("nothing to prefetch: no prefetch directory");39 return Ok(());40 }41 let tasks = <FuturesUnordered<LocalBoxFuture<Result<()>>>>::new();42 for entry in std::fs::read_dir(&prefetch_dir)? {43 tasks.push(Box::pin(async {44 let entry = entry?;45 if !entry.metadata()?.is_file() {46 bail!("only files should exist in prefetch directory");47 }48 let span = info_span!(49 "prefetching",50 name = entry.file_name().to_string_lossy().as_ref()51 );52 let mut path = OsString::new();53 path.push("file://");54 path.push(entry.path());5556 let mut status = config.local_host().cmd("nix").await?;57 status.args(&config.nix_args);58 status.arg("store").arg("prefetch-file").arg(path);59 status.run_nix_string().instrument(span).await?;60 Ok(())61 }));62 }63 tasks.try_collect::<Vec<()>>().await?;64 Ok(())65 }66}6768#[derive(Parser)]69enum Opts {70 /// Prepare systems for deployments71 BuildSystems(BuildSystems),7273 Deploy(Deploy),74 /// Secret management75 #[clap(subcommand)]76 Secret(Secret),77 /// Upload prefetch directory to the nix store78 Prefetch(Prefetch),79 /// Config parsing80 Info(Info),81 /// Command completions82 #[clap(hide(true))]83 Complete(Complete),84 /// Compile and evaluate terranix configuration85 Tf(Tf),86}8788#[derive(Parser)]89#[clap(version, author)]90struct RootOpts {91 #[clap(flatten)]92 fleet_opts: FleetOpts,93 #[clap(subcommand)]94 command: Opts,95}9697async fn run_command(config: &Config, opts: FleetOpts, command: Opts) -> Result<()> {98 match command {99 Opts::BuildSystems(c) => c.run(config, &opts).await?,100 Opts::Deploy(d) => d.run(config, &opts).await?,101 Opts::Secret(s) => s.run(config, &opts).await?,102 Opts::Info(i) => i.run(config).await?,103 Opts::Prefetch(p) => p.run(config).await?,104 Opts::Tf(t) => t.run(config).await?,105 // TODO: actually parse commands before starting the async runtime106 Opts::Complete(c) => {107 tokio::task::spawn_blocking(move || c.run(RootOpts::command())).await?108 }109 };110 Ok(())111}112113fn setup_logging() {114 #[cfg(feature = "indicatif")]115 let indicatif_layer = {116 use std::time::Duration;117118 IndicatifLayer::new().with_progress_style(119 ProgressStyle::with_template(120 "{color_start}{span_child_prefix} {span_name}{{{span_fields}}}{color_end} {wide_msg} {color_start}{download_progress} {elapsed}{color_end}",121 )122 .unwrap()123 .with_key("download_progress", |state: &ProgressState, writer: &mut dyn std::fmt::Write| {124 let Some(len) = state.len() else {125 return;126 };127 let pos = state.pos();128 if pos > len {129 let _ = write!(writer, "{}", pos.human_count_bare());130 } else {131 let _ = write!(writer, "{} / {}", pos.human_count_bare(), len.human_count_bare());132 }133 })134 .with_key(135 "color_start",136 |state: &ProgressState, writer: &mut dyn std::fmt::Write| {137 let elapsed = state.elapsed();138139 if elapsed > Duration::from_secs(60) {140 // Red141 let _ = write!(writer, "\x1b[{}m", 1 + 30);142 } else if elapsed > Duration::from_secs(30) {143 // Yellow144 let _ = write!(writer, "\x1b[{}m", 3 + 30);145 }146 },147 )148 .with_key(149 "color_end",150 |state: &ProgressState, writer: &mut dyn std::fmt::Write| {151 if state.elapsed() > Duration::from_secs(30) {152 let _ = write!(writer, "\x1b[0m");153 }154 },155 ),156 )157 };158159 let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));160161 let reg = tracing_subscriber::registry().with({162 let sub = tracing_subscriber::fmt::layer()163 .without_time()164 .with_target(false);165 #[cfg(feature = "indicatif")]166 let sub = sub.with_writer(indicatif_layer.get_stdout_writer());167 sub.with_filter(filter) // .without,168 });169 // #[cfg(feature = "indicatif")]170 #[cfg(feature = "indicatif")]171 let reg = reg.with(indicatif_layer);172 reg.init();173}174175fn main() -> ExitCode {176 let opts = RootOpts::parse();177 if let Opts::Complete(c) = &opts.command {178 c.run(RootOpts::command());179 return ExitCode::SUCCESS;180 }181182 setup_logging();183 async_main(opts)184}185186#[tokio::main]187async fn async_main(opts: RootOpts) -> ExitCode {188 if let Err(e) = main_real(opts).await {189 // If I remove this line, the next error!() line gets eaten.190 // This is a bug in indicatif, it needs to be fixed191 #[cfg(feature = "indicatif")]192 info!("fixme: this line gets eaten by tracing-indicatif on levels info+");193 error!("{e:#}");194 return ExitCode::FAILURE;195 }196 ExitCode::SUCCESS197}198199async fn main_real(opts: RootOpts) -> Result<()> {200 nix_eval::init_tokio();201202 let nix_args = std::env::var_os("NIX_ARGS")203 .map(|a| extra_args::parse_os(&a))204 .transpose()?205 .unwrap_or_default();206 let config = opts.fleet_opts.build(nix_args).await?;207208 match run_command(&config, opts.fleet_opts, opts.command).await {209 Ok(()) => {210 config.save()?;211 Ok(())212 }213 Err(e) => {214 let _ = config.save();215 Err(e)216 }217 }218}219220#[cfg(test)]221mod tests {222 use super::*;223224 #[test]225 fn verify_command() {226 use clap::CommandFactory;227 RootOpts::command().debug_assert();228 }229}cmds/terraform-provider-fleet/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/cmds/terraform-provider-fleet/Cargo.toml
@@ -0,0 +1,11 @@
+[package]
+name = "terraform-provider-fleet"
+edition = "2021"
+version.workspace = true
+
+[dependencies]
+anyhow.workspace = true
+async-trait = "0.1.81"
+serde = { workspace = true, features = ["derive"] }
+tf-provider = "0.2.2"
+tokio.workspace = true