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 | { |
| 57 | startActivation(); |
| 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 | { |
| 97 | finishActivation(); |
| 98 | } |
| 99 | if (newStateResult == "failed" || newStateResult == "dependency") |
| 100 | { |
| 101 | activation(Status::Failed); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void Activation::startActivation() |
| 107 | { |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame^] | 108 | if (!activationProgress) |
| 109 | { |
| 110 | activationProgress = std::make_unique<ActivationProgress>(bus, path); |
| 111 | } |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 112 | if (!activationBlocksTransition) |
| 113 | { |
| 114 | activationBlocksTransition = |
| 115 | std::make_unique<ActivationBlocksTransition>(bus, path); |
| 116 | } |
| 117 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 118 | // TODO: for now only update one psu, future commits shall handle update |
| 119 | // multiple psus |
| 120 | auto psuPaths = utils::getPSUInventoryPath(bus); |
| 121 | if (psuPaths.empty()) |
| 122 | { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | psuUpdateUnit = internal::getUpdateService(psuPaths[0], versionId); |
| 127 | |
| 128 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 129 | SYSTEMD_INTERFACE, "StartUnit"); |
| 130 | method.append(psuUpdateUnit, "replace"); |
| 131 | bus.call_noreply(method); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame^] | 132 | |
| 133 | activationProgress->progress(10); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void Activation::finishActivation() |
| 137 | { |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame^] | 138 | activationProgress->progress(100); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 139 | activationBlocksTransition.reset(); |
Lei YU | 90c8a8b | 2019-09-11 17:20:03 +0800 | [diff] [blame^] | 140 | activationProgress.reset(); |
Lei YU | 81c6772 | 2019-09-11 16:47:29 +0800 | [diff] [blame] | 141 | |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 142 | // TODO: delete the old software object |
| 143 | // TODO: create related associations |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 144 | deleteImageManagerObject(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 145 | activation(Status::Active); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 146 | } |
| 147 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame] | 148 | void Activation::deleteImageManagerObject() |
| 149 | { |
| 150 | // Get the Delete object for <versionID> inside image_manager |
| 151 | constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version"; |
| 152 | constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete"; |
| 153 | std::string versionService; |
| 154 | auto services = utils::getServices(bus, path.c_str(), deleteInterface); |
| 155 | |
| 156 | // We need to find the phosphor-version-software-manager's version service |
| 157 | // to invoke the delete interface |
| 158 | for (const auto& service : services) |
| 159 | { |
| 160 | if (service.find(versionServiceStr) != std::string::npos) |
| 161 | { |
| 162 | versionService = service; |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | if (versionService.empty()) |
| 167 | { |
| 168 | log<level::ERR>("Error finding version service"); |
| 169 | return; |
| 170 | } |
| 171 | |
| 172 | // Call the Delete object for <versionID> inside image_manager |
| 173 | auto method = bus.new_method_call(versionService.c_str(), path.c_str(), |
| 174 | deleteInterface, "Delete"); |
| 175 | try |
| 176 | { |
| 177 | bus.call(method); |
| 178 | } |
| 179 | catch (const SdBusError& e) |
| 180 | { |
| 181 | log<level::ERR>("Error performing call to Delete object path", |
| 182 | entry("ERROR=%s", e.what()), |
| 183 | entry("PATH=%s", path.c_str())); |
| 184 | } |
| 185 | } |
| 186 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 187 | } // namespace updater |
| 188 | } // namespace software |
| 189 | } // namespace phosphor |