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 | { |
| 61 | // TODO |
| 62 | } |
| 63 | |
| 64 | return SoftwareActivation::activation(value); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | auto Activation::requestedActivation(RequestedActivations value) |
| 68 | -> RequestedActivations |
| 69 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 70 | if ((value == SoftwareActivation::RequestedActivations::Active) && |
| 71 | (SoftwareActivation::requestedActivation() != |
| 72 | SoftwareActivation::RequestedActivations::Active)) |
| 73 | { |
| 74 | if ((activation() == Status::Ready) || (activation() == Status::Failed)) |
| 75 | { |
| 76 | activation(Status::Activating); |
| 77 | } |
| 78 | } |
| 79 | return SoftwareActivation::requestedActivation(value); |
| 80 | } |
| 81 | |
| 82 | void Activation::unitStateChange(sdbusplus::message::message& msg) |
| 83 | { |
| 84 | uint32_t newStateID{}; |
| 85 | sdbusplus::message::object_path newStateObjPath; |
| 86 | std::string newStateUnit{}; |
| 87 | std::string newStateResult{}; |
| 88 | |
| 89 | // Read the msg and populate each variable |
| 90 | msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult); |
| 91 | |
| 92 | if (newStateUnit == psuUpdateUnit) |
| 93 | { |
| 94 | if (newStateResult == "done") |
| 95 | { |
| 96 | finishActivation(); |
| 97 | } |
| 98 | if (newStateResult == "failed" || newStateResult == "dependency") |
| 99 | { |
| 100 | activation(Status::Failed); |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void Activation::startActivation() |
| 106 | { |
| 107 | // TODO: for now only update one psu, future commits shall handle update |
| 108 | // multiple psus |
| 109 | auto psuPaths = utils::getPSUInventoryPath(bus); |
| 110 | if (psuPaths.empty()) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | psuUpdateUnit = internal::getUpdateService(psuPaths[0], versionId); |
| 116 | |
| 117 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 118 | SYSTEMD_INTERFACE, "StartUnit"); |
| 119 | method.append(psuUpdateUnit, "replace"); |
| 120 | bus.call_noreply(method); |
| 121 | } |
| 122 | |
| 123 | void Activation::finishActivation() |
| 124 | { |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 125 | // TODO: delete the old software object |
| 126 | // TODO: create related associations |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame^] | 127 | deleteImageManagerObject(); |
Lei YU | 12c9f4c | 2019-09-11 15:08:15 +0800 | [diff] [blame] | 128 | activation(Status::Active); |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 129 | } |
| 130 | |
Lei YU | d0bbfa9 | 2019-09-11 16:10:54 +0800 | [diff] [blame^] | 131 | void Activation::deleteImageManagerObject() |
| 132 | { |
| 133 | // Get the Delete object for <versionID> inside image_manager |
| 134 | constexpr auto versionServiceStr = "xyz.openbmc_project.Software.Version"; |
| 135 | constexpr auto deleteInterface = "xyz.openbmc_project.Object.Delete"; |
| 136 | std::string versionService; |
| 137 | auto services = utils::getServices(bus, path.c_str(), deleteInterface); |
| 138 | |
| 139 | // We need to find the phosphor-version-software-manager's version service |
| 140 | // to invoke the delete interface |
| 141 | for (const auto& service : services) |
| 142 | { |
| 143 | if (service.find(versionServiceStr) != std::string::npos) |
| 144 | { |
| 145 | versionService = service; |
| 146 | break; |
| 147 | } |
| 148 | } |
| 149 | if (versionService.empty()) |
| 150 | { |
| 151 | log<level::ERR>("Error finding version service"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | // Call the Delete object for <versionID> inside image_manager |
| 156 | auto method = bus.new_method_call(versionService.c_str(), path.c_str(), |
| 157 | deleteInterface, "Delete"); |
| 158 | try |
| 159 | { |
| 160 | bus.call(method); |
| 161 | } |
| 162 | catch (const SdBusError& e) |
| 163 | { |
| 164 | log<level::ERR>("Error performing call to Delete object path", |
| 165 | entry("ERROR=%s", e.what()), |
| 166 | entry("PATH=%s", path.c_str())); |
| 167 | } |
| 168 | } |
| 169 | |
Lei YU | 01539e7 | 2019-07-31 10:57:38 +0800 | [diff] [blame] | 170 | } // namespace updater |
| 171 | } // namespace software |
| 172 | } // namespace phosphor |