blob: f1181fb3f4c3543ed01581ae16429ac0e41ed3ef [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{
Lei YU7f2a2152019-09-16 16:50:18 +0800108 currentUpdatingPsu = psuInventoryPath;
109 psuUpdateUnit = internal::getUpdateService(currentUpdatingPsu, versionId);
Lei YUff83c2a2019-09-12 13:55:18 +0800110 try
111 {
112 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
113 SYSTEMD_INTERFACE, "StartUnit");
114 method.append(psuUpdateUnit, "replace");
115 bus.call_noreply(method);
116 return true;
117 }
118 catch (const SdBusError& e)
119 {
120 log<level::ERR>("Error staring service", entry("ERROR=%s", e.what()));
121 onUpdateFailed();
122 return false;
123 }
124}
125
126bool Activation::doUpdate()
127{
128 // When the queue is empty, all updates are done
129 if (psuQueue.empty())
130 {
131 finishActivation();
132 return true;
133 }
134
135 // Do the update on a PSU
136 const auto& psu = psuQueue.front();
137 return doUpdate(psu);
138}
139
140void Activation::onUpdateDone()
141{
142 auto progress = activationProgress->progress() + progressStep;
143 activationProgress->progress(progress);
144
Lei YU7f2a2152019-09-16 16:50:18 +0800145 // Update the activation association
146 auto assocs = associations();
147 assocs.emplace_back(ACTIVATION_FWD_ASSOCIATION, ACTIVATION_REV_ASSOCIATION,
148 currentUpdatingPsu);
149 currentUpdatingPsu.clear();
150 associations(assocs);
151
Lei YUff83c2a2019-09-12 13:55:18 +0800152 psuQueue.pop();
153 doUpdate(); // Update the next psu
154}
155
156void Activation::onUpdateFailed()
157{
158 // TODO: report an event
159 log<level::ERR>("Failed to udpate PSU",
160 entry("PSU=%s", psuQueue.front().c_str()));
161 std::queue<std::string>().swap(psuQueue); // Clear the queue
162 activation(Status::Failed);
163}
164
165Activation::Status Activation::startActivation()
Lei YU12c9f4c2019-09-11 15:08:15 +0800166{
Lei YU90c8a8b2019-09-11 17:20:03 +0800167 if (!activationProgress)
168 {
169 activationProgress = std::make_unique<ActivationProgress>(bus, path);
170 }
Lei YU81c67722019-09-11 16:47:29 +0800171 if (!activationBlocksTransition)
172 {
173 activationBlocksTransition =
174 std::make_unique<ActivationBlocksTransition>(bus, path);
175 }
176
Lei YU12c9f4c2019-09-11 15:08:15 +0800177 auto psuPaths = utils::getPSUInventoryPath(bus);
178 if (psuPaths.empty())
179 {
Lei YUff83c2a2019-09-12 13:55:18 +0800180 log<level::WARNING>("No PSU inventory found");
181 return Status::Failed;
Lei YU12c9f4c2019-09-11 15:08:15 +0800182 }
183
Lei YUff83c2a2019-09-12 13:55:18 +0800184 for (const auto& p : psuPaths)
185 {
186 psuQueue.push(p);
187 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800188
Lei YUff83c2a2019-09-12 13:55:18 +0800189 // The progress to be increased for each successful update of PSU
190 // E.g. in case we have 4 PSUs:
191 // 1. Initial progrss is 10
192 // 2. Add 20 after each update is done, so we will see progress to be 30,
193 // 50, 70, 90
194 // 3. When all PSUs are updated, it will be 100 and the interface is
195 // removed.
196 progressStep = 80 / psuQueue.size();
197 if (doUpdate())
198 {
199 activationProgress->progress(10);
200 return Status::Activating;
201 }
202 else
203 {
204 return Status::Failed;
205 }
Lei YU12c9f4c2019-09-11 15:08:15 +0800206}
207
208void Activation::finishActivation()
209{
Lei YU90c8a8b2019-09-11 17:20:03 +0800210 activationProgress->progress(100);
Lei YU81c67722019-09-11 16:47:29 +0800211
Lei YU12c9f4c2019-09-11 15:08:15 +0800212 // TODO: delete the old software object
Lei YUd0bbfa92019-09-11 16:10:54 +0800213 deleteImageManagerObject();
Lei YU7f2a2152019-09-16 16:50:18 +0800214
215 associationInterface->createActiveAssociation(path);
216 associationInterface->addFunctionalAssociation(path);
217
Lei YU12c9f4c2019-09-11 15:08:15 +0800218 activation(Status::Active);
Lei YU01539e72019-07-31 10:57:38 +0800219}
220
Lei YUd0bbfa92019-09-11 16:10:54 +0800221void Activation::deleteImageManagerObject()
222{
223 // Get the Delete object for <versionID> inside image_manager
224 constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version";
225 constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete";
226 std::string versionService;
227 auto services = utils::getServices(bus, path.c_str(), deleteInterface);
228
229 // We need to find the phosphor-version-software-manager's version service
230 // to invoke the delete interface
231 for (const auto& service : services)
232 {
233 if (service.find(versionServiceStr) != std::string::npos)
234 {
235 versionService = service;
236 break;
237 }
238 }
239 if (versionService.empty())
240 {
241 log<level::ERR>("Error finding version service");
242 return;
243 }
244
245 // Call the Delete object for <versionID> inside image_manager
246 auto method = bus.new_method_call(versionService.c_str(), path.c_str(),
247 deleteInterface, "Delete");
248 try
249 {
250 bus.call(method);
251 }
252 catch (const SdBusError& e)
253 {
254 log<level::ERR>("Error performing call to Delete object path",
255 entry("ERROR=%s", e.what()),
256 entry("PATH=%s", path.c_str()));
257 }
258}
259
Lei YU01539e72019-07-31 10:57:38 +0800260} // namespace updater
261} // namespace software
262} // namespace phosphor