JaiaBot  1.12.0+7+gdc1e5561
JaiaBot micro-AUV software
liaison_upgrade.h
Go to the documentation of this file.
1 #ifndef LIAISON_UPGRADE_H
2 #define LIAISON_UPGRADE_H
3 
4 #include <Wt/Http/Response>
5 #include <Wt/WFileResource>
6 
7 #include <chrono>
8 #include <future>
9 
10 #include <boost/asio/io_service.hpp>
11 #include <boost/process.hpp>
12 #include <boost/process/pipe.hpp>
13 
14 #include <goby/util/thirdparty/nlohmann/json.hpp>
15 #include <goby/zeromq/liaison/liaison_container.h>
16 
17 #include "config.pb.h"
18 
19 namespace jaiabot
20 {
21 class LiaisonUpgrade : public goby::zeromq::LiaisonContainer
22 {
23  public:
24  LiaisonUpgrade(const goby::apps::zeromq::protobuf::LiaisonConfig& cfg,
25  Wt::WContainerWidget* parent = 0);
26 
27  private:
28  class LogFileResource : public Wt::WResource
29  {
30  public:
31  LogFileResource() {}
32 
33  void set_last_log(const std::string& log) { last_log_ = log; }
34 
35  void handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response) override
36  {
37  suggestFileName("jaiabot_upgrade_ansible_log_ " + goby::time::file_str() + ".json");
38  response.addHeader("Content-Type", "text/plain");
39  response.out() << last_log_;
40  }
41 
42  private:
43  std::string last_log_;
44  };
45 
47  {
48  AnsiblePlaybookConfig(const jaiabot::protobuf::UpgradeConfig::AnsiblePlaybook& playbook,
49  LiaisonUpgrade* parent, std::size_t playbook_index);
50  AnsiblePlaybookConfig() = delete;
53 
54  std::string file;
55  Wt::WGroupBox* group_box;
56  Wt::WContainerWidget* group_div;
57  Wt::WContainerWidget* iv_group_div;
58  Wt::WContainerWidget* run_button_div;
59  Wt::WPushButton* run_button;
60  Wt::WContainerWidget* log_button_div;
61  Wt::WPushButton* log_button;
62  Wt::WContainerWidget* result_div;
63  Wt::WText* result_text;
64  Wt::WTable* result_table;
65  std::vector<std::string>::const_iterator run_text_it;
66  // name -> value
67  std::map<std::string, std::string> input_var;
68  // name -> display name
69  std::map<std::string, std::string> output_var;
70  std::vector<std::string> output_var_order;
71 
72  struct ProcessData
73  {
74  ProcessData(const protobuf::UpgradeConfig& cfg, const std::string& playbook_file,
75  const jaiabot::protobuf::UpgradeConfig::AnsiblePlaybook& pb_playbook,
76  const std::string& input_vars);
78 
79  boost::asio::io_service io;
80  std::future<std::string> stdout;
81  std::future<std::string> stderr;
82  boost::process::child process;
83  std::thread io_thread;
84  };
85  std::unique_ptr<ProcessData> pdata;
86  std::string last_log;
87  std::shared_ptr<LogFileResource> log_resource;
88  const jaiabot::protobuf::UpgradeConfig::AnsiblePlaybook& pb_playbook;
89  };
90  friend struct AnsiblePlaybookConfig;
91 
92  struct Result
93  {
94  enum ResultType
95  {
96  FAILURE,
97  UNREACHABLE,
98  SUCCESS // not FAILURE or UNREACHABLE
99  };
100  ResultType result{SUCCESS};
101  std::map<std::string, std::string> output_vars;
102  };
103 
104  void run_ansible_playbook(std::size_t playbook_index);
105  void set_input_var(int selection_index, Wt::WComboBox* selection, std::string name,
106  std::size_t playbook_index);
107 
108  void process_ansible_json_result(nlohmann::json j, AnsiblePlaybookConfig& playbook);
109 
110  void loop();
111  void focus() override { timer_.start(); }
112  void unfocus() override { timer_.stop(); }
113 
114  private:
115  const protobuf::UpgradeConfig& cfg_;
116  std::vector<AnsiblePlaybookConfig> playbooks_;
117  Wt::WTimer timer_;
118  static const std::vector<std::string> running_;
119  static const Wt::WColor color_success_;
120  static const Wt::WColor color_failure_;
121 };
122 
123 } // namespace jaiabot
124 
125 #endif
friend struct AnsiblePlaybookConfig
LiaisonUpgrade(const goby::apps::zeromq::protobuf::LiaisonConfig &cfg, Wt::WContainerWidget *parent=0)
ProcessData(const protobuf::UpgradeConfig &cfg, const std::string &playbook_file, const jaiabot::protobuf::UpgradeConfig::AnsiblePlaybook &pb_playbook, const std::string &input_vars)