Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions coman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ tera = "1.20.1"
inquire = "0.9.1"
oci-distribution = "0.11.0"
docker_credential = "1.3.2"
chrono = "0.4.42"

[build-dependencies]
anyhow = "1.0.90"
Expand Down
2 changes: 2 additions & 0 deletions coman/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub enum CscsJobCommands {
#[clap(short, long, trailing_var_arg = true)]
command: Option<Vec<String>>,
},
#[clap(alias("c"))]
Cancel { job_id: i64 },
}
#[derive(Subcommand, Debug)]
pub enum CscsSystemCommands {
Expand Down
75 changes: 58 additions & 17 deletions coman/src/cscs/api_client.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use chrono::prelude::*;
use color_eyre::eyre::{Context, Result};
use firecrest_client::{
client::FirecrestClient,
compute_api::{
get_compute_system_job, get_compute_system_job_metadata, get_compute_system_jobs,
post_compute_system_job,
cancel_compute_system_job, get_compute_system_job, get_compute_system_job_metadata,
get_compute_system_jobs, post_compute_system_job,
},
filesystem_api::{
post_filesystem_ops_mkdir, post_filesystem_ops_upload, put_filesystem_ops_chmod,
Expand Down Expand Up @@ -80,27 +81,46 @@ pub enum JobStatus {
Cancelled,
Failed,
}
impl From<String> for JobStatus {
fn from(value: String) -> Self {
match value.as_str() {
"RUNNING" => JobStatus::Running,
"FAILED" => JobStatus::Failed,
"COMPLETED" => JobStatus::Finished,
"CANCELLED" => JobStatus::Cancelled,
"PENDING" => JobStatus::Pending,
other => panic!("got job status: {}", other),
}
}
}
#[derive(Debug, Eq, Clone, PartialEq, PartialOrd, Ord, tabled::Tabled)]
pub struct Job {
pub id: usize,
pub name: String,
pub status: JobStatus,
pub user: String,
#[tabled(display("display_option_datetime"))]
pub start_date: Option<DateTime<Local>>,
#[tabled(display("display_option_datetime"))]
pub end_date: Option<DateTime<Local>>,
}
impl From<JobModelOutput> for Job {
fn from(value: JobModelOutput) -> Self {
Self {
id: value.job_id as usize,
name: value.name,
status: match value.status.state.as_str() {
"RUNNING" => JobStatus::Running,
"FAILED" => JobStatus::Failed,
"COMPLETED" => JobStatus::Finished,
"CANCELLED" => JobStatus::Cancelled,
"PENDING" => JobStatus::Pending,
other => panic!("got job status: {}", other),
},
status: value.status.state.into(),
user: value.user.unwrap_or("".to_string()),
start_date: value.time.start.map(|s| {
DateTime::from_timestamp_secs(s)
.unwrap()
.with_timezone(&Local)
}),
end_date: value.time.end.map(|e| {
DateTime::from_timestamp_secs(e)
.unwrap()
.with_timezone(&Local)
}),
}
}
}
Expand All @@ -109,6 +129,10 @@ impl From<JobModelOutput> for Job {
pub struct JobDetail {
pub id: usize,
pub name: String,
#[tabled(display("display_option_datetime"))]
pub start_date: Option<DateTime<Local>>,
#[tabled(display("display_option_datetime"))]
pub end_date: Option<DateTime<Local>>,
pub status: JobStatus,
pub status_reason: String,
pub exit_code: i64,
Expand All @@ -117,18 +141,28 @@ pub struct JobDetail {
pub stderr: String,
pub stdin: String,
}
fn display_option_datetime(value: &Option<DateTime<Local>>) -> String {
match value {
Some(dt) => dt.to_string(),
None => "".to_owned(),
}
}
impl From<(JobModelOutput, JobMetadataModel)> for JobDetail {
fn from(value: (JobModelOutput, JobMetadataModel)) -> Self {
Self {
id: value.0.job_id as usize,
name: value.0.name,
status: match value.0.status.state.as_str() {
"RUNNING" => JobStatus::Running,
"FAILED" => JobStatus::Failed,
"COMPLETED" => JobStatus::Finished,
"CANCELLED" => JobStatus::Cancelled,
other => panic!("got job status: {}", other),
},
start_date: value.0.time.start.map(|s| {
DateTime::from_timestamp_secs(s)
.unwrap()
.with_timezone(&Local)
}),
end_date: value.0.time.end.map(|e| {
DateTime::from_timestamp_secs(e)
.unwrap()
.with_timezone(&Local)
}),
status: value.0.status.state.into(),
status_reason: value.0.status.state_reason.unwrap_or("".to_owned()),
exit_code: value.0.status.exit_code.unwrap_or(0),
user: value.0.user.unwrap_or("".to_string()),
Expand Down Expand Up @@ -288,6 +322,13 @@ impl CscsApi {
Ok(Some((job, job_metadata).into()))
}

pub async fn cancel_job(&self, system_name: &str, job_id: i64) -> Result<()> {
cancel_compute_system_job(&self.client, system_name, job_id)
.await
.wrap_err("couldn't delete job")?;
Ok(())
}

pub async fn mkdir(&self, system_name: &str, path: PathBuf) -> Result<()> {
let _ = post_filesystem_ops_mkdir(&self.client, system_name, path)
.await
Expand Down
22 changes: 19 additions & 3 deletions coman/src/cscs/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use std::path::PathBuf;

use crate::{
cscs::{
handlers::{cscs_job_details, cscs_job_list, cscs_start_job, cscs_system_list},
oauth2::{
CLIENT_ID_SECRET_NAME, CLIENT_SECRET_SECRET_NAME, client_credentials_login,
handlers::{
cscs_job_cancel, cscs_job_details, cscs_job_list, cscs_start_job, cscs_system_list,
},
oauth2::{CLIENT_ID_SECRET_NAME, CLIENT_SECRET_SECRET_NAME, client_credentials_login},
},
util::{
keyring::{Secret, get_secret, store_secret},
Expand Down Expand Up @@ -60,6 +60,18 @@ pub(crate) async fn cli_cscs_job_detail(job_id: i64) -> Result<()> {
let data = &[
("Id", job.id.to_string()),
("Name", job.name),
(
"Start Date",
job.start_date
.map(|dt| dt.to_string())
.unwrap_or("".to_owned()),
),
(
"End Date",
job.end_date
.map(|dt| dt.to_string())
.unwrap_or("".to_owned()),
),
("Status", job.status.to_string()),
("Status Reason", job.status_reason),
("Exit Code", job.exit_code.to_string()),
Expand All @@ -85,6 +97,10 @@ pub(crate) async fn cli_cscs_job_start(
cscs_start_job(script_file, image, command).await
}

pub(crate) async fn cli_cscs_job_cancel(job_id: i64) -> Result<()> {
cscs_job_cancel(job_id).await
}

pub(crate) async fn cli_cscs_system_list() -> Result<()> {
match cscs_system_list().await {
Ok(systems) => {
Expand Down
13 changes: 13 additions & 0 deletions coman/src/cscs/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ pub async fn cscs_job_details(job_id: i64) -> Result<Option<JobDetail>> {
}
}

pub async fn cscs_job_cancel(job_id: i64) -> Result<()> {
match get_access_token().await {
Ok(access_token) => {
let api_client = CscsApi::new(access_token.0).unwrap();
let config = Config::new().unwrap();
api_client
.cancel_job(&config.cscs.current_system, job_id)
.await
}
Err(e) => Err(e),
}
}

pub async fn cscs_start_job(
script_file: Option<PathBuf>,
image: Option<DockerImageUrl>,
Expand Down
5 changes: 3 additions & 2 deletions coman/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::{
components::{global_listener::GlobalListener, toolbar::Toolbar, workload_list::WorkloadList},
cscs::{
cli::{
cli_cscs_job_detail, cli_cscs_job_list, cli_cscs_job_start, cli_cscs_login,
cli_cscs_system_list,
cli_cscs_job_cancel, cli_cscs_job_detail, cli_cscs_job_list, cli_cscs_job_start,
cli_cscs_login, cli_cscs_system_list,
},
ports::{AsyncDeviceFlowPort, AsyncFetchWorkloadsPort},
},
Expand Down Expand Up @@ -62,6 +62,7 @@ async fn main() -> Result<()> {
image,
command,
} => cli_cscs_job_start(script_file, image, command).await?,
cli::CscsJobCommands::Cancel { job_id } => cli_cscs_job_cancel(job_id).await?,
},
cli::CscsCommands::System { command } => match command {
cli::CscsSystemCommands::List => cli_cscs_system_list().await?,
Expand Down
14 changes: 14 additions & 0 deletions firecrest_client/src/compute_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,17 @@ pub async fn get_compute_system_job_metadata(
let model: GetJobMetadataResponse = serde_json::from_str(response.as_str())?;
Ok(model)
}

pub async fn cancel_compute_system_job(
client: &FirecrestClient,
system_name: &str,
job_id: i64,
) -> Result<()> {
let _ = client
.delete(
format!("compute/{system_name}/jobs/{job_id}").as_str(),
None,
)
.await?;
Ok(())
}