| 158 | 158 | | let _ = child.wait(); |
| 159 | 159 | | } |
| 160 | 160 | | |
| 161 | + | /// Removes the containers, networks and volumes of the jobs of the runner |
| 162 | + | /// registered in `dir`, as `g1t-runner` does itself when it starts: each |
| 163 | + | /// carries the label `sh.g1t.runner=<its id>`. |
| 164 | + | pub fn sweep(dir: &Path) { |
| 165 | + | #[derive(Deserialize)] |
| 166 | + | struct Registered { |
| 167 | + | runner: String, |
| 168 | + | } |
| 169 | + | let Some(registered) = std::fs::read(dir.join("config.json")).ok().and_then(|bytes| serde_json::from_slice::<Registered>(&bytes).ok()) else { return }; |
| 170 | + | let label = format!("label=sh.g1t.runner={}", registered.runner); |
| 171 | + | for (list, remove) in [ |
| 172 | + | (vec!["ps", "-aq", "--filter", &label], vec!["rm", "-f", "-v"]), |
| 173 | + | (vec!["network", "ls", "-q", "--filter", &label], vec!["network", "rm"]), |
| 174 | + | (vec!["volume", "ls", "-q", "--filter", &label], vec!["volume", "rm", "-f"]), |
| 175 | + | ] { |
| 176 | + | let Ok(out) = quiet("docker").args(&list).stderr(Stdio::null()).output() else { return }; |
| 177 | + | let ids: Vec<String> = String::from_utf8_lossy(&out.stdout).split_whitespace().map(str::to_owned).collect(); |
| 178 | + | if !ids.is_empty() { |
| 179 | + | let _ = quiet("docker").args(&remove).args(&ids).stdout(Stdio::null()).stderr(Stdio::null()).status(); |
| 180 | + | } |
| 181 | + | } |
| 182 | + | } |
| 183 | + | |
| 161 | 184 | | /// A runner the app started before and did not stop (it was ended |
| 162 | 185 | | /// without quitting): stopped, if that process is still `g1t-runner`. |
| 163 | 186 | | pub fn reap(pid: u32) { |