1 /* 2 * Copyright 2019 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include "buildjson.hpp" 17 18 #include "file_handler.hpp" 19 #include "fs.hpp" 20 #include "general_systemd.hpp" 21 #include "prepare_systemd.hpp" 22 #include "update_systemd.hpp" 23 24 #include <algorithm> 25 #include <cstdio> 26 #include <exception> 27 #include <fstream> 28 #include <nlohmann/json.hpp> 29 #include <phosphor-logging/log.hpp> 30 #include <regex> 31 #include <sdbusplus/bus.hpp> 32 #include <string> 33 #include <vector> 34 35 namespace ipmi_flash 36 { 37 38 std::unique_ptr<TriggerableActionInterface> 39 buildFileSystemd(const nlohmann::json& data) 40 { 41 /* This type of action requires a path and unit, and optionally a mode. */ 42 const auto& path = data.at("path"); 43 const auto& unit = data.at("unit"); 44 45 /* the mode parameter is optional. */ 46 std::string systemdMode = "replace"; 47 const auto& mode = data.find("mode"); 48 if (mode != data.end()) 49 { 50 systemdMode = data.at("mode").get<std::string>(); 51 } 52 53 return SystemdWithStatusFile::CreateSystemdWithStatusFile( 54 sdbusplus::bus::new_default(), path, unit, systemdMode); 55 } 56 57 std::vector<HandlerConfig> buildHandlerFromJson(const nlohmann::json& data) 58 { 59 std::vector<HandlerConfig> handlers; 60 61 for (const auto& item : data) 62 { 63 try 64 { 65 HandlerConfig output; 66 67 /* at() throws an exception when the key is not present. */ 68 item.at("blob").get_to(output.blobId); 69 70 /* name must be: /flash/... */ 71 if (!std::regex_match(output.blobId, std::regex("^\\/flash\\/.+"))) 72 { 73 throw std::runtime_error("Invalid blob name: '" + 74 output.blobId + 75 "' must start with /flash/"); 76 } 77 78 /* handler is required. */ 79 const auto& h = item.at("handler"); 80 const std::string handlerType = h.at("type"); 81 if (handlerType == "file") 82 { 83 const auto& path = h.at("path"); 84 output.handler = std::make_unique<FileHandler>(path); 85 } 86 else 87 { 88 throw std::runtime_error("Invalid handler type: " + 89 handlerType); 90 } 91 92 /* actions are required (presently). */ 93 const auto& a = item.at("actions"); 94 std::unique_ptr<ActionPack> pack = std::make_unique<ActionPack>(); 95 96 /* It hasn't been fully determined if any action being optional is 97 * useful, so for now they will be required. 98 * TODO: Evaluate how the behaviors change if some actions are 99 * missing, does the code just assume it was successful? I would 100 * think not. 101 */ 102 const auto& prep = a.at("preparation"); 103 const std::string prepareType = prep.at("type"); 104 if (prepareType == "systemd") 105 { 106 const auto& unit = prep.at("unit"); 107 pack->preparation = SystemdPreparation::CreatePreparation( 108 sdbusplus::bus::new_default(), unit); 109 } 110 else 111 { 112 throw std::runtime_error("Invalid preparation type: " + 113 prepareType); 114 } 115 116 const auto& verify = a.at("verification"); 117 const std::string verifyType = verify.at("type"); 118 if (verifyType == "fileSystemdVerify") 119 { 120 pack->verification = std::move(buildFileSystemd(verify)); 121 } 122 else 123 { 124 throw std::runtime_error("Invalid verification type:" + 125 verifyType); 126 } 127 128 const auto& update = a.at("update"); 129 const std::string updateType = update.at("type"); 130 if (updateType == "reboot") 131 { 132 pack->update = SystemdUpdateMechanism::CreateSystemdUpdate( 133 sdbusplus::bus::new_default(), "reboot.target", 134 "replace-irreversibly"); 135 } 136 else if (updateType == "fileSystemdUpdate") 137 { 138 pack->update = std::move(buildFileSystemd(update)); 139 } 140 else if (updateType == "systemd") 141 { 142 const auto& unit = update.at("unit"); 143 144 /* the mode parameter is optional. */ 145 std::string systemdMode = "replace"; 146 const auto& mode = update.find("mode"); 147 if (mode != update.end()) 148 { 149 systemdMode = update.at("mode").get<std::string>(); 150 } 151 152 pack->update = SystemdUpdateMechanism::CreateSystemdUpdate( 153 sdbusplus::bus::new_default(), unit, systemdMode); 154 } 155 else 156 { 157 throw std::runtime_error("Invalid update type: " + updateType); 158 } 159 160 output.actions = std::move(pack); 161 handlers.push_back(std::move(output)); 162 } 163 catch (const std::exception& e) 164 { 165 /* TODO: Once phosphor-logging supports unit-test injection, fix 166 * this to log. 167 */ 168 std::fprintf(stderr, 169 "Excepted building HandlerConfig from json: %s\n", 170 e.what()); 171 } 172 } 173 174 return handlers; 175 } 176 177 std::vector<HandlerConfig> BuildHandlerConfigs(const std::string& directory) 178 { 179 using namespace phosphor::logging; 180 181 std::vector<HandlerConfig> output; 182 183 std::vector<std::string> jsonPaths = GetJsonList(directory); 184 185 for (const auto& path : jsonPaths) 186 { 187 std::ifstream jsonFile(path); 188 if (!jsonFile.is_open()) 189 { 190 log<level::ERR>("Unable to open json file", 191 entry("PATH=%s", path.c_str())); 192 continue; 193 } 194 195 auto data = nlohmann::json::parse(jsonFile, nullptr, false); 196 if (data.is_discarded()) 197 { 198 log<level::ERR>("Parsing json failed", 199 entry("PATH=%s", path.c_str())); 200 continue; 201 } 202 203 std::vector<HandlerConfig> configs = buildHandlerFromJson(data); 204 std::move(configs.begin(), configs.end(), std::back_inserter(output)); 205 } 206 207 return output; 208 } 209 210 } // namespace ipmi_flash 211