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

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

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

6996 of 8950 relevant lines covered (78.17%)

10353.4 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
        DeploymentAbortedError,
61
};
62

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

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

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

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

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

91
string DeploymentStatusString(DeploymentStatus status);
92

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

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

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

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

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

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

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

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

170
        static size_t TotalDataSize(size_t raw_data_size) {
×
171
                return raw_data_size + header_.size() + closing_.size();
×
172
        }
173

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

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

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

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

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

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