mirror of https://github.com/Icinga/icinga2.git
Merge pull request #6786 from Icinga/feature/dsl-getenv
DSL: Implement getenv()
This commit is contained in:
commit
15e3aa1762
|
@ -425,6 +425,22 @@ Example:
|
||||||
warn = null
|
warn = null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
### getenv <a id="global-functions-getenv"></a>
|
||||||
|
|
||||||
|
Signature:
|
||||||
|
|
||||||
|
function getenv(key)
|
||||||
|
|
||||||
|
Returns the value from the specified environment variable key.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
$ MY_ENV_VAR=icinga2 icinga2 console
|
||||||
|
Icinga 2 (version: v2.11.0)
|
||||||
|
Type $help to view available commands.
|
||||||
|
<1> => getenv("MY_ENV_VAR")
|
||||||
|
"icinga2"
|
||||||
|
|
||||||
### dirname <a id="global-functions-dirname"></a>
|
### dirname <a id="global-functions-dirname"></a>
|
||||||
|
|
||||||
Signature:
|
Signature:
|
||||||
|
|
|
@ -925,7 +925,8 @@ static VOID WINAPI ServiceMain(DWORD argc, LPSTR *argv)
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
if (!getenv("ICINGA2_KEEP_FDS")) {
|
String keepFDs = Utility::GetFromEnvironment("ICINGA2_KEEP_FDS");
|
||||||
|
if (keepFDs.IsEmpty()) {
|
||||||
rlimit rl;
|
rlimit rl;
|
||||||
if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
|
if (getrlimit(RLIMIT_NOFILE, &rl) >= 0) {
|
||||||
rlim_t maxfds = rl.rlim_max;
|
rlim_t maxfds = rl.rlim_max;
|
||||||
|
|
|
@ -491,8 +491,8 @@ String Application::GetExePath(const String& argv0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!foundSlash) {
|
if (!foundSlash) {
|
||||||
const char *pathEnv = getenv("PATH");
|
String pathEnv = Utility::GetFromEnvironment("PATH");
|
||||||
if (pathEnv) {
|
if (!pathEnv.IsEmpty()) {
|
||||||
std::vector<String> paths = String(pathEnv).Split(":");
|
std::vector<String> paths = String(pathEnv).Split(":");
|
||||||
|
|
||||||
bool foundPath = false;
|
bool foundPath = false;
|
||||||
|
|
|
@ -61,6 +61,7 @@ REGISTER_SAFE_FUNCTION(System, bool, &ScriptUtils::CastBool, "value");
|
||||||
REGISTER_SAFE_FUNCTION(System, get_time, &Utility::GetTime, "");
|
REGISTER_SAFE_FUNCTION(System, get_time, &Utility::GetTime, "");
|
||||||
REGISTER_SAFE_FUNCTION(System, basename, &Utility::BaseName, "path");
|
REGISTER_SAFE_FUNCTION(System, basename, &Utility::BaseName, "path");
|
||||||
REGISTER_SAFE_FUNCTION(System, dirname, &Utility::DirName, "path");
|
REGISTER_SAFE_FUNCTION(System, dirname, &Utility::DirName, "path");
|
||||||
|
REGISTER_SAFE_FUNCTION(System, getenv, &ScriptUtils::GetEnv, "value");
|
||||||
REGISTER_SAFE_FUNCTION(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim, "component");
|
REGISTER_SAFE_FUNCTION(System, msi_get_component_path, &ScriptUtils::MsiGetComponentPathShim, "component");
|
||||||
REGISTER_SAFE_FUNCTION(System, track_parents, &ScriptUtils::TrackParents, "child");
|
REGISTER_SAFE_FUNCTION(System, track_parents, &ScriptUtils::TrackParents, "child");
|
||||||
REGISTER_SAFE_FUNCTION(System, escape_shell_cmd, &Utility::EscapeShellCmd, "cmd");
|
REGISTER_SAFE_FUNCTION(System, escape_shell_cmd, &Utility::EscapeShellCmd, "cmd");
|
||||||
|
@ -530,3 +531,8 @@ Value ScriptUtils::GlobRecursive(const std::vector<Value>& args)
|
||||||
|
|
||||||
return Array::FromVector(paths);
|
return Array::FromVector(paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String ScriptUtils::GetEnv(const String& key)
|
||||||
|
{
|
||||||
|
return Utility::GetFromEnvironment(key);
|
||||||
|
}
|
||||||
|
|
|
@ -58,6 +58,7 @@ public:
|
||||||
static double Ptr(const Object::Ptr& object);
|
static double Ptr(const Object::Ptr& object);
|
||||||
static Value Glob(const std::vector<Value>& args);
|
static Value Glob(const std::vector<Value>& args);
|
||||||
static Value GlobRecursive(const std::vector<Value>& args);
|
static Value GlobRecursive(const std::vector<Value>& args);
|
||||||
|
static String GetEnv(const String& key);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ScriptUtils();
|
ScriptUtils();
|
||||||
|
|
|
@ -36,6 +36,7 @@
|
||||||
#include <ios>
|
#include <ios>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <future>
|
#include <future>
|
||||||
|
|
||||||
#ifdef __FreeBSD__
|
#ifdef __FreeBSD__
|
||||||
|
@ -1936,16 +1937,18 @@ String Utility::GetIcingaDataPath()
|
||||||
|
|
||||||
#endif /* _WIN32 */
|
#endif /* _WIN32 */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the environment variable value by given key.
|
||||||
|
*
|
||||||
|
* @param env Environment variable name.
|
||||||
|
*/
|
||||||
|
|
||||||
String Utility::GetFromEnvironment(const String& env)
|
String Utility::GetFromEnvironment(const String& env)
|
||||||
{
|
{
|
||||||
#ifndef _WIN32
|
|
||||||
const char *envValue = getenv(env.CStr());
|
const char *envValue = getenv(env.CStr());
|
||||||
|
|
||||||
if (envValue == NULL)
|
if (envValue == NULL)
|
||||||
return String();
|
return String();
|
||||||
else
|
else
|
||||||
return String(envValue);
|
return String(envValue);
|
||||||
#else /* _WIN32 */
|
|
||||||
// While getenv exists on Windows, we don't set them. Therefore there is no reason to read them.
|
|
||||||
return String();
|
|
||||||
#endif /* _WIN32 */
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,6 +69,8 @@ public:
|
||||||
static String DirName(const String& path);
|
static String DirName(const String& path);
|
||||||
static String BaseName(const String& path);
|
static String BaseName(const String& path);
|
||||||
|
|
||||||
|
static String GetEnv(const String& key);
|
||||||
|
|
||||||
static void NullDeleter(void *);
|
static void NullDeleter(void *);
|
||||||
|
|
||||||
static double GetTime();
|
static double GetTime();
|
||||||
|
|
|
@ -245,8 +245,8 @@ int ConsoleCommand::Run(const po::variables_map& vm, const std::vector<std::stri
|
||||||
<< "Type $help to view available commands.\n";
|
<< "Type $help to view available commands.\n";
|
||||||
|
|
||||||
|
|
||||||
const char *addrEnv = getenv("ICINGA2_API_URL");
|
String addrEnv = Utility::GetFromEnvironment("ICINGA2_API_URL");
|
||||||
if (addrEnv)
|
if (!addrEnv.IsEmpty())
|
||||||
addr = addrEnv;
|
addr = addrEnv;
|
||||||
|
|
||||||
if (vm.count("connect"))
|
if (vm.count("connect"))
|
||||||
|
@ -290,12 +290,12 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
|
||||||
int next_line = 1;
|
int next_line = 1;
|
||||||
|
|
||||||
#ifdef HAVE_EDITLINE
|
#ifdef HAVE_EDITLINE
|
||||||
char *homeEnv = getenv("HOME");
|
String homeEnv = Utility::GetFromEnvironment("HOME");
|
||||||
|
|
||||||
String historyPath;
|
String historyPath;
|
||||||
std::fstream historyfp;
|
std::fstream historyfp;
|
||||||
|
|
||||||
if (homeEnv) {
|
if (!homeEnv.IsEmpty()) {
|
||||||
historyPath = String(homeEnv) + "/.icinga2_history";
|
historyPath = String(homeEnv) + "/.icinga2_history";
|
||||||
|
|
||||||
historyfp.open(historyPath.CStr(), std::fstream::in);
|
historyfp.open(historyPath.CStr(), std::fstream::in);
|
||||||
|
@ -321,12 +321,12 @@ int ConsoleCommand::RunScriptConsole(ScriptFrame& scriptFrame, const String& add
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *usernameEnv = getenv("ICINGA2_API_USERNAME");
|
String usernameEnv = Utility::GetFromEnvironment("ICINGA2_API_USERNAME");
|
||||||
const char *passwordEnv = getenv("ICINGA2_API_PASSWORD");
|
String passwordEnv = Utility::GetFromEnvironment("ICINGA2_API_PASSWORD");
|
||||||
|
|
||||||
if (usernameEnv)
|
if (!usernameEnv.IsEmpty())
|
||||||
url->SetUsername(usernameEnv);
|
url->SetUsername(usernameEnv);
|
||||||
if (passwordEnv)
|
if (!passwordEnv.IsEmpty())
|
||||||
url->SetPassword(passwordEnv);
|
url->SetPassword(passwordEnv);
|
||||||
|
|
||||||
if (url->GetPort().IsEmpty())
|
if (url->GetPort().IsEmpty())
|
||||||
|
|
Loading…
Reference in New Issue