summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Client/Tools/Action.py
diff options
context:
space:
mode:
authorSol Jerome <sol.jerome@gmail.com>2012-03-24 11:20:07 -0500
committerSol Jerome <sol.jerome@gmail.com>2012-03-24 11:20:07 -0500
commitdab1d03d81c538966d03fb9318a4588a9e803b44 (patch)
treef51e27fa55887e9fb961766805fe43f0da56c5b9 /src/lib/Bcfg2/Client/Tools/Action.py
parent5cd6238df496a3cea178e4596ecd87967cce1ce6 (diff)
downloadbcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.tar.gz
bcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.tar.bz2
bcfg2-dab1d03d81c538966d03fb9318a4588a9e803b44.zip
Allow to run directly from a git checkout (#1037)
Signed-off-by: Sol Jerome <sol.jerome@gmail.com>
Diffstat (limited to 'src/lib/Bcfg2/Client/Tools/Action.py')
-rw-r--r--src/lib/Bcfg2/Client/Tools/Action.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/src/lib/Bcfg2/Client/Tools/Action.py b/src/lib/Bcfg2/Client/Tools/Action.py
new file mode 100644
index 000000000..dc49347e9
--- /dev/null
+++ b/src/lib/Bcfg2/Client/Tools/Action.py
@@ -0,0 +1,107 @@
+"""Action driver"""
+
+import Bcfg2.Client.Tools
+from Bcfg2.Client.Frame import matches_white_list, passes_black_list
+
+"""
+<Action timing='pre|post|both'
+ name='name'
+ command='cmd text'
+ when='always|modified'
+ status='ignore|check'/>
+<PostInstall name='foo'/>
+ => <Action timing='post'
+ when='modified'
+ name='n'
+ command='foo'
+ status='ignore'/>
+"""
+
+
+class Action(Bcfg2.Client.Tools.Tool):
+ """Implement Actions"""
+ name = 'Action'
+ __handles__ = [('PostInstall', None), ('Action', None)]
+ __req__ = {'PostInstall': ['name'],
+ 'Action': ['name', 'timing', 'when', 'command', 'status']}
+
+ def _action_allowed(self, action):
+ if self.setup['decision'] == 'whitelist' and \
+ not matches_white_list(action, self.setup['decision_list']):
+ self.logger.info("In whitelist mode: suppressing Action:" + \
+ action.get('name'))
+ return False
+ if self.setup['decision'] == 'blacklist' and \
+ not passes_black_list(action, self.setup['decision_list']):
+ self.logger.info("In blacklist mode: suppressing Action:" + \
+ action.get('name'))
+ return False
+ return True
+
+ def RunAction(self, entry):
+ """This method handles command execution and status return."""
+ if not self.setup['dryrun']:
+ if self.setup['interactive']:
+ prompt = ('Run Action %s, %s: (y/N): ' %
+ (entry.get('name'), entry.get('command')))
+ # py3k compatibility
+ try:
+ ans = raw_input(prompt)
+ except NameError:
+ ans = input(prompt)
+ if ans not in ['y', 'Y']:
+ return False
+ if self.setup['servicemode'] == 'build':
+ if entry.get('build', 'true') == 'false':
+ self.logger.debug("Action: Deferring execution of %s due to build mode" % (entry.get('command')))
+ return False
+ self.logger.debug("Running Action %s" % (entry.get('name')))
+ rc = self.cmd.run(entry.get('command'))[0]
+ self.logger.debug("Action: %s got rc %s" % (entry.get('command'), rc))
+ entry.set('rc', str(rc))
+ if entry.get('status', 'check') == 'ignore':
+ return True
+ else:
+ return rc == 0
+ else:
+ self.logger.debug("In dryrun mode: not running action:\n %s" %
+ (entry.get('name')))
+ return False
+
+ def VerifyAction(self, dummy, _):
+ """Actions always verify true."""
+ return True
+
+ def VerifyPostInstall(self, dummy, _):
+ """Actions always verify true."""
+ return True
+
+ def InstallAction(self, entry):
+ """Run actions as pre-checks for bundle installation."""
+ if entry.get('timing') != 'post':
+ return self.RunAction(entry)
+ return True
+
+ def InstallPostInstall(self, entry):
+ return self.InstallAction(self, entry)
+
+ def BundleUpdated(self, bundle, states):
+ """Run postinstalls when bundles have been updated."""
+ for postinst in bundle.findall("PostInstall"):
+ if not self._action_allowed(postinst):
+ continue
+ self.cmd.run(postinst.get('name'))
+ for action in bundle.findall("Action"):
+ if action.get('timing') in ['post', 'both']:
+ if not self._action_allowed(action):
+ continue
+ states[action] = self.RunAction(action)
+
+ def BundleNotUpdated(self, bundle, states):
+ """Run Actions when bundles have not been updated."""
+ for action in bundle.findall("Action"):
+ if action.get('timing') in ['post', 'both'] and \
+ action.get('when') != 'modified':
+ if not self._action_allowed(action):
+ continue
+ states[action] = self.RunAction(action)