Skip to content

Commit 1281c8f

Browse files
committed
add periodic self update
1 parent 4b4c4bf commit 1281c8f

6 files changed

Lines changed: 55 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coman/.config/config.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
update_check_interval_hours = 24
2+
13
[cscs]
24
# check https://docs.cscs.ch/access/firecrest/#firecrest-deployment-on-alps for possible system and platform combinations
35
current_system = "daint" # what system/cluster to execute commands on

coman/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "coman"
3-
version = "0.8.7"
3+
version = "0.8.8"
44
edition = "2024"
55
description = "Compute Manager for managing HPC compute"
66
authors = ["Ralf Grubenmann <ralf.grubenmann@sdsc.ethz.ch>"]

coman/src/cli/app.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::{error::Error, path::PathBuf, str::FromStr, thread};
22

3+
use chrono::{DateTime, Local, TimeDelta};
34
use clap::{Args, Command, Parser, Subcommand, ValueHint, builder::TypedValueParser};
45
use clap_complete::{ArgValueCompleter, CompletionCandidate, Generator, Shell, generate};
56
use color_eyre::{Report, Result};
67
use itertools::Itertools;
8+
use self_update::cargo_crate_version;
79
use strum::VariantNames;
810
use tokio::sync::mpsc;
911

@@ -568,3 +570,45 @@ fn is_bare_string(value_str: &str) -> bool {
568570
pub fn print_completions<G: Generator>(generator: G, cmd: &mut Command) {
569571
generate(generator, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
570572
}
573+
574+
pub fn update() -> Result<()> {
575+
let status = self_update::backends::github::Update::configure()
576+
.repo_owner("SwissDataScienceCenter")
577+
.repo_name("coman")
578+
.bin_name("coman")
579+
.bin_path_in_archive("coman")
580+
.show_download_progress(true)
581+
.current_version(cargo_crate_version!())
582+
.build()?
583+
.update()?;
584+
if status.updated() {
585+
println!("Successfully updated to version: `{}`", status.version());
586+
} else {
587+
println!("Already up to date at version: `{}`", status.version());
588+
}
589+
Ok(())
590+
}
591+
592+
pub async fn check_update() -> () {
593+
let Ok(config) = Config::new() else {
594+
return;
595+
};
596+
let data_dir = get_data_dir();
597+
let stamp_path = data_dir.join("selfupdate.stamp");
598+
let Ok(update_stamp) = std::fs::read_to_string(&stamp_path) else {
599+
std::fs::write(&stamp_path, Local::now().to_rfc3339()).unwrap();
600+
return;
601+
};
602+
let Ok(update_stamp) = DateTime::parse_from_rfc3339(&update_stamp) else {
603+
return;
604+
};
605+
606+
let now = Local::now();
607+
if now.naive_local() - update_stamp.naive_local()
608+
> TimeDelta::hours(config.values.update_check_interval_hours as i64)
609+
{
610+
println!("checking for updates");
611+
let _ = update();
612+
std::fs::write(&stamp_path, Local::now().to_rfc3339()).unwrap();
613+
}
614+
}

coman/src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub struct ComanConfig {
8383
#[serde(default)]
8484
pub coman_squash_path: Option<PathBuf>,
8585
#[serde(default)]
86+
pub update_check_interval_hours: u64,
87+
#[serde(default)]
8688
pub cscs: CscsConfig,
8789
}
8890

coman/src/main.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clap::{CommandFactory, Parser};
44
use clap_complete::CompleteEnv;
55
use color_eyre::Result;
66
use keyring::set_global_service_name;
7-
use self_update::cargo_crate_version;
87
use tokio::{runtime::Handle, sync::mpsc};
98
use tuirealm::{
109
Application, EventListenerCfg, PollStrategy, Sub, SubClause, SubEventClause, Update,
@@ -22,7 +21,7 @@ use crate::{
2221
cli::{
2322
app::{
2423
Cli, CliCommands, ConfigCommands, CscsCommands, CscsFileCommands, CscsJobCommands, CscsSystemCommands,
25-
get_config, print_completions, set_config, version,
24+
check_update, get_config, print_completions, set_config, update, version,
2625
},
2726
exec::cli_exec_command,
2827
proxy::cli_proxy_command,
@@ -64,6 +63,10 @@ async fn main() -> Result<()> {
6463
set_global_service_name(env!("CARGO_PKG_NAME"));
6564
crate::logging::init()?;
6665
CompleteEnv::with_factory(Cli::command).complete();
66+
67+
// check self-update
68+
check_update().await;
69+
6770
let args = Cli::parse();
6871
match args.command {
6972
Some(command) => match command {
@@ -379,21 +382,3 @@ fn popup_exclusion_clause() -> SubClause<Id> {
379382
SubClause::IsMounted(Id::SystemSelectPopup),
380383
])))
381384
}
382-
383-
fn update() -> Result<()> {
384-
let status = self_update::backends::github::Update::configure()
385-
.repo_owner("SwissDataScienceCenter")
386-
.repo_name("coman")
387-
.bin_name("coman")
388-
.bin_path_in_archive("coman")
389-
.show_download_progress(true)
390-
.current_version(cargo_crate_version!())
391-
.build()?
392-
.update()?;
393-
if status.updated() {
394-
println!("Successfully updated to version: `{}`", status.version());
395-
} else {
396-
println!("Already up to date at version: `{}`", status.version());
397-
}
398-
Ok(())
399-
}

0 commit comments

Comments
 (0)