• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 950545443

pending completion
950545443

push

gitlab-ci

vpodzime
feat: Introduce deployments::DeploymentLog

A simple class that can scope logging of a specific deployment.

Ticket: MEN-6578
Changelog: none
Signed-off-by: Vratislav Podzimek <v.podzimek@mykolab.com>

93 of 93 new or added lines in 1 file covered. (100.0%)

4283 of 5387 relevant lines covered (79.51%)

159.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/mender-update/deployments.hpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#ifndef MENDER_UPDATE_DEPLOYMENTS_HPP
16
#define MENDER_UPDATE_DEPLOYMENTS_HPP
17

18
#include <config.h>
19

20
#ifdef MENDER_LOG_BOOST
21
#include <boost/log/sinks/sync_frontend.hpp>
22
#include <boost/log/sinks/text_ostream_backend.hpp>
23
#include <boost/smart_ptr/shared_ptr.hpp>
24
#endif // MENDER_LOG_BOOST
25

26
#include <string>
27
#include <vector>
28

29
#include <common/error.hpp>
30
#include <common/events.hpp>
31
#include <common/expected.hpp>
32
#include <common/http.hpp>
33
#include <common/io.hpp>
34
#include <common/json.hpp>
35
#include <common/optional.hpp>
36
#include <mender-update/context.hpp>
37

38
namespace mender {
39
namespace update {
40
namespace deployments {
41

42
using namespace std;
43

44
#ifdef MENDER_LOG_BOOST
45
namespace sinks = boost::log::sinks;
46
#endif // MENDER_LOG_BOOST
47

48
namespace context = mender::update::context;
49
namespace error = mender::common::error;
50
namespace events = mender::common::events;
51
namespace expected = mender::common::expected;
52
namespace io = mender::common::io;
53
namespace json = mender::common::json;
54
namespace optional = mender::common::optional;
55

56
enum DeploymentsErrorCode {
57
        NoError = 0,
58
        InvalidDataError,
59
        BadResponseError,
60
};
61

62
class DeploymentsErrorCategoryClass : public std::error_category {
63
public:
64
        const char *name() const noexcept override;
65
        string message(int code) const override;
66
};
67
extern const DeploymentsErrorCategoryClass DeploymentsErrorCategory;
68

69
error::Error MakeError(DeploymentsErrorCode code, const string &msg);
70

71
using CheckUpdatesAPIResponse = expected::expected<optional::optional<json::Json>, error::Error>;
72
using CheckUpdatesAPIResponseHandler = function<void(CheckUpdatesAPIResponse)>;
73

74
error::Error CheckNewDeployments(
75
        context::MenderContext &ctx,
76
        const string &server_url,
77
        http::Client &client,
78
        CheckUpdatesAPIResponseHandler api_handler);
79

80
enum class DeploymentStatus {
81
        Installing = 0,
82
        PauseBeforeInstalling,
83
        Downloading,
84
        PauseBeforeRebooting,
85
        Rebooting,
86
        PauseBeforeCommitting,
87
        Success,
88
        Failure,
89
        AlreadyInstalled,
90

91
        // Not a valid status, just used as an int representing the number of values
92
        // above
93
        End_
94
};
95

96
using StatusAPIResponse = error::Error;
97
using StatusAPIResponseHandler = function<void(StatusAPIResponse)>;
98

99
error::Error PushStatus(
100
        const string &deployment_id,
101
        DeploymentStatus status,
102
        const string &substate,
103
        const string &server_url,
104
        http::Client &client,
105
        StatusAPIResponseHandler api_handler);
106

107
using LogsAPIResponse = error::Error;
108
using LogsAPIResponseHandler = function<void(LogsAPIResponse)>;
109

110
error::Error PushLogs(
111
        const string &deployment_id,
112
        const string &log_file_path,
113
        const string &server_url,
114
        http::Client &client,
115
        LogsAPIResponseHandler api_handler);
116

117
/**
118
 * A helper class only declared here because of testing. Not to be used
119
 * separately outside of PushLogs().
120
 */
121
class JsonLogMessagesReader : virtual public io::Reader {
122
public:
123
        /**
124
         * @see GetLogFileDataSize() for details about #data_size
125
         */
126
        JsonLogMessagesReader(shared_ptr<io::FileReader> raw_data_reader, size_t data_size) :
×
127
                reader_ {raw_data_reader},
128
                raw_data_size_ {data_size},
129
                rem_raw_data_size_ {data_size} {};
×
130

131
        expected::ExpectedSize Read(
132
                vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) override;
133

134
        error::Error Rewind() {
×
135
                header_rem_ = header_.size();
×
136
                closing_rem_ = closing_.size();
×
137
                rem_raw_data_size_ = raw_data_size_;
×
138
                return reader_->Rewind();
×
139
        }
140

141
        static size_t TotalDataSize(size_t raw_data_size) {
×
142
                return raw_data_size + header_.size() + closing_.size();
×
143
        }
144

145
private:
146
        shared_ptr<io::FileReader> reader_;
147
        size_t raw_data_size_;
148
        size_t rem_raw_data_size_;
149
        static const vector<uint8_t> header_;
150
        static const vector<uint8_t> closing_;
151
        io::Vsize header_rem_ = header_.size();
152
        io::Vsize closing_rem_ = closing_.size();
153
};
154

155
class DeploymentLog {
156
public:
157
        DeploymentLog(const string &data_store_dir, const string &deployment_id) :
158
                data_store_dir_ {data_store_dir},
159
                id_ {deployment_id} {};
160
        error::Error BeginLogging();
161
        error::Error FinishLogging();
162
        ~DeploymentLog() {
163
                if (sink_) {
164
                        FinishLogging();
165
                }
166
        };
167

168
private:
169
        const string data_store_dir_;
170
        const string id_;
171
#ifdef MENDER_LOG_BOOST
172
        typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
173
        boost::shared_ptr<text_sink> sink_;
174
#endif // MENDER_LOG_BOOST
175
        error::Error PrepareLogDirectory();
176
};
177

178
} // namespace deployments
179
} // namespace update
180
} // namespace mender
181

182
#endif // MENDER_UPDATE_DEPLOYMENTS_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc