Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 3 | #include "activation.hpp" |
| 4 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 5 | #include "utils.hpp" |
| 6 | |
| 7 | #include <cassert> |
| 8 | #include <filesystem> |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 11 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace updater |
| 17 | { |
| 18 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 19 | constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1"; |
| 20 | constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1"; |
| 21 | constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 22 | |
| 23 | namespace fs = std::filesystem; |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 24 | namespace softwareServer = sdbusplus::xyz::openbmc_project::Software::server; |
| 25 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 26 | using namespace phosphor::logging; |
| 27 | using sdbusplus::exception::SdBusError; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 28 | using SoftwareActivation = softwareServer::Activation; |
| 29 | |
| 30 | namespace internal |
| 31 | { |
| 32 | /** Construct the systemd service name */ |
| 33 | std::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 YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 53 | auto Activation::activation(Activations value) -> Activations |
| 54 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 55 | if (value == Status::Activating) |
| 56 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 57 | value = startActivation(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 58 | } |
| 59 | else |
| 60 | { |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 61 | activationBlocksTransition.reset(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 62 | activationProgress.reset(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | return SoftwareActivation::activation(value); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | auto Activation::requestedActivation(RequestedActivations value) |
| 69 | -> RequestedActivations |
| 70 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 71 | 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 | |
| 83 | void 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 YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 97 | onUpdateDone(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 98 | } |
| 99 | if (newStateResult == "failed" || newStateResult == "dependency") |
| 100 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 101 | onUpdateFailed(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 106 | bool 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 | |
| 125 | bool 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 | |
| 139 | void 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 | |
| 148 | void 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 | |
| 157 | Activation::Status Activation::startActivation() |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 158 | { |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 159 | if (!activationProgress) |
| 160 | { |
| 161 | activationProgress = std::make_unique<ActivationProgress>(bus, path); |
| 162 | } |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 163 | if (!activationBlocksTransition) |
| 164 | { |
| 165 | activationBlocksTransition = |
| 166 | std::make_unique<ActivationBlocksTransition>(bus, path); |
| 167 | } |
| 168 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 169 | auto psuPaths = utils::getPSUInventoryPath(bus); |
| 170 | if (psuPaths.empty()) |
| 171 | { |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 172 | log<level::WARNING>("No PSU inventory found"); |
| 173 | return Status::Failed; |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 176 | for (const auto& p : psuPaths) |
| 177 | { |
| 178 | psuQueue.push(p); |
| 179 | } |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 180 | |
Lei YU | ff83c2a | 2019-09-12 13:55:18 +0800 | [diff] [blame^] | 181 | // 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 YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void Activation::finishActivation() |
| 201 | { |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame] | 202 | activationProgress->progress(100); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 203 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 204 | // TODO: delete the old software object |
| 205 | // TODO: create related associations |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 206 | deleteImageManagerObject(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 207 | activation(Status::Active); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 208 | } |
| 209 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 210 | void 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 YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 249 | } // namespace updater |
| 250 | } // namespace software |
| 251 | } // namespace phosphor |