From 8b7d1040b0d4c74adc4e5d9ff7443f7f64d2cf05 Mon Sep 17 00:00:00 2001 From: Bruno Paulino Date: Tue, 2 Jan 2024 12:03:54 +0100 Subject: [PATCH] Write state to GHA state file --- .../src/{commands.rs => cli_commands.rs} | 10 ++-- decay_gha/src/gha_commands.rs | 27 +++++++++++ decay_gha/src/main.rs | 46 +++++++++++++------ 3 files changed, 65 insertions(+), 18 deletions(-) rename decay_gha/src/{commands.rs => cli_commands.rs} (71%) create mode 100644 decay_gha/src/gha_commands.rs diff --git a/decay_gha/src/commands.rs b/decay_gha/src/cli_commands.rs similarity index 71% rename from decay_gha/src/commands.rs rename to decay_gha/src/cli_commands.rs index b636ebe..bd5bf68 100644 --- a/decay_gha/src/commands.rs +++ b/decay_gha/src/cli_commands.rs @@ -11,7 +11,8 @@ pub struct Cli { #[derive(Debug, Subcommand)] pub enum Commands { - /// Starts the given Decay server + /// Starts the given Decay server and + /// store its pid on the Github state #[command(arg_required_else_help = true)] Start { /// Path to the Decay server binary @@ -19,10 +20,13 @@ pub enum Commands { /// Port to bind the Decay server to during startup port: String, }, - /// Stop a previusly started Decay server using this CLI + /// Stop a process with the given ID #[command(arg_required_else_help = true)] - Stop { + StopWithPid { /// The pid of the Decay server pid: String, }, + /// Stop a previously running Decay server with + /// its pid stored on the Github Actions runner state + Stop, } diff --git a/decay_gha/src/gha_commands.rs b/decay_gha/src/gha_commands.rs new file mode 100644 index 0000000..72af0f2 --- /dev/null +++ b/decay_gha/src/gha_commands.rs @@ -0,0 +1,27 @@ +use std::{ + env::{self}, + fs::OpenOptions, +}; + +use std::io::prelude::*; + +pub fn save_state(key: &str, value: &str) { + let state = format!("{}={}", key, value); + let gha_file_path = env::var("GITHUB_STATE") + .unwrap_or_else(|_| panic!("Could not read GITHUB_STATE env variable")); + println!("GHA state file: {}", gha_file_path); + + let mut gha_file = OpenOptions::new() + .write(true) + .append(true) + .open(gha_file_path) + .expect("GHA file should be available"); + + if let Err(e) = writeln!(gha_file, "{}", &state) { + panic!("Could not write to GHA state file: {}", e); + } +} + +pub fn get_state(key: &str) -> Result { + env::var(format!("STATE_{}", key)) +} diff --git a/decay_gha/src/main.rs b/decay_gha/src/main.rs index 1f27eaf..c60822c 100644 --- a/decay_gha/src/main.rs +++ b/decay_gha/src/main.rs @@ -1,5 +1,6 @@ extern crate clap; -mod commands; +mod cli_commands; +mod gha_commands; use std::io::{self, Write}; use std::{ @@ -8,7 +9,12 @@ use std::{ }; use clap::Parser; -use commands::{Cli, Commands}; +use cli_commands::{Cli, Commands}; +use gha_commands::get_state; + +use crate::gha_commands::save_state; + +const PID_STATE_KEY: &str = "decay-pid"; fn main() { let args = Cli::parse(); @@ -26,6 +32,7 @@ fn main() { { Ok(child_process) => { println!("Starting up server with pid {}", child_process.id()); + save_state(PID_STATE_KEY, &child_process.id().to_string()); process::exit(0); } Err(err) => { @@ -34,19 +41,28 @@ fn main() { } } } - Commands::Stop { pid } => { - println!("Stopping process with pid: {}", pid); - let output = Command::new("kill") - .arg(&pid) - .output() - .unwrap_or_else(|_| panic!("Could not stop decay server with pid {}", &pid)); - io::stdout().write_all(&output.stdout).unwrap(); - io::stderr().write_all(&output.stderr).unwrap(); - if output.status.success() { - process::exit(0); - } else { - process::exit(1); - } + Commands::StopWithPid { pid } => { + stop_process(&pid); + } + Commands::Stop => { + let pid = get_state(PID_STATE_KEY) + .unwrap_or_else(|_| panic!("pid could not be read from GHA state")); + stop_process(&pid); } } } + +fn stop_process(pid: &str) { + println!("Stopping process with pid: {}", pid); + let output = Command::new("kill") + .arg(pid) + .output() + .unwrap_or_else(|_| panic!("Could not stop decay server with pid {}", &pid)); + io::stdout().write_all(&output.stdout).unwrap(); + io::stderr().write_all(&output.stderr).unwrap(); + if output.status.success() { + process::exit(0); + } else { + process::exit(1); + } +}