added shell attribute to unit object definition to allow more flexibility; defaults to sh

master
Master 2020-07-01 22:30:51 -04:00
parent c9567f20e4
commit c4828d506b
8 changed files with 62 additions and 18 deletions

View File

@ -41,7 +41,7 @@ int groupname_to_gid( std::string groupname, int & gid )
/// ///
/// \param input - The commandline input to execute. /// \param input - The commandline input to execute.
/// \return - The return code of the execution of input in the calling shell. /// \return - The return code of the execution of input in the calling shell.
int Sproc::execute(std::string run_as, std::string group, std::string command ) int Sproc::execute( std::string shell, std::string environment_file, std::string run_as, std::string group, std::string command )
{ {
Logger slog = Logger( E_INFO, "_sproc" ); Logger slog = Logger( E_INFO, "_sproc" );
@ -64,7 +64,7 @@ int Sproc::execute(std::string run_as, std::string group, std::string command )
{ {
slog.log( E_DEBUG, "GID of '" + group + "' is '" + std::to_string( run_as_gid ) + "'." ); slog.log( E_DEBUG, "GID of '" + group + "' is '" + std::to_string( run_as_gid ) + "'." );
} else { } else {
slog.log( E_FATAL, "Failed to look up DID for '" + group + "'."); slog.log( E_FATAL, "Failed to look up GID for '" + group + "'.");
return -404; return -404;
} }
@ -95,6 +95,14 @@ int Sproc::execute(std::string run_as, std::string group, std::string command )
return -401; return -401;
} }
std::string sourcer_string = shell + " -c '. " + environment_file + "'";
slog.log( E_DEBUG, "Shell call for loading: ``" + sourcer_string + "``." );
int sourcer = system( sourcer_string.c_str() );
if ( sourcer != 0)
{
slog.log(E_FATAL, "Failed to source environment file.");
return -127;
}
exit_code_raw = system( command.c_str() ); exit_code_raw = system( command.c_str() );
exit( WEXITSTATUS( exit_code_raw ) ); exit( WEXITSTATUS( exit_code_raw ) );
} else if ( pid > 0 ) } else if ( pid > 0 )
@ -106,6 +114,4 @@ int Sproc::execute(std::string run_as, std::string group, std::string command )
slog.log( E_FATAL, "Fork Failed"); slog.log( E_FATAL, "Fork Failed");
} }
return WEXITSTATUS( exit_code_raw ); return WEXITSTATUS( exit_code_raw );
} }

View File

@ -31,7 +31,7 @@
class Sproc { class Sproc {
public: public:
// call the object. returnvalue is enum representing external execution attempt not binary exit code // call the object. returnvalue is enum representing external execution attempt not binary exit code
static int execute(std::string run_as, std::string group, std::string command ); static int execute( std::string shell, std::string enviornment_file, std::string run_as, std::string group, std::string command );
}; };
#endif //FTESTS_SPROC_H #endif //FTESTS_SPROC_H

View File

