From a5317bfeda3af2b414b9ccf5dddf20c8b3183e44 Mon Sep 17 00:00:00 2001 From: Master Date: Fri, 3 Jul 2020 00:24:01 -0400 Subject: [PATCH] units now can choose their environment. consider moving to plan. --- src/loaders/abstract/Conf.cpp | 20 -------------------- src/loaders/abstract/Conf.h | 4 ---- src/loaders/abstract/Task.cpp | 10 ++++++---- src/loaders/abstract/Unit.cpp | 17 ++++++++++++++++- src/loaders/abstract/Unit.h | 3 +++ test/units/all_test.units | 6 ++++-- test/units/dependent_tests.units | 22 ---------------------- 7 files changed, 29 insertions(+), 53 deletions(-) delete mode 100644 test/units/dependent_tests.units diff --git a/src/loaders/abstract/Conf.cpp b/src/loaders/abstract/Conf.cpp index 79ad6a8..745f3ac 100644 --- a/src/loaders/abstract/Conf.cpp +++ b/src/loaders/abstract/Conf.cpp @@ -116,21 +116,6 @@ Conf::Conf( std::string filename, int LOG_LEVEL ): JSON_Loader( LOG_LEVEL ), slo } else { this->execution_context_literal = this->execution_context.asString(); } - - if ( this->get_serialized(this->env_vars_file, "env_vars_file" ) != 0 ) - { - throw ConfigLoadException("env_vars_file is not set in the config file supplied: " + filename); - } - - this->env_vars_file_literal = this->execution_context_literal + "/" + this->env_vars_file.asString(); - - if ( exists( this->get_env_vars_file() ) ) - { - this->slog.log( E_DEBUG, "Environment variables file exists: '" + this->get_env_vars_file() + "'." ); - } else { - this->slog.log( E_FATAL, "Variables file does not exist: '" + this->env_vars_file_literal + "'."); - throw ConfigLoadException( "env_vars_file points to an incorrect path." ); - } }; /// Conf::has_context_override - Specifies whether or not the override context function is enabled in the conf file. @@ -155,8 +140,3 @@ void Conf::set_execution_context( std::string execution_context ) this->execution_context_literal = execution_context; } -/// Conf::get_env_vars_file() - returns the path to the environment variables file. -std::string Conf::get_env_vars_file() -{ - return this->env_vars_file_literal; -} diff --git a/src/loaders/abstract/Conf.h b/src/loaders/abstract/Conf.h index ae54f8d..479f257 100644 --- a/src/loaders/abstract/Conf.h +++ b/src/loaders/abstract/Conf.h @@ -37,9 +37,6 @@ private: Json::Value units_path; Json::Value execution_context; Json::Value config_version; - Json::Value env_vars_file; - - std::string env_vars_file_literal; // flag to indicate if execution context should be overriden in config file // if set to true Examplar should use whats in the config file for current working directory @@ -60,7 +57,6 @@ public: void set_execution_context( std::string ); - std::string get_env_vars_file(); private: int LOG_LEVEL; Logger slog; diff --git a/src/loaders/abstract/Task.cpp b/src/loaders/abstract/Task.cpp index 56e2d59..9030276 100644 --- a/src/loaders/abstract/Task.cpp +++ b/src/loaders/abstract/Task.cpp @@ -207,12 +207,14 @@ void Task::execute( Conf * configuration ) this->slog.log( E_FATAL, "Executable does not exist." ); throw Task_NotReady(); } - this->slog.log( E_DEBUG, "Vars file: " + configuration->get_env_vars_file() ); + this->slog.log( E_DEBUG, "Vars file: " + this->definition.get_env_vars_file() ); this->slog.log( E_DEBUG, "Shell: " + this->definition.get_shell() ); + + std::string static_env_file = configuration->get_execution_context() + "/" + this->definition.get_env_vars_file(); int return_code = Sproc::execute( this->definition.get_shell(), - configuration->get_env_vars_file(), + static_env_file, this->definition.get_user(), this->definition.get_group(), target_command @@ -277,7 +279,7 @@ void Task::execute( Conf * configuration ) int rectifier_error = Sproc::execute( this->definition.get_shell(), - configuration->get_env_vars_file(), + static_env_file, this->definition.get_user(), this->definition.get_group(), rectifier_command @@ -324,7 +326,7 @@ void Task::execute( Conf * configuration ) int retry_code = Sproc::execute( this->definition.get_shell(), - configuration->get_env_vars_file(), + static_env_file, this->definition.get_user(), this->definition.get_group(), target_command diff --git a/src/loaders/abstract/Unit.cpp b/src/loaders/abstract/Unit.cpp index 76c5d59..97527f8 100644 --- a/src/loaders/abstract/Unit.cpp +++ b/src/loaders/abstract/Unit.cpp @@ -161,7 +161,12 @@ int Unit::load_root(Json::Value loader_root) { this->group = loader_root.get( "group", errmsg_group ).asString(); } else this->group = grp->gr_name; if ( loader_root.isMember( "shell" ) ) - { this->shell = loader_root.get( "shell", errmsg ).asString(); } else this->shell = "sh"; + { this->shell = loader_root.get( "shell", errmsg ).asString(); } else this->shell = "/usr/bin/env sh"; + + if ( loader_root.isMember( "environment") ) + { this->env_vars_file = loader_root.get( "environment", errmsg ).asString(); } else { + throw UnitException("No environment file specified for a unit, and environment files are required for unit definitions."); + } this->populated = true; @@ -271,3 +276,13 @@ std::string Unit::get_shell() if ( ! this->populated ) { throw UnitException("Attempted to access an unpopulated unit."); } return this->shell; } + +/// Unit::get_env_vars_file - retrieves the file path to use for the unit environment file. This is a file that is +/// sourced by the chosen shell to populate any environment variables. +/// \return the string value of the shell path. +std::string Unit::get_env_vars_file() +{ + if ( ! this->populated ) { throw UnitException("Attempted to access an unpopulated unit."); } + return this->env_vars_file; +} + diff --git a/src/loaders/abstract/Unit.h b/src/loaders/abstract/Unit.h index 40b002f..13e3bf8 100644 --- a/src/loaders/abstract/Unit.h +++ b/src/loaders/abstract/Unit.h @@ -70,6 +70,7 @@ private: // shell to use for env std::string shell; + std::string env_vars_file; public: Unit( int LOG_LEVEL ); @@ -85,6 +86,8 @@ public: std::string get_target(); std::string get_output(); std::string get_rectifier(); + std::string get_env_vars_file(); + bool get_active(); bool get_required(); bool get_rectify(); diff --git a/test/units/all_test.units b/test/units/all_test.units index 68e10d7..875483c 100644 --- a/test/units/all_test.units +++ b/test/units/all_test.units @@ -9,7 +9,8 @@ "user": "bagira", "group": "bagira", "rectify": false, - "shell": "/usr/bin/env bash" + "shell": "/usr/bin/env bash", + "environment": "examplar.variables" }, { "name": "independent test 2", @@ -20,7 +21,8 @@ "user": "bagira", "group": "bagira", "rectify": false, - "shell": "/usr/bin/env bash" + "shell": "/usr/bin/env bash", + "environment": "examplar.variables" } ] } diff --git a/test/units/dependent_tests.units b/test/units/dependent_tests.units deleted file mode 100644 index 8543b47..0000000 --- a/test/units/dependent_tests.units +++ /dev/null @@ -1,22 +0,0 @@ -{ - "units": [ - { - "name": "A DEFINITION THAT IS NOT USED", - "target": "/usr/bin/dialog --yesno test 50 50", - "rectifier": "/usr/bin/false", - "active": false, - "required": true, - "rectify": true, - "user": "root" - }, - { - "name": "dependent test", - "target": "/usr/bin/false", - "rectifier": "/usr/bin/true", - "active": false, - "required": true, - "rectify": true, - "user": "root" - } - ] -}