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

mendersoftware / mender / 951615729

pending completion
951615729

push

gitlab-ci

larsewi
fix: Add 'build/' to '.gitignore'

Ticket: None
Changelog: None
Signed-off-by: Lars Erik Wik <lars.erik.wik@northern.tech>

4199 of 5286 relevant lines covered (79.44%)

166.43 hits per line

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

78.68
/mender-update/deployments.cpp
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
#include <mender-update/deployments.hpp>
16

17
#include <algorithm>
18
#include <sstream>
19
#include <string>
20

21
#include <api/api.hpp>
22
#include <common/common.hpp>
23
#include <common/error.hpp>
24
#include <common/events.hpp>
25
#include <common/expected.hpp>
26
#include <common/http.hpp>
27
#include <common/io.hpp>
28
#include <common/json.hpp>
29
#include <common/log.hpp>
30
#include <common/optional.hpp>
31
#include <common/path.hpp>
32
#include <mender-update/context.hpp>
33

34
namespace mender {
35
namespace update {
36
namespace deployments {
37

38
using namespace std;
39

40
namespace api = mender::api;
41
namespace common = mender::common;
42
namespace context = mender::update::context;
43
namespace error = mender::common::error;
44
namespace events = mender::common::events;
45
namespace expected = mender::common::expected;
46
namespace io = mender::common::io;
47
namespace json = mender::common::json;
48
namespace log = mender::common::log;
49
namespace optional = mender::common::optional;
50
namespace path = mender::common::path;
51

52
const DeploymentsErrorCategoryClass DeploymentsErrorCategory;
53

54
const char *DeploymentsErrorCategoryClass::name() const noexcept {
×
55
        return "DeploymentsErrorCategory";
×
56
}
57

58
string DeploymentsErrorCategoryClass::message(int code) const {
×
59
        switch (code) {
×
60
        case NoError:
×
61
                return "Success";
×
62
        case InvalidDataError:
×
63
                return "Invalid data error";
×
64
        case BadResponseError:
×
65
                return "Bad response error";
×
66
        }
67
        assert(false);
×
68
        return "Unknown";
69
}
70

71
error::Error MakeError(DeploymentsErrorCode code, const string &msg) {
4✔
72
        return error::Error(error_condition(code, DeploymentsErrorCategory), msg);
8✔
73
}
74

75
static const string check_updates_v1_uri = "/api/devices/v1/deployments/device/deployments/next";
76
static const string check_updates_v2_uri = "/api/devices/v2/deployments/device/deployments/next";
77

78
error::Error CheckNewDeployments(
6✔
79
        context::MenderContext &ctx,
80
        const string &server_url,
81
        http::Client &client,
82
        CheckUpdatesAPIResponseHandler api_handler) {
83
        auto ex_dev_type = ctx.GetDeviceType();
12✔
84
        if (!ex_dev_type) {
6✔
85
                return ex_dev_type.error();
×
86
        }
87
        string device_type = ex_dev_type.value();
12✔
88

89
        auto ex_provides = ctx.LoadProvides();
12✔
90
        if (!ex_provides) {
6✔
91
                return ex_provides.error();
×
92
        }
93
        auto provides = ex_provides.value();
12✔
94
        if (provides.find("artifact_name") == provides.end()) {
6✔
95
                return MakeError(InvalidDataError, "Missing artifact name data");
×
96
        }
97

98
        stringstream ss;
12✔
99
        ss << R"({"update_control_map":false,"device_provides":{)";
6✔
100
        ss << R"("device_type":")";
6✔
101
        ss << json::EscapeString(device_type);
6✔
102

103
        for (const auto &kv : provides) {
14✔
104
                ss << "\",\"" + json::EscapeString(kv.first) + "\":\"";
8✔
105
                ss << json::EscapeString(kv.second);
8✔
106
        }
107

108
        ss << R"("}})";
6✔
109

110
        string v2_payload = ss.str();
12✔
111
        http::BodyGenerator payload_gen = [v2_payload]() {
6✔
112
                return make_shared<io::StringReader>(v2_payload);
6✔
113
        };
