blob: 9768d415704f73a3411570d698f95eed34f59efa [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
22 if (value != softwareServer::Activation::Activations::Active)
23 {
24 redundancyPriority.reset(nullptr);
25 }
26
27 if (value == softwareServer::Activation::Activations::Activating)
28 {
29 parent.freeSpace();
30 startActivation();
31 return softwareServer::Activation::activation(value);
32 }
33 else
34 {
35 activationBlocksTransition.reset(nullptr);
36 activationProgress.reset(nullptr);
37 }
38
39 return softwareServer::Activation::activation(value);
40}
41
Lei YUb53425d2019-02-22 11:38:40 +080042void ActivationStatic::startActivation()
43{
Lei YUa2e67162019-02-22 17:35:24 +080044 fs::path pnorFile;
45 fs::path imagePath(IMG_DIR);
46 imagePath /= versionId;
47
48 for (const auto& entry : fs::directory_iterator(imagePath))
49 {
50 if (entry.path().extension() == ".pnor")
51 {
52 pnorFile = entry;
53 break;
54 }
55 }
56 if (pnorFile.empty())
57 {
58 log<level::ERR>("Unable to find pnor file",
59 entry("DIR=%s", imagePath.c_str()));
60 return;
61 }
62
63 if (!activationProgress)
64 {
65 activationProgress = std::make_unique<ActivationProgress>(bus, path);
66 }
67
68 if (!activationBlocksTransition)
69 {
70 activationBlocksTransition =
71 std::make_unique<ActivationBlocksTransition>(bus, path);
72 }
73
74 // TODO: check why the signal is still received without calling this
75 // function?
76 subscribeToSystemdSignals();
77
78 log<level::INFO>("Start programming...",
79 entry("PNOR=%s", pnorFile.c_str()));
80
81 std::string pnorFileEscaped = pnorFile.string();
82 // Escape all '/' to '-'
83 std::replace(pnorFileEscaped.begin(), pnorFileEscaped.end(), '/', '-');
84
85 constexpr auto updatePNORService = "openpower-pnor-update@";
86 pnorUpdateUnit =
87 std::string(updatePNORService) + pnorFileEscaped + ".service";
88 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
89 SYSTEMD_INTERFACE, "StartUnit");
90 method.append(pnorUpdateUnit, "replace");
91 bus.call_noreply(method);
92
93 activationProgress->progress(10);
Lei YUb53425d2019-02-22 11:38:40 +080094}
95
96void ActivationStatic::unitStateChange(sdbusplus::message::message& msg)
97{
Lei YUa2e67162019-02-22 17:35:24 +080098 uint32_t newStateID{};
99 sdbusplus::message::object_path newStateObjPath;
100 std::string newStateUnit{};
101 std::string newStateResult{};
102
103 // Read the msg and populate each variable
104 msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
105
106 if (newStateUnit == pnorUpdateUnit)
107 {
108 if (newStateResult == "done")
109 {
110 finishActivation();
111 }
112 if (newStateResult == "failed" || newStateResult == "dependency")
113 {
114 Activation::activation(
115 softwareServer::Activation::Activations::Failed);
116 }
117 }
Lei YUb53425d2019-02-22 11:38:40 +0800118}
119
120void ActivationStatic::finishActivation()
121{
Lei YUa2e67162019-02-22 17:35:24 +0800122 activationProgress->progress(90);
123
124 // Set Redundancy Priority before setting to Active
125 if (!redundancyPriority)
126 {
127 redundancyPriority =
128 std::make_unique<RedundancyPriority>(bus, path, *this, 0);
129 }
130
131 activationProgress->progress(100);
132
133 activationBlocksTransition.reset();
134 activationProgress.reset();
135
136 unsubscribeFromSystemdSignals();
137 // Remove version object from image manager
138 deleteImageManagerObject();
139 // Create active association
140 parent.createActiveAssociation(path);
141 // Create functional assocaition
142 parent.updateFunctionalAssociation(versionId);
143
144 Activation::activation(Activation::Activations::Active);
Lei YUb53425d2019-02-22 11:38:40 +0800145}
146
147} // namespace updater
148} // namespace software
149} // namespace openpower