Leosac  0.8.0
Open Source Access Control
SyncConfig.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "SyncConfig.hpp"
21 #include "FetchRemoteConfig.hpp"
23 #include "core/kernel.hpp"
24 #include "tools/log.hpp"
25 #include <cassert>
26 #include <fstream>
27 
28 using namespace Leosac;
29 using namespace Leosac::Tasks;
30 
32  bool sync_general_config, bool autocommit)
33  : kernel_(kref)
34  , fetch_task_(fetch_task)
35  , sync_general_config_(sync_general_config)
36  , autocommit_(autocommit)
37 {
38  INFO("Creating SyncConfig task. Guid = " << get_guid());
39 }
40 
42 {
43  assert(fetch_task_->is_complete());
44  if (!fetch_task_->succeed())
45  {
46  WARN("Parent task (fetching config data) failed. Aborting SyncConfig task.");
47  return false;
48  }
49 
50  try
51  {
52  kernel_.core_utils()->config_checker().clear();
53  sync_config();
54  }
55  catch (const std::exception &e)
56  {
57  ERROR("SyncConfig task had a problem " << e.what());
58  return false;
59  }
60 
61  return true;
62 }
63 
65 {
66  const RemoteConfigCollector &collector = fetch_task_->collector();
68 
70  {
71  INFO("Also syncing general configuration.");
72  // syncing the global configure requires restart.
75  }
76 
78  for (const auto &name : collector.modules_list())
79  {
80  DEBUG("Handling module {" << name << "}");
82  {
83  INFO("Updating config for {" << name << "}");
85  collector.module_config(name));
86  // write additional file.
87  for (const std::pair<std::string, std::string> &file_info :
88  collector.additional_files(name))
89  {
90  INFO("Writing additional config file " << file_info.first);
91  std::ofstream of(file_info.first);
92  of << file_info.second;
93  }
94  }
95  else
96  { // If the module is immutable (aka conf not synchronized)
97  // we simply load its config from backup.
98  DEBUG("Apparently {" << name
99  << "} is immutable (declared in <no_import>)");
100  if (backup.has_config(name))
101  {
102  // if we prevent the import of a non-loaded module, we can't
103  // load from backup
105  backup.load_config(name));
106  }
107  }
108  }
109 
110  // reload config for our local immutable modules.
111  for (const auto &name : backup.get_non_importable_modules())
112  {
113  // Non importable can also hold "blacklisted" module that we don't want.
114  // We have to check.
115  if (backup.has_config(name))
116  {
117  DEBUG("Reloading config from backup for " << name);
118  kernel_.config_manager().store_config(name, backup.load_config(name));
119  }
120  else
121  {
122  DEBUG("Not reload config from backup for " << name);
123  }
124  }
125 
126  assert(kernel_.module_manager()
127  .modules_names()
128  .empty()); // we should have 0 module loaded.
129  for (const auto &name : collector.modules_list())
130  {
131  if (kernel_.config_manager().has_config(name))
132  { // load new module.
133  bool ret = kernel_.module_manager().loadModule(name);
134  if (!ret)
135  ERROR("Cannot load module " << name << "after synchronisation.");
136  assert(ret);
137  }
138  }
141  if (autocommit_)
142  {
143  INFO("Saving configuration to disk after synchronization.");
145  }
146 }
Leosac::RemoteConfigCollector::remote_version
uint64_t remote_version() const
Definition: RemoteConfigCollector.cpp:267
Leosac::Tasks::SyncConfig::kernel_
Kernel & kernel_
Definition: SyncConfig.hpp:46
Leosac::ConfigManager::load_config
const boost::property_tree::ptree & load_config(const std::string &module) const
Return the stored configuration for a given module.
Definition: ConfigManager.cpp:133
Leosac::Kernel::restart_later
void restart_later()
Set the running_ and want_restart flag so that leosac will restart in the next main loop iteration.
Definition: kernel.cpp:441
WARN
@ WARN
Definition: log.hpp:33
Leosac::Tasks::SyncConfig::sync_general_config_
bool sync_general_config_
Definition: SyncConfig.hpp:52
Leosac::RemoteConfigCollector
This class provides an API to collect the configuration of a remote Leosac unit.
Definition: RemoteConfigCollector.hpp:51
Leosac::ConfigManager
That class helps manage the configuration for the application and its module.
Definition: ConfigManager.hpp:45
SyncConfig.hpp
ERROR
@ ERROR
Definition: log.hpp:32
DEBUG
@ DEBUG
Definition: log.hpp:35
Leosac::RemoteConfigCollector::module_config
const boost::property_tree::ptree & module_config(const std::string &name) const
Returns the configuration for one specific module, identified by name.
Definition: RemoteConfigCollector.cpp:229
Leosac::Tasks::SyncConfig::SyncConfig
SyncConfig(Kernel &kref, FetchRemoteConfigPtr fetch_task, bool sync_general_config, bool autocommit)
Definition: SyncConfig.cpp:31
INFO
@ INFO
Definition: log.hpp:34
ModuleManager::initModules
void initModules()
Actually call the init_module() function of each library we loaded.
Definition: module_manager.cpp:69
Leosac::Tasks::SyncConfig::do_run
virtual bool do_run()
Definition: SyncConfig.cpp:41
Leosac::ConfigManager::store_config
bool store_config(const std::string &module, const boost::property_tree::ptree &cfg)
Store the configuration tree for a module.
Definition: ConfigManager.cpp:117
Leosac::ConfigManager::set_kconfig
void set_kconfig(const boost::property_tree::ptree &new_cfg)
Update Leosac's core config ptree.
Definition: ConfigManager.cpp:186
Leosac::RemoteConfigCollector::general_config
const boost::property_tree::ptree & general_config() const
Returns the tree for the general configuration option.
Definition: RemoteConfigCollector.cpp:240
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
kernel.hpp
ModuleManager::modules_names
std::vector< std::string > modules_names() const
Returns the list of the name of the loaded modules.
Definition: module_manager.cpp:278
Leosac::ConfigManager::has_config
bool has_config(const std::string &module) const
Do we have config information for the module.
Definition: ConfigManager.cpp:261
Leosac::Kernel::save_config
bool save_config()
Save the current configuration to its original file if autosave is enabled.
Definition: kernel.cpp:415
Leosac::RemoteConfigCollector::modules_list
const std::list< std::string > & modules_list() const noexcept
Return the list of modules that are loaded on the remote host.
Definition: RemoteConfigCollector.cpp:215
Leosac::Tasks::SyncConfig::fetch_task_
FetchRemoteConfigPtr fetch_task_
The task that fetch the data.
Definition: SyncConfig.hpp:50
Leosac::ConfigManager::config_version
uint64_t config_version() const
Return the current configuration version.
Definition: ConfigManager.cpp:251
Leosac::ConfigManager::get_non_importable_modules
std::list< std::string > get_non_importable_modules() const
Returns a list of module name that should not be imported.
Definition: ConfigManager.cpp:230
Leosac::Kernel
Core of Leosac.
Definition: kernel.hpp:73
FetchRemoteConfig.hpp
Leosac::ConfigManager::is_module_importable
bool is_module_importable(const std::string &) const
Helper around get_non_importable_modules()
Definition: ConfigManager.cpp:245
Leosac::Tasks::SyncConfig::autocommit_
bool autocommit_
Definition: SyncConfig.hpp:53
log.hpp
ModuleManager::stopModules
void stopModules(bool soft=false)
Opposite of init module.
Definition: module_manager.cpp:198
Leosac::RemoteConfigCollector::additional_files
const FileNameContentList & additional_files(const std::string module) const
Definition: RemoteConfigCollector.cpp:246
Leosac::Tasks
Definition: FetchRemoteConfig.hpp:28
ModuleManager::loadModule
bool loadModule(const std::string &module_name)
Search the path and load a module based on a property tree for this module.
Definition: module_manager.cpp:154
Leosac::Tasks::FetchRemoteConfigPtr
std::shared_ptr< FetchRemoteConfig > FetchRemoteConfigPtr
Definition: LeosacFwd.hpp:51
Leosac::Kernel::core_utils
CoreUtilsPtr core_utils()
Returns a (smart) pointer to the core utils: some thread-safe utilities.
Definition: kernel.cpp:447
Leosac::Tasks::Task::get_guid
const std::string & get_guid() const
Definition: Task.cpp:98
ConfigManager.hpp
Leosac::Kernel::module_manager
const ModuleManager & module_manager() const
Returns a reference to the module manager object (const version).
Definition: kernel.cpp:405
Leosac::Kernel::config_manager
ConfigManager & config_manager()
Retrieve a reference to the configuration manager object.
Definition: kernel.cpp:436
Leosac::Tasks::SyncConfig::sync_config
void sync_config()
Definition: SyncConfig.cpp:64