From c65b784cc1ebb1828323083f390e6ceb37ed383f Mon Sep 17 00:00:00 2001
From: Peter Vreman <peter.vreman@acision.com>
Date: Wed, 29 Jul 2009 16:09:16 +0200
Subject: [PATCH] enable support power commands in cli and web

---
 cobbler/action_power.py                     |   14 -----------
 cobbler/cli.py                              |   33 ++++++++++++++++++++------
 cobbler/remote.py                           |    7 +++--
 web/cobbler_web/templates/generic_list.tmpl |    2 +-
 4 files changed, 30 insertions(+), 26 deletions(-)

diff --git a/cobbler/action_power.py b/cobbler/action_power.py
index 7d61e19..1df1200 100644
--- a/cobbler/action_power.py
+++ b/cobbler/action_power.py
@@ -67,20 +67,6 @@ class PowerTool:
         interested in maximum security should take that route.
         """
 
-        if self.system.virt_host != '' and func_utils.HAZFUNC:
-            try:
-                client = func_utils.func.Client(self.system.virt_host)
-                if desired_state == 'on':
-                    rc = client.virt.create(self.system.hostname)[self.system.virt_host]
-                else:
-                    rc = client.virt.destroy(self.system.hostname)[self.system.virt_host]
-                if rc != 0:
-                    self.logger.info("virt rc=%s" % rc[2])
-                else:
-                    return rc
-            except func_utils.Func_Client_Exception:
-                pass
-
         template = self.get_command_template()
         template_file = open(template, "r")
 
diff --git a/cobbler/cli.py b/cobbler/cli.py
index c4a3f6a..53b44f9 100755
--- a/cobbler/cli.py
+++ b/cobbler/cli.py
@@ -39,7 +39,7 @@ import item_image
 OBJECT_ACTIONS   = {
    "distro"  : "add copy edit find list remove rename report".split(" "),
    "profile" : "add copy dumpvars edit find getks list remove rename report".split(" "),
-   "system"  : "add copy dumpvars edit find getks list remove rename report".split(" "),
+   "system"  : "add copy dumpvars edit find getks list remove rename report poweron poweroff reboot".split(" "),
    "image"   : "add copy edit find list remove rename report".split(" "),
    "repo"    : "add copy edit find list remove rename report".split(" ")
 } 
@@ -183,8 +183,10 @@ class BootCLI:
         """
         Process object-based commands such as "distro add" or "profile rename"
         """
+        task_id = -1 # if assigned, we must tail the logfile
+        
         fields = self.get_fields(object_type)
-        if object_action not in [ "report", "getks", "dumpvars", "remove" ]:
+        if object_action in [ "add", "edit", "copy", "rename", "find" ]:
             utils.add_options_from_fields(object_type, self.parser, fields, object_action)
         elif object_action in [ "list" ]:
             pass
@@ -210,6 +212,11 @@ class BootCLI:
                 data = self.remote.get_blended_data("",options.name)
             # FIXME: pretty-printing and sorting here
             print data
+        elif object_action in [ "poweron", "poweroff", "reboot" ]:
+            power={}
+            power["power"] = object_action.replace("power","")
+            power["systems"] = [options.name]
+            task_id = self.remote.background_power_system(power, self.token)
         elif object_action == "report":
             if options.name is not None:
                 report_item(self.remote,object_type,None,options.name)
@@ -223,7 +230,13 @@ class BootCLI:
                 print item
         else:
             raise exceptions.NotImplementedError() 
-        
+            
+        # FIXME: add tail/polling code here
+        if task_id != -1:
+            self.print_task(task_id)
+            self.follow_task(task_id)
+                                                
+        return True
 
     # BOOKMARK
     def direct_command(self, action_name):
@@ -361,16 +374,20 @@ class BootCLI:
 
         # FIXME: add tail/polling code here
         if task_id != -1:
-            print "task started: %s" % task_id
-            events = self.remote.get_events()
-            (etime, name, status, who_viewed) = events[task_id]
-            atime = time.asctime(time.localtime(etime))
-            print "task started (id=%s, time=%s)" % (name, atime)
+            self.print_task(task_id) 
             self.follow_task(task_id) 
 
         return True
 
 
+    def print_task(self, task_id):
+        print "task started: %s" % task_id
+        events = self.remote.get_events()
+        (etime, name, status, who_viewed) = events[task_id]
+        atime = time.asctime(time.localtime(etime))
+        print "task started (id=%s, time=%s)" % (name, atime)
+
+    
     def follow_task(self, task_id):
         logfile = "/var/log/cobbler/tasks/%s.log" % task_id
         # adapted from:  http://code.activestate.com/recipes/157035/        
diff --git a/cobbler/remote.py b/cobbler/remote.py
index 4d9441c..e1857d9 100644
--- a/cobbler/remote.py
+++ b/cobbler/remote.py
@@ -227,13 +227,14 @@ class CobblerXMLRPCInterface:
 
     def background_power_system(self, options, token):
         def runner(self):
+            action=self.options.get("power","")
             for x in self.options.get("systems",[]):
-                self.logger.debug("performing power actions for system %s" % x)
+                self.logger.debug("performing power %s actions for system %s" % (action,x))
                 object_id = self.remote.get_system_handle(x,token)
-                self.remote.power_system(object_id,self.options.get("power",""),token,logger=self.logger)
+                self.remote.power_system(object_id,action,token,logger=self.logger)
             return True
         self.check_access(token, "power")
-        return self.__start_task(runner, token, "power", "Power management (%s)" % self.options.get("power",""), options)
+        return self.__start_task(runner, token, "power", "Power management (%s)" % options.get("power",""), options)
 
     def get_events(self, for_user=""):
         """
diff --git a/web/cobbler_web/templates/generic_list.tmpl b/web/cobbler_web/templates/generic_list.tmpl
index 3e35d63..b7e9ac1 100644
--- a/web/cobbler_web/templates/generic_list.tmpl
+++ b/web/cobbler_web/templates/generic_list.tmpl
@@ -153,7 +153,7 @@ function action_multi(otype, action, param) {
    <br/>
    <br/>
    <input type="button" onClick="javascript:action_multi('system','power','on')" value="Power On"/>
-   <input type="button" onClick="javascript:action_multi('system','power','off')" value="Power On"/>
+   <input type="button" onClick="javascript:action_multi('system','power','off')" value="Power Off"/>
    <input type="button" onClick="javascript:action_multi('system','power','reboot')" value="Reboot"/>
    <input type="button" onClick="javascript:action_multi('system','profile','')" value="Change Profile"/>
    <input type="button" onClick="javascript:action_multi('system','netboot','enable')" value="Enable Netboot"/>
-- 
1.5.5.6