12✔
114

115
        // TODO: APIRequest
116
        auto v2_req = make_shared<http::OutgoingRequest>();
12✔
117
        v2_req->SetAddress(http::JoinUrl(server_url, check_updates_v2_uri));
6✔
118
        v2_req->SetMethod(http::Method::POST);
6✔
119
        v2_req->SetHeader("Content-Type", "application/json");
6✔
120
        v2_req->SetHeader("Content-Length", to_string(v2_payload.size()));
6✔
121
        v2_req->SetHeader("Accept", "application/json");
6✔
122
        v2_req->SetBodyGenerator(payload_gen);
6✔
123

124
        string v1_args = "artifact_name=" + http::URLEncode(provides["artifact_name"])
12✔
125
                                         + "&device_type=" + http::URLEncode(device_type);
24✔
126
        auto v1_req = make_shared<http::OutgoingRequest>();
12✔
127
        v1_req->SetAddress(http::JoinUrl(server_url, check_updates_v1_uri) + "?" + v1_args);
6✔
128
        v1_req->SetMethod(http::Method::GET);
6✔
129
        v1_req->SetHeader("Accept", "application/json");
6✔
130

131
        auto received_body = make_shared<vector<uint8_t>>();
12✔
132
        auto handle_data = [received_body, api_handler](unsigned status) {
4✔
133
                if (status == http::StatusOK) {
4✔
134
                        auto ex_j = json::Load(common::StringFromByteVector(*received_body));
4✔
135
                        if (ex_j) {
2✔
136
                                CheckUpdatesAPIResponse response {optional::optional<json::Json> {ex_j.value()}};
2✔
137
                                api_handler(response);
2✔
138
                        } else {
139
                                api_handler(expected::unexpected(ex_j.error()));
×
140
                        }
141
                } else if (status == http::StatusNoContent) {
2✔
142
                        api_handler(CheckUpdatesAPIResponse {optional::nullopt});
2✔
143
                }
144
        };
16✔
145

146
        http::ResponseHandler header_handler =
147
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
9✔
148
                        if (!exp_resp) {
9✔
149
                                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
150
                                CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
151
                                api_handler(response);
×
152
                                return;
×
153
                        }
154

155
                        auto resp = exp_resp.value();
18✔
156
                        received_body->clear();
9✔
157
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
9✔
158
                        body_writer->SetUnlimited(true);
9✔
159
                        resp->SetBodyWriter(body_writer);
9✔
160
                };
12✔
161

162
        http::ResponseHandler v1_body_handler =
163
                [received_body, api_handler, handle_data](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
164
                        if (!exp_resp) {
3✔
165
                                log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
166
                                CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
167
                                api_handler(response);
×
168
                                return;
×
169
                        }
170
                        auto resp = exp_resp.value();
6✔
171
                        auto status = resp->GetStatusCode();
3✔
172
                        if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
3✔
173
                                handle_data(status);
2✔
174
                        } else {
175
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
176
                                string err_str;
1✔
177
                                if (ex_err_msg) {
1✔
178
                                        err_str = ex_err_msg.value();
×
179
                                } else {
180
                                        err_str = resp->GetStatusMessage();
1✔
181
                                }
182
                                api_handler(expected::unexpected(MakeError(
2✔
183
                                        BadResponseError,
184
                                        "Got unexpected response " + to_string(status) + ": " + err_str)));
3✔
185
                        }
186
                };
12✔
187

