From patchwork Fri Feb 1 06:17:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Donnellan X-Patchwork-Id: 1034601 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [203.11.71.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43rRjW3pd1z9sDX for ; Fri, 1 Feb 2019 17:17:51 +1100 (AEDT) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 43rRjW2nDyzDqg9 for ; Fri, 1 Feb 2019 17:17:51 +1100 (AEDT) X-Original-To: snowpatch@lists.ozlabs.org Delivered-To: snowpatch@lists.ozlabs.org Authentication-Results: lists.ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=au1.ibm.com (client-ip=148.163.156.1; helo=mx0a-001b2d01.pphosted.com; envelope-from=andrew.donnellan@au1.ibm.com; receiver=) Authentication-Results: lists.ozlabs.org; dmarc=none (p=none dis=none) header.from=au1.ibm.com Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 43rRjB4jzYzDqfK for ; Fri, 1 Feb 2019 17:17:34 +1100 (AEDT) Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x1166eti125001 for ; Fri, 1 Feb 2019 01:17:32 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2qcgangwe3-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 01 Feb 2019 01:17:32 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Fri, 1 Feb 2019 06:17:30 -0000 Received: from b06cxnps4074.portsmouth.uk.ibm.com (9.149.109.196) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Fri, 1 Feb 2019 06:17:27 -0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x116HQUq4325722 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Fri, 1 Feb 2019 06:17:26 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AB318A4065 for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0FAEFA405F for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from ozlabs.au.ibm.com (unknown [9.192.253.14]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Fri, 1 Feb 2019 06:17:26 +0000 (GMT) Received: from intelligence.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id E0165A0274 for ; Fri, 1 Feb 2019 17:17:24 +1100 (AEDT) From: Andrew Donnellan To: snowpatch@lists.ozlabs.org Date: Fri, 1 Feb 2019 17:17:14 +1100 X-Mailer: git-send-email 2.11.0 In-Reply-To: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> References: <20190201061714.7110-1-andrew.donnellan@au1.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 19020106-4275-0000-0000-00000308B34D X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020106-4276-0000-0000-00003816BFF0 Message-Id: <20190201061714.7110-5-andrew.donnellan@au1.ibm.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2019-02-01_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1902010044 Subject: [snowpatch] [PATCH 5/5] ci: Move externally-visible prototypes from JenkinsBackend to CIBackend X-BeenThere: snowpatch@lists.ozlabs.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Continuous Integration for patch-based workflows List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: snowpatch-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "snowpatch" Move the functions we actually use from the main code from the JenkinsBackend impl into the CIBackend trait. Signed-off-by: Andrew Donnellan --- src/ci.rs | 11 +++++ src/jenkins.rs | 136 ++++++++++++++++++++++++++++----------------------------- 2 files changed, 79 insertions(+), 68 deletions(-) diff --git a/src/ci.rs b/src/ci.rs index 7d21edb7662f..82af1594f2c7 100644 --- a/src/ci.rs +++ b/src/ci.rs @@ -14,6 +14,11 @@ // ci.rs - CI backend interface definitions // +use std::collections::BTreeMap; +use std::error::Error; + +use patchwork::TestState; + #[derive(Eq, PartialEq)] pub enum BuildStatus { Running, @@ -23,4 +28,10 @@ pub enum BuildStatus { pub trait CIBackend { fn start_test(&self, job_name: &str, params: Vec<(&str, &str)>) -> Result; + fn get_build_handle(&self, build_queue_entry: &str) -> Result>; + fn get_build_result(&self, build_handle: &str) -> Result>; + fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String; + fn get_description(&self, build_handle: &str, job: &BTreeMap) + -> Option; + fn wait_build(&self, build_handle: &str) -> Result>; } diff --git a/src/jenkins.rs b/src/jenkins.rs index 4738fca7a069..91ee2eafddf6 100644 --- a/src/jenkins.rs +++ b/src/jenkins.rs @@ -79,63 +79,8 @@ impl CIBackend for JenkinsBackend { None => Err("No Location header returned"), } } -} - -impl JenkinsBackend { - fn headers(&self) -> HeaderMap { - let mut headers = HeaderMap::new(); - if let Some(ref username) = self.username { - if let Some(ref token) = self.token { - headers.insert( - AUTHORIZATION, - format!( - "Basic {}", - base64::encode(&format!("{}:{}", username, token)) - ) - .parse() - .unwrap(), - ); - } - }; - headers - } - - fn get_url(&self, url: U) -> Result { - self.reqwest_client.get(url).headers(self.headers()).send() - } - fn post_url(&self, url: U) -> Result { - self.reqwest_client.post(url).headers(self.headers()).send() - } - - fn get_api_json_object(&self, base_url: &str) -> Result> { - let url = format!("{}api/json", base_url); - let mut result_str = String::new(); - loop { - let mut resp = match self.get_url(&url) { - Ok(r) => r, - Err(e) => { - // TODO: We have to time out rather than spinning indefinitely - warn!("Couldn't hit Jenkins API: {}", e); - sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); - continue; - } - }; - - if resp.status().is_server_error() { - // TODO: Timeout - sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); - continue; - } - resp.read_to_string(&mut result_str) - .map_err(|e| format!("Couldn't read from server: {}", e))?; - break; - } - serde_json::from_str(&result_str) - .map_err(|e| format!("Couldn't parse JSON from Jenkins: {}", e).into()) - } - - pub fn get_build_handle(&self, build_queue_entry: &str) -> Result> { + fn get_build_handle(&self, build_queue_entry: &str) -> Result> { loop { let entry = self.get_api_json_object(build_queue_entry)?; match entry.get("executable") { @@ -156,15 +101,7 @@ impl JenkinsBackend { } } - pub fn get_build_status(&self, build_handle: &str) -> Result> { - match self.get_api_json_object(build_handle)?["building"].as_bool() { - Some(true) => Ok(BuildStatus::Running), - Some(false) => Ok(BuildStatus::Done), - None => Err("Error getting build status".into()), - } - } - - pub fn get_build_result(&self, build_handle: &str) -> Result> { + fn get_build_result(&self, build_handle: &str) -> Result> { match self .get_api_json_object(build_handle)? .get("result") @@ -181,7 +118,7 @@ impl JenkinsBackend { } } - pub fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String { + fn get_results_url(&self, build_handle: &str, job: &BTreeMap) -> String { let default_url = format!("{}/", build_handle); match job.get("artifact") { Some(artifact) => { @@ -198,7 +135,7 @@ impl JenkinsBackend { } } - pub fn get_description( + fn get_description( &self, build_handle: &str, job: &BTreeMap, @@ -220,7 +157,7 @@ impl JenkinsBackend { } } - pub fn wait_build(&self, build_handle: &str) -> Result> { + fn wait_build(&self, build_handle: &str) -> Result> { // TODO: Implement a timeout? while self.get_build_status(build_handle)? != BuildStatus::Done { sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); @@ -228,3 +165,66 @@ impl JenkinsBackend { Ok(BuildStatus::Done) } } + +impl JenkinsBackend { + fn headers(&self) -> HeaderMap { + let mut headers = HeaderMap::new(); + if let Some(ref username) = self.username { + if let Some(ref token) = self.token { + headers.insert( + AUTHORIZATION, + format!( + "Basic {}", + base64::encode(&format!("{}:{}", username, token)) + ) + .parse() + .unwrap(), + ); + } + }; + headers + } + + fn get_url(&self, url: U) -> Result { + self.reqwest_client.get(url).headers(self.headers()).send() + } + + fn post_url(&self, url: U) -> Result { + self.reqwest_client.post(url).headers(self.headers()).send() + } + + fn get_api_json_object(&self, base_url: &str) -> Result> { + let url = format!("{}api/json", base_url); + let mut result_str = String::new(); + loop { + let mut resp = match self.get_url(&url) { + Ok(r) => r, + Err(e) => { + // TODO: We have to time out rather than spinning indefinitely + warn!("Couldn't hit Jenkins API: {}", e); + sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); + continue; + } + }; + + if resp.status().is_server_error() { + // TODO: Timeout + sleep(Duration::from_millis(JENKINS_POLLING_INTERVAL)); + continue; + } + resp.read_to_string(&mut result_str) + .map_err(|e| format!("Couldn't read from server: {}", e))?; + break; + } + serde_json::from_str(&result_str) + .map_err(|e| format!("Couldn't parse JSON from Jenkins: {}", e).into()) + } + + pub fn get_build_status(&self, build_handle: &str) -> Result> { + match self.get_api_json_object(build_handle)?["building"].as_bool() { + Some(true) => Ok(BuildStatus::Running), + Some(false) => Ok(BuildStatus::Done), + None => Err("Error getting build status".into()), + } + } +}