use std::time::Duration;
use crate::check::Stage;
use crate::manifest::{External, Kind, Manifest};
use super::{
key, last_line, log_line, machine_keys, run_label, sanitize_repo_dir, token_from, Call,
ClusterConfig, Fallback, Kube, CALL_SECS, SEQ,
};
use crate::json_read::Value;
mod collect;
mod pending;
mod workflow;
pub use collect::collect_command;
pub(crate) use collect::{collect, lists_live, settled_lines, status_lines, stop_pending};
#[cfg(test)]
pub(crate) use pending::sample_file as sample_pending_file;
pub(crate) use pending::PENDING_FILE;
use pending::{Gate, GateState, Pending, PendingError, RecordPhase, Source, Store};
const REHEARSE_CLUSTER: &str = "greengate.rehearse.cluster";
const PUSH_SECS: u64 = 60;
const LS_REMOTE_SECS: u64 = 20;
const LOCK_WAIT: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ask {
Flag,
Setting,
}
pub fn wanted(settings: &crate::config::Settings, ask: Ask) -> bool {
ask == Ask::Flag || crate::config::boolean_or(settings, REHEARSE_CLUSTER, false)
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Detached {
pub gates: Vec<String>,
}
#[cfg(feature = "test-hooks")]
fn crash_point(at: &str) {
if std::env::var("GREENGATE_TEST_CRASH").is_ok_and(|v| v == at) {
std::process::exit(137);
}
}
#[cfg(not(feature = "test-hooks"))]
fn crash_point(_at: &str) {}
struct Machine {
base: ClusterConfig,
snapshot_remote: String,
snapshot_fetch: String,
mirror: Option<Mirror>,
}
struct Mirror {
remote: String,
fetch: String,
}
fn machine(settings: &crate::config::Settings) -> Result<Machine, Fallback> {
let base = super::cluster_config(settings)?;
let keys = machine_keys();
let need = |name: &'static str, shown: &'static str| {
key(&keys, name)
.map(str::to_string)
.ok_or(Fallback::Unconfigured(shown))
};
let snapshot_remote = need(
"greengate.cluster.snapshotremote",
"greengate.cluster.snapshotRemote is not set: `git config --global greengate.cluster.snapshotRemote 'git@host:org/{repo}.git'`",
)?;
let snapshot_fetch = need(
"greengate.cluster.snapshotfetch",
"greengate.cluster.snapshotFetch is not set: `git config --global greengate.cluster.snapshotFetch 'http://forgejo-http.forgejo.svc.cluster.local:3000/org/{repo}.git'`",
)?;
let mirror = match (
key(&keys, "greengate.cluster.mirrorremote"),
key(&keys, "greengate.cluster.mirror"),
) {
(Some(remote), Some(fetch)) => Some(Mirror {
remote: remote.to_string(),
fetch: fetch.to_string(),
}),
_ => None,
};
Ok(Machine {
base,
snapshot_remote,
snapshot_fetch,
mirror,
})
}
fn repo_name() -> Result<String, String> {
let url = crate::git::stdout(&["remote", "get-url", "origin"])
.ok_or("the repository has no origin remote to name it by")?;
let last = url
.trim_end_matches('/')
.rsplit(['/', ':'])
.next()
.unwrap_or_default();
if last.is_empty() {
return Err(format!("cannot name the repository from {url:?}"));
}
Ok(sanitize_repo_dir(last))
}
fn expand(template: &str, repo: &str) -> String {
template.replace("{repo}", repo)
}
fn eligible<'a>(
settings: &crate::config::Settings,
manifest: &'a Manifest,
scoped: &[String],
) -> Vec<&'a External> {
if !super::executor_is_cluster(settings) {
return Vec::new();
}
scoped
.iter()
.filter_map(|name| {
manifest
.externals
.iter()
.find(|e| e.stage == Stage::PrePush && e.id == *name)
})
.filter(|e| super::is_platform_any(e))
.collect()
}
fn known_for(tree: &str) -> (Vec<String>, Vec<String>) {
let Some(store) = Store::here() else {
return (Vec::new(), Vec::new());
};
let records = match store.read() {
Ok(records) => records,
Err(why) => {
log_line("detach", &format!("cluster-pending is unreadable: {why}"));
return (Vec::new(), Vec::new());
}
};
let ours = records.iter().filter(|r| r.tree == tree);
let waiting = ours
.clone()
.flat_map(|r| r.waiting().map(|g| g.id.clone()))
.collect();
let all = ours
.flat_map(|r| r.gates.iter().map(|g| g.id.clone()))
.collect();
(waiting, all)
}
pub fn detach(
settings: &crate::config::Settings,
manifest: &Manifest,
scoped: &[String],
ask: Ask,
) -> Detached {
let Some(tree) = crate::git::stdout(&["rev-parse", "HEAD^{tree}"]) else {
return Detached::default();
};
let (waiting, known) = known_for(&tree);
let mut riding: Vec<String> = scoped
.iter()
.filter(|gate| waiting.contains(gate))
.cloned()
.collect();
if !wanted(settings, ask) {
return Detached { gates: riding };
}
let fresh: Vec<&External> = eligible(settings, manifest, scoped)
.into_iter()
.filter(|e| !known.contains(&e.id))
.collect();
if fresh.is_empty() {
if ask == Ask::Flag {
if riding.is_empty() {
eprintln!(
"no gate of this rehearsal can go to the cluster: it needs a trusted \
`platform=any` pre-push gate and `set executor cluster`"
);
} else {
eprintln!("already on the cluster: {}", riding.join(" "));
}
}
return Detached { gates: riding };
}
match submit(settings, &tree, &fresh) {
Ok(submitted) => {
eprintln!(
"submitted {} to the cluster: {} — the next commit or push collects the result",
submitted.name,
submitted.gates.join(" ")
);
riding.extend(submitted.gates);
}
Err(error) => {
let (line, spoken) = error.render();
for e in &fresh {
log_line(&e.id, &format!("detached: {line}"));
}
if ask == Ask::Flag {
crate::hooks::common::warn(&crate::ui::sanitize(&spoken));
}
}
}
Detached { gates: riding }
}
#[derive(Debug)]
enum SubmitError {
NotSubmitted(Fallback),
Unconfirmed(String),
}
impl From<Fallback> for SubmitError {
fn from(f: Fallback) -> SubmitError {
SubmitError::NotSubmitted(f)
}
}
impl SubmitError {
fn render(&self) -> (String, String) {
match self {
SubmitError::NotSubmitted(Fallback::Misconfigured { why, fix }) => (
why.clone(),
format!("not submitted to the cluster, the gates run here: {why} — fix with {fix}"),
),
SubmitError::NotSubmitted(f) => (
f.to_string(),
format!("not submitted to the cluster, the gates run here: {f}"),
),
SubmitError::Unconfirmed(why) => (
format!("not confirmed: {why}"),
format!(
"submitted, not confirmed ({why}); the gates also run here, and the next \
commit or push collects the Workflow if it landed"
),
),
}
}
}
struct Submitted {
name: String,
gates: Vec<String>,
}
struct Origin {
source: Source,
url: String,
}
fn submit(
settings: &crate::config::Settings,
tree: &str,
gates: &[&External],
) -> Result<Submitted, SubmitError> {
let machine = machine(settings)?;
let head = crate::git::stdout(&["rev-parse", "HEAD"])
.ok_or_else(|| Fallback::NotProved("git would not name HEAD".into()))?;
let repo = repo_name().map_err(Fallback::NotProved)?;
let label = run_label(SEQ.fetch_add(1, std::sync::atomic::Ordering::SeqCst));
let frozen = freeze(settings, &machine, gates)?;
let origin = choose_origin(&machine, &repo, &head, tree, &label)?;
let record = Pending {
name: workflow::workflow_name(&label),
uid: None,
label,
namespace: machine.base.namespace.clone(),
kubeconfig: machine.base.kubeconfig.clone(),
tree: tree.to_string(),
source: origin.source,
submitted: crate::gate_evidence::now(),
phase: RecordPhase::Submitting,
gates: frozen,
};
let store = Store::here()
.ok_or_else(|| Fallback::NotProved("git would not name the common directory".into()))?;
record_intent(&store, &record)?;
crash_point("before-create");
let plan = workflow::Plan {
config: &machine.base,
record: &record,
repo_dir: &super::repo_dir(),
url: &origin.url,
};
let kube = Kube::new(&machine.base);
let uid = create(&kube, &record, &workflow::workflow_json(&plan))?;
crash_point("after-create");
adopt(&store, &record.name, &uid)?;
Ok(Submitted {
name: record.name,
gates: record.gates.into_iter().map(|g| g.id).collect(),
})
}
fn freeze(
settings: &crate::config::Settings,
machine: &Machine,
gates: &[&External],
) -> Result<Vec<Gate>, Fallback> {
let keys = machine_keys();
gates
.iter()
.enumerate()
.map(|(i, ext)| {
let Kind::Runnable {
program,
args,
scope,
..
} = &ext.kind
else {
return Err(Fallback::NotProved(format!("{} is not runnable", ext.id)));
};
let token = token_from(&keys, settings, ext, &ext.id).ok_or_else(|| {
Fallback::NotProved(format!("git would not hash the identity of {}", ext.id))
})?;
let mut argv = vec![program.clone()];
argv.extend(args.iter().cloned());
Ok(Gate {
id: ext.id.clone(),
task: format!("g{i}"),
argv,
scope: format!("{scope:?}"),
prepare: machine.base.prepare.clone(),
digest: machine.base.digest.clone(),
contract: crate::gate_identity::CLUSTER_CONTRACT.to_string(),
token,
state: GateState::Waiting,
})
})
.collect()
}
fn choose_origin(
machine: &Machine,
repo: &str,
head: &str,
tree: &str,
label: &str,
) -> Result<Origin, Fallback> {
if let Some(mirror) = &machine.mirror {
match advertised(&expand(&mirror.remote, repo)) {
Ok(oids) if oids.iter().any(|o| o == head) => {
return Ok(Origin {
source: Source {
snapshot_ref: None,
oid: head.to_string(),
},
url: expand(&mirror.fetch, repo),
})
}
Ok(_) => {}
Err(why) => log_line("detach", &format!("mirror not listed: {why}")),
}
}
let oid = snapshot_commit(head, tree, label)?;
let snapshot_ref = format!("refs/snapshots/{label}");
let remote = expand(&machine.snapshot_remote, repo);
push_snapshot(&remote, &oid, &snapshot_ref)?;
let seen = advertised_at(&remote, &snapshot_ref)
.map_err(|why| Fallback::NotProved(format!("the snapshot was not read back: {why}")))?;
if seen.as_deref() != Some(oid.as_str()) {
return Err(Fallback::NotProved(
"the snapshot was pushed but not read back from the remote".into(),
));
}
Ok(Origin {
source: Source {
snapshot_ref: Some(snapshot_ref),
oid,
},
url: expand(&machine.snapshot_fetch, repo),
})
}
fn advertised(remote: &str) -> Result<Vec<String>, String> {
let (probe, out) = crate::git::probe_remote_stdout(
&["ls-remote", remote, "refs/heads/*", "refs/tags/*"],
LS_REMOTE_SECS,
);
match probe {
crate::git::Probe::Exit(0) => Ok(out
.lines()
.filter_map(|l| l.split_whitespace().next())
.map(str::to_string)
.collect()),
other => Err(format!("git ls-remote {remote}: {other:?}")),
}
}
fn advertised_at(remote: &str, ref_name: &str) -> Result<Option<String>, String> {
let (probe, out) =
crate::git::probe_remote_stdout(&["ls-remote", remote, ref_name], LS_REMOTE_SECS);
match probe {
crate::git::Probe::Exit(0) => Ok(out
.lines()
.filter_map(|l| l.split_once('\t'))
.find(|(_, name)| *name == ref_name)
.map(|(oid, _)| oid.trim().to_string())),
other => Err(format!("git ls-remote {remote}: {other:?}")),
}
}
fn snapshot_commit(head: &str, tree: &str, label: &str) -> Result<String, Fallback> {
let now = format!("@{} +0000", crate::gate_evidence::now());
let out = std::process::Command::new("git")
.args(["commit-tree", tree, "-p", head, "-m"])
.arg(format!("greengate snapshot {label}"))
.env("GIT_AUTHOR_NAME", crate::names::GIT_AUTHOR)
.env("GIT_AUTHOR_EMAIL", crate::names::GIT_EMAIL)
.env("GIT_COMMITTER_NAME", crate::names::GIT_AUTHOR)
.env("GIT_COMMITTER_EMAIL", crate::names::GIT_EMAIL)
.env("GIT_AUTHOR_DATE", &now)
.env("GIT_COMMITTER_DATE", &now)
.stdin(std::process::Stdio::null())
.output()
.map_err(|e| Fallback::NotProved(format!("git commit-tree: {e}")))?;
let oid = String::from_utf8_lossy(&out.stdout).trim().to_string();
if out.status.success() && !oid.is_empty() {
Ok(oid)
} else {
Err(Fallback::NotProved("git commit-tree failed".into()))
}
}
fn push_snapshot(remote: &str, oid: &str, ref_name: &str) -> Result<(), Fallback> {
let (mut args, mut env) = crate::git::remote_env(crate::git::configured_ssh());
env.push((crate::attest::PUSH_GUARD, "1"));
let refspec = format!("{oid}:{ref_name}");
args.extend(["push", "-q", remote, &refspec]);
match crate::git::probe_env(&args, PUSH_SECS, &env) {
crate::git::Probe::Exit(0) => Ok(()),
other => Err(Fallback::NotProved(format!(
"git push of the snapshot to {remote}: {other:?}"
))),
}
}
fn record_intent(store: &Store, record: &Pending) -> Result<(), Fallback> {
let not = |why: PendingError| Fallback::NotProved(format!("cluster-pending: {why}"));
let held = store.lock_within(LOCK_WAIT).ok_or_else(|| {
Fallback::NotProved("cluster-pending: busy, or no flock on this platform".into())
})?;
let mut records = store.read().map_err(not)?;
if let Some(taken) = holder_of(&records, record) {
return Err(Fallback::NotProved(format!(
"another submission of this tree already holds {taken}"
)));
}
records.push(record.clone());
store.write(&held, &records).map_err(not)
}
fn holder_of(records: &[Pending], record: &Pending) -> Option<String> {
records
.iter()
.filter(|other| other.tree == record.tree)
.flat_map(|other| other.gates.iter())
.find(|held| record.gates.iter().any(|g| g.id == held.id))
.map(|held| held.id.clone())
}
fn adopt(store: &Store, name: &str, uid: &str) -> Result<(), SubmitError> {
let not = |why: String| {
SubmitError::Unconfirmed(format!(
"the Workflow exists but its UID was not recorded: {why}"
))
};
let held = store
.lock_within(LOCK_WAIT)
.ok_or_else(|| not("cluster-pending is busy".into()))?;
let records: Vec<Pending> = store
.read()
.map_err(|e| not(e.to_string()))?
.into_iter()
.map(|mut r| {
if r.name == name && r.uid.is_none() {
r.uid = Some(uid.to_string());
r.phase = RecordPhase::Pending;
}
r
})
.collect();
store.write(&held, &records).map_err(|e| not(e.to_string()))
}
#[derive(Debug, PartialEq, Eq)]
enum Created {
Made(String),
Taken,
Rejected(String),
NotSent(String),
Unknown(String),
}
fn classify(call: Call) -> Created {
match call {
Call::Ok(uid) if !uid.trim().is_empty() => Created::Made(uid.trim().to_string()),
Call::Ok(_) => Created::Unknown("kubectl create printed no UID".into()),
Call::Refused(err) if err.contains("AlreadyExists") || err.contains("already exists") => {
Created::Taken
}
Call::Refused(err) if err.contains("Error from server") => {
Created::Rejected(last_line(&err))
}
Call::Refused(err) | Call::TimedOut(err) => Created::Unknown(last_line(&err)),
Call::NotStarted(err) => Created::NotSent(err),
}
}
#[derive(Debug, PartialEq, Eq)]
enum Uncertain {
Taken,
Lost(String),
}
fn create(kube: &Kube, record: &Pending, json: &str) -> Result<String, SubmitError> {
let file = ManifestFile::write(&record.label, json)?;
let path = file.path.to_string_lossy().into_owned();
let args = ["create", "-f", &path, "-o", "jsonpath={.metadata.uid}"];
let outcome = classify(kube.capture(&args, CALL_SECS));
drop(file);
match outcome {
Created::Made(uid) => Ok(uid),
Created::Rejected(why) => {
forget(record);
Err(Fallback::NotProved(format!("the cluster refused it: {why}")).into())
}
Created::NotSent(why) => {
forget(record);
Err(Fallback::Unreachable(why).into())
}
Created::Taken => reconcile(kube, record, Uncertain::Taken),
Created::Unknown(why) => reconcile(kube, record, Uncertain::Lost(why)),
}
}
fn reconcile(kube: &Kube, record: &Pending, uncertain: Uncertain) -> Result<String, SubmitError> {
let said = match uncertain {
Uncertain::Lost(why) => why,
Uncertain::Taken => "the name was taken".to_string(),
};
match kube.capture(&["get", "workflow", &record.name, "-o", "json"], CALL_SECS) {
Call::Ok(json) => {
let wf = crate::json_read::parse(&json).ok_or_else(|| {
SubmitError::Unconfirmed("the Workflow found by name is not JSON".into())
})?;
let uid = wf
.get("metadata")
.and_then(|m| m.get("uid"))
.and_then(Value::as_str)
.ok_or_else(|| SubmitError::Unconfirmed("the Workflow found has no UID".into()))?;
match workflow::check_workflow(&wf, record) {
Ok(()) => Ok(uid.to_string()),
Err(why) => {
forget(record);
Err(Fallback::NotProved(format!(
"a Workflow named {} exists and is not the one submitted: {why}",
record.name
))
.into())
}
}
}
Call::Refused(err) if err.contains("NotFound") => Err(SubmitError::Unconfirmed(format!(
"the cluster did not show the Workflow ({said}); the intent is kept"
))),
Call::Refused(err) | Call::NotStarted(err) | Call::TimedOut(err) => {
Err(SubmitError::Unconfirmed(format!(
"the Workflow could not be looked up ({said}; {})",
last_line(&err)
)))
}
}
}
fn forget(record: &Pending) {
let Some(store) = Store::here() else { return };
let Some(held) = store.lock_within(LOCK_WAIT) else {
log_line(
"detach",
&format!("could not drop the intent of {}", record.name),
);
return;
};
let kept: Result<Vec<Pending>, PendingError> = store
.read()
.map(|rs| rs.into_iter().filter(|r| r.name != record.name).collect());
let written = kept.and_then(|rs| store.write(&held, &rs));
if let Err(why) = written {
log_line(
"detach",
&format!("could not drop the intent of {}: {why}", record.name),
);
}
}
struct ManifestFile {
path: std::path::PathBuf,
}
impl ManifestFile {
fn write(label: &str, json: &str) -> Result<ManifestFile, Fallback> {
let path = std::env::temp_dir().join(format!("greengate-workflow-{label}.json"));
let mut options = std::fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
std::os::unix::fs::OpenOptionsExt::mode(&mut options, 0o600);
options
.open(&path)
.and_then(|mut f| std::io::Write::write_all(&mut f, json.as_bytes()))
.map_err(|e| Fallback::NotProved(format!("manifest file: {e}")))?;
super::track_temp_file(&path);
Ok(ManifestFile { path })
}
}
impl Drop for ManifestFile {
fn drop(&mut self) {
super::untrack_temp_file(&self.path);
if let Err(e) = std::fs::remove_file(&self.path) {
crate::hooks::common::warn(&format!("could not remove {}: {e}", self.path.display()));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_repository_is_named_by_its_origin() {
for (url, name) in [
("git@git.example:org/relais.git", "relais"),
(
"https://github.com/o/duro-design-system",
"duro-design-system",
),
("/tmp/x/remote.git/", "remote"),
] {
let last = url
.trim_end_matches('/')
.rsplit(['/', ':'])
.next()
.unwrap_or_default();
assert_eq!(sanitize_repo_dir(last), name, "{url}");
}
assert_eq!(
expand("git@h:amont-snapshots/{repo}.git", "relais"),
"git@h:amont-snapshots/relais.git"
);
}
#[test]
fn a_create_is_only_repeated_never() {
let made = classify(Call::Ok("uid-1\n".into()));
assert_eq!(made, Created::Made("uid-1".into()));
assert_eq!(
classify(Call::Refused(
"Error from server (AlreadyExists): workflows.argoproj.io \"x\" already exists"
.into()
)),
Created::Taken
);
assert!(matches!(
classify(Call::Refused(
"Error from server (Forbidden): workflows is forbidden".into()
)),
Created::Rejected(_)
));
assert!(matches!(
classify(Call::Refused("error: unexpected EOF".into())),
Created::Unknown(_)
));
assert!(matches!(
classify(Call::TimedOut("kubectl timed out".into())),
Created::Unknown(_)
));
assert!(matches!(
classify(Call::Ok(String::new())),
Created::Unknown(_)
));
assert!(matches!(
classify(Call::NotStarted("kubectl: not found".into())),
Created::NotSent(_)
));
}
#[test]
fn a_second_submission_of_a_tree_cannot_take_a_gate_already_held() {
let held = super::pending::tests::sample("wf-1");
let mut other = super::pending::tests::sample("wf-2");
assert_eq!(
holder_of(std::slice::from_ref(&held), &other),
Some("pre-push-lint".to_string())
);
other.tree = "0".repeat(40);
assert_eq!(holder_of(&[held], &other), None, "another tree");
}
#[cfg(unix)]
#[test]
fn record_intent_refuses_a_gate_a_record_of_the_tree_holds() {
let dir = std::env::temp_dir().join(format!("greengate-intent-{}", std::process::id()));
std::fs::create_dir_all(&dir).expect("mkdir");
let store = Store::in_dir(&dir);
let first = super::pending::tests::sample("wf-1");
assert!(record_intent(&store, &first).is_ok());
let second = super::pending::tests::sample("wf-2");
let refused = record_intent(&store, &second).expect_err("held");
assert!(
refused.to_string().contains("already holds pre-push-lint"),
"{refused}"
);
assert_eq!(store.read().expect("read").len(), 1);
std::fs::remove_dir_all(&dir).expect("clean");
}
}