baker.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. from getpass import getpass
  2. from hashlib import md5
  3. from itertools import chain
  4. from collections import ChainMap, OrderedDict, defaultdict
  5. import argparse
  6. import logging
  7. import os
  8. import posixpath
  9. import sys
  10. from fabric import Connection, Config
  11. from invoke import run
  12. from tanker import yaml_load
  13. import invoke
  14. import yaml
  15. try:
  16. import keyring
  17. except ImportError:
  18. keyring = None
  19. __version__ = '0.0'
  20. fmt = '%(levelname)s:%(asctime).19s: %(message)s'
  21. logging.basicConfig(format=fmt)
  22. logger = logging.getLogger('baker')
  23. logger.setLevel(logging.INFO)
  24. try:
  25. import colorama
  26. colorama.init()
  27. MAGENTA = colorama.Fore.MAGENTA
  28. RED = colorama.Fore.RED
  29. RESET = colorama.Style.RESET_ALL
  30. # We define custom handler ..
  31. class Handler(logging.StreamHandler):
  32. def format(self, record):
  33. if record.levelname == 'INFO':
  34. record.msg = MAGENTA + record.msg + RESET
  35. elif record.levelname in ('WARNING', 'ERROR', 'CRITICAL'):
  36. record.msg = RED + record.msg + RESET
  37. return super(Handler, self).format(record)
  38. # .. and plug it
  39. handler = Handler()
  40. handler.setFormatter(logging.Formatter(fmt))
  41. logger.addHandler(handler)
  42. logger.propagate = 0
  43. except ImportError:
  44. pass
  45. def yaml_load(stream):
  46. class OrderedLoader(yaml.Loader):
  47. pass
  48. def construct_mapping(loader, node):
  49. loader.flatten_mapping(node)
  50. return OrderedDict(loader.construct_pairs(node))
  51. OrderedLoader.add_constructor(
  52. yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
  53. construct_mapping)
  54. return yaml.load(stream, OrderedLoader)
  55. def edits(word):
  56. yield word
  57. splits = ((word[:i], word[i:]) for i in range(len(word) + 1))
  58. for left, right in splits:
  59. if right:
  60. yield left + right[1:]
  61. def gen_candidates(wordlist):
  62. candidates = defaultdict(set)
  63. for word in wordlist:
  64. for ed1 in edits(word):
  65. for ed2 in edits(ed1):
  66. candidates[ed2].add(word)
  67. return candidates
  68. def spell(candidates, word):
  69. matches = set(chain.from_iterable(
  70. candidates[ed] for ed in edits(word) if ed in candidates
  71. ))
  72. return matches
  73. def spellcheck(objdict, word):
  74. if word in objdict:
  75. return
  76. candidates = objdict.get('_candidates')
  77. if not candidates:
  78. candidates = gen_candidates(list(objdict))
  79. objdict['_candidates'] = candidates
  80. msg = '"%s" not found in %s' % (word, objdict._path)
  81. matches = spell(candidates, word)
  82. if matches:
  83. msg += ', try: %s' % ' or '.join(matches)
  84. abort(msg)
  85. class ObjectDict(dict):
  86. """
  87. Simple objet sub-class that allows to transform a dict into an
  88. object, like: `ObjectDict({'ham': 'spam'}).ham == 'spam'`
  89. """
  90. def __getattr__(self, key):
  91. if key in self:
  92. return self[key]
  93. else:
  94. return None
  95. def __setattr__(self, key, value):
  96. self[key] = value
  97. def __iter__(self):
  98. for key in self.keys():
  99. if key.startswith('_'):
  100. continue
  101. yield key
  102. class Node:
  103. @staticmethod
  104. def fail(path, kind):
  105. msg = 'Error while parsing config: expecting "%s" while parsing "%s"'
  106. abort(msg % (kind, '->'.join(path)))
  107. @classmethod
  108. def parse(cls, cfg, path=tuple()):
  109. children = getattr(cls, '_children', None)
  110. type_name = children and type(children).__name__ \
  111. or ' or '.join((c.__name__ for c in cls._type))
  112. res = None
  113. if type_name == 'dict':
  114. if not isinstance(cfg, dict):
  115. cls.fail(path, type_name)
  116. res = ObjectDict()
  117. if '*' in children:
  118. assert len(children) == 1, "Don't mix '*' and other keys"
  119. child_class = children['*']
  120. for name, value in cfg.items():
  121. res[name] = child_class.parse(value, path + (name,))
  122. else:
  123. # Enforce known pre-defined
  124. for key in cfg:
  125. if key not in children:
  126. path = ' -> '.join(path)
  127. msg = 'Attribute "%s" not understoodin %s' % (key, path)
  128. candidates = gen_candidates(children.keys())
  129. matches = spell(candidates, key)
  130. if matches:
  131. msg += ', try: %s' % ' or '.join(matches)
  132. abort(msg)
  133. for name, child_class in children.items():
  134. if name not in cfg:
  135. continue
  136. res[name] = child_class.parse(cfg.pop(name), path + (name,))
  137. elif type_name == 'list':
  138. if not isinstance(cfg, list):
  139. cls.fail(path, type_name)
  140. child_class = children[0]
  141. res = [child_class.parse(c, path+ ('[]',)) for c in cfg]
  142. else:
  143. if not isinstance(cfg, cls._type):
  144. cls.fail(path, type_name)
  145. res = cfg
  146. return cls.setup(res, path)
  147. @classmethod
  148. def setup(cls, values, path):
  149. if isinstance(values, dict):
  150. values['_path'] = '->'.join(path)
  151. return values
  152. class Atom(Node):
  153. _type = (str, bool)
  154. class AtomList(Node):
  155. _children = [Atom]
  156. class Hosts(Node):
  157. _children = [Atom]
  158. class Auth(Node):
  159. _children = {'*': Atom}
  160. class EnvNode(Node):
  161. _children = {'*': Atom}
  162. class HostGroup(Node):
  163. _children = {
  164. 'hosts': Hosts,
  165. }
  166. class Network(Node):
  167. _children = {
  168. '*': HostGroup,
  169. }
  170. class Multi(Node):
  171. _children = {
  172. 'task': Atom,
  173. 'export': Atom,
  174. 'network': Atom,
  175. 'env': EnvNode,
  176. }
  177. class MultiList(Node):
  178. _children = [Multi]
  179. class Command(Node):
  180. _children = {
  181. 'desc': Atom,
  182. 'local': Atom,
  183. 'once': Atom,
  184. 'run': Atom,
  185. 'send': Atom,
  186. 'to': Atom,
  187. 'assert': Atom,
  188. 'env': EnvNode,
  189. 'multi': MultiList,
  190. }
  191. @classmethod
  192. def setup(cls, values, path):
  193. if path:
  194. values['name'] = path[-1]
  195. if 'desc' not in values:
  196. values['desc'] = values['name']
  197. return values
  198. class Task(Node):
  199. _children = {
  200. '*': Command,
  201. }
  202. class LoadNode(Node):
  203. _children = {
  204. 'file': Atom,
  205. 'as': Atom,
  206. }
  207. class LoadList(Node):
  208. _children = [LoadNode]
  209. class ConfigRoot(Node):
  210. _children = {
  211. 'networks': Network,
  212. 'tasks': Task,
  213. 'auth': Auth,
  214. 'env': EnvNode,
  215. 'load': LoadList,
  216. }
  217. class Env(ChainMap):
  218. def __init__(self, *dicts):
  219. return super().__init__(*filter(bool, dicts))
  220. def fmt(self, string):
  221. try:
  222. return string.format(**self)
  223. except KeyError as exc:
  224. msg = 'Unable to format "%s" (missing: "%s")'% (string, exc.args[0])
  225. candidates = gen_candidates(self.keys())
  226. key = exc.args[0]
  227. matches = spell(candidates, key)
  228. if msg:
  229. msg += ', try: %s' % ' or '.join(matches)
  230. abort(msg )
  231. except IndexError as exc:
  232. msg = 'Unable to format "%s", positional argument not supported'
  233. abort(msg)
  234. def get_passphrase(key_path):
  235. service = 'SSH private key'
  236. csum = md5(open(key_path, 'rb').read()).digest().hex()
  237. ssh_pass = keyring.get_password(service, csum)
  238. if not ssh_pass:
  239. ssh_pass = getpass('Password for %s: ' % key_path)
  240. keyring.set_password(service, csum, ssh_pass)
  241. return ssh_pass
  242. def get_sudo_passwd():
  243. service = "Sudo password"
  244. passwd = keyring.get_password(service, '-')
  245. if not passwd:
  246. passwd = getpass('Sudo password:')
  247. keyring.set_password(service, '-', passwd)
  248. return passwd
  249. CONNECTION_CACHE = {}
  250. def connect(host, auth, with_sudo=False):
  251. if (host, with_sudo) in CONNECTION_CACHE:
  252. return CONNECTION_CACHE[host, with_sudo]
  253. connect_kwargs = {}
  254. if auth and auth.get('ssh_private_key'):
  255. connect_kwargs['key_filename'] = auth.ssh_private_key
  256. if not os.path.exists(auth.ssh_private_key):
  257. msg = 'Private key file "%s" not found' % auth.ssh_private_key
  258. abort(msg)
  259. ssh_pass = get_passphrase(auth.ssh_private_key)
  260. connect_kwargs['password'] = ssh_pass
  261. if with_sudo:
  262. config = Config(overrides={
  263. 'sudo': {
  264. 'password': get_sudo_passwd()
  265. }
  266. })
  267. else:
  268. config = None
  269. con = Connection(host, config=config, connect_kwargs=connect_kwargs)
  270. CONNECTION_CACHE[host, with_sudo] = con
  271. return con
  272. def run_local(task, env, cli):
  273. # Run local task
  274. cmd = env.fmt(task.local)
  275. # TODO log only task_desc and let desc contains env info like {host}
  276. logger.info(env.fmt('RUN {task_name} locally'))
  277. if cli.dry_run:
  278. logger.info('[DRY-RUN] ' + cmd)
  279. return None
  280. res = run(cmd, env=env)
  281. return res
  282. def run_remote(task, host, env, cli):
  283. res = None
  284. host = env.fmt(host)
  285. env = env.new_child({
  286. 'host': host,
  287. })
  288. con = connect(host, cli.cfg.auth, bool(task.sudo))
  289. if task.run:
  290. cmd = env.fmt(task.run)
  291. logger.info(env.fmt('RUN {task_name} ON {host}'))
  292. if cli.dry_run:
  293. logger.info('[DRY-RUN] ' + cmd)
  294. else:
  295. res = con.run(cmd, pty=True, env=env)
  296. elif task.sudo:
  297. cmd = env.fmt(task.sudo)
  298. logger.info(env.fmt('SUDO {task_name} ON {host}'))
  299. if cli.dry_run:
  300. logger.info('[DRY-RUN] %s' + cmd)
  301. else:
  302. res = con.sudo(cmd)
  303. elif task.send:
  304. local_path = env.fmt(task.send)
  305. remote_path = env.fmt(task.to)
  306. logger.info(f'SEND {local_path} TO {host}:{remote_path}')
  307. if cli.dry_run:
  308. logger.info('[DRY-RUN]')
  309. elif os.path.isfile(local_path):
  310. con.put(local_path, remote=remote_path)
  311. else:
  312. for root, subdirs, files in os.walk(local_path):
  313. rel_dir = os.path.relpath(root, local_path)
  314. rem_dir = posixpath.join(remote_path, rel_dir)
  315. con.run('mkdir -p {}'.format(rem_dir))
  316. for f in files:
  317. rel_f = os.path.join(root, f)
  318. rem_file = posixpath.join(rem_dir, f)
  319. con.put(os.path.abspath(rel_f), remote=rem_file)
  320. else:
  321. abort('Unable to run task "%s"' % task.name)
  322. return res
  323. def run_task(task, host, cli, parent_env=None):
  324. '''
  325. Execute one task on one host (or locally)
  326. '''
  327. # Prepare environment
  328. env = Env(
  329. # Cli is top priority
  330. dict(e.split('=') for e in cli.env),
  331. # Then comes env from parent task
  332. parent_env,
  333. # Env on the task itself
  334. task.get('env'),
  335. # Top-level env
  336. cli.cfg.get('env'),
  337. # OS env
  338. os.environ,
  339. ).new_child()
  340. env.update({
  341. 'task_desc': env.fmt(task.desc),
  342. 'task_name': task.name,
  343. 'host': host or '',
  344. })
  345. if task.local:
  346. res = run_local(task, env, cli)
  347. else:
  348. res = run_remote(task, host, env, cli)
  349. if task.get('assert'):
  350. env.update({
  351. 'stdout': res.stdout,
  352. 'stderr': res.stderr,
  353. })
  354. assert_ = env.fmt(task['assert'])
  355. ok = eval(assert_, dict(env))
  356. if ok:
  357. logger.info('Assert ok')
  358. else:
  359. abort('Assert "%s" failed!' % assert_)
  360. return res
  361. def run_batch(task, hosts, cli, env=None):
  362. '''
  363. Run one task on a list of hosts
  364. '''
  365. env = Env(task.get('env'), env)
  366. res = None
  367. export_env = {}
  368. if task.get('multi'):
  369. for multi in task.multi:
  370. task = multi.task
  371. spellcheck(cli.cfg.tasks, task)
  372. sub_task = cli.cfg.tasks[task]
  373. network = multi.get('network')
  374. if network:
  375. spellcheck(cli.cfg.networks, network)
  376. hosts = cli.cfg.networks[network].hosts
  377. child_env = multi.get('env', {}).copy()
  378. for k, v in child_env.items():
  379. # env wrap-around!
  380. child_env[k] = env.fmt(child_env[k])
  381. run_env = Env(export_env, child_env, env)
  382. res = run_batch(sub_task, hosts, cli, run_env)
  383. if multi.export:
  384. export_env[multi.export] = res and res.stdout.strip() or ''
  385. else:
  386. if task.once and task.local:
  387. res = run_task(task, None, cli, env)
  388. return res
  389. for host in hosts:
  390. res = run_task(task, host, cli, env)
  391. if task.once:
  392. break
  393. return res
  394. def base_cli(args=None):
  395. parser = argparse.ArgumentParser()
  396. parser.add_argument('names', nargs='*',
  397. help='Hosts and commands to run them on')
  398. parser.add_argument('-c', '--config', default='bk.yaml',
  399. help='Config file')
  400. parser.add_argument('-r', '--run', nargs='*', default=[],
  401. help='Run custom task')
  402. parser.add_argument('-d', '--dry-run', action='store_true',
  403. help='Do not run actual tasks, just print them')
  404. parser.add_argument('-e', '--env', nargs='*', default=[],
  405. help='Add value to execution environment '
  406. '(ex: -e foo=bar "name=John Doe")')
  407. parser.add_argument('-s', '--sudo', default='auto',
  408. help='Enable sudo (auto|yes|no')
  409. parser.add_argument('-v', '--verbose', action='count',
  410. default=0, help='Increase verbosity')
  411. cli = parser.parse_args(args=args)
  412. return ObjectDict(vars(cli))
  413. def abort(msg):
  414. logger.error(msg)
  415. sys.exit(1)
  416. def load(path, prefix=None):
  417. load_sections = ('networks', 'tasks', 'auth', 'env')
  418. if os.path.isfile(path):
  419. logger.info('Load config %s' % path)
  420. cfg = yaml_load(open(path))
  421. cfg = ConfigRoot.parse(cfg)
  422. else:
  423. abort('Config file "%s" not found' % path)
  424. # Define useful defaults
  425. cfg.networks = cfg.networks or ObjectDict()
  426. cfg.tasks = cfg.tasks or ObjectDict()
  427. if prefix:
  428. fn = lambda x: '/'.join(prefix + [x])
  429. # Apply prefix
  430. for section in load_sections:
  431. if not cfg.get(section):
  432. continue
  433. items = cfg[section].items()
  434. cfg[section] = {fn(k): v for k, v in items if not k.startswith('_')}
  435. # Recursive load
  436. if cfg.load:
  437. cfg_path = os.path.dirname(path)
  438. for item in cfg.load:
  439. if item.get('as'):
  440. child_prefix = item['as']
  441. else:
  442. child_prefix, _ = os.path.splitext(item.file)
  443. child_path = os.path.join(cfg_path, item.file)
  444. child_cfg = load(child_path, child_prefix.split('/'))
  445. for section in load_sections:
  446. if not cfg.get(section):
  447. cfg[section] = {}
  448. cfg[section].update(child_cfg.get(section, {}))
  449. return cfg
  450. def main():
  451. cli = base_cli()
  452. if cli.verbose:
  453. level = 'INFO' if cli.verbose == 1 else 'DEBUG'
  454. logger.setLevel(level)
  455. logger.info('Log level set to: %s' % level)
  456. # Load config
  457. cfg = load(cli.config)
  458. cli.cfg = cfg
  459. # Make sure we don't have overlap between hosts and tasks
  460. items = list(cfg.networks) + list(cfg.tasks)
  461. msg = 'Name collision between tasks and networks'
  462. assert len(set(items)) == len(items), msg
  463. # Build task list
  464. tasks = []
  465. networks = []
  466. for name in cli.names:
  467. if name in cfg.networks:
  468. host = cfg.networks[name]
  469. networks.append(host)
  470. elif name in cfg.tasks:
  471. task = cfg.tasks[name]
  472. tasks.append(task)
  473. else:
  474. msg = 'Name "%s" not understood' % name
  475. matches = spell(cfg.networks, name) | spell(cfg.tasks, name)
  476. if matches:
  477. msg += ', try: %s' % ' or '.join(matches)
  478. abort(msg)
  479. for custom_task in cli.run:
  480. task = Command.parse(yaml_load(custom_task))
  481. task.desc = 'Custom command'
  482. tasks.append(task)
  483. hosts = list(chain.from_iterable(n.hosts for n in networks))
  484. try:
  485. for task in tasks:
  486. run_batch(task, hosts, cli)
  487. except invoke.exceptions.Failure as e:
  488. logger.error(str(e))
  489. if __name__ == '__main__':
  490. main()