command_in_slice moved to tools.py
[nodemanager.git] / plugins / omf_resctl.py
index 12e639a..85824e1 100644 (file)
@@ -16,7 +16,7 @@ import logger
 priority = 50
 
 def start():
-    logger.log("omf_resctl: plugin starting up...")
+    pass
 
 ### the new template for v6
 # hard-wire this for now
@@ -41,9 +41,10 @@ config_ple_template="""---
 # the path where the config is expected from within the sliver
 yaml_slice_path="/etc/omf_rc/config.yml"
 # the path for the script that we call when a change occurs
-omf_rc_trigger_script="/some/path/to/the/change/script"
+omf_rc_trigger_script="plc_trigger_omf_rc"
 
 def GetSlivers(data, conf = None, plc = None):
+    logger.log("omf_resctl.GetSlivers")
     if 'accounts' not in data:
         logger.log_missing_data("omf_resctl.GetSlivers",'accounts')
         return
@@ -55,8 +56,7 @@ def GetSlivers(data, conf = None, plc = None):
             raise Exception
     except:
         # disabled feature - bailing out
-        # xxx might need to clean up more deeply..
-        logger.log("PLC config unsufficient (not enabled, or no server set), see the PLC_OMF category -- plugin exiting")
+        logger.log("omf_resctl: PLC_OMF config unsufficient (not enabled, or no server set), -- plugin exiting")
         return
 
     hostname = data['hostname']
@@ -76,18 +76,36 @@ def GetSlivers(data, conf = None, plc = None):
             .replace('_hostname_',hostname)
         yaml_full_path="/vservers/%s/%s"%(slicename,yaml_slice_path)
         yaml_full_dir=os.path.dirname(yaml_full_path)
-        if not isdir(yaml_full_dir):
+        if not os.path.isdir(yaml_full_dir):
             try: os.makedirs(yaml_full_dir)
             except OSError: pass
 
-        changes=tools.replace_file_with_string(yaml_full_path,yaml_contents)
-        logger.log("yaml_contents length=%d, changes=%r"%(len(yaml_contents),changes))
-        if changes:
+        config_changes=tools.replace_file_with_string(yaml_full_path,yaml_contents)
+        logger.log("yaml_contents length=%d, config_changes=%r"%(len(yaml_contents),config_changes))
+        # would make sense to also check for changes to authorized_keys 
+        # would require saving a copy of that some place for comparison
+        # xxx todo
+        keys_changes = False
+        if config_changes or keys_changes:
             # instead of restarting the service we call a companion script
-            sp=subprocess.Popen([omf_rc_trigger_script],
-                                stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
-            (output,retcod)=sp.communicate()
-            logger.log("omf_resctl: %s: called OMF rc control script (retcod=%r)"%(slicename,retcod))
-            logger.log("omf_resctl: got output\n%s"%output)
+            try:
+                # the trigger script actually needs to be run in the slice context of course
+                # xxx we might need to use
+                # slice_command=['bash','-l','-c',omf_rc_trigger_script] 
+                slice_command = [omf_rc_trigger_script]
+                to_run = tools.command_in_slice (slicename, slice_command)
+                sp=subprocess.Popen(to_run, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
+                (out,err)=sp.communicate()
+                retcod=sp.returncode
+                # we don't wait for that, try to display a retcod for info purpose only
+                # might be None if that config script lasts or hangs whatever
+                logger.log("omf_resctl: %s: called OMF rc control script (imm. retcod=%r)"%(slicename,retcod))
+                logger.log("omf_resctl: got stdout\n%s"%out)
+                logger.log("omf_resctl: got stderr\n%s"%err)
+            except:
+                import traceback
+                traceback.print_exc()
+                logger.log_exc("omf_resctl: WARNING: Could not call trigger script %s"%\
+                                   omf_rc_trigger_script, name=slicename)
         else:
             logger.log("omf_resctl: %s: omf_control'ed sliver has no change" % slicename)