| 1 | '''Action driver''' |
|---|
| 2 | __revision__ = '$Revision$' |
|---|
| 3 | |
|---|
| 4 | import Bcfg2.Client.Tools |
|---|
| 5 | |
|---|
| 6 | # <Action timing='pre|post|both' name='name' command='cmd text' when='always|modified' |
|---|
| 7 | # status='ignore|check'/> |
|---|
| 8 | # <PostInstall name='foo'/> |
|---|
| 9 | # => <Action timing='post' when='modified' name='n' command='foo' status='ignore'/> |
|---|
| 10 | |
|---|
| 11 | class Action(Bcfg2.Client.Tools.Tool): |
|---|
| 12 | '''Implement Actions''' |
|---|
| 13 | name = 'Action' |
|---|
| 14 | __handles__ = [('PostInstall', None), ('Action', None)] |
|---|
| 15 | __req__ = {'PostInstall': ['name'], |
|---|
| 16 | 'Action':['name', 'timing', 'when', 'command', 'status']} |
|---|
| 17 | |
|---|
| 18 | def RunAction(self, entry): |
|---|
| 19 | '''This method handles command execution and status return''' |
|---|
| 20 | if not self.setup['dryrun']: |
|---|
| 21 | if self.setup['interactive']: |
|---|
| 22 | prompt = 'Run Action %s, %s: (y/N): ' % (entry.get('name'), entry.get('command')) |
|---|
| 23 | if raw_input(prompt) not in ['y', 'Y']: |
|---|
| 24 | return False |
|---|
| 25 | if self.setup['servicemode'] == 'build': |
|---|
| 26 | if entry.get('build', 'true') == 'false': |
|---|
| 27 | self.logger.debug("Action: Deferring execution of %s due to build mode" % (entry.get('command'))) |
|---|
| 28 | return False |
|---|
| 29 | self.logger.debug("Running Action %s" % (entry.get('name'))) |
|---|
| 30 | rc = self.cmd.run(entry.get('command'))[0] |
|---|
| 31 | self.logger.debug("Action: %s got rc %s" % (entry.get('command'), rc)) |
|---|
| 32 | entry.set('rc', str(rc)) |
|---|
| 33 | if entry.get('status', 'check') == 'ignore': |
|---|
| 34 | return True |
|---|
| 35 | else: |
|---|
| 36 | return rc == 0 |
|---|
| 37 | else: |
|---|
| 38 | self.logger.debug("In dryrun mode: not running action:\n %s" % |
|---|
| 39 | (entry.get('name'))) |
|---|
| 40 | return False |
|---|
| 41 | |
|---|
| 42 | def VerifyAction(self, dummy, _): |
|---|
| 43 | '''Actions always verify true''' |
|---|
| 44 | return True |
|---|
| 45 | |
|---|
| 46 | def VerifyPostInstall(self, dummy, _): |
|---|
| 47 | '''Actions always verify true''' |
|---|
| 48 | return True |
|---|
| 49 | |
|---|
| 50 | def InstallAction(self, entry): |
|---|
| 51 | '''Run actions as pre-checks for bundle installation''' |
|---|
| 52 | if entry.get('timing') != 'post': |
|---|
| 53 | return self.RunAction(entry) |
|---|
| 54 | return True |
|---|
| 55 | |
|---|
| 56 | def InstallPostInstall(self, entry): |
|---|
| 57 | return self.InstallAction(self, entry) |
|---|
| 58 | |
|---|
| 59 | def BundleUpdated(self, bundle, states): |
|---|
| 60 | '''Run postinstalls when bundles have been updated''' |
|---|
| 61 | for postinst in bundle.findall("PostInstall"): |
|---|
| 62 | self.cmd.run(postinst.get('name')) |
|---|
| 63 | for action in bundle.findall("Action"): |
|---|
| 64 | if action.get('timing') in ['post', 'both']: |
|---|
| 65 | states[action] = self.RunAction(action) |
|---|
| 66 | |
|---|
| 67 | def BundleNotUpdated(self, bundle, states): |
|---|
| 68 | '''Run Actions when bundles have not been updated''' |
|---|
| 69 | for action in bundle.findall("Action"): |
|---|
| 70 | if action.get('timing') in ['post', 'both'] and \ |
|---|
| 71 | action.get('when') != 'modified': |
|---|
| 72 | states[action] = self.RunAction(action) |
|---|