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