| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | use std::path::Path; |
| 7 | use std::time::Duration; |
| 8 | |
| 9 | use forge_ci::proto::{ |
| 10 | ClaimRequest, FinishRequest, HeartbeatResponse, JobAssignment, RegisterRequest, |
| 11 | RegisterResponse, |
| 12 | }; |
| 13 | use reqwest::StatusCode; |
| 14 | use reqwest::header::CONTENT_LENGTH; |
| 15 | use serde::de::DeserializeOwned; |
| 16 | |
| 17 | |
| 18 | pub const CLAIM_TIMEOUT: Duration = Duration::from_secs(35); |
| 19 | const REQUEST_TIMEOUT: Duration = Duration::from_secs(60); |
| 20 | const LOG_TIMEOUT: Duration = Duration::from_secs(60); |
| 21 | const ARTIFACT_TIMEOUT: Duration = Duration::from_secs(60 * 60); |
| 22 | const HEADER_LOG_OFFSET: &str = "X-Forge-Log-Offset"; |
| 23 | const HEADER_LOG_SIZE: &str = "X-Forge-Log-Size"; |
| 24 | |
| 25 | |
| 26 | #[derive(Debug, thiserror::Error)] |
| 27 | pub enum ClientError { |
| 28 | #[error("http: {0}")] |
| 29 | Http(#[from] reqwest::Error), |
| 30 | #[error("forge answered {status}: {body}")] |
| 31 | Status { status: StatusCode, body: String }, |
| 32 | #[error("local io: {0}")] |
| 33 | Io(#[from] std::io::Error), |
| 34 | #[error("protocol: {0}")] |
| 35 | Protocol(String), |
| 36 | } |
| 37 | |
| 38 | |
| 39 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 40 | pub enum LogAck { |
| 41 | |
| 42 | Accepted, |
| 43 | |
| 44 | Resync(u64), |
| 45 | } |
| 46 | |
| 47 | |
| 48 | #[derive(Clone, Debug)] |
| 49 | pub struct ForgeClient { |
| 50 | http: reqwest::Client, |
| 51 | base: String, |
| 52 | } |
| 53 | |
| 54 | impl ForgeClient { |
| 55 | |
| 56 | pub fn new(base_url: &str) -> Result<Self, ClientError> { |
| 57 | let http = reqwest::Client::builder() |
| 58 | .user_agent(concat!("forge-runner/", env!("CARGO_PKG_VERSION"))) |
| 59 | .connect_timeout(Duration::from_secs(10)) |
| 60 | .build()?; |
| 61 | Ok(Self { |
| 62 | http, |
| 63 | base: base_url.trim_end_matches('/').to_owned(), |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | fn url(&self, path: &str) -> String { |
| 68 | format!("{}{}", self.base, path) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | pub async fn register(&self, req: &RegisterRequest) -> Result<RegisterResponse, ClientError> { |
| 73 | let resp = self |
| 74 | .http |
| 75 | .post(self.url("/api/v1/ci/runners/register")) |
| 76 | .json(req) |
| 77 | .timeout(REQUEST_TIMEOUT) |
| 78 | .send() |
| 79 | .await?; |
| 80 | json_or_error(resp).await |
| 81 | } |
| 82 | |
| 83 | |
| 84 | pub async fn claim( |
| 85 | &self, |
| 86 | runner_secret: &str, |
| 87 | labels: &[String], |
| 88 | ) -> Result<Option<JobAssignment>, ClientError> { |
| 89 | let req = ClaimRequest { |
| 90 | labels: labels.to_vec(), |
| 91 | }; |
| 92 | let resp = self |
| 93 | .http |
| 94 | .post(self.url("/api/v1/ci/runners/claim")) |
| 95 | .bearer_auth(runner_secret) |
| 96 | .json(&req) |
| 97 | .timeout(CLAIM_TIMEOUT) |
| 98 | .send() |
| 99 | .await?; |
| 100 | match resp.status() { |
| 101 | StatusCode::NO_CONTENT => Ok(None), |
| 102 | status if status.is_success() => Ok(Some(resp.json().await?)), |
| 103 | _ => Err(status_error(resp).await), |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | |
| 108 | pub async fn post_logs( |
| 109 | &self, |
| 110 | job_id: &str, |
| 111 | job_token: &str, |
| 112 | offset: u64, |
| 113 | body: Vec<u8>, |
| 114 | ) -> Result<LogAck, ClientError> { |
| 115 | let resp = self |
| 116 | .http |
| 117 | .post(self.url(&format!("/api/v1/ci/jobs/{job_id}/logs"))) |
| 118 | .bearer_auth(job_token) |
| 119 | .header(HEADER_LOG_OFFSET, offset.to_string()) |
| 120 | .header(reqwest::header::CONTENT_TYPE, "text/plain; charset=utf-8") |
| 121 | .body(body) |
| 122 | .timeout(LOG_TIMEOUT) |
| 123 | .send() |
| 124 | .await?; |
| 125 | match resp.status() { |
| 126 | StatusCode::CONFLICT => { |
| 127 | let size = resp |
| 128 | .headers() |
| 129 | .get(HEADER_LOG_SIZE) |
| 130 | .and_then(|v| v.to_str().ok()) |
| 131 | .and_then(|v| v.parse::<u64>().ok()) |
| 132 | .ok_or_else(|| { |
| 133 | ClientError::Protocol("409 without a valid X-Forge-Log-Size".into()) |
| 134 | })?; |
| 135 | Ok(LogAck::Resync(size)) |
| 136 | } |
| 137 | status if status.is_success() => Ok(LogAck::Accepted), |
| 138 | _ => Err(status_error(resp).await), |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | |
| 143 | pub async fn heartbeat( |
| 144 | &self, |
| 145 | job_id: &str, |
| 146 | job_token: &str, |
| 147 | ) -> Result<HeartbeatResponse, ClientError> { |
| 148 | let resp = self |
| 149 | .http |
| 150 | .post(self.url(&format!("/api/v1/ci/jobs/{job_id}/heartbeat"))) |
| 151 | .bearer_auth(job_token) |
| 152 | .timeout(REQUEST_TIMEOUT) |
| 153 | .send() |
| 154 | .await?; |
| 155 | json_or_error(resp).await |
| 156 | } |
| 157 | |
| 158 | |
| 159 | pub async fn upload_artifact( |
| 160 | &self, |
| 161 | job_id: &str, |
| 162 | job_token: &str, |
| 163 | name: &str, |
| 164 | path: &Path, |
| 165 | ) -> Result<(), ClientError> { |
| 166 | let file = tokio::fs::File::open(path).await?; |
| 167 | let len = file.metadata().await?.len(); |
| 168 | let stream = tokio_util::io::ReaderStream::new(file); |
| 169 | let resp = self |
| 170 | .http |
| 171 | .put(self.url(&format!("/api/v1/ci/jobs/{job_id}/artifacts/{name}"))) |
| 172 | .bearer_auth(job_token) |
| 173 | .header(CONTENT_LENGTH, len) |
| 174 | .body(reqwest::Body::wrap_stream(stream)) |
| 175 | .timeout(ARTIFACT_TIMEOUT) |
| 176 | .send() |
| 177 | .await?; |
| 178 | if resp.status().is_success() { |
| 179 | Ok(()) |
| 180 | } else { |
| 181 | Err(status_error(resp).await) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | pub async fn finish( |
| 187 | &self, |
| 188 | job_id: &str, |
| 189 | job_token: &str, |
| 190 | req: &FinishRequest, |
| 191 | ) -> Result<(), ClientError> { |
| 192 | let resp = self |
| 193 | .http |
| 194 | .post(self.url(&format!("/api/v1/ci/jobs/{job_id}/finish"))) |
| 195 | .bearer_auth(job_token) |
| 196 | .json(req) |
| 197 | .timeout(REQUEST_TIMEOUT) |
| 198 | .send() |
| 199 | .await?; |
| 200 | if resp.status().is_success() { |
| 201 | Ok(()) |
| 202 | } else { |
| 203 | Err(status_error(resp).await) |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | async fn json_or_error<T: DeserializeOwned>(resp: reqwest::Response) -> Result<T, ClientError> { |
| 209 | if resp.status().is_success() { |
| 210 | Ok(resp.json().await?) |
| 211 | } else { |
| 212 | Err(status_error(resp).await) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | async fn status_error(resp: reqwest::Response) -> ClientError { |
| 217 | let status = resp.status(); |
| 218 | let body: String = resp |
| 219 | .text() |
| 220 | .await |
| 221 | .unwrap_or_default() |
| 222 | .chars() |
| 223 | .take(512) |
| 224 | .collect(); |
| 225 | ClientError::Status { status, body } |
| 226 | } |