blob: 0ba7e6479ee8b664e36a216e327a71a6c063d1d1 [file] [log] [blame]
Lei YU12c9f4c2019-09-11 15:08:15 +08001#include "config.h"
2
Lei YU01539e72019-07-31 10:57:38 +08003#include "activation.hpp"
4
Lei YU12c9f4c2019-09-11 15:08:15 +08005#include "utils.hpp"
6
7#include <cassert>
8#include <filesystem>
Lei YUd0bbfa92019-09-11 16:10:54 +08009#include <phosphor-logging/elog-errors.hpp>
10#include <phosphor-logging/log.hpp>
Lei YU12c9f4c2019-09-11 15:08:15 +080011
Lei YU01539e72019-07-31 10:57:38 +080012namespace phosphor
13{
14namespace software
15{
16namespace updater
17{
18
Lei YU12c9f4c2019-09-11 15:08:15 +080019constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1";
20constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1";
21constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
22
23namespace fs = std::filesystem;
Lei YU01539e72019-07-31 10:57:38 +080024namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
25
Lei YUd0bbfa92019-09-11 16:10:54 +080026using namespace phosphor::logging;
27using sdbusplus::exception::SdBusError;
Lei YU12c9f4c2019-09-11 15:08:15 +080028using SoftwareActivation = softwareServer::Activation;
29
30namespace internal
31{
32/** Construct the systemd service name */
33std::string getUpdateService(const std::string& psuInventoryPath,
34 const std::string& versionId)
35{
36 fs::path imagePath(IMG_DIR);
37 imagePath /= versionId;
38
39 // The systemd unit shall be escaped
40 std::string args = psuInventoryPath;
41 args += "\\x20";
42 args += imagePath;
43 std::replace(args.begin(), args.end(), '/', '-');
44
45 std::string service = PSU_UPDATE_SERVICE;
46 auto p = service.find('@');
47 assert(p != std::string::npos);
48 service.insert(p + 1, args);
49 return service;
50}
51
52} // namespace internal
Lei YU01539e72019-07-31 10:57:38 +080053auto Activation::activation(Activations value) -> Activations
54{
Lei YU12c9f4c2019-09-11 15:08:15 +080055 if (value == Status::Activating)
56 {
57 startActivation();
58 }
59 else
60 {
Lei YU81c67722019-09-11 16:47:29 +080061 activationBlocksTransition.reset();
Lei YU90c8a8b2019-09-11 17:20:03 +080062 activationProgress.reset();
Lei YU12c9f4c2019-09-11 15:08:15 +080063 }
64
65 return SoftwareActivation::activation(value);
Lei YU01539e72019-07-31 10:57:38 +080066}
67
68auto Activation::requestedActivation(RequestedActivations value)
69 -> RequestedActivations
70{
Lei YU12c9f4c2019-09-11 15:08:15 +080071 if ((value == SoftwareActivation::RequestedActivations::Active) &&
72 (SoftwareActivation::requestedActivation() !=
73 SoftwareActivation::RequestedActivations::Active))
74 {
75 if ((activation() == Status::Ready) || (activation() == Status::Failed))
76 {
77 activation(Status::Activating);
78 }
79 }
80 return SoftwareActivation::requestedActivation(value);
81}
82
83void Activation::unitStateChange(sdbusplus::message::message& msg)
84{
85 uint32_t newStateID{};
86 sdbusplus::message::object_path newStateObjPath;
87 std::string newStateUnit{};
88 std::string newStateResult{};
89
90 // Read the msg and populate each variable
91 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
92
93 if (newStateUnit == psuUpdateUnit)
94 {
95 if (newStateResult == "done")
96 {
97 finishActivation();
98 }
99 if (newStateResult == "failed" || newStateResult == "dependency")
100 {
101 activation(Status::Failed);
102 }
103 }
104}
105
106void Activation::startActivation()
107{
Lei YU90c8a8b2019-09-11 17:20:03 +0800108 if (!activationProgress)
109 {
110 activationProgress = std::make_unique<ActivationProgress>(bus, path);
111 }
Lei YU81c67722019-09-11 16:47:29 +0800112 if (!activationBlocksTransition)
113 {
114 activationBlocksTransition =
115 std::make_unique<ActivationBlocksTransition>(bus, path);
116 }
117
Lei YU12c9f4c2019-09-11 15:08:15 +0800118 // TODO: for now only update one psu, future commits shall handle update
119 // multiple psus
120 auto psuPaths = utils::getPSUInventoryPath(bus);
121 if (psuPaths.empty())
122 {
123 return;
124 }
125
126 psuUpdateUnit = internal::getUpdateService(psuPaths[0], versionId);
127
128 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
129 SYSTEMD_INTERFACE, "StartUnit");
130 method.append(psuUpdateUnit, "replace");
131 bus.call_noreply(method);
Lei YU90c8a8b2019-09-11 17:20:03 +0800132
133 activationProgress->progress(10);
Lei YU12c9f4c2019-09-11 15:08:15 +0800134}
135
136void Activation::finishActivation()
137{
Lei YU90c8a8b2019-09-11 17:20:03 +0800138 activationProgress->progress(100);
Lei YU81c67722019-09-11 16:47:29 +0800139 activationBlocksTransition.reset();
Lei YU90c8a8b2019-09-11 17:20:03 +0800140 activationProgress.reset();
Lei YU81c67722019-09-11 16:47:29 +0800141
Lei YU12c9f4c2019-09-11 15:08:15 +0800142 // TODO: delete the old software object
143 // TODO: create related associations
Lei YUd0bbfa92019-09-11 16:10:54 +0800144 deleteImageManagerObject();
Lei YU12c9f4c2019-09-11 15:08:15 +0800145 activation(Status::Active);
Lei YU01539e72019-07-31 10:57:38 +0800146}
147
Lei YUd0bbfa92019-09-11 16:10:54 +0800148void Activation::deleteImageManagerObject()
149{
150 // Get the Delete object for <versionID> inside image_manager
151 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
152 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
153 std::string versionService;
154 auto services = utils::getServices(bus, path.c_str(), deleteInterface);
155
156 // We need to find the phosphor-version-software-manager's version service
157 // to invoke the delete interface
158 for (const auto& service : services)
159 {
160 if (service.find(versionServiceStr) != std::string::npos)
161 {
162 versionService = service;
163 break;
164 }
165 }
166 if (versionService.empty())
167 {
168 log<level::ERR>("Error finding version service");
169 return;
170 }
171
172 // Call the Delete object for <versionID> inside image_manager
173 auto method = bus.new_method_call(versionService.c_str(), path.c_str(),
174 deleteInterface, "Delete");
175 try
176 {
177 bus.call(method);
178 }
179 catch (const SdBusError& e)
180 {
181 log<level::ERR>("Error performing call to Delete object path",
182 entry("ERROR=%s", e.what()),
183 entry("PATH=%s", path.c_str()));
184 }
185}
186
Lei YU01539e72019-07-31 10:57:38 +0800187} // namespace updater
188} // namespace software
189} // namespace phosphor