Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test progress bars #17

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/generate-diff.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ jobs:

- name: Generate Diff
run: |
docker build . -f Dockerfile_AMD64 -t image
docker run \
-t \
--network=host \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(pwd)/main:/base-branch \
-v $(pwd)/pull-request:/target-branch \
-v $(pwd)/output:/output \
-e TARGET_BRANCH=${{ github.head_ref }} \
-e REPO=${{ github.repository }} \
dagandersen/argocd-diff-preview:v0.0.9
image

- name: Post diff as comment
run: |
Expand Down
54 changes: 54 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ schemars = "0.8.19"
structopt = { version = "0.3" }
regex = "1.10.4"
log = "0.4.21"
env_logger = "0.11.3"
env_logger = "0.11.3"
indicatif = "0.17.8"
27 changes: 22 additions & 5 deletions src/argocd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use base64::prelude::*;
use std::{
error::Error,
io::Write,
process::{Command, Stdio},
process::{Command, Stdio}, time::Duration,
};

use log::{debug, error, info};
Expand All @@ -24,7 +24,14 @@ data:
"#;

pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>> {
info!("🦑 Installing Argo CD...");
let progress_bar = indicatif::ProgressBar::new(6);
progress_bar.enable_steady_tick(Duration::from_millis(100));
progress_bar.set_style(
indicatif::ProgressStyle::default_bar()
.template("🦑 [{elapsed_precise}] [{bar:20.cyan/blue}] {pos}/{len} {msg}")?
.progress_chars("##-"),
);
progress_bar.set_message("Installing Argo CD...");

match run_command("kubectl create ns argocd", None).await {
Ok(_) => (),
Expand All @@ -34,6 +41,8 @@ pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>
}
}

progress_bar.inc(1);

// Install Argo CD
let install_url = format!(
"https://raw.githubusercontent.com/argoproj/argo-cd/{}/manifests/install.yaml",
Expand All @@ -46,7 +55,9 @@ pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>
panic!("error: {}", String::from_utf8_lossy(&e.stderr))
}
}
info!("🦑 Waiting for Argo CD to start...");

progress_bar.inc(1);
progress_bar.set_message("Waiting for Argo CD to start...");

// apply argocd-cmd-params-cm
let mut child = Command::new("kubectl")
Expand Down Expand Up @@ -78,7 +89,8 @@ pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>
.await
.expect("failed to wait for argocd-repo-server");

info!("🦑 Logging in to Argo CD through CLI...");
progress_bar.inc(1);
progress_bar.set_message("Logging in to Argo CD...");
debug!("Port-forwarding Argo CD server...");

// port-forward Argo CD server
Expand Down Expand Up @@ -107,6 +119,8 @@ pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>
}
};

progress_bar.inc(1);

let password_decoded_vec = BASE64_STANDARD
.decode(password_encoded.stdout)
.expect("failed to decode password");
Expand Down Expand Up @@ -134,10 +148,13 @@ pub async fn install_argo_cd(version: Option<&str>) -> Result<(), Box<dyn Error>
.await
.expect("failed to login to argocd");

progress_bar.inc(1);

run_command("argocd app list", None)
.await
.expect("Failed to run: argocd app list");

info!("🦑 Argo CD installed successfully");
progress_bar.inc(1);
progress_bar.finish_with_message("🦑 Argo CD installed successfully");
Ok(())
}
27 changes: 19 additions & 8 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ pub async fn get_resources(
}
}

let multi_progress = indicatif::MultiProgress::new();
let progress_bar = multi_progress.add(indicatif::ProgressBar::new(100));
let spinner_with_status = multi_progress.add(indicatif::ProgressBar::new_spinner());

// style including, emoji, progress bar, x out of y, and time left
progress_bar.set_style(
indicatif::ProgressStyle::default_bar()
.template("💦 [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}")
.expect("Failed to set style")
.progress_chars("##-"),
);

let mut set_of_processed_apps = HashSet::new();
let mut set_of_failed_apps = BTreeMap::new();

Expand All @@ -59,6 +71,9 @@ pub async fn get_resources(
break;
}

progress_bar.set_length(items.len() as u64);
progress_bar.set_position(set_of_processed_apps.len() as u64);

if items.len() == set_of_processed_apps.len() {
break;
}
Expand All @@ -70,9 +85,11 @@ pub async fn get_resources(

for item in items {
let name = item["metadata"]["name"].as_str().unwrap();
spinner_with_status.set_message(format!("Timeout in {} seconds, Processing application: {}", timeout - start_time.elapsed().as_secs(), name));
if set_of_processed_apps.contains(name) {
continue;
}

match item["status"]["sync"]["status"].as_str() {
Some("OutOfSync") | Some("Synced") => {
debug!("Getting manifests for application: {}", name);
Expand All @@ -87,6 +104,7 @@ pub async fn get_resources(
Err(e) => error!("error: {}", String::from_utf8_lossy(&e.stderr)),
}
set_of_processed_apps.insert(name.to_string().clone());
progress_bar.set_position(set_of_processed_apps.len() as u64);
continue;
}
Some("Unknown") => {
Expand Down Expand Up @@ -171,14 +189,7 @@ pub async fn get_resources(
}
}

if apps_left > 0 {
info!(
"⏳ Waiting for {} out of {} applications to become 'OutOfSync'. Retrying in 5 seconds. Timeout in {} seconds...",
apps_left,
items.len(),
timeout - time_elapsed
);
}
spinner_with_status.set_message(format!("Timeout in {} seconds, Waiting for applications to sync...", timeout - start_time.elapsed().as_secs()));

tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
}
Expand Down
Loading