blob: 193d6b32af3a54d04ce2f3219bf4d813f5223081 [file] [log] [blame]
Lei YUb53425d2019-02-22 11:38:40 +08001#include "activation_static.hpp"
2
Lei YUa2e67162019-02-22 17:35:24 +08003#include "item_updater.hpp"
4
5#include <filesystem>
6#include <phosphor-logging/log.hpp>
7
Lei YUb53425d2019-02-22 11:38:40 +08008namespace openpower
9{
10namespace software
11{
12namespace updater
13{
Lei YUa2e67162019-02-22 17:35:24 +080014namespace fs = std::filesystem;
Lei YUb53425d2019-02-22 11:38:40 +080015namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server;
16
Lei YUa2e67162019-02-22 17:35:24 +080017using namespace phosphor::logging;
18
19auto ActivationStatic::activation(Activations value) -> Activations
20{
21
Lei YU6da3dae2019-02-28 14:26:37 +080022 auto ret = value;
Lei YUa2e67162019-02-22 17:35:24 +080023 if (value != softwareServer::Activation::Activations::Active)
24 {
25 redundancyPriority.reset(nullptr);
26 }
27
28 if (value == softwareServer::Activation::Activations::Activating)
29 {
Lei YU6da3dae2019-02-28 14:26:37 +080030 if (parent.freeSpace())
31 {
32 startActivation();
33 }
34 else
35 {
36 ret = softwareServer::Activation::Activations::Failed;
37 }
Lei YUa2e67162019-02-22 17:35:24 +080038 }
39 else
40 {
41 activationBlocksTransition.reset(nullptr);
42 activationProgress.reset(nullptr);
43 }
44
Lei YU6da3dae2019-02-28 14:26:37 +080045 return softwareServer::Activation::activation(ret);
Lei YUa2e67162019-02-22 17:35:24 +080046}
47
Lei YUb53425d2019-02-22 11:38:40 +080048void ActivationStatic::startActivation()
49{
Lei YUa2e67162019-02-22 17:35:24 +080050 fs::path pnorFile;
51 fs::path imagePath(IMG_DIR);
52 imagePath /= versionId;
53
54 for (const auto& entry : fs::directory_iterator(imagePath))
55 {
56 if (entry.path().extension() == ".pnor")
57 {
58 pnorFile = entry;
59 break;
60 }
61 }
62 if (pnorFile.empty())
63 {
64 log<level::ERR>("Unable to find pnor file",
65 entry("DIR=%s", imagePath.c_str()));
66 return;
67 }
68
69 if (!activationProgress)
70 {
71 activationProgress = std::make_unique<ActivationProgress>(bus, path);
72 }
73
74 if (!activationBlocksTransition)
75 {
76 activationBlocksTransition =
77 std::make_unique<ActivationBlocksTransition>(bus, path);
78 }
79
80 // TODO: check why the signal is still received without calling this
81 // function?
82 subscribeToSystemdSignals();
83
84 log<level::INFO>("Start programming...",
85 entry("PNOR=%s", pnorFile.c_str()));
86
87 std::string pnorFileEscaped = pnorFile.string();
88 // Escape all '/' to '-'
89 std::replace(pnorFileEscaped.begin(), pnorFileEscaped.end(), '/', '-');
90
91 constexpr auto updatePNORService = "openpower-pnor-update@";
92 pnorUpdateUnit =
93 std::string(updatePNORService) + pnorFileEscaped + ".service";
94 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
95 SYSTEMD_INTERFACE, "StartUnit");
96 method.append(pnorUpdateUnit, "replace");
97 bus.call_noreply(method);
98
99 activationProgress->progress(10);
Lei YUb53425d2019-02-22 11:38:40 +0800100}
101
102void ActivationStatic::unitStateChange(sdbusplus::message::message& msg)
103{
Lei YUa2e67162019-02-22 17:35:24 +0800104 uint32_t newStateID{};
105 sdbusplus::message::object_path newStateObjPath;
106 std::string newStateUnit{};
107 std::string newStateResult{};
108
109 // Read the msg and populate each variable
110 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
111
112 if (newStateUnit == pnorUpdateUnit)
113 {
114 if (newStateResult == "done")
115 {
116 finishActivation();
117 }
118 if (newStateResult == "failed" || newStateResult == "dependency")
119 {
120 Activation::activation(
121 softwareServer::Activation::Activations::Failed);
122 }
123 }
Lei YUb53425d2019-02-22 11:38:40 +0800124}
125
126void ActivationStatic::finishActivation()
127{
Lei YUa2e67162019-02-22 17:35:24 +0800128 activationProgress->progress(90);
129
130 // Set Redundancy Priority before setting to Active
131 if (!redundancyPriority)
132 {
133 redundancyPriority =
134 std::make_unique<RedundancyPriority>(bus, path, *this, 0);
135 }
136
137 activationProgress->progress(100);
138
139 activationBlocksTransition.reset();
140 activationProgress.reset();
141
142 unsubscribeFromSystemdSignals();
143 // Remove version object from image manager
144 deleteImageManagerObject();
145 // Create active association
146 parent.createActiveAssociation(path);
147 // Create functional assocaition
148 parent.updateFunctionalAssociation(versionId);
149
150 Activation::activation(Activation::Activations::Active);
Lei YUb53425d2019-02-22 11:38:40 +0800151}
152
153} // namespace updater
154} // namespace software
155} // namespace openpower