188
        http::ResponseHandler v2_body_handler = [received_body,
6✔
189
                                                                                         v1_req,
190
                                                                                         header_handler,
191
                                                                                         v1_body_handler,
192
                                                                                         api_handler,
193
                                                                                         handle_data,
194
                                                                                         &client](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
195
                if (!exp_resp) {
6✔
196
                        log::Error("Request to check new deployments failed: " + exp_resp.error().message);
×
197
                        CheckUpdatesAPIResponse response = expected::unexpected(exp_resp.error());
×
198
                        api_handler(response);
×
199
                        return;
×
200
                }
201
                auto resp = exp_resp.value();
12✔
202
                auto status = resp->GetStatusCode();
6✔
203
                if ((status == http::StatusOK) || (status == http::StatusNoContent)) {
6✔
204
                        handle_data(status);
2✔
205
                } else if (status == http::StatusNotFound) {
4✔
206
                        log::Info(
3✔
207
                                "POST request to v2 version of the deployments API failed, falling back to v1 version and GET");
6✔
208
                        auto err = client.AsyncCall(v1_req, header_handler, v1_body_handler);
9✔
209
                        if (err != error::NoError) {
3✔
210
                                api_handler(expected::unexpected(err.WithContext("While calling v1 endpoint")));
×
211
                        }
212
                } else {
213
                        auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
214
                        string err_str;
1✔
215
                        if (ex_err_msg) {
1✔
216
                                err_str = ex_err_msg.value();
1✔
217
                        } else {
218
                                err_str = resp->GetStatusMessage();
×
219
                        }
220
                        api_handler(expected::unexpected(MakeError(
2✔
221
                                BadResponseError,
222
                                "Got unexpected response " + to_string(status) + ": " + err_str)));
3✔
223
                }
224
        };
6✔
225

226
        return client.AsyncCall(v2_req, header_handler, v2_body_handler);
6✔
227
}
228

229
static const string deployment_status_strings[static_cast<int>(DeploymentStatus::End_) + 1] = {
230
        "installing",
231
        "pause_before_installing",
232
        "downloading",
233
        "pause_before_rebooting",
234
        "rebooting",
235
        "pause_before_committing",
236
        "success",
237
        "failure",
238
        "already-installed"};
239

240
static const string deployments_uri_prefix = "/api/devices/v1/deployments/device/deployments";
241
static const string status_uri_suffix = "/status";
242

243
error::Error PushStatus(
3✔
244
        const string &deployment_id,
245
        DeploymentStatus status,
246
        const string &substate,
247
        const string &server_url,
248
        http::Client &client,
249
        StatusAPIResponseHandler api_handler) {
250
        string payload = R"({"status":")" + deployment_status_strings[static_cast<int>(status)] + "\"";
6✔
251
        if (substate != "") {
3✔
252
                payload += R"(,"substate":")" + json::EscapeString(substate) + "\"}";
2✔
253
        } else {
254
                payload += "}";
1✔
255
        }
256
        http::BodyGenerator payload_gen = [payload]() {
3✔
257
                return make_shared<io::StringReader>(payload);
3✔
258
        };
6✔
259

260
        auto req = make_shared<http::OutgoingRequest>();
6✔
261
        req->SetAddress(
3✔
262
                http::JoinUrl(server_url, deployments_uri_prefix, deployment_id, status_uri_suffix));
6✔
263
        req->SetMethod(http::Method::PUT);
3✔
264
        req->SetHeader("Content-Type", "application/json");
3✔
265
        req->SetHeader("Content-Length", to_string(payload.size()));
3✔
266
        req->SetHeader("Accept", "application/json");
3✔
267
        req->SetBodyGenerator(payload_gen);
3✔
268

269
        auto received_body = make_shared<vector<uint8_t>>();
3✔
270
        return client.AsyncCall(
271
                req,
272
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
273
                        if (!exp_resp) {
3✔
274
                                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
275
                                api_handler(exp_resp.error());
×
276
                                return;
×
277
                        }
278

279
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
6✔
280
                        auto resp = exp_resp.value();
6✔
281
                        auto content_length = resp->GetHeader("Content-Length");
9✔
282
                        auto ex_len = common::StringToLongLong(content_length.value());
3✔
283
                        if (!ex_len) {
3✔
284
                                log::Error("Failed to get content length from the status API response headers");
×
285
                                body_writer->SetUnlimited(true);
×
286
                        } else {
287
                                received_body->resize(ex_len.value());
3✔
288
                        }
289
                        resp->SetBodyWriter(body_writer);
3✔
290
                },
