Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 1 | #include "activation_static.hpp" |
| 2 | |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 3 | #include "item_updater.hpp" |
| 4 | |
| 5 | #include <filesystem> |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 8 | namespace openpower |
| 9 | { |
| 10 | namespace software |
| 11 | { |
| 12 | namespace updater |
| 13 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 14 | namespace fs = std::filesystem; |
Lei YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 15 | namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server; |
| 16 | |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 17 | using namespace phosphor::logging; |
| 18 | |
| 19 | auto 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 YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 42 | void ActivationStatic::startActivation() |
| 43 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 44 | 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 YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void ActivationStatic::unitStateChange(sdbusplus::message::message& msg) |
| 97 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 98 | 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 YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void ActivationStatic::finishActivation() |
| 121 | { |
Lei YU | a2e6716 | 2019-02-22 17:35:24 +0800 | [diff] [blame] | 122 | 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 YU | b53425d | 2019-02-22 11:38:40 +0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace updater |
| 148 | } // namespace software |
| 149 | } // namespace openpower |