spacepaste

  1.  
  2. import os
  3. import salt.client
  4. import salt.config
  5. import tds.utils
  6. from .base import DeployStrategy
  7. import logging
  8. log = logging.getLogger('tds')
  9. # Note: in order to do multi-host in one call, need to specify them as
  10. # comma-separated string in the first arguent to publish.publish, like:
  11. # 'x.tagged.com,y.tagged.com'
  12. # Then, after all the args to the function call being published, add
  13. # 'expr_form=list'
  14. class TDSSaltDeployStrategy(DeployStrategy):
  15. """Salt (master publish.publish) based DeployStrategy."""
  16. def __init__(self, c_dir=None):
  17. self.c_dir = c_dir
  18. @tds.utils.debug
  19. def _publish(self, host, cmd, *args):
  20. """Dispatch to salt master."""
  21. if self.c_dir is not None:
  22. opts = salt.config.minion_config(
  23. os.path.join(self.c_dir, 'minion')
  24. )
  25. else:
  26. opts = salt.config.minion_config('/etc/salt.tds/minion')
  27. caller = salt.client.Caller(mopts=opts)
  28. host_re = '%s.*' % host # To allow FQDN matching
  29. # Set timeout high because... RedHat
  30. result = caller.sminion.functions['publish.full_data'](
  31. host_re, cmd, args, timeout=120
  32. )
  33. if not result:
  34. return (False, 'No data returned from host %s' % host)
  35. # We're assuming only one key is being returned currently
  36. host_result = result.values()[0]['ret']
  37. success = host_result.endswith('successful')
  38. return (success, host_result)
  39. @tds.utils.debug
  40. def deploy_to_host(self, dep_host, app, version, retry=4):
  41. """Deploy an application to a given host"""
  42. log.debug('Deploying to host %r', dep_host)
  43. return self._publish(dep_host, 'tds.install', app, version)
  44. @tds.utils.debug
  45. def restart_host(self, dep_host, app, retry=4):
  46. """Restart application on a given host"""
  47. return self._publish(dep_host, 'tds.restart', app)
  48.