291
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
292
                        if (!exp_resp) {
3✔
293
                                log::Error("Request to push status data failed: " + exp_resp.error().message);
×
294
                                api_handler(exp_resp.error());
×
295
                                return;
×
296
                        }
297

298
                        auto resp = exp_resp.value();
6✔
299
                        auto status = resp->GetStatusCode();
3✔
300
                        if (status == http::StatusNoContent) {
3✔
301
                                api_handler(error::NoError);
2✔
302
                        } else {
303
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
304
                                string err_str;
1✔
305
                                if (ex_err_msg) {
1✔
306
                                        err_str = ex_err_msg.value();
1✔
307
                                } else {
308
                                        err_str = resp->GetStatusMessage();
×
309
                                }
310
                                api_handler(MakeError(
1✔
311
                                        BadResponseError,
312
                                        "Got unexpected response " + to_string(status)
2✔
313
                                                + " from status API: " + err_str));
2✔
314
                        }
315
                });
6✔
316
}
317

318
using mender::common::expected::ExpectedSize;
319

320
static ExpectedSize GetLogFileDataSize(const string &path) {
3✔
321
        auto ex_istr = io::OpenIfstream(path);
6✔
322
        if (!ex_istr) {
3✔
323
                return expected::unexpected(ex_istr.error());
×
324
        }
325
        auto istr = std::move(ex_istr.value());
6✔
326

327
        // We want the size of the actual data without a potential trailing
328
        // newline. So let's seek one byte before the end of file, check if the last
329
        // byte is a newline and return the appropriate number.
330
        istr.seekg(-1, ios_base::end);
3✔
331
        char c = istr.get();
3✔
332
        if (c == '\n') {
3✔
333
                return istr.tellg() - static_cast<ifstream::off_type>(1);
6✔
334
        } else {
335
                return istr.tellg();
×
336
        }
337
}
338

339
const vector<uint8_t> JsonLogMessagesReader::header_ = {
340
        '{', '"', 'm', 'e', 's', 's', 'a', 'g', 'e', 's', '"', ':', '['};
341
const vector<uint8_t> JsonLogMessagesReader::closing_ = {']', '}'};
342

343
ExpectedSize JsonLogMessagesReader::Read(
89✔
344
        vector<uint8_t>::iterator start, vector<uint8_t>::iterator end) {
345
        if (header_rem_ > 0) {
89✔
346
                io::Vsize target_size = end - start;
9✔
347
                auto copy_end = copy_n(
348
                        header_.begin() + (header_.size() - header_rem_), min(header_rem_, target_size), start);
9✔
349
                auto n_copied = copy_end - start;
9✔
350
                header_rem_ -= n_copied;
9✔
351
                return static_cast<size_t>(n_copied);
18✔
352
        } else if (rem_raw_data_size_ > 0) {
80✔
353
                if (static_cast<size_t>(end - start) > rem_raw_data_size_) {
64✔
354
                        end = start + rem_raw_data_size_;
8✔
355
                }
356
                auto ex_sz = reader_->Read(start, end);
128✔
357
                if (!ex_sz) {
64✔
358
                        return ex_sz;
×
359
                }
360
                auto n_read = ex_sz.value();
64✔
361
                rem_raw_data_size_ -= n_read;
64✔
362

363
                // We control how much we read from the file so we should never read
364
                // 0 bytes (meaning EOF reached). If we do, it means the file is
365
                // smaller than what we were told.
366
                assert(n_read > 0);
64✔
367
                if (n_read == 0) {
64✔
368
                        return expected::unexpected(
×
369
                                MakeError(InvalidDataError, "Unexpected EOF when reading logs file"));
×
370
                }
371

372
                // Replace all newlines with commas
373
                const auto read_end = start + n_read;
64✔
374
                for (auto it = start; it < read_end; it++) {
1,916✔
375
                        if (it[0] == '\n') {
1,852✔
376
                                it[0] = ',';
12✔
377
                        }
378
                }
379
                return n_read;
64✔
380
        } else if (closing_rem_ > 0) {
16✔
381
                io::Vsize target_size = end - start;
8✔
382
                auto copy_end = copy_n(
383
                        closing_.begin() + (closing_.size() - closing_rem_),
×
384
                        min(closing_rem_, target_size),
8✔
385
                        start);
8✔
386
                auto n_copied = copy_end - start;
8✔
387
                closing_rem_ -= n_copied;
8✔
388
                return static_cast<size_t>(copy_end - start);
16✔
389
        } else {
390
                return 0;
8✔
391
        }
392
};
393

