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

mendersoftware / mender / 1022567176

02 Oct 2023 07:50AM UTC coverage: 80.127% (+2.5%) from 77.645%
1022567176

push

gitlab-ci

kacf
chore: Centralize selection of `std::filesystem` library.

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

6447 of 8046 relevant lines covered (80.13%)

9912.21 hits per line

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

77.78
/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

55
enum DeploymentsErrorCode {
56
        NoError = 0,
57
        InvalidDataError,
58
        BadResponseError,
59
        DeploymentAbortedError,
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<json::Json>, error::Error>;
72
using CheckUpdatesAPIResponseHandler = function<void(CheckUpdatesAPIResponse)>;
73

74
enum class DeploymentStatus {
75
        Installing = 0,
76
        PauseBeforeInstalling,
77
        Downloading,
78
        PauseBeforeRebooting,
79
        Rebooting,
80
        PauseBeforeCommitting,
81
        Success,
82
        Failure,
83
        AlreadyInstalled,
84

85
        // Not a valid status, just used as an int representing the number of values
86
        // above
87
        End_
88
};
89

90
string DeploymentStatusString(DeploymentStatus status);
91

92
using StatusAPIResponse = error::Error;
93
using StatusAPIResponseHandler = function<void(StatusAPIResponse)>;
94

95
using LogsAPIResponse = error::Error;
96
using LogsAPIResponseHandler = function<void(LogsAPIResponse)>;
97

98
class DeploymentAPI {
99
public:
100
        virtual ~DeploymentAPI() {
101
        }
102

103
        virtual error::Error CheckNewDeployments(
104
                context::MenderContext &ctx,
105
                const string &server_url,
106
                http::Client &client,
107
                CheckUpdatesAPIResponseHandler api_handler) = 0;
108
        virtual error::Error PushStatus(
109
                const string &deployment_id,
110
                DeploymentStatus status,
111
                const string &substate,
112
                const string &server_url,
113
                http::Client &client,
114
                StatusAPIResponseHandler api_handler) = 0;
115
        virtual error::Error PushLogs(
116
                const string &deployment_id,
117
                const string &log_file_path,
118
                const string &server_url,
119
                http::Client &client,
120
                LogsAPIResponseHandler api_handler) = 0;
121
};
122

123
class DeploymentClient : virtual public DeploymentAPI {
124
public:
125
        error::Error CheckNewDeployments(
126
                context::MenderContext &ctx,
127
                const string &server_url,
128
                http::Client &client,
129
                CheckUpdatesAPIResponseHandler api_handler) override;
130
        error::Error PushStatus(
131
                const string &deployment_id,
132
                DeploymentStatus status,
133
                const string &substate,
134
                const string &server_url,
135
                http::Client &client,
136
                StatusAPIResponseHandler api_handler) override;
137
        error::Error PushLogs(
138
                const string &deployment_id,
139
                const string &log_file_path,
140
                const string &server_url,
141
                http::Client &client,
142
                LogsAPIResponseHandler api_handler) override;
143
};
144

145
/**
146
 * A helper class only declared here because of testing. Not to be used
147
 * separately outside of PushLogs().
148
 */
149
class JsonLogMessagesReader : virtual public io::Reader {
150
public:
151
        /**
152
         * @see GetLogFileDataSize() for details about #data_size
153
         */
154
        JsonLogMessagesReader(shared_ptr<io::FileReader> raw_data_reader, size_t data_size) :
×
155
                reader_ {raw_data_reader},
156
                raw_data_size_ {data_size},
157
                rem_raw_data_size_ {data_size} {};
×
158

159
        expected::ExpectedSize Read(
160
                vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) override;
161

162
        error::Error Rewind() {
3✔
163
                header_rem_ = header_.size();
3✔
164
                closing_rem_ = closing_.size();
3✔
165
                rem_raw_data_size_ = raw_data_size_;
3✔
166
                return reader_->Rewind();
3✔
167
        }
168

169
        static size_t TotalDataSize(size_t raw_data_size) {
3✔
170
                return raw_data_size + header_.size() + closing_.size();
3✔
171
        }
172

173
private:
174
        shared_ptr<io::FileReader> reader_;
175
        size_t raw_data_size_;
176
        size_t rem_raw_data_size_;
177
        static const vector<uint8_t> header_;
178
        static const vector<uint8_t> closing_;
179
        io::Vsize header_rem_ = header_.size();
180
        io::Vsize closing_rem_ = closing_.size();
181
};
182

183
class DeploymentLog {
184
public:
185
        DeploymentLog(const string &data_store_dir, const string &deployment_id) :
186
                data_store_dir_ {data_store_dir},
187
                id_ {deployment_id} {};
188
        error::Error BeginLogging();
189
        error::Error FinishLogging();
190
        ~DeploymentLog() {
191
                if (sink_) {
192
                        FinishLogging();
193
                }
194
        };
195

196
        string LogFileName();
197
        string LogFilePath();
198

199
private:
200
        const string data_store_dir_;
201
        const string id_;
202
#ifdef MENDER_LOG_BOOST
203
        typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
204
        boost::shared_ptr<text_sink> sink_;
205
#endif // MENDER_LOG_BOOST
206
        error::Error PrepareLogDirectory();
207
};
208

209
} // namespace deployments
210
} // namespace update
211
} // namespace mender
212

213
#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