$darkmode
command.hpp
1 /*
2  * Copyright 2020 Robert Bosch GmbH
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  * SPDX-License-Identifier: Apache-2.0
17  */
25 #pragma once
26 
27 #include <optional> // for optional<>
28 #include <string> // for string
29 #include <system_error> // for system_error
30 #include <utility> // for move
31 #include <vector> // for vector<>
32 
33 #include <boost/process/child.hpp> // for child
34 #include <cloe/core.hpp> // for Logger, Json, Conf, ...
35 #include <cloe/trigger.hpp> // for Action, ActionFactory, ...
36 #include <cloe/utility/command.hpp> // for Command
37 
38 namespace engine {
39 
40 struct CommandResult {
41  std::string name;
42  std::string command;
43  std::optional<boost::process::child> child;
44  std::optional<int> exit_code;
45  std::optional<std::system_error> error;
46  std::vector<std::string> output;
47 };
48 
50  public:
51  explicit CommandExecuter(cloe::Logger logger, bool enabled = true)
52  : logger_(std::move(logger)), enabled_(enabled) {}
53 
54  [[nodiscard]] bool is_enabled() const { return enabled_; }
55  void set_enabled(bool v) { enabled_ = v; }
56 
57  CommandResult run_and_release(const cloe::Command&) const; // NOLINT
58 
59  void run(const cloe::Command&);
60 
61  void run_all(const std::vector<cloe::Command>& cmds);
62 
63  void wait(CommandResult&) const;
64 
65  void wait_all();
66 
67  std::vector<CommandResult> release_all();
68 
69  [[nodiscard]] cloe::Logger logger() const { return logger_; }
70 
71  private:
72  std::vector<CommandResult> handles_;
73  cloe::Logger logger_{nullptr};
74  bool enabled_{false};
75 };
76 
77 namespace actions {
78 
79 class Command : public cloe::Action {
80  public:
81  Command(const std::string& name, cloe::Command cmd, CommandExecuter* exec)
82  : Action(name), command_(std::move(cmd)), executer_(exec) {
83  assert(executer_ != nullptr);
84  }
85 
86  cloe::ActionPtr clone() const override {
87  return std::make_unique<Command>(name(), command_, executer_);
88  }
89 
90  cloe::CallbackResult operator()(const cloe::Sync& sync, cloe::TriggerRegistrar& registrar) override;
91 
92  protected:
93  void to_json(cloe::Json& j) const override;
94 
95  private:
96  cloe::Command command_;
97  CommandExecuter* executer_{nullptr};
98 };
99 
101  public:
102  using ActionType = Command;
104  : cloe::ActionFactory("command", "run a system command"), executer_(exec) {
105  assert(executer_ != nullptr);
106  }
107  [[nodiscard]] cloe::TriggerSchema schema() const override;
108  [[nodiscard]] cloe::ActionPtr make(const cloe::Conf& c) const override;
109  [[nodiscard]] cloe::ActionPtr make(const std::string& s) const override;
110 
111  private:
112  CommandExecuter* executer_{nullptr};
113 };
114 
115 } // namespace actions
116 } // namespace engine
Definition: trigger.hpp:619
Definition: command.hpp:51
const std::string & name() const
Definition: entity.hpp:67
Definition: sync.hpp:34
Definition: trigger.hpp:290
Definition: trigger.hpp:437
Definition: command.hpp:49
Definition: command.hpp:100
cloe::ActionPtr make(const cloe::Conf &c) const override
Definition: command.cpp:145
cloe::TriggerSchema schema() const override
Definition: command.cpp:135
Definition: command.hpp:79
cloe::ActionPtr clone() const override
Definition: command.hpp:86
cloe::CallbackResult operator()(const cloe::Sync &sync, cloe::TriggerRegistrar &registrar) override
Definition: command.cpp:131
Definition: conf.hpp:76
std::string exec(const char *cmd)
Definition: server_test.cpp:50
Definition: trigger.hpp:207
Definition: command.hpp:40
CallbackResult
Definition: trigger.hpp:514