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

mendersoftware / mender / 939977254

pending completion
939977254

push

gitlab-ci

kacf
chore: Move database key values to more central location.

There will also be more keys later, further motivating this move.

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

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

4174 of 5691 relevant lines covered (73.34%)

154.25 hits per line

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

0.0
/mender-update/daemon/context.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/daemon/context.hpp>
16

17
#include <common/log.hpp>
18

19
namespace mender {
20
namespace update {
21
namespace daemon {
22

23
namespace log = mender::common::log;
24
namespace main_context = mender::update::context;
25

26
const int STATE_DATA_VERSION = 2;
27

28
ExpectedStateData ApiResponseJsonToStateData(const json::Json &json) {
×
29
        StateData data;
×
30

31
        expected::ExpectedString str = json.Get("id").and_then(json::ToString);
×
32
        if (!str) {
×
33
                return expected::unexpected(str.error().WithContext("Could not get deployment ID"));
×
34
        }
35
        data.update_info.id = str.value();
×
36

37
        str = json.Get("artifact")
×
38
                          .and_then([](const json::Json &json) { return json.Get("source"); })
×
39
                          .and_then([](const json::Json &json) { return json.Get("uri"); })
×
40
                          .and_then(json::ToString);
×
41
        if (!str) {
×
42
                return expected::unexpected(
×
43
                        str.error().WithContext("Could not get artifact URI for deployment"));
×
44
        }
45
        data.update_info.artifact.source.uri = str.value();
×
46
        log::Debug("Artifact Download URL: " + data.update_info.artifact.source.uri);
×
47

48
        str = json.Get("artifact")
×
49
                          .and_then([](const json::Json &json) { return json.Get("source"); })
×
50
                          .and_then([](const json::Json &json) { return json.Get("expire"); })
×
51
                          .and_then(json::ToString);
×
52
        if (str) {
×
53
                data.update_info.artifact.source.expire = str.value();
×
54
                // If it's not available, we don't care.
55
        }
56

57
        // For later: Update Control Maps should be handled here.
58

59
        // Note: There is more information available in the response than we collect here, but we
60
        // prefer to get the information from the artifact instead, since it is the authoritative
61
        // source. And it's also signed, unlike the response.
62

63
        return data;
×
64
}
65

66
const string Context::kRollbackNotSupported = "rollback-not-supported";
67
const string Context::kRollbackSupported = "rollback-supported";
68

69
string SupportsRollbackToDbString(bool support) {
×
70
        return support ? Context::kRollbackSupported : Context::kRollbackNotSupported;
×
71
}
72

73
expected::ExpectedBool DbStringToSupportsRollback(const string &str) {
×
74
        if (str == Context::kRollbackSupported) {
×
75
                return true;
×
76
        } else if (str == Context::kRollbackNotSupported) {
×
77
                return false;
×
78
        } else {
79
                return expected::unexpected(main_context::MakeError(
×
80
                        main_context::DatabaseValueError,
81
                        "\"" + str + "\" is not a valid value for SupportsRollback"));
×
82
        }
83
}
84

85
const string Context::kRebootTypeNone = "";
86
const string Context::kRebootTypeCustom = "reboot-type-custom";
87
const string Context::kRebootTypeAutomatic = "reboot-type-automatic";
88

89
string NeedsRebootToDbString(update_module::RebootAction action) {
×
90
        switch (action) {
×
91
        case update_module::RebootAction::No:
×
92
                return Context::kRebootTypeNone;
×
93
        case update_module::RebootAction::Automatic:
×
94
                return Context::kRebootTypeAutomatic;
×
95
        case update_module::RebootAction::Yes:
×
96
                return Context::kRebootTypeCustom;
×
97
        default:
×
98
                // Should not happen.
99
                assert(false);
×
100
                return Context::kRebootTypeNone;
101
        }
102
}
103

104
update_module::ExpectedRebootAction DbStringToNeedsReboot(const string &str) {
×
105
        if (str == Context::kRebootTypeNone) {
×
106
                return update_module::RebootAction::No;
×
107
        } else if (str == Context::kRebootTypeAutomatic) {
×
108
                return update_module::RebootAction::Automatic;
×
109
        } else if (str == Context::kRebootTypeCustom) {
×
110
                return update_module::RebootAction::Yes;
×
111
        } else {
112
                return expected::unexpected(main_context::MakeError(
×
113
                        main_context::DatabaseValueError,
114
                        "\"" + str + "\" is not a valid value for RebootRequested"));
×
115
        }
116
}
117

118
void StateData::FillUpdateDataFromArtifact(artifact::PayloadHeaderView &view) {
×
119
        version = view.version;
×
120
        auto &artifact = update_info.artifact;
×
121
        auto &header = view.header;
×
122
        artifact.compatible_devices = header.header_info.depends.device_type;
×
123
        artifact.payload_types = {header.payload_type};
×
124
        artifact.artifact_name = header.artifact_name;
×
125
        artifact.artifact_group = header.artifact_group;
×
126
        if (header.type_info.artifact_provides) {
×
127
                artifact.type_info_provides = header.type_info.artifact_provides.value();
×
128
        } else {
129
                artifact.type_info_provides.clear();
×
130
        }
131
        if (header.type_info.clears_artifact_provides) {
×
132
                artifact.clears_artifact_provides = header.type_info.clears_artifact_provides.value();
×
133
        } else {
134
                artifact.clears_artifact_provides.clear();
×
135
        }
136
}
×
137

138
Context::Context(main_context::MenderContext &mender_context, events::EventLoop &event_loop) :
×
139
        mender_context(mender_context),
140
        event_loop(event_loop),
141
        deployment_client(http::ClientConfig(mender_context.GetConfig().server_url), event_loop),
×
142
        download_client(http::ClientConfig(mender_context.GetConfig().server_url), event_loop) {
×
143
}
×
144

145
} // namespace daemon
146
} // namespace update
147
} // 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