From cfcd1975fd7baf419321bc410c3137f6e0be2fe9 Mon Sep 17 00:00:00 2001 From: Stanislav Date: Wed, 26 Feb 2025 19:51:37 +0200 Subject: [PATCH] Migrate scenario configuration from JSON to TOML --- TODO.md | 1 - core/Cargo.toml | 1 + core/src/config.rs | 9 ++- core/src/scenario/errors.rs | 4 +- example-scenario.json | 119 ----------------------------------- example-scenario.toml | 99 +++++++++++++++++++++++++++++ gui/src/app/app.component.ts | 2 +- 7 files changed, 107 insertions(+), 128 deletions(-) delete mode 100644 example-scenario.json create mode 100644 example-scenario.toml diff --git a/TODO.md b/TODO.md index 7046c90..a2aaf33 100644 --- a/TODO.md +++ b/TODO.md @@ -1,6 +1,5 @@ # core - Scenario config merging -- Migrate to toml for scenario config - Validate path variables and load files # gui diff --git a/core/Cargo.toml b/core/Cargo.toml index 3a7381b..3383ff5 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -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" diff --git a/core/src/config.rs b/core/src/config.rs index 5b83fad..70ccae0 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -3,7 +3,6 @@ use serde::Deserialize; use std::collections::BTreeMap; use std::{ collections::HashMap, - fs::File, ops::{Deref, DerefMut}, path::PathBuf, }; @@ -21,10 +20,10 @@ impl TryFrom for ScenarioConfig { type Error = ScenarioConfigError; fn try_from(value: PathBuf) -> Result { - 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::(&config_string) + .map_err(ScenarioConfigError::CannotReadToml)?; Ok(config) } } diff --git a/core/src/scenario/errors.rs b/core/src/scenario/errors.rs index 9b94241..6decda2 100644 --- a/core/src/scenario/errors.rs +++ b/core/src/scenario/errors.rs @@ -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)] diff --git a/example-scenario.json b/example-scenario.json deleted file mode 100644 index ebede53..0000000 --- a/example-scenario.json +++ /dev/null @@ -1,119 +0,0 @@ -{ - "credentials": { - "//": [ - "// username - will be added to the variables", - "// password - if not provided, will use the ssh-agent", - "// [!] will not be added to the variables" - ], - "username": "my_username", - "password": "my_password" - }, - "server": { - "host": "localhost", - "port": "22" - }, - "execute": { - "steps": [ - { - "task": "copy_jar_to_server" - }, - { - "task": "stop_service" - }, - { - "task": "create_backup", - "rollback": [ - "start_service" - ] - }, - { - "task": "remove_current_deploy", - "rollback": [ - "restore_backup", - "start_service" - ] - }, - { - "task": "deploy_new_file", - "rollback": [ - "restore_backup", - "start_service" - ] - }, - { - "task": "start_service", - "rollback": [ - "restore_backup", - "start_service" - ] - } - ] - }, - "variables": { - "//": [ - "// required - will be prompted for input", - "// special - generated by the app", - "// defined - plain string variables defined in this file" - ], - "required": { - "path:local_jar_path": "Local JAR Path", - "some_variable": "Some Variable", - "path:some_other_path": "Some Other Path" - }, - "special": { - "timestamp": "%Y-%m-%dT%H%M%S%:z" - }, - "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": { - "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." - }, - "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." - }, - "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." - }, - "remove_current_deploy": { - "type": "RemoteSudo", - "description": "Removing current deployment", - "command": "sudo rm {remote_deploy_path} -f", - "error_message": "Failed to remove the current deployment." - }, - "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." - }, - "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." - }, - "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." - } - } -} diff --git a/example-scenario.toml b/example-scenario.toml new file mode 100644 index 0000000..9095525 --- /dev/null +++ b/example-scenario.toml @@ -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." \ No newline at end of file diff --git a/gui/src/app/app.component.ts b/gui/src/app/app.component.ts index d999abb..44bce11 100644 --- a/gui/src/app/app.component.ts +++ b/gui/src/app/app.component.ts @@ -94,7 +94,7 @@ export class AppComponent { multiple: false, filters: [{ name: 'Configuration File', - extensions: ['json'] + extensions: ['toml'] }] });