Merge pull request #5811 from Icinga/feature/update-constants-conf-api-setup

Update NodeName/ZoneName constants with 'api setup'
This commit is contained in:
Michael Friedrich 2017-12-07 17:35:46 +01:00 committed by GitHub
commit aec231b119
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 32 deletions

View File

@ -54,6 +54,9 @@ bool ApiSetupUtility::SetupMaster(const String& cn, bool prompt_restart)
if (!SetupMasterEnableApi()) if (!SetupMasterEnableApi())
return false; return false;
if (!SetupMasterUpdateConstants(cn))
return false;
if (prompt_restart) { if (prompt_restart) {
std::cout << "Done.\n\n"; std::cout << "Done.\n\n";
std::cout << "Now restart your Icinga 2 daemon to finish the installation!\n\n"; std::cout << "Now restart your Icinga 2 daemon to finish the installation!\n\n";
@ -196,3 +199,9 @@ bool ApiSetupUtility::SetupMasterEnableApi(void)
return true; return true;
} }
bool ApiSetupUtility::SetupMasterUpdateConstants(const String& cn)
{
NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn);
}

View File

@ -42,6 +42,7 @@ public:
static bool SetupMasterCertificates(const String& cn); static bool SetupMasterCertificates(const String& cn);
static bool SetupMasterApiUser(void); static bool SetupMasterApiUser(void);
static bool SetupMasterEnableApi(void); static bool SetupMasterEnableApi(void);
static bool SetupMasterUpdateConstants(const String& cn);
static String GetConfdPath(void); static String GetConfdPath(void);

View File

@ -208,10 +208,6 @@ int NodeSetupCommand::SetupMaster(const boost::program_options::variables_map& v
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!"; << "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
} }
Log(LogInformation, "cli", "Updating constants.conf.");
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn); NodeUtility::UpdateConstant("ZoneName", cn);
@ -426,10 +422,6 @@ int NodeSetupCommand::SetupNode(const boost::program_options::variables_map& vm,
<< "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!"; << "CN '" << cn << "' does not match the default FQDN '" << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!";
} }
Log(LogInformation, "cli", "Updating constants.conf.");
NodeUtility::CreateBackupFile(Application::GetSysconfDir() + "/icinga2/constants.conf");
NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", vm["zone"].as<std::string>()); NodeUtility::UpdateConstant("ZoneName", vm["zone"].as<std::string>());

View File

@ -43,6 +43,11 @@
using namespace icinga; using namespace icinga;
String NodeUtility::GetConstantsConfPath(void)
{
return Application::GetSysconfDir() + "/icinga2/constants.conf";
}
/* /*
* Node Setup helpers * Node Setup helpers
*/ */
@ -240,7 +245,7 @@ bool NodeUtility::CreateBackupFile(const String& target, bool is_private)
String backup = target + ".orig"; String backup = target + ".orig";
if (Utility::PathExists(backup)) { if (Utility::PathExists(backup)) {
Log(LogWarning, "cli") Log(LogInformation, "cli")
<< "Backup file '" << backup << "' already exists. Skipping backup."; << "Backup file '" << backup << "' already exists. Skipping backup.";
return false; return false;
} }
@ -283,16 +288,18 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec
void NodeUtility::UpdateConstant(const String& name, const String& value) void NodeUtility::UpdateConstant(const String& name, const String& value)
{ {
String constantsFile = Application::GetSysconfDir() + "/icinga2/constants.conf"; String constantsConfPath = NodeUtility::GetConstantsConfPath();
std::ifstream ifp(constantsFile.CStr());
std::fstream ofp;
String tempFile = Utility::CreateTempFile(constantsFile + ".XXXXXX", 0644, ofp);
bool found = false;
Log(LogInformation, "cli") Log(LogInformation, "cli")
<< "Updating constants file '" << constantsFile << "'."; << "Updating '" << name << "' constant in '" << constantsConfPath << "'.";
NodeUtility::CreateBackupFile(constantsConfPath);
std::ifstream ifp(constantsConfPath.CStr());
std::fstream ofp;
String tempFile = Utility::CreateTempFile(constantsConfPath + ".XXXXXX", 0644, ofp);
bool found = false;
std::string line; std::string line;
while (std::getline(ifp, line)) { while (std::getline(ifp, line)) {
@ -310,13 +317,13 @@ void NodeUtility::UpdateConstant(const String& name, const String& value)
ofp.close(); ofp.close();
#ifdef _WIN32 #ifdef _WIN32
_unlink(constantsFile.CStr()); _unlink(constantsConfPath.CStr());
#endif /* _WIN32 */ #endif /* _WIN32 */
if (rename(tempFile.CStr(), constantsFile.CStr()) < 0) { if (rename(tempFile.CStr(), constantsConfPath.CStr()) < 0) {
BOOST_THROW_EXCEPTION(posix_error() BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename") << boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno) << boost::errinfo_errno(errno)
<< boost::errinfo_file_name(constantsFile)); << boost::errinfo_file_name(constantsConfPath));
} }
} }

View File

@ -37,6 +37,8 @@ namespace icinga
class I2_CLI_API NodeUtility class I2_CLI_API NodeUtility
{ {
public: public:
static String GetConstantsConfPath(void);
static bool CreateBackupFile(const String& target, bool is_private = false); static bool CreateBackupFile(const String& target, bool is_private = false);
static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects); static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects);

View File

@ -507,12 +507,6 @@ wizard_ticket:
<< Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!"; << Utility::GetFQDN() << "'. Requires update for NodeName constant in constants.conf!";
} }
Log(LogInformation, "cli", "Updating constants.conf.");
String constants_file = Application::GetSysconfDir() + "/icinga2/constants.conf";
NodeUtility::CreateBackupFile(constants_file);
NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn); NodeUtility::UpdateConstant("ZoneName", cn);
@ -660,12 +654,6 @@ int NodeWizardCommand::MasterSetup(void) const
<< Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!"; << Utility::GetFQDN() << "'. Requires an update for the NodeName constant in constants.conf!";
} }
Log(LogInformation, "cli", "Updating constants.conf.");
String constants_file = Application::GetSysconfDir() + "/icinga2/constants.conf";
NodeUtility::CreateBackupFile(constants_file);
NodeUtility::UpdateConstant("NodeName", cn); NodeUtility::UpdateConstant("NodeName", cn);
NodeUtility::UpdateConstant("ZoneName", cn); NodeUtility::UpdateConstant("ZoneName", cn);