2014-10-13 18:09:58 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012-2014 Icinga Development Team (http://www.icinga.org) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "cli/featureenablecommand.hpp"
|
2014-10-18 19:31:52 +02:00
|
|
|
#include "cli/featureutility.hpp"
|
2014-10-19 14:21:12 +02:00
|
|
|
#include "base/logger.hpp"
|
2014-10-13 18:09:58 +02:00
|
|
|
#include "base/application.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2014-10-18 19:31:52 +02:00
|
|
|
#include "base/console.hpp"
|
2014-10-13 18:09:58 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/algorithm/string/join.hpp>
|
2014-10-19 00:15:42 +02:00
|
|
|
#include <iostream>
|
2014-10-13 18:09:58 +02:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
|
|
|
|
REGISTER_CLICOMMAND("feature/enable", FeatureEnableCommand);
|
|
|
|
|
|
|
|
String FeatureEnableCommand::GetDescription(void) const
|
|
|
|
{
|
|
|
|
return "Enables specified Icinga 2 feature.";
|
|
|
|
}
|
|
|
|
|
|
|
|
String FeatureEnableCommand::GetShortDescription(void) const
|
|
|
|
{
|
|
|
|
return "enables specified feature";
|
|
|
|
}
|
|
|
|
|
2014-10-18 19:31:52 +02:00
|
|
|
std::vector<String> FeatureEnableCommand::GetPositionalSuggestions(const String& word) const
|
2014-10-13 18:09:58 +02:00
|
|
|
{
|
2014-10-18 19:31:52 +02:00
|
|
|
return FeatureUtility::GetFieldCompletionSuggestions(FeatureCommandEnable, word);
|
2014-10-13 18:09:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The entry point for the "feature enable" CLI command.
|
|
|
|
*
|
|
|
|
* @returns An exit status.
|
|
|
|
*/
|
|
|
|
int FeatureEnableCommand::Run(const boost::program_options::variables_map& vm, const std::vector<std::string>& ap) const
|
|
|
|
{
|
|
|
|
String features_available_dir = Application::GetSysconfDir() + "/icinga2/features-available";
|
2014-10-15 08:43:20 +02:00
|
|
|
String features_enabled_dir = Application::GetSysconfDir() + "/icinga2/features-enabled";
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (ap.empty()) {
|
|
|
|
Log(LogCritical, "cli", "Cannot enable feature(s). Name(s) are missing!");
|
|
|
|
return 0;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (!Utility::PathExists(features_available_dir) ) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot parse available features. Path '" << features_available_dir << "' does not exist.";
|
2014-10-15 08:43:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (!Utility::PathExists(features_enabled_dir) ) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot enable features. Path '" << features_enabled_dir << "' does not exist.";
|
2014-10-15 08:43:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
std::vector<std::string> errors;
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
BOOST_FOREACH(const String& feature, ap) {
|
|
|
|
String source = features_available_dir + "/" + feature + ".conf";
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (!Utility::PathExists(source) ) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot enable feature '" << feature << "'. Source file '" << source + "' does not exist.";
|
2014-10-15 08:43:20 +02:00
|
|
|
errors.push_back(feature);
|
|
|
|
continue;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
String target = features_enabled_dir + "/" + feature + ".conf";
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (Utility::PathExists(target) ) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogWarning, "cli")
|
|
|
|
<< "Feature '" << feature << "' already enabled.";
|
2014-10-15 08:43:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-20 13:16:22 +02:00
|
|
|
std::cout << "Enabling feature " << ConsoleColorTag(Console_ForegroundMagenta | Console_Bold) << feature
|
|
|
|
<< ConsoleColorTag(Console_Normal) << ". Make sure to restart Icinga 2 for these changes to take effect.\n";
|
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
if (symlink(source.CStr(), target.CStr()) < 0) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot enable feature '" << feature << "'. Linking source '" << source << "' to target file '" << target
|
|
|
|
<< "' failed with error code " << errno << ", \"" << Utility::FormatErrorNumber(errno) << "\".";
|
2014-10-15 08:43:20 +02:00
|
|
|
errors.push_back(feature);
|
|
|
|
continue;
|
2014-10-13 18:09:58 +02:00
|
|
|
}
|
2014-10-15 08:43:20 +02:00
|
|
|
#else /* _WIN32 */
|
|
|
|
std::ofstream fp;
|
|
|
|
fp.open(target.CStr());
|
2014-10-20 13:16:22 +02:00
|
|
|
fp << "include \"../features-available/" << feature << ".conf\"" << std::endl;
|
|
|
|
fp.close();
|
|
|
|
|
|
|
|
if (fp.fail()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot enable feature '" << feature << "'. Failed to open file '" << target << "'.";
|
2014-10-15 08:43:20 +02:00
|
|
|
errors.push_back(feature);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
2014-10-15 08:43:20 +02:00
|
|
|
if (!errors.empty()) {
|
2014-10-19 17:52:17 +02:00
|
|
|
Log(LogCritical, "cli")
|
|
|
|
<< "Cannot enable feature(s): " << boost::algorithm::join(errors, " ");
|
2014-10-13 18:09:58 +02:00
|
|
|
errors.clear();
|
2014-10-15 08:43:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2014-10-13 18:09:58 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|