394
static const string logs_uri_suffix = "/log";
395

396
error::Error PushLogs(
3✔
397
        const string &deployment_id,
398
        const string &log_file_path,
399
        const string &server_url,
400
        http::Client &client,
401
        LogsAPIResponseHandler api_handler) {
402
        auto ex_size = GetLogFileDataSize(log_file_path);
6✔
403
        if (!ex_size) {
3✔
404
                // api_handler(ex_size.error()) ???
405
                return ex_size.error();
×
406
        }
407
        auto data_size = ex_size.value();
3✔
408

409
        auto file_reader = make_shared<io::FileReader>(log_file_path);
6✔
410
        auto logs_reader = make_shared<JsonLogMessagesReader>(file_reader, data_size);
6✔
411

412
        auto req = make_shared<http::OutgoingRequest>();
6✔
413
        req->SetAddress(
3✔
414
                http::JoinUrl(server_url, deployments_uri_prefix, deployment_id, logs_uri_suffix));
6✔
415
        req->SetMethod(http::Method::PUT);
3✔
416
        req->SetHeader("Content-Type", "application/json");
3✔
417
        req->SetHeader("Content-Length", to_string(JsonLogMessagesReader::TotalDataSize(data_size)));
3✔
418
        req->SetHeader("Accept", "application/json");
3✔
419
        req->SetBodyGenerator([logs_reader]() {
6✔
420
                logs_reader->Rewind();
3✔
421
                return logs_reader;
3✔
422
        });
6✔
423

424
        auto received_body = make_shared<vector<uint8_t>>();
3✔
425
        return client.AsyncCall(
426
                req,
427
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
428
                        if (!exp_resp) {
3✔
429
                                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
430
                                api_handler(exp_resp.error());
×
431
                                return;
×
432
                        }
433

434
                        auto body_writer = make_shared<io::ByteWriter>(received_body);
6✔
435
                        auto resp = exp_resp.value();
6✔
436
                        auto content_length = resp->GetHeader("Content-Length");
9✔
437
                        auto ex_len = common::StringToLongLong(content_length.value());
3✔
438
                        if (!ex_len) {
3✔
439
                                log::Error("Failed to get content length from the logs API response headers");
×
440
                                body_writer->SetUnlimited(true);
×
441
                        } else {
442
                                received_body->resize(ex_len.value());
3✔
443
                        }
444
                        resp->SetBodyWriter(body_writer);
3✔
445
                },
446
                [received_body, api_handler](http::ExpectedIncomingResponsePtr exp_resp) {
3✔
447
                        if (!exp_resp) {
3✔
448
                                log::Error("Request to push logs data failed: " + exp_resp.error().message);
×
449
                                api_handler(exp_resp.error());
×
450
                                return;
×
451
                        }
452

453
                        auto resp = exp_resp.value();
6✔
454
                        auto status = resp->GetStatusCode();
3✔
455
                        if (status == http::StatusNoContent) {
3✔
456
                                api_handler(error::NoError);
2✔
457
                        } else {
458
                                auto ex_err_msg = api::ErrorMsgFromErrorResponse(*received_body);
2✔
459
                                string err_str;
1✔
460
                                if (ex_err_msg) {
1✔
461
                                        err_str = ex_err_msg.value();
1✔
462
                                } else {
463
                                        err_str = resp->GetStatusMessage();
×
464
                                }
465
                                api_handler(MakeError(
1✔
466
                                        BadResponseError,
467
                                        "Got unexpected response " + to_string(status) + " from logs API: " + err_str));
2✔
468
                        }
469
                });
3✔
470
}
471

472
} // namespace deployments
473
} // namespace update
474
} // namespace mender
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