RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/launcher/launcher.cc
00001 /* launcher.cc                                        -*- C++ -*-
00002    Eric Robert, 29 February 2013
00003    Copyright (c) 2012 Datacratic.  All rights reserved.
00004 
00005    Tool to launch the stack.
00006 */
00007 
00008 
00009 #include <boost/program_options/cmdline.hpp>
00010 #include <boost/program_options/options_description.hpp>
00011 #include <boost/program_options/positional_options.hpp>
00012 #include <boost/program_options/parsers.hpp>
00013 #include <boost/program_options/variables_map.hpp>
00014 #include <boost/algorithm/string.hpp>
00015 #include <boost/thread/thread.hpp>
00016 #include <boost/make_shared.hpp>
00017 #include <fstream>
00018 
00019 #include "soa/launcher/launcher.h"
00020 
00021 int main(int argc, char ** argv)
00022 {
00023     using namespace boost::program_options;
00024     options_description configuration_options("Configuration options");
00025 
00026     std::string filename;
00027     std::string node;
00028     std::string script;
00029     bool launch = false;
00030     bool master = false;
00031 
00032     configuration_options.add_options()
00033         ("file,F", value(&filename), "filename of the launch sequence")
00034         ("launch,L", value(&launch)->zero_tokens(), "run the launch monitoring process?")
00035         ("master,M", value(&master)->zero_tokens(), "specify that this will be the master node?")
00036         ("node,N", value(&node), "name of the current node")
00037         ("script,S", value(&script), "filename of the launch script sequence to generate and use");
00038 
00039     options_description all_opt;
00040     all_opt.add(configuration_options);
00041     all_opt.add_options()
00042         ("help,h", "print this message");
00043 
00044     positional_options_description p;
00045     p.add("file", -1);
00046 
00047     variables_map vm;
00048     store(command_line_parser(argc, argv).options(all_opt).positional(p).run(), vm);
00049     notify(vm);
00050 
00051     if(vm.count("help")) {
00052         std::cerr << all_opt << std::endl;
00053         exit(1);
00054     }
00055 
00056     if(filename.empty()) {
00057         std::cerr << "configuration file is required" << std::endl;
00058         exit(1);
00059     }
00060 
00061     if(node.empty()) {
00062         std::cerr << "current node is required" << std::endl;
00063         exit(1);
00064     }
00065 
00066     std::ifstream file(filename);
00067     if(!file) {
00068         std::cerr << "failed to open file " << filename << std::endl;
00069     }
00070 
00071     Json::Reader reader;
00072     Json::Value root;
00073     if(!reader.parse(file, root)) {
00074         std::cerr << "cannot read file '" << filename << "'" << std::endl;
00075         std::cerr << reader.getFormattedErrorMessages();
00076         exit(1);
00077     }
00078 
00079     Datacratic::Launcher::Service::get().run(root, node, filename, script, launch, master);
00080 
00081     if(!launch) {
00082         int res = system(script.c_str());
00083         if(res == -1) {
00084             std::cerr << "cannot launch script" << std::endl;
00085             exit(1);
00086         } 
00087     }
00088 }
00089 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator