This commit is contained in:
Robin Appelman 2021-03-28 19:52:48 +02:00
commit b1a1131ac6
2 changed files with 36 additions and 12 deletions

19
README.md Normal file
View file

@ -0,0 +1,19 @@
# Dispenser
Automatically spawn and destroy a tf2 server on a schedule
## Usage
- Copy `config.sample.toml` to `config.toml` and edit accordingly
- Start `dispenser config.toml` as a system service
When the configured start schedule is reached it will create a new cloud server, update the dyndns (optional)
and install a tf2 server.
This server is then destroyed when the stop schedule is reached.
As a failsafe against unexpected costs or destroying the wrong server, this program will not spawn any server
if it already detects a running one and it will only destroy a server that was created by the program.
This does mean that if the program is (re-)started while a server is already active, the program will not
start and destroy any server because it can't be sure it should control the running server.
You'll need to manually destroy the existing server in that case.

View file

@ -135,17 +135,19 @@ async fn main() -> Result<(), Error> {
Ok(()) Ok(())
} }
fn stop_job(_cloud: Arc<dyn Cloud>, config: &Config, server_id: Arc<Mutex<Option<String>>>) -> Job { fn stop_job(cloud: Arc<dyn Cloud>, config: &Config, server_id: Arc<Mutex<Option<String>>>) -> Job {
Job::new(&config.schedule.stop, move |_uuid, _l| { Job::new(&config.schedule.stop, move |_uuid, _l| {
let server_id = server_id.clone(); let server_id = server_id.clone();
let cloud = cloud.clone();
spawn(async move { spawn(async move {
println!("Stopping server");
if let Some(id) = server_id.lock().unwrap().take() { if let Some(id) = server_id.lock().unwrap().take() {
println!("Would have killed {}", id); println!("Stopping server {}", id);
// match cloud.kill(&id).await { match cloud.kill(&id).await {
// Ok(_) => {} Ok(_) => {}
// Err(e) => eprintln!("{:#}", e), Err(e) => eprintln!("{:#}", e),
// }; };
} else {
println!("No server to stop")
} }
}); });
}) })
@ -161,11 +163,14 @@ fn start_job(cloud: Arc<dyn Cloud>, config: Config, server_id: Arc<Mutex<Option<
let server_id = server_id.clone(); let server_id = server_id.clone();
spawn(async move { spawn(async move {
let cloud = cloud.as_ref(); let cloud = cloud.as_ref();
println!("Starting server"); let already_started = { server_id.lock().unwrap().is_some() };
match start(cloud, &config).await { if !already_started {
Ok(id) => *server_id.lock().unwrap() = Some(id), println!("Starting server");
Err(e) => eprintln!("{:#}", e), match start(cloud, &config).await {
}; Ok(id) => *server_id.lock().unwrap() = Some(id),
Err(e) => eprintln!("{:#}", e),
};
}
}); });
}) })
.unwrap() .unwrap()