Skip to content

Commit

Permalink
Write state to GHA state file
Browse files Browse the repository at this point in the history
  • Loading branch information
brunojppb committed Jan 2, 2024
1 parent 727cc39 commit 8b7d104
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
10 changes: 7 additions & 3 deletions decay_gha/src/commands.rs → decay_gha/src/cli_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ 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
binary_path: String,
/// 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,
}
27 changes: 27 additions & 0 deletions decay_gha/src/gha_commands.rs
Original file line number Diff line number Diff line change
@@ -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<String, env::VarError> {
env::var(format!("STATE_{}", key))
}
46 changes: 31 additions & 15 deletions decay_gha/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
extern crate clap;
mod commands;
mod cli_commands;
mod gha_commands;
use std::io::{self, Write};

use std::{
Expand All @@ -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();
Expand All @@ -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) => {
Expand All @@ -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);
}
}

0 comments on commit 8b7d104

Please sign in to comment.