Module builtin
[hide private]
[frames] | no frames]

Source Code for Module builtin

  1  # 
  2  # Copyright (c) 2011 Red Hat, Inc. 
  3  # 
  4  # This software is licensed to you under the GNU Lesser General Public 
  5  # License as published by the Free Software Foundation; either version 
  6  # 2 of the License (LGPLv2) or (at your option) any later version. 
  7  # There is NO WARRANTY for this software, express or implied, 
  8  # including the implied warranties of MERCHANTABILITY, 
  9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
 10  # have received a copy of LGPLv2 along with this software; if not, see 
 11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
 12  # 
 13  # Jeff Ortel <jortel@redhat.com> 
 14  # 
 15   
 16  """ 
 17  Builtin plugin. 
 18  """ 
 19  import os 
 20  import socket 
 21  import inspect 
 22  from uuid import uuid4 
 23  from gofer.decorators import * 
 24  from gofer.rmi.tracker import Tracker 
 25  from gofer.rmi.criteria import Builder 
 26  from gofer.agent.plugin import Plugin 
 27  from gofer.agent.action import Actions 
 28  from gofer.agent.config import Config 
 29   
 30  from logging import getLogger 
 31   
 32  log = getLogger(__name__) 
 33  plugin = Plugin.find(__name__) 
34 35 36 -def indent(v, n, *args):
37 s = [] 38 for n in range(0,n): 39 s.append(' ') 40 s.append(str(v) % args) 41 return ''.join(s)
42
43 44 -class TestAction:
45 46 @action(hours=36)
47 - def hello(self):
48 plugin = Plugin.find(__name__) 49 log.info('Hello:\n%s', plugin.cfg())
50
51 52 -class Admin:
53 54 @remote
55 - def cancel(self, sn=None, criteria=None):
56 """ 57 Cancel by serial number or user defined property. 58 @param sn: An RMI serial number. 59 @type sn: str 60 @param criteria: The criteria used to match the 61 I{any} property on an RMI request. 62 @type criteria: str 63 @return: The list of cancelled serial numbers. 64 @rtype: list 65 @raise Exception, on (sn) not found. 66 @see: L{gofer.rmi.criteria} 67 """ 68 sn_list = [] 69 cancelled = [] 70 tracker = Tracker() 71 if sn: 72 sn_list = [sn] 73 if criteria: 74 b = Builder() 75 criteria = b.build(criteria) 76 sn_list = tracker.find(criteria) 77 for sn in sn_list: 78 _sn = tracker.cancel(sn) 79 if _sn: 80 cancelled.append(_sn) 81 return cancelled
82 83 @remote
84 - def hello(self):
85 s = [] 86 cfg = Config() 87 s.append('Hello, I am gofer agent "%s"' % plugin.getuuid()) 88 s.append('Here is my configuration:\n%s' % cfg) 89 s.append('Status: ready') 90 return '\n'.join(s)
91 92 @remote
93 - def help(self):
94 s = [] 95 s.append('Plugins:') 96 for p in Plugin.all(): 97 if not p.enabled(): 98 continue 99 # plugin 100 s.append('') 101 if p.synonyms: 102 s.append(indent('<plugin> %s %s', 2, p.name, p.synonyms)) 103 else: 104 s.append(indent('<plugin> %s', 2, p.name)) 105 # classes 106 s.append(indent('Classes:', 4)) 107 for n,v in p.dispatcher.catalog.items(): 108 if inspect.ismodule(v): 109 continue 110 s.append(indent('<class> %s', 6, n)) 111 s.append(indent('methods:', 8)) 112 for n,v in inspect.getmembers(v, inspect.ismethod): 113 fn = v.im_func 114 if not hasattr(fn, 'gofer'): 115 continue 116 s.append(indent(self.__signature(n, fn), 10)) 117 # functions 118 s.append(indent('Functions:', 4)) 119 for n,v in p.dispatcher.catalog.items(): 120 if not inspect.ismodule(v): 121 continue 122 for n,v in inspect.getmembers(v, inspect.isfunction): 123 fn = v 124 if not hasattr(fn, 'gofer'): 125 continue 126 s.append(indent(self.__signature(n, fn), 6)) 127 s.append('') 128 s.append('Actions:') 129 for a in self.__actions(): 130 s.append(' %s %s' % a) 131 return '\n'.join(s)
132
133 - def __actions(self):
134 actions = [] 135 for a in Actions().collated(): 136 actions.append((a.name(), a.interval)) 137 return actions
138
139 - def __signature(self, n, fn):
140 s = [] 141 s.append(n) 142 s.append('(') 143 spec = inspect.getargspec(fn) 144 if 'self' in spec[0]: 145 spec[0].remove('self') 146 if spec[1]: 147 spec[0].append('*%s' % spec[1]) 148 if spec[2]: 149 spec[0].append('**%s' % spec[2]) 150 s.append(', '.join(spec[0])) 151 s.append(')') 152 return ''.join(s)
153
154 155 @remote 156 -def echo(something):
157 return something
158 159 # 160 # Set the uuid to the hostname when not 161 # specified in the config. 162 # 163 if not plugin.getuuid(): 164 hostname = socket.gethostname() 165 uuid = str(uuid4()) 166 if not hostname.startswith('localhost'): 167 uuid = 'admin@%s' % hostname 168 plugin.setuuid(uuid) 169