create /etc/planetlab/virt on startup if not already there
[nodemanager.git] / plugins / omf_resctl.py
index f6c2a8e..a0f14ab 100644 (file)
@@ -18,7 +18,7 @@ priority = 50
 def start():
     pass
 
-### the new template for v6
+### the new template for OMF v6
 # hard-wire this for now
 # once the variables are expanded, this is expected to go into
 config_ple_template="""---
@@ -41,7 +41,22 @@ 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"
+# given that we're now responsible for fetching this one, I have to
+# decide on an actual path - not jsut a name to search for in PATH
+omf_rc_trigger_script="/usr/bin/plc_trigger_omf_rc"
+
+# hopefully temporary: when trigger script is missing, fetch it at the url here
+omf_rc_trigger_url="http://git.mytestbed.net/?p=omf.git;a=blob_plain;f=omf_rc/bin/plc_trigger_omf_rc;hb=HEAD"
+def fetch_trigger_script_if_missing (slicename):
+    full_path="/vservers/%s/%s"%(slicename,omf_rc_trigger_script)
+    if not os.path.isfile (full_path):
+        retcod=subprocess.call (['curl','--silent','-o',full_path,omf_rc_trigger_url])
+        if retcod!=0:
+            logger.log("Could not fetch %s"%omf_rc_trigger_url)
+        else:
+            subprocess.call(['chmod','+x',full_path])
+            logger.log("omf_resctl: fetched %s"%(full_path))
+            logger.log("omf_resctl: from %s"%(omf_rc_trigger_url))
 
 def GetSlivers(data, conf = None, plc = None):
     logger.log("omf_resctl.GetSlivers")
@@ -56,8 +71,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']
@@ -81,17 +95,32 @@ def GetSlivers(data, conf = None, plc = None):
             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
             try:
-                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)
+                fetch_trigger_script_if_missing (slicename)
+                # the trigger script actually needs to be run in the slice context of course
+                slice_command = ["sudo -i %s"%omf_rc_trigger_script]
+                to_run = tools.command_in_slice (slicename, slice_command)
+                logger.log("command_in_slice: %s"%to_run)
+                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:
-                logger.log("omf_resctl: WARNING: Could not call trigger script %s"%omf_rc_trigger_script)
+                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)