Changeset fb7e36a in nscp


Ignore:
Timestamp:
09/03/11 21:17:11 (21 months ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
a78a985
Parents:
4cac39f
Message:
  • Added installer for unittests to test.py run like so: nscp --client --module PythonScript --command execute-and-load-python --script test.py
  • Fixed so settings are created if not existing
  • Fixed so plugins are not loaded in client mode
  • Fixed so core is booting correctly in client mode
  • Added new option --settings --switch <context> to allow switching context without migrating data
  • Fixed unicode error on linux (settings file)
Files:
16 edited

Legend:

Unmodified
Added
Removed
  • changelog

    r4cac39f rfb7e36a  
    1010 * Upgraded to new version of simpleinin class 
    1111 * Added new component for python protocol buffer library to installer 
     12 * Added installer for unittests to test.py run like so: 
     13   nscp --client --module PythonScript --command execute-and-load-python --script test.py 
     14 * Fixed so settings are created if not existing 
     15 * Fixed so plugins are not loaded in client mode 
     16 * Fixed so core is booting correctly in client mode 
     17 * Added new option --settings --switch <context> to allow switching context without migrating data 
     18 * Fixed unicode error on linux (settings file) 
    1219 
    13202011-09-02 MickeM 
  • files/old-settings.map

    re11d494 rfb7e36a  
    1818modules/CheckTaskSched.dll=/modules/CheckTaskSched 
    1919modules/Scheduler.dll=/modules/Scheduler 
     20 
     21modules=/modules 
    2022 
    2123# Crash section 
  • helpers/installers/installer/Product.wxs

    r4cac39f rfb7e36a  
    174174 
    175175            <Directory Id="INSTALLLOCATION_SCRIPTS_PYTHON_LIB1" Name="lib"> 
    176               <Component Id="PythonScriptsPBLib1" Guid="8820A304-C697-4393-B72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     176              <Component Id="PythonScriptsPBLib1" Guid="555165B3-5CBF-4204-A349-6AD7CCEF14EB" Win64="$(var.Win64)"> 
    177177                <File Id="p_script_lib_001" Name="__init__.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/__init__.py" Vital="no" /> 
    178178              </Component> 
    179179              <Directory Id="INSTALLLOCATION_SCRIPTS_PYTHON_LIB2" Name="google"> 
    180                 <Component Id="PythonScriptsPBLib2" Guid="8820A304-C697-4393-B72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     180                <Component Id="PythonScriptsPBLib2" Guid="555165B3-5CBF-4204-A349-6AD7CCEF14E1" Win64="$(var.Win64)"> 
    181181                  <File Id="p_script_lib_002" Name="__init__.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/__init__.py" Vital="no" /> 
    182182                </Component> 
    183183                <Directory Id="INSTALLLOCATION_SCRIPTS_PYTHON_LIB3" Name="protobuf"> 
    184                   <Component Id="PythonScriptsPBLib3" Guid="8820A304-C697-4393-B72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     184                  <Component Id="PythonScriptsPBLib3" Guid="555165B3-5CBF-4204-A349-6AD7CCEF14E2" Win64="$(var.Win64)"> 
    185185                    <File Id="p_script_lib_003" Name="descriptor.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/protobuf/descriptor.py" Vital="no" /> 
    186186                    <File Id="p_script_lib_004" Name="descriptor_pb2.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/protobuf/descriptor_pb2.py" Vital="no" /> 
     
    193193                  </Component> 
    194194                  <Directory Id="INSTALLLOCATION_SCRIPTS_PYTHON_LIB4" Name="compiler"> 
    195                     <Component Id="PythonScriptsPBLib4" Guid="8820A304-C697-4393-B72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     195                    <Component Id="PythonScriptsPBLib4" Guid="555165B3-5CBF-4204-A349-6AD7CCEF14E3" Win64="$(var.Win64)"> 
    196196                      <File Id="p_script_lib_011" Name="plugin_pb2.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/protobuf/compiler/plugin_pb2.py" Vital="no" /> 
    197197                    </Component> 
    198198                  </Directory> 
    199199                  <Directory Id="INSTALLLOCATION_SCRIPTS_PYTHON_LIB5" Name="internal"> 
    200                     <Component Id="PythonScriptsPBLib5" Guid="8820A304-C697-4393-B72F-$(var.Postfix.GUID)" Win64="$(var.Win64)"> 
     200                    <Component Id="PythonScriptsPBLib5" Guid="555165B3-5CBF-4204-A349-6AD7CCEF14E4" Win64="$(var.Win64)"> 
    201201                      <File Id="p_script_lib_012" Name="api_implementation.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/protobuf/internal/api_implementation.py" Vital="no" /> 
    202202                      <File Id="p_script_lib_013" Name="containers.py" DiskId="1" Source="$(var.Source)/scripts/python/lib/google/protobuf/internal/containers.py" Vital="no" /> 
     
    214214              </Directory> 
    215215            </Directory> 
    216  
    217  
    218216          </Directory> 
    219217        </Directory> 
  • helpers/settings_manager/settings_manager_impl.cpp

    re11d494 rfb7e36a  
    175175      set_instance(key); 
    176176      return; 
    177     }  
    178  
     177    } 
     178 
     179    get_logger()->debug(__FILE__, __LINE__, _T("No valid settings found (tried): ") + boot_order); 
     180 
     181    std::wstring tgt = get_boot_string(_T("main"), _T("write"), _T("")); 
     182    if (!tgt.empty()) { 
     183      get_logger()->debug(__FILE__, __LINE__, _T("Creating new settings file: ") + tgt); 
     184      set_instance(tgt); 
     185      return; 
     186    } 
    179187    get_logger()->err(__FILE__, __LINE__, _T("No valid settings found (tried): ") + boot_order); 
    180188  } 
     
    195203      set_boot_string(_T("settings"), strEx::itos(i++), k); 
    196204    } 
     205    set_boot_string(_T("main"), _T("write"), key); 
    197206  } 
    198207 
  • include/settings/settings_core.hpp

    rb7d17f8 rfb7e36a  
    230230    virtual void migrate_from(std::wstring from) = 0; 
    231231 
     232    virtual void set_primary(std::wstring context) = 0; 
     233 
    232234    ////////////////////////////////////////////////////////////////////////// 
    233235    /// Overwrite the (current) settings store with default values. 
  • include/settings/settings_handler_impl.hpp

    re11d494 rfb7e36a  
    195195      migrate(ifrom, ito); 
    196196    } 
    197     virtual void set_primary(std::wstring key) = 0; 
    198197 
    199198    ////////////////////////////////////////////////////////////////////////// 
  • include/settings/settings_ini.hpp

    r4cac39f rfb7e36a  
    99#include <settings/settings_core.hpp> 
    1010#include <settings/settings_core_impl.hpp> 
     11//#define SI_CONVERT_ICU 
    1112#include <simpleini/simpleini.h> 
    1213#include <error.hpp> 
     
    226227      } 
    227228      std::wstring f = get_file_name(); 
     229      ini.SetUnicode(); 
    228230      get_core()->get_logger()->debug(__FILE__, __LINE__, _T("Loading: ") + f + _T(" from ") + get_context()); 
    229231      SI_Error rc = ini.LoadFile(f.c_str()); 
  • include/simpleini/simpleini.h

    r4cac39f rfb7e36a  
    13331333    return rc; 
    13341334#else // !_WIN32 (therefore SI_CONVERT_ICU) 
    1335 #if SI_CONVERT_ICU 
     1335#ifdef SI_CONVERT_ICU 
    13361336    char szFile[256]; 
    13371337    u_austrncpy(szFile, a_pwszFile, sizeof(szFile)); 
     
    16891689    ) const 
    16901690{ 
    1691     return (a_c == '\n' || a_c == '\r'); 
     1691    return (a_c == '\n' || a_c == '\r' || a_c == 0); 
    16921692} 
    16931693 
  • modules/PythonScript/PythonScript.cpp

    ra14aa07 rfb7e36a  
    2121#include "stdafx.h" 
    2222#include "PythonScript.h" 
    23 #include <strEx.h> 
    2423#include <time.h> 
    2524#include <error.hpp> 
     25 
     26#include <boost/python.hpp> 
     27#include <boost/program_options.hpp> 
     28 
     29#include <strEx.h> 
    2630#include <file_helpers.hpp> 
    27  
    28 #include <boost/python.hpp> 
    29  
    3031#include <settings/client/settings_client.hpp> 
    3132#include <nscapi/functions.hpp> 
     
    3940 
    4041namespace sh = nscapi::settings_helper; 
     42namespace po = boost::program_options; 
    4143 
    4244bool PythonScript::loadModule() { 
     
    102104 
    103105python_script::python_script(unsigned int plugin_id, const std::string alias, const script_container& script) : alias(alias), plugin_id(plugin_id) { 
     106  NSC_DEBUG_MSG_STD(_T("Loading python script: ") + script.script.string()); 
     107  std::wcout << script.script.string() << std::endl;; 
    104108  _exec(utf8::cvt<std::string>(script.script.string())); 
    105109  callFunction("init", plugin_id, alias, utf8::cvt<std::string>(script.alias)); 
     
    213217  NSC_LOG_ERROR(_T("Script not found: ") + file); 
    214218  return boost::optional<boost::filesystem::wpath>(); 
     219} 
     220 
     221NSCAPI::nagiosReturn PythonScript::execute_and_load_python(std::list<std::wstring> args) { 
     222  try { 
     223    po::options_description desc; 
     224    boost::program_options::variables_map vm; 
     225    std::wstring file; 
     226    desc.add_options() 
     227      ("script", po::wvalue<std::wstring>(&file), "The script to run") 
     228      ("file", po::wvalue<std::wstring>(&file), "The script to run") 
     229      ; 
     230 
     231    std::vector<std::wstring> vargs(args.begin(), args.end()); 
     232    po::wparsed_options parsed = po::basic_command_line_parser<wchar_t>(vargs).options(desc).run(); 
     233    po::store(parsed, vm); 
     234    po::notify(vm); 
     235 
     236    boost::optional<boost::filesystem::wpath> ofile = find_file(file); 
     237    if (!ofile) 
     238      return false; 
     239    script_container sc(*ofile); 
     240    python_script script(get_id(), "", sc); 
     241    script.callFunction("__main__"); 
     242  } catch (const std::exception &e) { 
     243    NSC_LOG_ERROR_STD(_T("Failed to execute script ") + utf8::cvt<std::wstring>(e.what())); 
     244  } catch (...) { 
     245    NSC_LOG_ERROR_STD(_T("Failed to execute script...")); 
     246  } 
    215247} 
    216248 
     
    275307 
    276308NSCAPI::nagiosReturn PythonScript::commandRAWLineExec(const wchar_t* char_command, const std::string &request, std::string &response) { 
     309  std::wstring command = char_command; 
     310  if (command == _T("execute-and-load-python")) { 
     311    nscapi::functions::decoded_simple_command_data data = nscapi::functions::parse_simple_exec_request(char_command, request); 
     312    return execute_and_load_python(data.args); 
     313  } 
    277314  boost::shared_ptr<script_wrapper::function_wrapper> inst = script_wrapper::function_wrapper::create(get_id()); 
    278315  std::string cmd = utf8::cvt<std::string>(char_command); 
  • modules/PythonScript/PythonScript.h

    ra44cb15 rfb7e36a  
    8888  NSCAPI::nagiosReturn handleRAWNotification(const std::wstring &channel, const std::wstring &command, std::string &request); 
    8989 
     90  NSCAPI::nagiosReturn execute_and_load_python(std::list<std::wstring> args); 
    9091  //NSCAPI::nagiosReturn RunLUA(const unsigned int argLen, wchar_t **char_args, std::wstring &message, std::wstring &perf); 
    9192  //NSCAPI::nagiosReturn extract_return(Lua_State &L, int arg_count,  std::wstring &message, std::wstring &perf); 
  • scripts/python/test.py

    r4cac39f rfb7e36a  
    202202    return (status.CRITICAL, 'Tests failed %d of %d'%(all_failed, all_count)) 
    203203 
    204  
     204def install_test(arguments): 
     205  log('-+---==(TEST INSTALLER)==---------------------------------------------------+-') 
     206  log(' | Setup nessecary configuration for running test                           |') 
     207  log(' | This includes: Loading the PythonScript module at startup                |') 
     208  log(' | To use this please run nsclient++ in "test mode" like so:                |') 
     209  log(' | nscp --test                                                              |') 
     210  log(' | Then start the pytest_test command by typing it and press enter like so: |') 
     211  log(' | pytest_test                                                              |') 
     212  log(' | Lastly exit by typing exit like so:                                      |') 
     213  log(' | exit                                                                     |') 
     214  log('-+--------------------------------------------------------==(DAS ENDE!)==---+-') 
     215  conf = Settings.get() 
     216  conf.set_string('/modules', 'pytest', 'PythonScript') 
     217  conf.set_string('/settings/pytest/scripts', 'pytest', 'test.py') 
     218  conf.save() 
     219 
     220def __main__(): 
     221  install_test([]) 
     222   
    205223def init(pid, plugin_alias, script_alias): 
    206224  global prefix 
     
    210228    prefix = '%s_'%script_alias 
    211229 
    212   log('Script: test.py with alias: %s (%s:%d)'%(script_alias, plugin_alias, plugin_id)) 
    213  
    214230  conf = Settings.get() 
    215   val = conf.get_string('/modules', 'PythonScript', 'foo') 
    216  
    217   log('Got it: %s'%val) 
     231  #val = conf.get_string('/modules', 'PythonScript', 'foo') 
     232 
     233  #log('Got it: %s'%val) 
    218234   
    219235  log('Testing to register a function') 
    220236  reg = Registry.get(plugin_id) 
    221237   
    222   #reg.simple_cmdline('help', get_help) 
    223   #reg.simple_cmdline('%stest'%prefix, test_cmd) 
     238  reg.simple_cmdline('help', get_help) 
     239  reg.simple_cmdline('install_python_test', install_test) 
    224240 
    225241  reg.simple_function('%stest'%prefix, test, 'Run python unittest') 
     
    228244  #core.simple_submit('test', 'test.py', status.WARNING, 'hello', '') 
    229245   
    230   (ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) 
    231   for l in list: 
    232     log('-- %s --'%l) 
    233  
    234   log('Testing to register settings keys') 
    235   conf.register_path('hello', 'PYTHON SETTINGS', 'This is stuff for python') 
    236   conf.register_key('hello', 'python', 'int', 'KEY', 'This is a key', '42') 
    237  
    238   log('Testing to get key (nonexistant): %d' % conf.get_int('hello', 'python', -1)) 
    239   conf.set_int('hello', 'python', 4) 
    240   log('Testing to get it (after setting it): %d' % conf.get_int('hello', 'python', -1)) 
    241  
    242   log('Saving configuration...') 
     246  #(ret, list) = core.simple_exec('%stest'%prefix, ['a', 'b', 'c']) 
     247  #for l in list: 
     248  # log('-- %s --'%l) 
     249 
     250  #log('Testing to register settings keys') 
     251  #conf.register_path('hello', 'PYTHON SETTINGS', 'This is stuff for python') 
     252  #conf.register_key('hello', 'python', 'int', 'KEY', 'This is a key', '42') 
     253 
     254  #log('Testing to get key (nonexistant): %d' % conf.get_int('hello', 'python', -1)) 
     255  #conf.set_int('hello', 'python', 4) 
     256  #log('Testing to get it (after setting it): %d' % conf.get_int('hello', 'python', -1)) 
     257 
     258  #log('Saving configuration...') 
    243259  #conf.save() 
    244260 
  • service/NSClient++.cpp

    ra44cb15 rfb7e36a  
    533533 * @author mickem 
    534534 */ 
    535 bool NSClientT::initCore(bool boot) { 
    536   LOG_INFO_CORE(_T("Attempting to start")); 
     535bool NSClientT::boot_init() { 
     536  LOG_INFO_CORE(SERVICE_NAME _T(" booting...")); 
    537537 
    538538  if (!settings_manager::init_settings(&provider, context_)) { 
     
    627627 
    628628  if (enable_shared_session_) { 
    629     LOG_DEBUG_CORE(_T("Enabling shared session...")); 
    630     if (boot) { 
    631       LOG_INFO_CORE(_T("shared session not ported yet!...")); 
     629    LOG_INFO_CORE(_T("shared session not ported yet!...")); 
     630//    if (boot) { 
     631//      LOG_INFO_CORE(_T("shared session not ported yet!...")); 
    632632//      try { 
    633633//        shared_server_.reset(new nsclient_session::shared_server_session(this)); 
     
    645645//        shared_server_.reset(NULL); 
    646646//      } 
    647     } else { 
    648       LOG_INFO_CORE(_T("shared session not ported yet!...")); 
     647//    } else { 
     648//      LOG_INFO_CORE(_T("shared session not ported yet!...")); 
    649649//      try { 
    650650//        std::wstring id = _T("_attached_") + strEx::itos(GetCurrentProcessId()) + _T("_"); 
     
    663663//        shared_client_.reset(NULL); 
    664664//      } 
    665     } 
     665//    } 
    666666  } 
    667667#ifdef WIN32 
     
    676676  } 
    677677#endif 
     678  return true; 
     679} 
     680bool NSClientT::boot_load_plugins(bool boot) { 
     681  LOG_DEBUG_CORE(_T("booting::loading plugins")); 
    678682  try { 
    679683    boost::filesystem::wpath pluginPath = expand_path(_T("${module-path}")); 
     
    13791383void NSClientT::handle_startup(std::wstring service_name) { 
    13801384  service_name_ = service_name; 
    1381   initCore(true); 
     1385  boot_init(); 
     1386  boot_load_plugins(true); 
    13821387/* 
    13831388  DWORD dwSessionId = remote_processes::getActiveSessionId(); 
  • service/NSClient++.h

    ra44cb15 rfb7e36a  
    131131 
    132132  // Service helper functions 
    133   bool initCore(bool boot); 
     133  bool boot_init(); 
     134  bool boot_load_plugins(bool boot); 
    134135  bool exitCore(bool boot); 
    135136  void set_settings_context(std::wstring context) { context_ = context; } 
  • service/cli_parser.hpp

    rd7e265d rfb7e36a  
    5858      ("key", po::value<std::wstring>()->default_value(_T("")), "Key to work with.") 
    5959      ("set", po::value<std::wstring>(), "Set a key and path to a given value.") 
     60      ("switch", po::value<std::wstring>(), "Set default context to use (similar to migrate but does NOT copy values)") 
    6061      ("show", "Set a value given a key and path.") 
    6162      ("list", "Set all keys below the path (or root).") 
     
    210211      if (vm.count("show")) { 
    211212        ret = client.show(vm["path"].as<std::wstring>(), vm["key"].as<std::wstring>()); 
     213      } 
     214      if (vm.count("switch")) { 
     215        client.switch_context(vm["switch"].as<std::wstring>()); 
     216        ret = 0; 
    212217      } 
    213218      client.exit(); 
     
    335340        mainClient.log_info(__FILE__, __LINE__, _T("Arguments: ") + args); 
    336341      } 
     342      core_->boot_init(); 
    337343      if (module.empty()) { 
    338         core_->initCore(false); 
     344        core_->boot_load_plugins(false); 
    339345      } 
    340346      int ret = 0; 
  • service/settings_client.hpp

    r7e54a5f rfb7e36a  
    2121      if (!current_.empty()) 
    2222        core_->set_settings_context(current_); 
    23       if (!core_->initCore(false)) { 
    24         std::wcout << _T("Service *NOT* started!") << std::endl; 
     23      if (!core_->boot_init()) { 
     24        std::wcout << _T("boot::init failed") << std::endl; 
     25        return; 
     26      } 
     27      if (!core_->boot_load_plugins(false)) { 
     28        std::wcout << _T("boot::load_plugins failed!") << std::endl; 
    2529        return; 
    2630      } 
     
    126130 
    127131 
     132    void switch_context(std::wstring contect) { 
     133      settings_manager::get_core()->set_primary(contect); 
     134    } 
     135 
    128136    int set(std::wstring path, std::wstring key, std::wstring val) { 
    129137      core_->load_all_plugins(NSCAPI::dontStart); 
  • service/simple_client.hpp

    r4b1e6fe rfb7e36a  
    1515    void start() { 
    1616      core_->enableDebug(true); 
    17       if (!core_->initCore(true)) { 
    18         core_->log_error(__FILE__, __LINE__, _T("Service failed to start")); 
     17      if (!core_->boot_init()) { 
     18        core_->log_error(__FILE__, __LINE__, _T("Service failed to init")); 
     19        return; 
     20      } 
     21      if (!core_->boot_load_plugins(true)) { 
     22        core_->log_error(__FILE__, __LINE__, _T("Service failed to load plugin")); 
    1923        return; 
    2024      } 
Note: See TracChangeset for help on using the changeset viewer.