From 6318670b6ca05dbeef14db9a6aa7f589557b3b46 Mon Sep 17 00:00:00 2001 From: mbecker20 Date: Fri, 3 May 2024 03:50:50 -0700 Subject: [PATCH] implement core pass tokens --- bin/core/src/api/execute/build.rs | 2 ++ bin/core/src/api/execute/deployment.rs | 1 + bin/core/src/api/execute/repo.rs | 1 + bin/periphery/src/api/build.rs | 28 +++++------------ bin/periphery/src/api/container.rs | 30 +++++++++---------- bin/periphery/src/api/git.rs | 4 +-- bin/periphery/src/helpers/docker/build.rs | 19 ++++++++++-- bin/periphery/src/helpers/docker/container.rs | 19 +++++++----- bin/periphery/src/helpers/git.rs | 12 ++++++-- client/periphery/rs/src/api/build.rs | 2 ++ client/periphery/rs/src/api/container.rs | 2 ++ client/periphery/rs/src/api/git.rs | 2 ++ 12 files changed, 72 insertions(+), 50 deletions(-) diff --git a/bin/core/src/api/execute/build.rs b/bin/core/src/api/execute/build.rs index 3ebf6da4f..916af31e8 100644 --- a/bin/core/src/api/execute/build.rs +++ b/bin/core/src/api/execute/build.rs @@ -147,6 +147,7 @@ impl Resolve for State { res = periphery .request(api::git::CloneRepo { args: (&build).into(), + github_token: None, }) => res, _ = cancel.cancelled() => { info!("build cancelled during clone, cleaning up builder"); @@ -178,6 +179,7 @@ impl Resolve for State { res = periphery .request(api::build::Build { build: build.clone(), + docker_token: None, }) => res.context("failed at call to periphery to build"), _ = cancel.cancelled() => { info!("build cancelled during build, cleaning up builder"); diff --git a/bin/core/src/api/execute/deployment.rs b/bin/core/src/api/execute/deployment.rs index c6406684d..98cfbdb8d 100644 --- a/bin/core/src/api/execute/deployment.rs +++ b/bin/core/src/api/execute/deployment.rs @@ -107,6 +107,7 @@ impl Resolve for State { deployment, stop_signal, stop_time, + docker_token: None, }) .await { diff --git a/bin/core/src/api/execute/repo.rs b/bin/core/src/api/execute/repo.rs index d297435b9..46d2c18c6 100644 --- a/bin/core/src/api/execute/repo.rs +++ b/bin/core/src/api/execute/repo.rs @@ -77,6 +77,7 @@ impl Resolve for State { let logs = match periphery .request(api::git::CloneRepo { args: (&repo).into(), + github_token: None, }) .await { diff --git a/bin/periphery/src/api/build.rs b/bin/periphery/src/api/build.rs index c194364a5..87a4030f9 100644 --- a/bin/periphery/src/api/build.rs +++ b/bin/periphery/src/api/build.rs @@ -1,18 +1,14 @@ use async_trait::async_trait; use monitor_client::entities::{ - optional_string, server::docker_image::ImageSummary, update::Log, + server::docker_image::ImageSummary, update::Log, }; use periphery_client::api::build::{ Build, GetImageList, PruneImages, }; use resolver_api::Resolve; -use serror::serialize_error_pretty; use crate::{ - helpers::{ - docker::{self, client::docker_client}, - get_docker_token, - }, + helpers::docker::{self, client::docker_client}, State, }; @@ -21,23 +17,13 @@ impl Resolve for State { #[instrument(name = "Build", skip(self))] async fn resolve( &self, - Build { build }: Build, + Build { + build, + docker_token, + }: Build, _: (), ) -> anyhow::Result> { - let log = match get_docker_token(&optional_string( - &build.config.docker_account, - )) { - Ok(docker_token) => { - match docker::build::build(&build, docker_token).await { - Ok(logs) => logs, - Err(e) => { - vec![Log::error("build", serialize_error_pretty(&e))] - } - } - } - Err(e) => vec![Log::error("build", serialize_error_pretty(&e))], - }; - Ok(log) + docker::build::build(&build, docker_token).await } } diff --git a/bin/periphery/src/api/container.rs b/bin/periphery/src/api/container.rs index 165f248ea..1803a794b 100644 --- a/bin/periphery/src/api/container.rs +++ b/bin/periphery/src/api/container.rs @@ -1,4 +1,4 @@ -use anyhow::{anyhow, Context}; +use anyhow::anyhow; use monitor_client::entities::{ deployment::{ContainerSummary, DockerContainerStats}, update::Log, @@ -203,25 +203,23 @@ impl Resolve for State { &self, Deploy { deployment, + docker_token, stop_signal, stop_time, }: Deploy, _: (), ) -> anyhow::Result { - let log = tokio::spawn(async move { - docker::container::deploy( - &deployment, - stop_signal - .unwrap_or(deployment.config.termination_signal) - .into(), - stop_time - .unwrap_or(deployment.config.termination_timeout) - .into(), - ) - .await - }) - .await - .context("failed at spawn thread for deploy")?; - Ok(log) + let res = docker::container::deploy( + &deployment, + stop_signal + .unwrap_or(deployment.config.termination_signal) + .into(), + stop_time + .unwrap_or(deployment.config.termination_timeout) + .into(), + docker_token, + ) + .await; + Ok(res) } } diff --git a/bin/periphery/src/api/git.rs b/bin/periphery/src/api/git.rs index 4ee2842e3..9336e8a30 100644 --- a/bin/periphery/src/api/git.rs +++ b/bin/periphery/src/api/git.rs @@ -9,10 +9,10 @@ impl Resolve for State { #[instrument(name = "CloneRepo", skip(self))] async fn resolve( &self, - CloneRepo { args }: CloneRepo, + CloneRepo { args, github_token }: CloneRepo, _: (), ) -> anyhow::Result> { - git::clone(args).await + git::clone(args, github_token).await } } diff --git a/bin/periphery/src/helpers/docker/build.rs b/bin/periphery/src/helpers/docker/build.rs index 321e2a12e..32c7bbb25 100644 --- a/bin/periphery/src/helpers/docker/build.rs +++ b/bin/periphery/src/helpers/docker/build.rs @@ -5,8 +5,12 @@ use monitor_client::entities::{ update::Log, EnvironmentVar, Version, }; +use serror::serialize_error_pretty; -use crate::{config::periphery_config, helpers::run_monitor_command}; +use crate::{ + config::periphery_config, + helpers::{get_docker_token, run_monitor_command}, +}; use super::{docker_login, parse_extra_args, parse_labels}; @@ -16,7 +20,7 @@ pub async fn prune_images() -> Log { run_monitor_command("prune images", command).await } -#[instrument] +#[instrument(skip(docker_token))] pub async fn build( Build { name, @@ -39,6 +43,17 @@ pub async fn build( docker_token: Option, ) -> anyhow::Result> { let mut logs = Vec::new(); + let docker_token = match ( + docker_token, + get_docker_token(&optional_string(docker_account)), + ) { + (Some(docker_token), _) => Some(docker_token), + (None, Ok(docker_token)) => docker_token, + (None, Err(e)) => { + logs.push(Log::error("build", serialize_error_pretty(&e))); + return Ok(logs); + } + }; let name = to_monitor_name(name); let using_account = docker_login(&optional_string(docker_account), &docker_token) diff --git a/bin/periphery/src/helpers/docker/container.rs b/bin/periphery/src/helpers/docker/container.rs index 0bbc82c38..484d1e3fb 100644 --- a/bin/periphery/src/helpers/docker/container.rs +++ b/bin/periphery/src/helpers/docker/container.rs @@ -178,21 +178,26 @@ async fn pull_image(image: &str) -> Log { run_monitor_command("docker pull", command).await } -#[instrument] +#[instrument(skip(docker_token))] pub async fn deploy( deployment: &Deployment, stop_signal: Option, stop_time: Option, + docker_token: Option, ) -> Log { - let docker_token = match get_docker_token(&optional_string( - &deployment.config.docker_account, - )) { - Ok(token) => token, - Err(e) => { + let docker_token = match ( + docker_token, + get_docker_token(&optional_string( + &deployment.config.docker_account, + )), + ) { + (Some(token), _) => Some(token), + (None, Ok(token)) => token, + (None, Err(e)) => { return Log::error("docker login", serialize_error_pretty(&e)) } }; - + if let Err(e) = docker_login( &optional_string(&deployment.config.docker_account), &docker_token, diff --git a/bin/periphery/src/helpers/git.rs b/bin/periphery/src/helpers/git.rs index 9a46f9805..13ea68437 100644 --- a/bin/periphery/src/helpers/git.rs +++ b/bin/periphery/src/helpers/git.rs @@ -55,7 +55,10 @@ pub async fn pull( } #[instrument] -pub async fn clone(clone_args: T) -> anyhow::Result> +pub async fn clone( + clone_args: T, + github_token: Option, +) -> anyhow::Result> where T: Into + std::fmt::Debug, { @@ -68,7 +71,12 @@ where github_account, } = clone_args.into(); - let access_token = get_github_token(&github_account)?; + let access_token = + match (github_token, get_github_token(&github_account)) { + (Some(token), _) => Some(token), + (None, Ok(token)) => token, + (None, Err(e)) => return Err(e), + }; let repo = repo.as_ref().context("build has no repo attached")?; let name = to_monitor_name(&name); diff --git a/client/periphery/rs/src/api/build.rs b/client/periphery/rs/src/api/build.rs index 9f0686e1e..f8db3c8e6 100644 --- a/client/periphery/rs/src/api/build.rs +++ b/client/periphery/rs/src/api/build.rs @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize}; #[response(BuildResponse)] pub struct Build { pub build: monitor_client::entities::build::Build, + /// Override docker token with one sent from core. + pub docker_token: Option, } pub type BuildResponse = Vec; diff --git a/client/periphery/rs/src/api/container.rs b/client/periphery/rs/src/api/container.rs index c0c176eff..ae552013e 100644 --- a/client/periphery/rs/src/api/container.rs +++ b/client/periphery/rs/src/api/container.rs @@ -101,6 +101,8 @@ pub struct PruneContainers {} #[response(Log)] pub struct Deploy { pub deployment: Deployment, + /// Override docker token with one sent from core. + pub docker_token: Option, pub stop_signal: Option, pub stop_time: Option, } diff --git a/client/periphery/rs/src/api/git.rs b/client/periphery/rs/src/api/git.rs index e5416b6ce..fa0e24f9e 100644 --- a/client/periphery/rs/src/api/git.rs +++ b/client/periphery/rs/src/api/git.rs @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize}; #[response(Vec)] pub struct CloneRepo { pub args: CloneArgs, + /// Override github token with one sent from core. + pub github_token: Option, } //