units now can choose their environment. consider moving to plan.

master
Master 2020-07-03 00:24:01 -04:00
parent 8b35a88643
commit a5317bfeda
7 changed files with 29 additions and 53 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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();

View File

@ -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"
}
]
}

View File

@ -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"
}
]
}