Leosac  0.8.0
Open Source Access Control
UpdateService.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 "UpdateService.hpp"
22 #include "core/update/Update_odb.h"
23 #include "tools/GenGuid.h"
25 
26 namespace Leosac
27 {
28 namespace update
29 {
31 {
32  check_update_sig_.connect(
33  CheckUpdateT::slot_type(&UpdateBackend::check_update, backend.get())
34  .track_foreign(backend));
35 
36  create_update_sig_.connect(
37  CreateUpdateT::slot_type(&UpdateBackend::create_update, backend.get(),
38  boost::placeholders::_1, boost::placeholders::_2)
39  .track_foreign(backend));
40 
41  ack_update_sig_.connect(
42  AckUpdateT::slot_type(&UpdateBackend::ack_update, backend.get(),
43  boost::placeholders::_1, boost::placeholders::_2)
44  .track_foreign(backend));
45 
46  cancel_update_sig_.connect(
47  CancelUpdateT::slot_type(&UpdateBackend::cancel_update, backend.get(),
48  boost::placeholders::_1, boost::placeholders::_2)
49  .track_foreign(backend));
50 }
51 
52 std::vector<UpdateDescriptorPtr> UpdateService::check_update()
53 {
54  auto descriptors = check_update_sig_();
55 
56  // When a new check_update happens, we clear all
57  // previous update descriptor as they would possibly
58  // become outdated.
59  published_descriptors_.clear();
60  for (const auto &descriptor : descriptors)
61  {
62  // This is so a client can reference a descriptor later to
63  // trigger creation of an update. (This is needed since we don't want
64  // update descriptor to be database-persisted).
65  published_descriptors_[descriptor->uuid] = descriptor;
66  }
67 
68  return descriptors;
69 }
70 
71 std::vector<IUpdatePtr> UpdateService::pending_updates()
72 {
73  const auto &db = get_service_registry().get_service<DBService>()->db();
74  db::OptionalTransaction t(db->begin());
75 
76  std::vector<IUpdatePtr> updates;
77  using Query = odb::query<update::Update>;
78  auto updates_odb =
79  db->query<update::Update>(Query::status == update::Status::PENDING);
80 
81  for (auto i(updates_odb.begin()); i != updates_odb.end(); ++i)
82  {
83  IUpdatePtr ptr(i.load());
84  ASSERT_LOG(ptr, "Loading failed, but object should already be loaded.");
85  updates.push_back(ptr);
86  }
87  t.commit();
88  return updates;
89 }
90 
91 IUpdatePtr UpdateService::create_update(const std::string &update_descriptor_uuid,
92  const ExecutionContext &ec)
93 {
94  auto itr = published_descriptors_.find(update_descriptor_uuid);
95  if (itr == published_descriptors_.end())
96  {
97  throw LEOSACException("UpdateDescriptor doesn't exist anymore.");
98  }
99 
100  return create_update_sig_(*(itr->second), ec);
101 }
102 
104 {
105  ack_update_sig_(update, ec);
106 }
107 
109 {
110  cancel_update_sig_(update, ec);
111 }
112 
114  : uuid(gen_uuid())
115  , severity(Severity::NORMAL)
116 {
117 }
118 }
119 }
Leosac::db::OptionalTransaction
An optional transaction is an object that behave like an odb::transaction if there is no currently ac...
Definition: OptionalTransaction.hpp:43
Leosac::db::OptionalTransaction::commit
void commit()
Commit the transaction, if there was no currently active transaction at the time of this object's cre...
Definition: OptionalTransaction.cpp:38
Leosac::update::Severity
Severity
Definition: UpdateService.hpp:34
Leosac::update::UpdateBackend::cancel_update
virtual void cancel_update(IUpdatePtr u, const ExecutionContext &)=0
Cancel (not rollback) the pending update u.
UpdateService.hpp
Leosac::update::Status::PENDING
@ PENDING
Leosac::get_service_registry
ServiceRegistry & get_service_registry()
A function to retrieve the ServiceRegistry from pretty much anywhere.
Definition: GetServiceRegistry.cpp:25
Leosac::update::UpdateBackend::check_update
virtual std::vector< UpdateDescriptorPtr > check_update()=0
Check for updates against arbitrary, module-owned object.
Leosac::update::UpdateService::published_descriptors_
std::map< std::string, UpdateDescriptorPtr > published_descriptors_
Definition: UpdateService.hpp:185
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::update::UpdateService::cancel_update
void cancel_update(IUpdatePtr update, const ExecutionContext &ec)
Definition: UpdateService.cpp:108
Leosac::update::IUpdatePtr
std::shared_ptr< IUpdate > IUpdatePtr
Definition: UpdateFwd.hpp:42
Leosac::update::UpdateService::pending_updates
std::vector< IUpdatePtr > pending_updates()
Retrieve the list of pending updates.
Definition: UpdateService.cpp:71
Leosac::update::UpdateDescriptor::UpdateDescriptor
UpdateDescriptor()
Definition: UpdateService.cpp:113
Query
odb::query< Tools::LogEntry > Query
Definition: LogEntry.cpp:36
Leosac::DBService
Provides various database-related services to consumer.
Definition: DBService.hpp:34
Leosac::update::UpdateService::check_update_sig_
CheckUpdateT check_update_sig_
Definition: UpdateService.hpp:180
Leosac::update::UpdateService::cancel_update_sig_
CancelUpdateT cancel_update_sig_
Definition: UpdateService.hpp:183
Leosac::update::UpdateBackendPtr
std::shared_ptr< UpdateBackend > UpdateBackendPtr
Definition: UpdateFwd.hpp:49
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::update::UpdateService::register_backend
void register_backend(UpdateBackendPtr backend)
Register a backend object.
Definition: UpdateService.cpp:30
Leosac::ExecutionContext
An ExecutionContext is passed around to service so they have context about who is making the call and...
Definition: SecurityContext.hpp:301
OptionalTransaction.hpp
GetServiceRegistry.hpp
Leosac::update::Update
Represent an update.
Definition: Update.hpp:39
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
GenGuid.h
Leosac::update::UpdateService::check_update
std::vector< UpdateDescriptorPtr > check_update()
Definition: UpdateService.cpp:52
Leosac::gen_uuid
std::string gen_uuid()
Generate a new UUID.
Definition: GenGuid.cpp:26
Leosac::update::UpdateService::ack_update_sig_
AckUpdateT ack_update_sig_
Definition: UpdateService.hpp:182
Leosac::update::UpdateService::ack_update
void ack_update(IUpdatePtr update, const ExecutionContext &ec)
Definition: UpdateService.cpp:103
Leosac::ServiceRegistry::get_service
std::shared_ptr< ServiceInterface > get_service() const
Retrieve the service instance implementing the ServiceInterface, or nullptr if no such service was re...
Definition: ServiceRegistry.hpp:290
Leosac::update::UpdateBackend::ack_update
virtual void ack_update(IUpdatePtr u, const ExecutionContext &)=0
Acknowledge the pending update u.
Leosac::update::UpdateService::create_update
IUpdatePtr create_update(const std::string &update_descriptor_uuid, const ExecutionContext &ec)
Create an Update object corresponding to the update descriptor whose uuid is update_descriptor_uuid.
Definition: UpdateService.cpp:91
Leosac::update::Severity::NORMAL
@ NORMAL
Leosac::update::UpdateService::create_update_sig_
CreateUpdateT create_update_sig_
Definition: UpdateService.hpp:181
Leosac::update::UpdateBackend::create_update
virtual IUpdatePtr create_update(const UpdateDescriptor &ud, const ExecutionContext &)=0
Create an update based on the UpdateDescriptor ud.