@ -92,7 +92,7 @@ Conf::Conf( std::string filename, int LOG_LEVEL ): JSON_Loader( LOG_LEVEL ), slo
} }
// find the path to the plan file // find the path to the plan file
if (this->get_serialized(this->plan_path, "plan_path" ) != 0 ) if ( this->get_serialized(this->plan_path, "plan_path" ) != 0 )
{ {
throw ConfigLoadException("plan_path string is not set in the config file supplied:" + filename); throw ConfigLoadException("plan_path string is not set in the config file supplied:" + filename);
} }
@ -131,9 +131,6 @@ Conf::Conf( std::string filename, int LOG_LEVEL ): JSON_Loader( LOG_LEVEL ), slo
this->slog.log( E_FATAL, "Variables file does not exist: '" + this->env_vars_file_literal + "'."); 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." ); 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. /// Conf::has_context_override - Specifies whether or not the override context function is enabled in the conf file.
@ -158,6 +155,7 @@ void Conf::set_execution_context( std::string execution_context )
this->execution_context_literal = 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() std::string Conf::get_env_vars_file()
{ {
return this->env_vars_file_literal; return this->env_vars_file_literal;

View File

@ -208,7 +208,15 @@ void Task::execute( Conf * configuration )
throw Task_NotReady(); throw Task_NotReady();
} }
this->slog.log( E_DEBUG, "Vars file: " + configuration->get_env_vars_file() ); this->slog.log( E_DEBUG, "Vars file: " + configuration->get_env_vars_file() );
int return_code = Sproc::execute( this->definition.get_user(), this->definition.get_group(), ". " + configuration->get_env_vars_file() + " && " + target_command ); this->slog.log( E_DEBUG, "Shell: " + this->definition.get_shell() );
int return_code = Sproc::execute(
this->definition.get_shell(),
configuration->get_env_vars_file(),
this->definition.get_user(),
this->definition.get_group(),
target_command
);
// ********************************************** // **********************************************
// d[0] Error Code Check // d[0] Error Code Check
@ -267,7 +275,13 @@ void Task::execute( Conf * configuration )
this->slog.log( E_INFO, "Executing rectification: " + rectifier_command + "." ); this->slog.log( E_INFO, "Executing rectification: " + rectifier_command + "." );
int rectifier_error = Sproc::execute( this->definition.get_user(), this->definition.get_group(), ". " + configuration->get_env_vars_file() + " && " + rectifier_command ); int rectifier_error = Sproc::execute(
this->definition.get_shell(),
configuration->get_env_vars_file(),
this->definition.get_user(),
this->definition.get_group(),
rectifier_command
);
// ********************************************** // **********************************************
// d[3] Error Code Check for Rectifier // d[3] Error Code Check for Rectifier
@ -308,7 +322,13 @@ void Task::execute( Conf * configuration )
// a[7] Re-execute Target // a[7] Re-execute Target
this->slog.log( E_INFO, "Re-Executing target \"" + this->definition.get_target() + "\"." ); this->slog.log( E_INFO, "Re-Executing target \"" + this->definition.get_target() + "\"." );
int retry_code = Sproc::execute( this->definition.get_user(), this->definition.get_group(), ". " + configuration->get_env_vars_file() + " && " + target_command ); int retry_code = Sproc::execute(
this->definition.get_shell(),
configuration->get_env_vars_file(),
this->definition.get_user(),
this->definition.get_group(),
target_command
);
// ********************************************** // **********************************************
// d[5] Error Code Check // d[5] Error Code Check

View File

@ -160,6 +160,9 @@ int Unit::load_root(Json::Value loader_root)
if ( loader_root.isMember( "group" ) ) if ( loader_root.isMember( "group" ) )
{ this->group = loader_root.get( "group", errmsg_group ).asString(); } else this->group = grp->gr_name; { 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->populated = true; this->populated = true;
return 0; return 0;
@ -258,4 +261,13 @@ std::string Unit::get_group()
{ {
if ( ! this->populated ) { throw UnitException("Attempted to access an unpopulated unit."); } if ( ! this->populated ) { throw UnitException("Attempted to access an unpopulated unit."); }
return this->group; return this->group;
} }
/// Unit::get_shell - retrieves the shell path to use for the unit execution.
///
/// \return the string value of the shell path.
std::string Unit::get_shell()
{
if ( ! this->populated ) { throw UnitException("Attempted to access an unpopulated unit."); }
return this->shell;
}

View File

@ -67,6 +67,10 @@ private:
// not intended for protected accounts, handle your own security // not intended for protected accounts, handle your own security
std::string group; std::string group;
// shell to use for env
std::string shell;
public: public:
Unit( int LOG_LEVEL ); Unit( int LOG_LEVEL );
@ -86,6 +90,7 @@ public:
bool get_rectify(); bool get_rectify();
std::string get_user(); std::string get_user();
std::string get_group(); std::string get_group();
std::string get_shell();
private: private:
int LOG_LEVEL; int LOG_LEVEL;

View File

@ -4,5 +4,6 @@
"units_path": "units/", "units_path": "units/",
"plan_path": "plans/atomic.plan", "plan_path": "plans/atomic.plan",
"config_version": "3", "config_version": "3",
"env_vars_file": "examplar.variables" "env_vars_file": "examplar.variables",
"shell": "/bin/bash"
} }

View File

@ -6,9 +6,10 @@
"rectifier": "", "rectifier": "",
"active": true, "active": true,
"required": true, "required": true,
"user": "root", "user": "bagira",
"group": "root", "group": "bagira",
"rectify": false "rectify": false,
"shell": "/usr/bin/env bash"
}, },
{ {
"name": "independent test 2", "name": "independent test 2",
@ -18,7 +19,8 @@
"required": false, "required": false,
"user": "bagira", "user": "bagira",
"group": "bagira", "group": "bagira",
"rectify": false "rectify": false,
"shell": "/usr/bin/env bash"
} }
] ]
} }