Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 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 "version_handlers_builder.hpp" |
| 17 | |
| 18 | #include "file_handler.hpp" |
| 19 | #include "fs.hpp" |
| 20 | #include "skip_action.hpp" |
| 21 | |
| 22 | #include <nlohmann/json.hpp> |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cstdio> |
| 26 | #include <exception> |
| 27 | #include <fstream> |
| 28 | #include <memory> |
| 29 | #include <regex> |
| 30 | #include <string> |
| 31 | #include <vector> |
| 32 | |
| 33 | namespace ipmi_flash |
| 34 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 35 | |
| 36 | std::vector<HandlerConfig<VersionBlobHandler::ActionPack>> |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 37 | VersionHandlersBuilder::buildHandlerFromJson(const nlohmann::json& data) |
| 38 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 39 | std::vector<HandlerConfig<VersionBlobHandler::ActionPack>> handlers; |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 40 | |
| 41 | for (const auto& item : data) |
| 42 | { |
| 43 | try |
| 44 | { |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 45 | HandlerConfig<VersionBlobHandler::ActionPack> output; |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 46 | |
| 47 | /* at() throws an exception when the key is not present. */ |
| 48 | item.at("blob").get_to(output.blobId); |
| 49 | |
| 50 | /* name must be: /flash/... or /version/...*/ |
| 51 | std::regex regexpr("^\\/(?:flash|version)\\/(.+)"); |
| 52 | std::smatch matches; |
| 53 | if (!std::regex_search(output.blobId, matches, regexpr)) |
| 54 | { |
| 55 | throw std::runtime_error( |
| 56 | "Invalid blob name: '" + output.blobId + |
| 57 | "' must start with /flash/ or /version/"); |
| 58 | } |
| 59 | output.blobId = "/version/" + matches[1].str(); |
| 60 | /* version is required. */ |
Willy Tu | b8fd6d3 | 2021-11-04 20:40:07 -0700 | [diff] [blame] | 61 | if (!item.contains("version")) |
| 62 | { |
| 63 | continue; |
| 64 | } |
| 65 | |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 66 | const auto& v = item.at("version"); |
| 67 | /* version must have handler */ |
| 68 | const auto& h = v.at("handler"); |
| 69 | |
William A. Kennington III | 8557a93 | 2020-12-22 12:05:10 -0800 | [diff] [blame] | 70 | const std::string& handlerType = h.at("type"); |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 71 | if (handlerType == "file") |
| 72 | { |
| 73 | const auto& path = h.at("path"); |
| 74 | output.handler = std::make_unique<FileHandler>(path); |
| 75 | } |
| 76 | else |
| 77 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 78 | throw std::runtime_error( |
| 79 | "Invalid handler type: " + handlerType); |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /* actions are required (presently). */ |
| 83 | const auto& a = v.at("actions"); |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 84 | auto pack = std::make_unique<VersionBlobHandler::ActionPack>(); |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 85 | |
| 86 | /* to make an action optional, assign type "skip" */ |
| 87 | const auto& onOpen = a.at("open"); |
William A. Kennington III | 8557a93 | 2020-12-22 12:05:10 -0800 | [diff] [blame] | 88 | const std::string& onOpenType = onOpen.at("type"); |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 89 | if (onOpenType == "systemd") |
| 90 | { |
| 91 | pack->onOpen = std::move(buildSystemd(onOpen)); |
| 92 | } |
| 93 | else if (onOpenType == "skip") |
| 94 | { |
| 95 | pack->onOpen = SkipAction::CreateSkipAction(); |
| 96 | } |
| 97 | else |
| 98 | { |
Patrick Williams | 42a44c2 | 2024-08-16 15:21:32 -0400 | [diff] [blame^] | 99 | throw std::runtime_error( |
| 100 | "Invalid preparation type: " + onOpenType); |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | output.actions = std::move(pack); |
| 104 | handlers.push_back(std::move(output)); |
| 105 | } |
| 106 | catch (const std::exception& e) |
| 107 | { |
| 108 | /* TODO: Once phosphor-logging supports unit-test injection, fix |
| 109 | * this to log. |
| 110 | */ |
| 111 | std::fprintf(stderr, |
| 112 | "Excepted building HandlerConfig from json: %s\n", |
| 113 | e.what()); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | return handlers; |
| 118 | } |
William A. Kennington III | abf1735 | 2020-12-22 21:07:11 -0800 | [diff] [blame] | 119 | |
Jason Ling | 85e54f1 | 2020-11-05 18:47:21 -0800 | [diff] [blame] | 120 | } // namespace ipmi_flash |