blob: f63352a64e083edb5ed1510126ca240c14c9deba [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 {
Lei YUff83c2a2019-09-12 13:55:18 +080057 value = startActivation();
Lei YU12c9f4c2019-09-11 15:08:15 +080058 }
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 {
Lei YUff83c2a2019-09-12 13:55:18 +080097 onUpdateDone();
Lei YU12c9f4c2019-09-11 15:08:15 +080098 }
99 if (newStateResult == "failed" || newStateResult == "dependency")
100 {
Lei YUff83c2a2019-09-12 13:55:18 +0800101 onUpdateFailed();
Lei YU12c9f4c2019-09-11 15:08:15 +0800102 }
103 }
104}
105
Lei YUff83c2a2019-09-12 13:55:18 +0800106bool Activation::doUpdate(const std::string& psuInventoryPath)
107{
108 psuUpdateUnit = internal::getUpdateService(psuInventoryPath, versionId);
109 try
110 {
111 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
112 SYSTEMD_INTERFACE, "StartUnit");
113 method.append(psuUpdateUnit, "replace");
114 bus.call_noreply(method);
115 return true;
116 }
117 catch (const SdBusError& e)
118 {
119 log<level::ERR>("Error staring service", entry("ERROR=%s", e.what()));
120 onUpdateFailed();
121 return false;
122 }
123}
124
125bool Activation::doUpdate()
126{
127 // When the queue is empty, all updates are done
128 if (psuQueue.empty())
129 {
130 finishActivation();
131 return true;
132 }
133
134 // Do the update on a PSU
135 const auto& psu = psuQueue.front();
136 return doUpdate(psu);
137}
138
139void Activation::onUpdateDone()
140{
141 auto progress = activationProgress->progress() + progressStep;
142 activationProgress->progress(progress);
143
144 psuQueue.pop();
145 doUpdate(); // Update the next psu
146}
147
148void Activation::onUpdateFailed()
149{
150 // TODO: report an event
151 log<level::ERR>("Failed to udpate PSU",
152 entry("PSU=%s", psuQueue.front().c_str()));
153 std::queue<std::string>().swap(psuQueue); // Clear the queue
154 activation(Status::Failed);
155}
156
157Activation::Status Activation::startActivation()
Lei YU12c9f4c2019-09-11 15:08:15 +0800158{
Lei YU90c8a8b2019-09-11 17:20:03 +0800159 if (!activationProgress)
160 {
161 activationProgress = std::make_unique<ActivationProgress>(bus, path);
162 }
Lei YU81c67722019-09-11 16:47:29 +0800163 if (!activationBlocksTransition)
164 {
165 activationBlocksTransition =
166 std::make_unique<ActivationBlocksTransition>(bus, path);
167 }
168
Lei YU12c9f4c2019-09-11 15:08:15 +0800169 auto psuPaths = utils::getPSUInventoryPath(bus);
170 if (psuPaths.empty())
171 {
Lei YUff83c2a2019-09-12 13:55:18 +0800172 log<level::WARNING>("No PSU inventory found");
173 return Status::Failed;
Lei YU12c9f4c2019-09-11 15:08:15 +0800174 }
175
Lei YUff83c2a2019-09-12 13:55:18 +0800176 for (const auto& p : psuPaths)
177 {
178 psuQueue.push(p);
179 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800180
Lei YUff83c2a2019-09-12 13:55:18 +0800181 // The progress to be increased for each successful update of PSU
182 // E.g. in case we have 4 PSUs:
183 // 1. Initial progrss is 10
184 // 2. Add 20 after each update is done, so we will see progress to be 30,
185 // 50, 70, 90
186 // 3. When all PSUs are updated, it will be 100 and the interface is
187 // removed.
188 progressStep = 80 / psuQueue.size();
189 if (doUpdate())
190 {
191 activationProgress->progress(10);
192 return Status::Activating;
193 }
194 else
195 {
196 return Status::Failed;
197 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800198}
199
200void Activation::finishActivation()
201{
Lei YU90c8a8b2019-09-11 17:20:03 +0800202 activationProgress->progress(100);
Lei YU81c67722019-09-11 16:47:29 +0800203
Lei YU12c9f4c2019-09-11 15:08:15 +0800204 // TODO: delete the old software object
205 // TODO: create related associations
Lei YUd0bbfa92019-09-11 16:10:54 +0800206 deleteImageManagerObject();
Lei YU12c9f4c2019-09-11 15:08:15 +0800207 activation(Status::Active);
Lei YU01539e72019-07-31 10:57:38 +0800208}
209
Lei YUd0bbfa92019-09-11 16:10:54 +0800210void Activation::deleteImageManagerObject()
211{
212 // Get the Delete object for <versionID> inside image_manager
213 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
214 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
215 std::string versionService;
216 auto services = utils::getServices(bus, path.c_str(), deleteInterface);
217
218 // We need to find the phosphor-version-software-manager's version service
219 // to invoke the delete interface
220 for (const auto& service : services)
221 {
222 if (service.find(versionServiceStr) != std::string::npos)
223 {
224 versionService = service;
225 break;
226 }
227 }
228 if (versionService.empty())
229 {
230 log<level::ERR>("Error finding version service");
231 return;
232 }
233
234 // Call the Delete object for <versionID> inside image_manager
235 auto method = bus.new_method_call(versionService.c_str(), path.c_str(),
236 deleteInterface, "Delete");
237 try
238 {
239 bus.call(method);
240 }
241 catch (const SdBusError& e)
242 {
243 log<level::ERR>("Error performing call to Delete object path",
244 entry("ERROR=%s", e.what()),
245 entry("PATH=%s", path.c_str()));
246 }
247}
248
Lei YU01539e72019-07-31 10:57:38 +0800249} // namespace updater
250} // namespace software
251} // namespace phosphor