Merge pull request 'Add wait-for-sbot to peach-config' (#131) from wait-for-sbot into main
Reviewed-on: #131
This commit is contained in:
commit
c83a22461d
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -2200,7 +2200,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "peach-config"
|
name = "peach-config"
|
||||||
version = "0.1.26"
|
version = "0.1.27"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-std",
|
"async-std",
|
||||||
"clap",
|
"clap",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "peach-config"
|
name = "peach-config"
|
||||||
version = "0.1.26"
|
version = "0.1.27"
|
||||||
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
|
authors = ["Andrew Reid <gnomad@cryptolab.net>", "Max Fowler <max@mfowler.info>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Command line tool for installing, updating and configuring PeachCloud"
|
description = "Command line tool for installing, updating and configuring PeachCloud"
|
||||||
|
@ -42,6 +42,8 @@ pub enum PeachConfigError {
|
|||||||
Golgi { source: GolgiError },
|
Golgi { source: GolgiError },
|
||||||
#[snafu(display("{}", message))]
|
#[snafu(display("{}", message))]
|
||||||
CmdInputError { message: String },
|
CmdInputError { message: String },
|
||||||
|
#[snafu(display("{}", message))]
|
||||||
|
WaitForSbotError { message: String },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<std::io::Error> for PeachConfigError {
|
impl From<std::io::Error> for PeachConfigError {
|
||||||
|
@ -10,6 +10,7 @@ mod setup_peach_deb;
|
|||||||
mod status;
|
mod status;
|
||||||
mod update;
|
mod update;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod wait_for_sbot;
|
||||||
|
|
||||||
use clap::arg_enum;
|
use clap::arg_enum;
|
||||||
use log::error;
|
use log::error;
|
||||||
@ -61,6 +62,10 @@ enum PeachConfig {
|
|||||||
/// It takes an address argument of the form host:port
|
/// It takes an address argument of the form host:port
|
||||||
#[structopt(name = "publish-address")]
|
#[structopt(name = "publish-address")]
|
||||||
PublishAddress(PublishAddressOpts),
|
PublishAddress(PublishAddressOpts),
|
||||||
|
|
||||||
|
/// Wait for a successful connection to sbot
|
||||||
|
#[structopt(name = "wait-for-sbot")]
|
||||||
|
WaitForSbot,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(StructOpt, Debug)]
|
#[derive(StructOpt, Debug)]
|
||||||
@ -193,6 +198,14 @@ async fn run() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
PeachConfig::WaitForSbot => match wait_for_sbot::wait_for_sbot().await {
|
||||||
|
Ok(sbot_id) => {
|
||||||
|
println!("connected with sbot and found sbot_id: {:?}", sbot_id)
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!("peach-config did not successfully connect to sbot: {}", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
52
peach-config/src/wait_for_sbot.rs
Normal file
52
peach-config/src/wait_for_sbot.rs
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
use std::{thread, time};
|
||||||
|
|
||||||
|
use crate::error::PeachConfigError;
|
||||||
|
use peach_lib::sbot::init_sbot;
|
||||||
|
|
||||||
|
static MAX_NUM_ATTEMPTS: u8 = 10;
|
||||||
|
|
||||||
|
/// Utility function to wait for a successful whoami call with sbot
|
||||||
|
/// After each attempt to call whoami it waits 2 seconds,
|
||||||
|
/// and if after MAX_NUM_ATTEMPTS (10) there is no successful whoami call
|
||||||
|
/// it returns an Error. Otherwise it returns Ok(sbot_id).
|
||||||
|
pub async fn wait_for_sbot() -> Result<String, PeachConfigError> {
|
||||||
|
let mut num_attempts = 0;
|
||||||
|
let mut whoami = None;
|
||||||
|
|
||||||
|
while num_attempts < MAX_NUM_ATTEMPTS {
|
||||||
|
let mut sbot = None;
|
||||||
|
|
||||||
|
let sbot_res = init_sbot().await;
|
||||||
|
match sbot_res {
|
||||||
|
Ok(sbot_instance) => {
|
||||||
|
sbot = Some(sbot_instance);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("failed to connect to sbot: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sbot.is_some() {
|
||||||
|
let sbot_id_res = sbot.unwrap().whoami().await;
|
||||||
|
match sbot_id_res {
|
||||||
|
Ok(sbot_id) => {
|
||||||
|
whoami = Some(sbot_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("whoami failed: {:?}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("trying to connect to sbot again {:?}", num_attempts);
|
||||||
|
num_attempts += 1;
|
||||||
|
|
||||||
|
let sleep_duration = time::Duration::from_secs(2);
|
||||||
|
thread::sleep(sleep_duration);
|
||||||
|
}
|
||||||
|
|
||||||
|
whoami.ok_or(PeachConfigError::WaitForSbotError {
|
||||||
|
message: "Failed to find sbot_id after 10 attempts".to_string(),
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user