Split ConfigItem::ActivateItems() into ConfigItem::ValidateItems() and ConfigItem::ActivateItems().

Also removes the -Z commandline parameter: won't be needed when this feature is done.

Refs #5788
This commit is contained in:
Gerd von Egidy 2014-04-13 21:12:18 +02:00 committed by Gunnar Beutner
parent 17d277ff16
commit 19afcd894a
4 changed files with 42 additions and 58 deletions

View File

@ -62,7 +62,7 @@ start() {
chmod 2755 $ICINGA2_STATE_DIR/run/icinga2/cmd chmod 2755 $ICINGA2_STATE_DIR/run/icinga2/cmd
echo "Starting Icinga 2: " echo "Starting Icinga 2: "
$DAEMON -c $ICINGA2_CONFIG_FILE -Z -d -e $ICINGA2_ERROR_LOG -u $ICINGA2_USER -g $ICINGA2_GROUP $DAEMON -c $ICINGA2_CONFIG_FILE -d -e $ICINGA2_ERROR_LOG -u $ICINGA2_USER -g $ICINGA2_GROUP
echo "Done" echo "Done"
echo echo

View File

@ -67,7 +67,7 @@ static String LoadAppType(const String& typeSpec)
return typeSpec.SubStr(index + 1); return typeSpec.SubStr(index + 1);
} }
static bool LoadConfigFiles(const String& appType, ValidationType validate) static bool LoadConfigFiles(const String& appType)
{ {
ConfigCompilerContext::GetInstance()->Reset(); ConfigCompilerContext::GetInstance()->Reset();
@ -88,7 +88,7 @@ static bool LoadConfigFiles(const String& appType, ValidationType validate)
ConfigItem::Ptr item = builder->Compile(); ConfigItem::Ptr item = builder->Compile();
item->Register(); item->Register();
bool result = ConfigItem::ActivateItems(validate); bool result = ConfigItem::ValidateItems();
int warnings = 0, errors = 0; int warnings = 0, errors = 0;
@ -250,7 +250,6 @@ int Main(void)
("config,c", po::value<std::vector<std::string> >(), "parse a configuration file") ("config,c", po::value<std::vector<std::string> >(), "parse a configuration file")
("no-config,z", "start without a configuration file") ("no-config,z", "start without a configuration file")
("validate,C", "exit after validating the configuration") ("validate,C", "exit after validating the configuration")
("no-validate,Z", "skip validating the configuration")
("debug,x", "enable debugging") ("debug,x", "enable debugging")
("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize)") ("errorlog,e", po::value<std::string>(), "log fatal errors to the specified log file (only works in combination with --daemonize)")
#ifndef _WIN32 #ifndef _WIN32
@ -406,6 +405,14 @@ int Main(void)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (!LoadConfigFiles(appType))
return EXIT_FAILURE;
if (g_AppParams.count("validate")) {
Log(LogInformation, "icinga-app", "Finished validating the configuration file(s).");
return EXIT_SUCCESS;
}
if (g_AppParams.count("daemonize")) { if (g_AppParams.count("daemonize")) {
String errorLog; String errorLog;
@ -416,20 +423,10 @@ int Main(void)
Logger::DisableConsoleLog(); Logger::DisableConsoleLog();
} }
ValidationType validate = ValidateStart; // activate config only after daemonization: it starts threads and that is not compatible with fork()
if (!ConfigItem::ActivateItems()) {
if (g_AppParams.count("validate")) Log(LogCritical, "icinga-app", "Error activating configuration.");
validate = ValidateOnly;
if (g_AppParams.count("no-validate"))
validate = ValidateNone;
if (!LoadConfigFiles(appType, validate))
return EXIT_FAILURE; return EXIT_FAILURE;
if (validate == ValidateOnly) {
Log(LogInformation, "icinga-app", "Finished validating the configuration file(s).");
return EXIT_SUCCESS;
} }
#ifndef _WIN32 #ifndef _WIN32

View File

@ -274,17 +274,13 @@ void ConfigItem::ValidateItem(void)
m_Validated = true; m_Validated = true;
} }
bool ConfigItem::ActivateItems(ValidationType validate) bool ConfigItem::ValidateItems(void)
{ {
if (ConfigCompilerContext::GetInstance()->HasErrors()) if (ConfigCompilerContext::GetInstance()->HasErrors())
return false; return false;
if (ConfigCompilerContext::GetInstance()->HasErrors())
return false;
ParallelWorkQueue upq; ParallelWorkQueue upq;
if (validate != ValidateNone) {
Log(LogInformation, "config", "Validating config items (step 1)..."); Log(LogInformation, "config", "Validating config items (step 1)...");
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) { BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
@ -295,8 +291,6 @@ bool ConfigItem::ActivateItems(ValidationType validate)
if (ConfigCompilerContext::GetInstance()->HasErrors()) if (ConfigCompilerContext::GetInstance()->HasErrors())
return false; return false;
} else
Log(LogInformation, "config", "Skipping validating config items (step 1)...");
Log(LogInformation, "config", "Committing config items"); Log(LogInformation, "config", "Committing config items");
@ -328,7 +322,6 @@ bool ConfigItem::ActivateItems(ValidationType validate)
Log(LogInformation, "config", "Evaluating 'object' rules..."); Log(LogInformation, "config", "Evaluating 'object' rules...");
ObjectRule::EvaluateRules(); ObjectRule::EvaluateRules();
if (validate != ValidateNone) {
Log(LogInformation, "config", "Validating config items (step 2)..."); Log(LogInformation, "config", "Validating config items (step 2)...");
BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) { BOOST_FOREACH(const ItemMap::value_type& kv, m_Items) {
@ -336,27 +329,25 @@ bool ConfigItem::ActivateItems(ValidationType validate)
} }
upq.Join(); upq.Join();
} else
Log(LogInformation, "config", "Skipping validating config items (step 2)...");
ConfigItem::DiscardItems(); ConfigItem::DiscardItems();
ConfigType::DiscardTypes(); ConfigType::DiscardTypes();
if (validate != ValidateNone) {
/* log stats for external parsers */ /* log stats for external parsers */
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) { BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
int count = std::distance(type->GetObjects().first, type->GetObjects().second); int count = std::distance(type->GetObjects().first, type->GetObjects().second);
if (count > 0) if (count > 0)
Log(LogInformation, "config", "Checked " + Convert::ToString(count) + " " + type->GetName() + "(s)."); Log(LogInformation, "config", "Checked " + Convert::ToString(count) + " " + type->GetName() + "(s).");
} }
return !ConfigCompilerContext::GetInstance()->HasErrors();
} }
bool ConfigItem::ActivateItems(void)
{
if (ConfigCompilerContext::GetInstance()->HasErrors()) if (ConfigCompilerContext::GetInstance()->HasErrors())
return false; return false;
if (validate == ValidateOnly)
return true;
/* restore the previous program state */ /* restore the previous program state */
try { try {
DynamicObject::RestoreObjects(Application::GetStatePath()); DynamicObject::RestoreObjects(Application::GetStatePath());
@ -366,6 +357,8 @@ bool ConfigItem::ActivateItems(ValidationType validate)
Log(LogInformation, "config", "Triggering Start signal for config items"); Log(LogInformation, "config", "Triggering Start signal for config items");
ParallelWorkQueue upq;
BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) { BOOST_FOREACH(const DynamicType::Ptr& type, DynamicType::GetTypes()) {
BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) { BOOST_FOREACH(const DynamicObject::Ptr& object, type->GetObjects()) {
if (object->IsActive()) if (object->IsActive())

View File

@ -27,13 +27,6 @@
namespace icinga namespace icinga
{ {
enum ValidationType
{
ValidateNone,
ValidateOnly,
ValidateStart
};
/** /**
* A configuration item. Non-abstract configuration items can be used to * A configuration item. Non-abstract configuration items can be used to
* create configuration objects at runtime. * create configuration objects at runtime.
@ -70,7 +63,8 @@ public:
void ValidateItem(void); void ValidateItem(void);
static bool ActivateItems(ValidationType validate); static bool ValidateItems(void);
static bool ActivateItems(void);
static void DiscardItems(void); static void DiscardItems(void);
private: private: