Leosac  0.7.0
OpenSourceAccessControl
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 }
SyncConfig(Kernel &kref, FetchRemoteConfigPtr fetch_task, bool sync_general_config, bool autocommit)
Definition: SyncConfig.cpp:31
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
const std::string & get_guid() const
Definition: Task.cpp:98
const boost::property_tree::ptree & module_config(const std::string &name) const
Returns the configuration for one specific module, identified by name.
This is the header file for a generated source file, GitSHA1.cpp.
const ModuleManager & module_manager() const
Returns a reference to the module manager object (const version).
Definition: kernel.cpp:405
Definition: log.hpp:34
bool has_config(const std::string &module) const
Do we have config information for the module.
That class helps manage the configuration for the application and its module.
const std::list< std::string > & modules_list() const noexcept
Return the list of modules that are loaded on the remote host.
Definition: log.hpp:33
FetchRemoteConfigPtr fetch_task_
The task that fetch the data.
Definition: SyncConfig.hpp:50
void initModules()
Actually call the init_module() function of each library we loaded.
bool is_module_importable(const std::string &) const
Helper around get_non_importable_modules()
const boost::property_tree::ptree & general_config() const
Returns the tree for the general configuration option.
Core of Leosac.
Definition: kernel.hpp:73
bool save_config()
Save the current configuration to its original file if autosave is enabled.
Definition: kernel.cpp:415
bool store_config(const std::string &module, const boost::property_tree::ptree &cfg)
Store the configuration tree for a module.
std::list< std::string > get_non_importable_modules() const
Returns a list of module name that should not be imported.
void set_kconfig(const boost::property_tree::ptree &new_cfg)
Update Leosac&#39;s core config ptree.
const FileNameContentList & additional_files(const std::string module) const
void stopModules(bool soft=false)
Opposite of init module.
bool loadModule(const std::string &module_name)
Search the path and load a module based on a property tree for this module.
CoreUtilsPtr core_utils()
Returns a (smart) pointer to the core utils: some thread-safe utilities.
Definition: kernel.cpp:447
std::vector< std::string > modules_names() const
Returns the list of the name of the loaded modules.
This class provides an API to collect the configuration of a remote Leosac unit.
Definition: log.hpp:32
Definition: log.hpp:35
const boost::property_tree::ptree & load_config(const std::string &module) const
Return the stored configuration for a given module.
uint64_t config_version() const
Return the current configuration version.
std::shared_ptr< FetchRemoteConfig > FetchRemoteConfigPtr
Definition: LeosacFwd.hpp:51
ConfigManager & config_manager()
Retrieve a reference to the configuration manager object.
Definition: kernel.cpp:436