1use std::path::Path;
6use std::process::Command;
7
8use crate::xmlrpc::{Client, Error, Value};
9
10const DEFAULT_RETRIES: u32 = 3;
12
13pub const TASK_CLOSED: i64 = 2;
15pub const TASK_FAILED: i64 = 5;
16
17fn retry<T, F: Fn() -> Result<T, Error>>(retries: u32, f: F) -> Result<T, Error> {
19 let mut last_err = None;
20 for attempt in 0..=retries {
21 match f() {
22 Ok(v) => return Ok(v),
23 Err(e) => {
24 if attempt < retries && e.is_retriable() {
25 let delay = std::time::Duration::from_secs(1 << attempt);
26 eprintln!(
27 " retrying in {}s ({}/{retries}): {e}",
28 delay.as_secs(),
29 attempt + 1,
30 );
31 std::thread::sleep(delay);
32 last_err = Some(e);
33 } else {
34 return Err(e);
35 }
36 }
37 }
38 }
39 Err(last_err.unwrap())
40}
41
42pub struct KojiClient {
44 client: Client,
45 instance: String,
46}
47
48#[derive(Debug)]
50pub struct BuildInfo {
51 pub id: i64,
52 pub task_id: i64,
53 pub name: String,
54 pub version: String,
55 pub release: String,
56 pub nvr: String,
57 pub state: i64,
58}
59
60#[derive(Debug, Clone)]
62pub struct TaskInfo {
63 pub id: i64,
64 pub method: String,
65 pub arch: String,
66 pub state: i64,
67}
68
69impl TaskInfo {
70 pub fn state_name(&self) -> &'static str {
71 match self.state {
72 0 => "FREE",
73 1 => "OPEN",
74 2 => "CLOSED",
75 3 => "CANCELED",
76 4 => "ASSIGNED",
77 5 => "FAILED",
78 _ => "UNKNOWN",
79 }
80 }
81}
82
83impl KojiClient {
84 pub fn new(instance: &str) -> Self {
85 let hub_url = format!("https://{instance}/kojihub");
86 Self {
87 client: Client::new(&hub_url),
88 instance: instance.to_string(),
89 }
90 }
91
92 pub fn instance(&self) -> &str {
93 &self.instance
94 }
95
96 pub fn get_build(&self, build_id: i64) -> Result<BuildInfo, Error> {
98 let result = retry(DEFAULT_RETRIES, || {
99 self.client.call("getBuild", &[Value::Int(build_id)])
100 })?;
101 Ok(BuildInfo {
102 id: result
103 .get("id")
104 .and_then(|v| v.as_int())
105 .unwrap_or(build_id),
106 task_id: result
107 .get("task_id")
108 .and_then(|v| v.as_int())
109 .ok_or_else(|| Error::Parse("missing task_id in build info".into()))?,
110 name: result
111 .get("name")
112 .and_then(|v| v.as_str())
113 .unwrap_or("")
114 .to_string(),
115 version: result
116 .get("version")
117 .and_then(|v| v.as_str())
118 .unwrap_or("")
119 .to_string(),
120 release: result
121 .get("release")
122 .and_then(|v| v.as_str())
123 .unwrap_or("")
124 .to_string(),
125 nvr: result
126 .get("nvr")
127 .and_then(|v| v.as_str())
128 .unwrap_or("")
129 .to_string(),
130 state: result.get("state").and_then(|v| v.as_int()).unwrap_or(-1),
131 })
132 }
133
134 pub fn get_task_info(&self, task_id: i64) -> Result<TaskInfo, Error> {
136 let result = retry(DEFAULT_RETRIES, || {
137 self.client.call("getTaskInfo", &[Value::Int(task_id)])
138 })?;
139 Ok(TaskInfo {
140 id: result.get("id").and_then(|v| v.as_int()).unwrap_or(task_id),
141 method: result
142 .get("method")
143 .and_then(|v| v.as_str())
144 .unwrap_or("")
145 .to_string(),
146 arch: result
147 .get("arch")
148 .and_then(|v| v.as_str())
149 .unwrap_or("")
150 .to_string(),
151 state: result.get("state").and_then(|v| v.as_int()).unwrap_or(-1),
152 })
153 }
154
155 pub fn get_task_children(&self, task_id: i64) -> Result<Vec<TaskInfo>, Error> {
157 let result = retry(DEFAULT_RETRIES, || {
158 self.client.call("getTaskChildren", &[Value::Int(task_id)])
159 })?;
160 let items = result
161 .as_array()
162 .ok_or_else(|| Error::Parse("expected array for task children".into()))?;
163 items
164 .iter()
165 .map(|v| {
166 Ok(TaskInfo {
167 id: v
168 .get("id")
169 .and_then(|v| v.as_int())
170 .ok_or_else(|| Error::Parse("missing id in child task".into()))?,
171 method: v
172 .get("method")
173 .and_then(|v| v.as_str())
174 .unwrap_or("")
175 .to_string(),
176 arch: v
177 .get("arch")
178 .and_then(|v| v.as_str())
179 .unwrap_or("")
180 .to_string(),
181 state: v.get("state").and_then(|v| v.as_int()).unwrap_or(-1),
182 })
183 })
184 .collect()
185 }
186
187 pub fn resolve_build_arch_task(&self, task_id: i64, arch: &str) -> Result<TaskInfo, Error> {
192 let info = self.get_task_info(task_id)?;
193 if info.method == "buildArch" {
194 return Ok(info);
195 }
196
197 let children = self.get_task_children(task_id)?;
198 let arch_tasks: Vec<_> = children
199 .iter()
200 .filter(|t| t.method == "buildArch" && t.arch == arch)
201 .collect();
202
203 if arch_tasks.is_empty() {
204 let available: Vec<_> = children
205 .iter()
206 .filter(|t| t.method == "buildArch")
207 .map(|t| t.arch.as_str())
208 .collect();
209 return Err(Error::Parse(format!(
210 "no buildArch task for arch '{arch}' under task {task_id}; \
211 available: {}",
212 if available.is_empty() {
213 "(none)".to_string()
214 } else {
215 available.join(", ")
216 }
217 )));
218 }
219
220 Ok(arch_tasks[0].clone())
221 }
222
223 pub fn download_log(
227 &self,
228 task_id: i64,
229 filename: &str,
230 dest_dir: &Path,
231 ) -> Result<String, Error> {
232 let profile = self.koji_profile();
233 retry(DEFAULT_RETRIES, || {
234 let mut cmd = Command::new("koji");
235 if let Some(p) = profile {
236 cmd.arg("-p").arg(p);
237 }
238 let output = cmd
239 .arg("download-logs")
240 .arg("--dir")
241 .arg(dest_dir)
242 .arg(task_id.to_string())
243 .output()
244 .map_err(|e| Error::Parse(format!("failed to run koji download-logs: {e}")))?;
245 if !output.status.success() {
246 let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
247 return Err(Error::Parse(format!(
248 "koji download-logs {task_id} failed: {stderr}"
249 )));
250 }
251 Ok(())
252 })?;
253
254 let path = dest_dir.join(filename);
255 if path.exists() {
256 return std::fs::read_to_string(&path).map_err(|e| {
257 Error::Parse(format!("failed to read {filename} for task {task_id}: {e}"))
258 });
259 }
260
261 if let Some(found) = find_file_recursive(dest_dir, filename) {
264 return std::fs::read_to_string(&found)
265 .map_err(|e| Error::Parse(format!("failed to read {}: {e}", found.display())));
266 }
267
268 let mut files = Vec::new();
270 collect_files(dest_dir, &mut files);
271 Err(Error::Parse(format!(
272 "{filename} not found for task {task_id}\n\
273 koji download-logs produced: {}",
274 if files.is_empty() {
275 "(no files)".to_string()
276 } else {
277 files
278 .iter()
279 .map(|p| p.strip_prefix(dest_dir).unwrap_or(p).display().to_string())
280 .collect::<Vec<_>>()
281 .join(", ")
282 }
283 )))
284 }
285
286 pub fn download_build_log(
293 &self,
294 build: &BuildInfo,
295 arch: &str,
296 filename: &str,
297 ) -> Result<String, Error> {
298 let topurl = self.packages_url().ok_or_else(|| {
299 Error::Parse(format!(
300 "no known packages URL for instance {}",
301 self.instance
302 ))
303 })?;
304 let url = format!(
305 "{topurl}/packages/{name}/{version}/{release}/data/logs/{arch}/{filename}",
306 name = build.name,
307 version = build.version,
308 release = build.release,
309 );
310 retry(DEFAULT_RETRIES, || {
311 let response = reqwest::blocking::get(&url)
312 .map_err(|e| Error::Parse(format!("failed to fetch {url}: {e}")))?;
313 if !response.status().is_success() {
314 return Err(Error::Parse(format!("{url}: HTTP {}", response.status())));
315 }
316 response
317 .text()
318 .map_err(|e| Error::Parse(format!("failed to read {url}: {e}")))
319 })
320 }
321
322 fn packages_url(&self) -> Option<&'static str> {
324 match self.instance.as_str() {
325 "koji.fedoraproject.org" => Some("https://kojipkgs.fedoraproject.org"),
326 "cbs.centos.org" => Some("https://cbs.centos.org/kojifiles"),
327 "kojihub.stream.centos.org" => Some("https://kojihub.stream.centos.org/kojifiles"),
328 _ => None,
329 }
330 }
331
332 fn koji_profile(&self) -> Option<&'static str> {
334 match self.instance.as_str() {
335 "koji.fedoraproject.org" => None,
336 "cbs.centos.org" => Some("cbs"),
337 "kojihub.stream.centos.org" => Some("stream"),
338 _ => None,
339 }
340 }
341}
342
343pub fn find_file_in(dir: &Path, name: &str) -> Option<std::path::PathBuf> {
345 find_file_recursive(dir, name)
346}
347
348fn find_file_recursive(dir: &Path, name: &str) -> Option<std::path::PathBuf> {
349 let entries = std::fs::read_dir(dir).ok()?;
350 for entry in entries.flatten() {
351 let path = entry.path();
352 if path.is_file() && path.file_name().and_then(|n| n.to_str()) == Some(name) {
353 return Some(path);
354 }
355 if path.is_dir()
356 && let Some(found) = find_file_recursive(&path, name)
357 {
358 return Some(found);
359 }
360 }
361 None
362}
363
364fn collect_files(dir: &Path, out: &mut Vec<std::path::PathBuf>) {
366 if let Ok(entries) = std::fs::read_dir(dir) {
367 for entry in entries.flatten() {
368 let path = entry.path();
369 if path.is_file() {
370 out.push(path);
371 } else if path.is_dir() {
372 collect_files(&path, out);
373 }
374 }
375 }
376}