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
//! What one attempt (one call of [`crate::run`] or [`crate::execute_plan`])
//! needs from its host: the environment it sees, where it keeps its files, and
//! the handle that stops it.
//!
//! Nothing in the library reads the process environment; the caller builds an
//! [`ExecContext`] (the `local-ci` binary builds it from `std::env`). Give each
//! attempt its own context: the processes and containers an attempt starts are
//! tracked in its [`CancelHandle`], so cancelling one attempt leaves the others
//! running.
use std::collections::BTreeMap;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
#[derive(Default)]
struct CancelState {
cancelled: AtomicBool,
pgids: Mutex<Vec<i32>>,
containers: Mutex<Vec<String>>,
compose_projects: Mutex<Vec<String>>,
}
/// Cancellation and child tracking owned by one attempt. Cloning shares the
/// state, so a signal handler can hold a clone while the attempt runs.
#[derive(Clone, Default)]
pub struct CancelHandle(Arc<CancelState>);
impl CancelHandle {
pub fn new() -> Self {
Self::default()
}
/// Stop the processes this attempt started and make it report as
/// cancelled. Safe to call from a signal handler thread, and more than once.
pub fn cancel(&self) {
self.0.cancelled.store(true, Ordering::SeqCst);
for &pg in self.0.pgids.lock().unwrap().iter() {
// SAFETY: a plain signal to a process group this attempt started.
unsafe {
libc::killpg(pg, libc::SIGTERM);
}
}
}
pub fn is_cancelled(&self) -> bool {
self.0.cancelled.load(Ordering::SeqCst)
}
/// Make [`CancelHandle::cancel`] signal this process group until it is
/// untracked. `pg` must be the pid of a process group leader and greater
/// than 1: anything else is ignored, so that `killpg(0)` or `killpg(1)`
/// can never signal local-ci's own group or init's.
pub fn track_pgid(&self, pg: i32) {
if pg <= 1 {
return;
}
self.0.pgids.lock().unwrap().push(pg);
}
pub fn untrack_pgid(&self, pg: i32) {
self.0.pgids.lock().unwrap().retain(|&p| p != pg);
}
pub(crate) fn track_container(&self, name: String) {
self.0.containers.lock().unwrap().push(name);
}
pub(crate) fn untrack_containers(&self, names: &std::collections::HashSet<&String>) {
self.0
.containers
.lock()
.unwrap()
.retain(|n| !names.contains(n));
}
pub(crate) fn track_compose(&self, project: String) {
self.0.compose_projects.lock().unwrap().push(project);
}
pub(crate) fn untrack_compose(&self, projects: &[String]) {
self.0
.compose_projects
.lock()
.unwrap()
.retain(|p| !projects.contains(p));
}
/// Take down what this attempt still has up: compose projects, then
/// containers.
pub(crate) fn remove_containers(&self, ctx: &ExecContext) {
let projects: Vec<String> = self.0.compose_projects.lock().unwrap().drain(..).collect();
crate::exec::compose_down(ctx, &projects);
let names: Vec<String> = self.0.containers.lock().unwrap().drain(..).collect();
if !names.is_empty() {
let _ = ctx
.command("docker")
.arg("rm")
.arg("-f")
.args(&names)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status();
}
}
}
/// The environment and the cancel handle of one attempt.
pub struct ExecContext {
env: BTreeMap<String, String>,
/// Stops this attempt; see [`CancelHandle`].
pub cancel: CancelHandle,
github_token: OnceLock<Option<String>>,
}
impl ExecContext {
/// A context over `env` (the variables `PATH`, `HOME`, `LOCAL_CI_HOME` and
/// `XDG_CACHE_HOME` matter; steps run with exactly these variables plus
/// their own).
pub fn new(env: BTreeMap<String, String>) -> Self {
ExecContext {
env,
cancel: CancelHandle::new(),
github_token: OnceLock::new(),
}
}
/// The same environment with a handle of its own: for a second attempt
/// that must not be cancelled with this one. (Not `Clone`: a plain clone
/// would share the handle and so the cancellation.)
pub fn fresh_attempt(&self) -> Self {
ExecContext {
env: self.env.clone(),
cancel: CancelHandle::new(),
github_token: OnceLock::new(),
}
}
/// A command that sees exactly this context's environment (not the
/// process's).
pub(crate) fn command(&self, program: impl AsRef<std::ffi::OsStr>) -> Command {
#[allow(clippy::disallowed_methods)] // the one place; the environment is cleared next
let mut c = Command::new(program);
c.env_clear().envs(&self.env);
c
}
/// The GitHub token of the machine this runs on (`gh auth token`), for
/// steps that read `github.token` or `secrets.GITHUB_TOKEN`. Asked once,
/// and only when a step needs it; `None` when `gh` is missing or not
/// logged in.
pub fn github_token(&self) -> Option<&str> {
self.github_token
.get_or_init(|| {
let gh = self.find_tool("gh")?;
let out = self
.command(gh)
.args(["auth", "token"])
.stdin(Stdio::null())
.stderr(Stdio::null())
.output()
.ok()?;
let t = String::from_utf8_lossy(&out.stdout).trim().to_string();
(out.status.success() && !t.is_empty()).then_some(t)
})
.as_deref()
}
/// The token, if it has been asked for already (to mask it in output).
pub(crate) fn github_token_if_fetched(&self) -> Option<&str> {
self.github_token.get()?.as_deref()
}
pub fn env(&self) -> &BTreeMap<String, String> {
&self.env
}
pub fn var(&self, key: &str) -> Option<&str> {
self.env.get(key).map(String::as_str)
}
/// `HOME`, when set and not empty.
pub fn home(&self) -> Option<&str> {
self.var("HOME").filter(|h| !h.is_empty())
}
/// Where runs, results, worktrees and locks live by default
/// (`LOCAL_CI_HOME`, else the platform cache directory).
pub fn cache_dir(&self) -> PathBuf {
if let Some(d) = self.var("LOCAL_CI_HOME") {
return PathBuf::from(d);
}
let home = PathBuf::from(self.var("HOME").unwrap_or("/tmp"));
if cfg!(target_os = "macos") {
return home.join("Library/Caches/local-ci");
}
match self.var("XDG_CACHE_HOME") {
Some(x) if !x.is_empty() => PathBuf::from(x).join("local-ci"),
_ => home.join(".cache/local-ci"),
}
}
/// An executable on this context's `PATH`, or in `~/.cargo/bin`.
pub fn find_tool(&self, name: &str) -> Option<PathBuf> {
for d in self.var("PATH").unwrap_or("").split(':') {
if d.is_empty() {
continue;
}
let p = PathBuf::from(d).join(name);
if p.is_file() {
return Some(p);
}
}
let p = PathBuf::from(self.var("HOME").unwrap_or(""))
.join(".cargo/bin")
.join(name);
p.is_file().then_some(p)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_command_sees_the_context_env_and_not_the_processs() {
let ctx = ExecContext::new([("ONLY".to_string(), "1".to_string())].into());
let out = ctx.command("/usr/bin/env").output().unwrap();
assert_eq!(String::from_utf8_lossy(&out.stdout), "ONLY=1\n");
}
#[test]
fn only_a_group_leader_pid_above_one_is_tracked() {
let c = CancelHandle::new();
for pg in [i32::MIN, -1, 0, 1] {
c.track_pgid(pg);
}
assert!(c.0.pgids.lock().unwrap().is_empty());
c.track_pgid(2);
assert_eq!(*c.0.pgids.lock().unwrap(), vec![2]);
c.untrack_pgid(2);
assert!(c.0.pgids.lock().unwrap().is_empty());
}
}