mirror of
https://github.com/Icinga/icinga2.git
synced 2025-07-26 15:14:07 +02:00
Merge pull request #7535 from tigercomputing/Icinga/feature/config-stage-activate-parameter
API /v1/config/stages 'activate' parameter
This commit is contained in:
commit
b1787883f7
@ -1849,8 +1849,12 @@ in the Director's deployment log.
|
|||||||
Send a `POST` request to the URL endpoint `/v1/config/stages` and add the name of an existing
|
Send a `POST` request to the URL endpoint `/v1/config/stages` and add the name of an existing
|
||||||
configuration package to the URL path (e.g. `example-cmdb`).
|
configuration package to the URL path (e.g. `example-cmdb`).
|
||||||
The request body must contain the `files` attribute with the value being
|
The request body must contain the `files` attribute with the value being
|
||||||
a dictionary of file targets and their content. You can also specify an optional `reload` attribute
|
a dictionary of file targets and their content.
|
||||||
that will tell icinga2 to reload after stage config validation. By default this is set to `true`.
|
|
||||||
|
Optional attributes include `reload` (defaults to `true`) and `activate` (defaults to `true`).
|
||||||
|
The `reload` attribute will tell icinga2 to reload after stage config validation.
|
||||||
|
The `activate` attribute will tell icinga2 to activate the stage if it validates.
|
||||||
|
If `activate` is set to `false`, `reload` must also be `false`.
|
||||||
|
|
||||||
The file path requires one of these two directories inside its path:
|
The file path requires one of these two directories inside its path:
|
||||||
|
|
||||||
@ -1900,6 +1904,10 @@ can be disabled by setting `reload` to `false` in the request.
|
|||||||
If the validation for the new config stage failed, the old stage
|
If the validation for the new config stage failed, the old stage
|
||||||
and its configuration objects will remain active.
|
and its configuration objects will remain active.
|
||||||
|
|
||||||
|
Activation may be inhibited even for stages that validate correctly by setting
|
||||||
|
`activate` to `false`. This may be useful for validating the contents of a stage
|
||||||
|
without making it active, for example in a CI (continuous integration) system.
|
||||||
|
|
||||||
> **Note**
|
> **Note**
|
||||||
>
|
>
|
||||||
> Old stages are not purged automatically. You can [remove stages](12-icinga2-api.md#icinga2-api-config-management-delete-config-stage) that are no longer in use.
|
> Old stages are not purged automatically. You can [remove stages](12-icinga2-api.md#icinga2-api-config-management-delete-config-stage) that are no longer in use.
|
||||||
|
@ -171,7 +171,7 @@ void ConfigPackageUtility::ActivateStage(const String& packageName, const String
|
|||||||
WritePackageConfig(packageName);
|
WritePackageConfig(packageName);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigPackageUtility::TryActivateStageCallback(const ProcessResult& pr, const String& packageName, const String& stageName, bool reload)
|
void ConfigPackageUtility::TryActivateStageCallback(const ProcessResult& pr, const String& packageName, const String& stageName, bool activate, bool reload)
|
||||||
{
|
{
|
||||||
String logFile = GetPackageDir() + "/" + packageName + "/" + stageName + "/startup.log";
|
String logFile = GetPackageDir() + "/" + packageName + "/" + stageName + "/startup.log";
|
||||||
std::ofstream fpLog(logFile.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
std::ofstream fpLog(logFile.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc);
|
||||||
@ -185,6 +185,7 @@ void ConfigPackageUtility::TryActivateStageCallback(const ProcessResult& pr, con
|
|||||||
|
|
||||||
/* validation went fine, activate stage and reload */
|
/* validation went fine, activate stage and reload */
|
||||||
if (pr.ExitStatus == 0) {
|
if (pr.ExitStatus == 0) {
|
||||||
|
if (activate) {
|
||||||
{
|
{
|
||||||
boost::mutex::scoped_lock lock(GetStaticPackageMutex());
|
boost::mutex::scoped_lock lock(GetStaticPackageMutex());
|
||||||
|
|
||||||
@ -193,6 +194,7 @@ void ConfigPackageUtility::TryActivateStageCallback(const ProcessResult& pr, con
|
|||||||
|
|
||||||
if (reload)
|
if (reload)
|
||||||
Application::RequestRestart();
|
Application::RequestRestart();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Log(LogCritical, "ConfigPackageUtility")
|
Log(LogCritical, "ConfigPackageUtility")
|
||||||
<< "Config validation failed for package '"
|
<< "Config validation failed for package '"
|
||||||
@ -200,7 +202,7 @@ void ConfigPackageUtility::TryActivateStageCallback(const ProcessResult& pr, con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigPackageUtility::AsyncTryActivateStage(const String& packageName, const String& stageName, bool reload)
|
void ConfigPackageUtility::AsyncTryActivateStage(const String& packageName, const String& stageName, bool activate, bool reload)
|
||||||
{
|
{
|
||||||
VERIFY(Application::GetArgC() >= 1);
|
VERIFY(Application::GetArgC() >= 1);
|
||||||
|
|
||||||
@ -226,7 +228,7 @@ void ConfigPackageUtility::AsyncTryActivateStage(const String& packageName, cons
|
|||||||
|
|
||||||
Process::Ptr process = new Process(Process::PrepareCommand(args));
|
Process::Ptr process = new Process(Process::PrepareCommand(args));
|
||||||
process->SetTimeout(Application::GetReloadTimeout());
|
process->SetTimeout(Application::GetReloadTimeout());
|
||||||
process->Run(std::bind(&TryActivateStageCallback, _1, packageName, stageName, reload));
|
process->Run(std::bind(&TryActivateStageCallback, _1, packageName, stageName, activate, reload));
|
||||||
}
|
}
|
||||||
|
|
||||||
void ConfigPackageUtility::DeleteStage(const String& packageName, const String& stageName)
|
void ConfigPackageUtility::DeleteStage(const String& packageName, const String& stageName)
|
||||||
|
@ -37,7 +37,7 @@ public:
|
|||||||
static void SetActiveStage(const String& packageName, const String& stageName);
|
static void SetActiveStage(const String& packageName, const String& stageName);
|
||||||
static void SetActiveStageToFile(const String& packageName, const String& stageName);
|
static void SetActiveStageToFile(const String& packageName, const String& stageName);
|
||||||
static void ActivateStage(const String& packageName, const String& stageName);
|
static void ActivateStage(const String& packageName, const String& stageName);
|
||||||
static void AsyncTryActivateStage(const String& packageName, const String& stageName, bool reload);
|
static void AsyncTryActivateStage(const String& packageName, const String& stageName, bool activate, bool reload);
|
||||||
|
|
||||||
static std::vector<std::pair<String, bool> > GetFiles(const String& packageName, const String& stageName);
|
static std::vector<std::pair<String, bool> > GetFiles(const String& packageName, const String& stageName);
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ private:
|
|||||||
static void WritePackageConfig(const String& packageName);
|
static void WritePackageConfig(const String& packageName);
|
||||||
static void WriteStageConfig(const String& packageName, const String& stageName);
|
static void WriteStageConfig(const String& packageName, const String& stageName);
|
||||||
|
|
||||||
static void TryActivateStageCallback(const ProcessResult& pr, const String& packageName, const String& stageName, bool reload);
|
static void TryActivateStageCallback(const ProcessResult& pr, const String& packageName, const String& stageName, bool activate, bool reload);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -112,6 +112,11 @@ void ConfigStagesHandler::HandlePost(
|
|||||||
if (params->Contains("reload"))
|
if (params->Contains("reload"))
|
||||||
reload = HttpUtility::GetLastParameter(params, "reload");
|
reload = HttpUtility::GetLastParameter(params, "reload");
|
||||||
|
|
||||||
|
bool activate = true;
|
||||||
|
|
||||||
|
if (params->Contains("activate"))
|
||||||
|
activate = HttpUtility::GetLastParameter(params, "activate");
|
||||||
|
|
||||||
Dictionary::Ptr files = params->Get("files");
|
Dictionary::Ptr files = params->Get("files");
|
||||||
|
|
||||||
String stageName;
|
String stageName;
|
||||||
@ -120,12 +125,15 @@ void ConfigStagesHandler::HandlePost(
|
|||||||
if (!files)
|
if (!files)
|
||||||
BOOST_THROW_EXCEPTION(std::invalid_argument("Parameter 'files' must be specified."));
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Parameter 'files' must be specified."));
|
||||||
|
|
||||||
|
if (reload && !activate)
|
||||||
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Parameter 'reload' must be false when 'activate' is false."));
|
||||||
|
|
||||||
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticPackageMutex());
|
boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticPackageMutex());
|
||||||
|
|
||||||
stageName = ConfigPackageUtility::CreateStage(packageName, files);
|
stageName = ConfigPackageUtility::CreateStage(packageName, files);
|
||||||
|
|
||||||
/* validate the config. on success, activate stage and reload */
|
/* validate the config. on success, activate stage and reload */
|
||||||
ConfigPackageUtility::AsyncTryActivateStage(packageName, stageName, reload);
|
ConfigPackageUtility::AsyncTryActivateStage(packageName, stageName, activate, reload);
|
||||||
} catch (const std::exception& ex) {
|
} catch (const std::exception& ex) {
|
||||||
return HttpUtility::SendJsonError(response, params, 500,
|
return HttpUtility::SendJsonError(response, params, 500,
|
||||||
"Stage creation failed.",
|
"Stage creation failed.",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user