1mod artifact;
13mod clean;
14mod coverage;
15mod deploy_render;
16mod heartbeat;
17mod postgres;
18mod publish;
19mod release;
20mod snapshot;
21mod watchdog;
22
23#[cfg(test)]
24mod test_support;
25
26use anyhow::{Context, Result};
27use clap::{Parser, Subcommand};
28
29use artifact::Kind;
30use heartbeat::Heartbeat;
31
32#[derive(Parser)]
33#[command(about = "nomisync CI tasks")]
34struct Cli {
35 #[command(subcommand)]
36 command: Command,
37}
38
39#[derive(Subcommand)]
40enum Command {
41 Postgres {
43 #[arg(required = true, trailing_var_arg = true)]
45 argv: Vec<String>,
46 },
47 Coverage,
49 Release,
51 CheckForms {
54 dumps: std::path::PathBuf,
56 #[arg(long, default_value = "postgres://localhost")]
58 server: String,
59 },
60 CheckReferences {
63 dumps: std::path::PathBuf,
65 #[arg(long, default_value = "postgres://localhost")]
67 server: String,
68 #[arg(long)]
70 sources: Option<std::path::PathBuf>,
71 },
72 Finalize,
74}
75
76fn main() -> Result<()> {
77 if let Some(failure) = coverage::wrapper::intercept() {
78 return Err(failure);
79 }
80 match Cli::parse().command {
81 Command::Postgres { argv } => with_postgres(&argv),
82 Command::Coverage => produce(Kind::Coverage),
83 Command::Release => produce(Kind::Release),
84 Command::CheckForms { dumps, server } => snapshot::check_forms(&dumps, &server),
85 Command::CheckReferences {
86 dumps,
87 server,
88 sources,
89 } => snapshot::check_references(&dumps, &server, sources.as_deref()),
90 Command::Finalize => finalize(),
91 }
92}
93
94fn finalize() -> Result<()> {
102 let identity = artifact::Identity::from_env()?;
103 let store = publish::Store::from_env()?;
104 let work =
105 tempfile::TempDir::with_prefix("nomisync-finalize").context("cannot make a temp dir")?;
106
107 let release = store.fetch_meta(Kind::Release, &identity, work.path())?;
108 let local = store.verify_object(&release, &identity, work.path())?;
109 println!(
110 ">> verified {} ({} bytes)",
111 local.file_name().unwrap_or_default().to_string_lossy(),
112 release.bytes
113 );
114
115 store.write_manifest(&[release], &identity, work.path())?;
116 println!(
117 ">> manifest written for {} run {}",
118 identity.commit, identity.run_id
119 );
120 Ok(())
121}
122
123fn with_artifacts() -> bool {
126 std::env::var("WITH_ARTIFACTS").as_deref() == Ok("1")
127}
128
129fn produce(kind: Kind) -> Result<()> {
130 let heartbeat = Heartbeat::start();
134 heartbeat.phase("starting postgres");
135
136 let (_termination, database) = watched_cluster()?;
139 let url = database.database_url();
140
141 match kind {
142 Kind::Coverage => coverage::run_coverage(&heartbeat, &url)?,
143 Kind::Release => release::build(&heartbeat, &url)?,
144 }
145 release::package(kind, &heartbeat, with_artifacts())
146}
147
148fn watched_cluster() -> Result<(watchdog::Termination, postgres::Postgres)> {
151 let termination = watchdog::Termination::watch()?;
152 let runtime = tokio::runtime::Builder::new_current_thread()
153 .enable_all()
154 .build()
155 .context("cannot build a tokio runtime")?;
156 let database = runtime.block_on(postgres::start())?;
157 Ok((termination, database))
158}
159
160fn with_postgres(argv: &[String]) -> Result<()> {
164 let (termination, database) = watched_cluster()?;
165
166 let (program, args) = argv.split_first().context("no command given")?;
167 watchdog::run_supervised(
168 program,
169 args,
170 &[("DATABASE_URL", &database.database_url())],
171 &termination,
172 )
173}