Skip to content

Commit

Permalink
Migrate scenario configuration from JSON to TOML
Browse files Browse the repository at this point in the history
  • Loading branch information
st4s1k committed Feb 26, 2025
1 parent 6cd80ae commit cfcd197
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 128 deletions.
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# core
- Scenario config merging
- Migrate to toml for scenario config
- Validate path variables and load files

# gui
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ serde = { version = "1.0.218", features = ["derive"] }
serde_json = "1.0.139"
indicatif = "0.17.11"
chrono = "0.4.39"
toml = "0.8.20"
9 changes: 4 additions & 5 deletions core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::Deserialize;
use std::collections::BTreeMap;
use std::{
collections::HashMap,
fs::File,
ops::{Deref, DerefMut},
path::PathBuf,
};
Expand All @@ -21,10 +20,10 @@ impl TryFrom<PathBuf> for ScenarioConfig {
type Error = ScenarioConfigError;

fn try_from(value: PathBuf) -> Result<Self, Self::Error> {
let config_file: File = File::open(value)
.map_err(ScenarioConfigError::CannotOpenFile)?;
let config: ScenarioConfig = serde_json::from_reader(config_file)
.map_err(ScenarioConfigError::CannotReadJson)?;
let config_string =
std::fs::read_to_string(value).map_err(ScenarioConfigError::CannotOpenFile)?;
let config = toml::from_str::<ScenarioConfig>(&config_string)
.map_err(ScenarioConfigError::CannotReadToml)?;
Ok(config)
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/scenario/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use thiserror::Error;
pub enum ScenarioConfigError {
#[error("Cannot open config file: {0}")]
CannotOpenFile(#[source] std::io::Error),
#[error("Cannot read JSON config file: {0}")]
CannotReadJson(#[source] serde_json::Error),
#[error("Cannot read TOML config file: {0}")]
CannotReadToml(#[source] toml::de::Error),
}

#[derive(Error, Debug)]
Expand Down
119 changes: 0 additions & 119 deletions example-scenario.json

This file was deleted.

99 changes: 99 additions & 0 deletions example-scenario.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Credentials section
# username - will be added to the variables
# password - if not provided, will use the ssh-agent
# [!] will not be added to the variables
[credentials]
username = "my_username"
password = "my_password"

[server]
host = "localhost"
port = "22"

# Execute section with steps
[[execute.steps]]
task = "copy_jar_to_server"

[[execute.steps]]
task = "stop_service"

[[execute.steps]]
task = "create_backup"
rollback = ["start_service"]

[[execute.steps]]
task = "remove_current_deploy"
rollback = ["restore_backup", "start_service"]

[[execute.steps]]
task = "deploy_new_file"
rollback = ["restore_backup", "start_service"]

[[execute.steps]]
task = "start_service"
rollback = ["restore_backup", "start_service"]

# Variables section
[variables]

# will be prompted for input
[variables.required]
"path:local_jar_path" = "Local JAR Path"
some_variable = "Some Variable"
"path:some_other_path" = "Some Other Path"

# generated by the app
[variables.special]
timestamp = "%Y-%m-%dT%H%M%S%:z"

# plain string variables defined in this file
[variables.defined]
service_name = "my_service"
remote_service_script_path = "/usr/local/bin/{service_name}.sh"
remote_deploy_path = "/usr/local/{service_name}/{service_name}.jar"
backup_path = "/u01/backup/{service_name}/{service_name}-{timestamp}.jar"
remote_base_path = "/home/{username}"

# Tasks section
[tasks.copy_jar_to_server]
type = "SftpCopy"
description = "Copying new deploy file to server"
source_path = "{local_jar_path}"
destination_path = "{remote_base_path}/{basename:local_jar_path}"
error_message = "Failed to copy new deploy file to server."

[tasks.stop_service]
type = "RemoteSudo"
description = "Stopping the service on remote server"
command = "sudo {remote_service_script_path} stop"
error_message = "Failed to stop the service on the remote server."

[tasks.create_backup]
type = "RemoteSudo"
description = "Creating backup of current deployment"
command = "sudo cp -a {remote_deploy_path} {backup_path}"
error_message = "Failed to create backup of the current deployment."

[tasks.remove_current_deploy]
type = "RemoteSudo"
description = "Removing current deployment"
command = "sudo rm {remote_deploy_path} -f"
error_message = "Failed to remove the current deployment."

[tasks.deploy_new_file]
type = "RemoteSudo"
description = "Deploying the new file"
command = "sudo mv {remote_base_path}/{basename:local_jar_path} {remote_deploy_path}"
error_message = "Failed to deploy the new file."

[tasks.start_service]
type = "RemoteSudo"
description = "Starting the service on remote server"
command = "sudo {remote_service_script_path} start"
error_message = "Failed to start the service on the remote server."

[tasks.restore_backup]
type = "RemoteSudo"
description = "Restoring backup of current deployment"
command = "sudo cp -a {backup_path} {remote_deploy_path}"
error_message = "Failed to restore backup of the current deployment."
2 changes: 1 addition & 1 deletion gui/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class AppComponent {
multiple: false,
filters: [{
name: 'Configuration File',
extensions: ['json']
extensions: ['toml']
}]
});

Expand Down

0 comments on commit cfcd197

Please sign in to comment.