Implement Windows support for "feature enable"

fixes #7377
This commit is contained in:
Gunnar Beutner 2014-10-15 08:43:20 +02:00
parent 14d8366d05
commit 39116e4906

View File

@ -27,7 +27,7 @@
#include <fstream> #include <fstream>
#include <vector> #include <vector>
#include <string> #include <string>
#include <unistd.h> #include <fstream>
using namespace icinga; using namespace icinga;
namespace po = boost::program_options; namespace po = boost::program_options;
@ -58,10 +58,6 @@ void FeatureEnableCommand::InitParameters(boost::program_options::options_descri
*/ */
int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
{ {
#ifdef _WIN32
//TODO: Add Windows support
Log(LogInformation, "cli", "This command is not available on Windows.");
#else
String features_available_dir = Application::GetSysconfDir() + "/icinga2/features-available"; String features_available_dir = Application::GetSysconfDir() + "/icinga2/features-available";
String features_enabled_dir = Application::GetSysconfDir() + "/icinga2/features-enabled"; String features_enabled_dir = Application::GetSysconfDir() + "/icinga2/features-enabled";
@ -98,14 +94,26 @@ int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, c
continue; continue;
} }
Log(LogInformation, "cli", "Enabling feature '" + feature + "' in '" + features_enabled_dir + "'.");
#ifndef _WIN32
if (symlink(source.CStr(), target.CStr()) < 0) { if (symlink(source.CStr(), target.CStr()) < 0) {
Log(LogCritical, "cli", "Cannot enable feature '" + feature + "'. Linking source '" + source + "' to target file '" + target + Log(LogCritical, "cli", "Cannot enable feature '" + feature + "'. Linking source '" + source + "' to target file '" + target +
"' failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\"."); "' failed with error code " + Convert::ToString(errno) + ", \"" + Utility::FormatErrorNumber(errno) + "\".");
errors.push_back(feature); errors.push_back(feature);
continue; continue;
} }
#else /* _WIN32 */
Log(LogInformation, "cli", "Enabling feature '" + feature + "' in '" + features_enabled_dir + "'."); std::ofstream fp;
fp.open(target.CStr());
if (!fp) {
Log(LogCritical, "cli", "Cannot enable feature '" + feature + "'. Failed to open file '" + target + "'.");
errors.push_back(feature);
continue;
}
fp << "include \"../features-available/" << feature << ".conf\"" << std::endl;
fp.close();
#endif /* _WIN32 */
} }
if (!errors.empty()) { if (!errors.empty()) {
@ -114,7 +122,5 @@ int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, c
return 1; return 1;
} }
#endif /* _WIN32 */
return 0; return 0;
} }