baker.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. logger.error(msg)
  85. exit(1)
  86. class ObjectDict(dict):
  87. """
  88. Simple objet sub-class that allows to transform a dict into an
  89. object, like: `ObjectDict({'ham': 'spam'}).ham == 'spam'`
  90. """
  91. def __getattr__(self, key):
  92. if key in self:
  93. return self[key]
  94. else:
  95. return None
  96. def __setattr__(self, key, value):
  97. self[key] = value
  98. def __iter__(self):
  99. for key in self.keys():
  100. if key.startswith('_'):
  101. continue
  102. yield key
  103. class Node:
  104. @staticmethod
  105. def fail(path, kind):
  106. msg = 'Error while parsing config: expecting "%s" while parsing "%s"'
  107. logger.error(msg % (kind, '->'.join(path)))
  108. sys.exit()
  109. @classmethod
  110. def parse(cls, cfg, path=tuple()):
  111. children = getattr(cls, '_children', None)
  112. type_name = children and type(children).__name__ \
  113. or ' or '.join((c.__name__ for c in cls._type))
  114. res = None
  115. if type_name == 'dict':
  116. if not isinstance(cfg, dict):
  117. cls.fail(path, type_name)
  118. res = ObjectDict()
  119. if '*' in children:
  120. assert len(children) == 1, "Don't mix '*' and other keys"
  121. child_class = children['*']
  122. for name, value in cfg.items():
  123. res[name] = child_class.parse(value, path + (name,))
  124. else:
  125. # Enforce known pre-defined
  126. for key in cfg:
  127. if key not in children:
  128. path = ' -> '.join(path)
  129. msg = 'Attribute "%s" not understoodin %s' % (key, path)
  130. candidates = gen_candidates(children.keys())
  131. matches = spell(candidates, key)
  132. if matches:
  133. msg += ', try: %s' % ' or '.join(matches)
  134. logger.error(msg)
  135. exit(1)
  136. for name, child_class in children.items():
  137. if name not in cfg:
  138. continue
  139. res[name] = child_class.parse(cfg.pop(name), path + (name,))
  140. elif type_name == 'list':
  141. if not isinstance(cfg, list):
  142. cls.fail(path, type_name)
  143. child_class = children[0]
  144. res = [child_class.parse(c, path+ ('[]',)) for c in cfg]
  145. else:
  146. if not isinstance(cfg, cls._type):
  147. cls.fail(path, type_name)
  148. res = cfg
  149. return cls.setup(res, path)
  150. @classmethod
  151. def setup(cls, values, path):
  152. if isinstance(values, dict):
  153. values['_path'] = '->'.join(path)
  154. return values
  155. class Atom(Node):
  156. _type = (str, bool)
  157. class AtomList(Node):
  158. _children = [Atom]
  159. class Hosts(Node):
  160. _children = [Atom]
  161. class Auth(Node):
  162. _children = {'*': Atom}
  163. class EnvNode(Node):
  164. _children = {'*': Atom}
  165. class HostGroup(Node):
  166. _children = {
  167. 'hosts': Hosts,
  168. }
  169. class Network(Node):
  170. _children = {
  171. '*': HostGroup,
  172. }
  173. class Multi(Node):
  174. _children = {
  175. 'task': Atom,
  176. 'export': Atom,
  177. 'env': EnvNode,
  178. }
  179. class MultiList(Node):
  180. _children = [Multi]
  181. class Command(Node):
  182. _children = {
  183. 'desc': Atom,
  184. 'local': Atom,
  185. 'once': Atom,
  186. 'run': Atom,
  187. 'env': EnvNode,
  188. 'multi': MultiList,
  189. }
  190. @classmethod
  191. def setup(cls, values, path):
  192. if path:
  193. values['name'] = path[-1]
  194. if 'desc' not in values:
  195. values['desc'] = values['name']
  196. return values
  197. class Task(Node):
  198. _children = {
  199. '*': Command,
  200. }
  201. class LoadNode(Node):
  202. _children = {
  203. 'file': Atom,
  204. 'as': Atom,
  205. }
  206. class LoadList(Node):
  207. _children = [LoadNode]
  208. class ConfigRoot(Node):
  209. _children = {
  210. 'networks': Network,
  211. 'tasks': Task,
  212. 'auth': Auth,
  213. 'env': EnvNode,
  214. 'load': LoadList,
  215. }
  216. class Env(ChainMap):
  217. def __init__(self, *dicts):
  218. return super().__init__(*filter(bool, dicts))
  219. def fmt(self, string):
  220. try:
  221. return string.format(**self)
  222. except KeyError as exc:
  223. msg = 'Unable to format "%s" (missing: "%s")'% (string, exc.args[0])
  224. candidates = gen_candidates(self.keys())
  225. key = exc.args[0]
  226. matches = spell(candidates, key)
  227. if msg:
  228. msg += ', try: %s' % ' or '.join(matches)
  229. logger.error(msg )
  230. exit(1)
  231. except IndexError as exc:
  232. msg = 'Unable to format "%s", positional argument not supported'
  233. logger.error(msg)
  234. sys.exit()
  235. def get_passphrase(key_path):
  236. service = 'SSH private key'
  237. csum = md5(open(key_path, 'rb').read()).digest().hex()
  238. ssh_pass = keyring.get_password(service, csum)
  239. if not ssh_pass:
  240. ssh_pass = getpass('Password for %s: ' % key_path)
  241. keyring.set_password(service, csum, ssh_pass)
  242. return ssh_pass
  243. def get_sudo_passwd():
  244. service = "Sudo password"
  245. passwd = keyring.get_password(service, '-')
  246. if not passwd:
  247. passwd = getpass('Sudo password:')
  248. keyring.set_password(service, '-', passwd)
  249. return passwd
  250. CONNECTION_CACHE = {}
  251. def connect(host, auth, with_sudo=False):
  252. if (host, with_sudo) in CONNECTION_CACHE:
  253. return CONNECTION_CACHE[host, with_sudo]
  254. connect_kwargs = {}
  255. if auth and auth.get('ssh_private_key'):
  256. connect_kwargs['key_filename'] = auth.ssh_private_key
  257. if not os.path.exists(auth.ssh_private_key):
  258. msg = 'Private key file "%s" not found' % auth.ssh_private_key
  259. logger.error(msg)
  260. sys.exit()
  261. ssh_pass = get_passphrase(auth.ssh_private_key)
  262. connect_kwargs['password'] = ssh_pass
  263. if with_sudo:
  264. config = Config(overrides={
  265. 'sudo': {
  266. 'password': get_sudo_passwd()
  267. }
  268. })
  269. else:
  270. config = None
  271. con = Connection(host, config=config, connect_kwargs=connect_kwargs)
  272. CONNECTION_CACHE[host, with_sudo] = con
  273. return con
  274. def run_local(task, env, cli):
  275. # Run local task
  276. cmd = env.fmt(task.local)
  277. # TODO log only task_desc and let desc contains env info like {host}
  278. logger.info(env.fmt('RUN {task_name} locally'))
  279. if cli.dry_run:
  280. logger.info('[DRY-RUN] ' + cmd)
  281. return None
  282. res = run(cmd, env=env)
  283. return res
  284. def run_remote(task, host, env, cli):
  285. res = None
  286. host = env.fmt(host)
  287. con = connect(host, cli.cfg.auth, bool(task.sudo))
  288. if task.run:
  289. cmd = env.fmt(task.run)
  290. logger.info(env.fmt('RUN {task_name} ON {host}'))
  291. if cli.dry_run:
  292. logger.info('[DRY-RUN] ' + cmd)
  293. else:
  294. res = con.run(cmd, pty=True, env=env)
  295. elif task.sudo:
  296. cmd = env.fmt(task.sudo)
  297. logger.info(env.fmt('SUDO {task_name} ON {host}'))
  298. if cli.dry_run:
  299. logger.info('[DRY-RUN] %s' + cmd)
  300. else:
  301. res = con.sudo(cmd)
  302. elif task.send:
  303. local_path = env.fmt(task.send)
  304. remote_path = env.fmt(task.to)
  305. logger.info(f'SEND {local_path} TO {host}:{remote_path}')
  306. if cli.dry_run:
  307. logger.info('[DRY-RUN]')
  308. elif os.path.isfile(local_path):
  309. con.put(local_path, remote=remote_path)
  310. else:
  311. for root, subdirs, files in os.walk(local_path):
  312. rel_dir = os.path.relpath(root, local_path)
  313. rem_dir = posixpath.join(remote_path, rel_dir)
  314. con.run('mkdir -p {}'.format(rem_dir))
  315. for f in files:
  316. rel_f = os.path.join(root, f)
  317. rem_file = posixpath.join(rem_dir, f)
  318. con.put(os.path.abspath(rel_f), remote=rem_file)
  319. else:
  320. logger.error('Unable to run task "%s"' % task.name)
  321. sys.exit()
  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. logger.error('Assert "%s" failed!' % assert_)
  360. sys.exit()
  361. return res
  362. def run_batch(task, hosts, cli, env=None):
  363. '''
  364. Run one task on a list of hosts
  365. '''
  366. env = Env(task.get('env'), env)
  367. res = None
  368. export_env = {}
  369. if task.get('multi'):
  370. for multi in task.multi:
  371. task = multi.task
  372. spellcheck(cli.cfg.tasks, task)
  373. sub_task = cli.cfg.tasks[task]
  374. network = multi.get('network')
  375. if network:
  376. spellcheck(cli.cfg.networks, network)
  377. hosts = cli.cfg.networks[network].hosts
  378. child_env = multi.get('env', {}).copy()
  379. for k, v in child_env.items():
  380. # env wrap-around!
  381. child_env[k] = env.fmt(child_env[k])
  382. run_env = Env(export_env, child_env, env)
  383. res = run_batch(sub_task, hosts, cli, run_env)
  384. if multi.export:
  385. export_env[multi.export] = res and res.stdout.strip() or ''
  386. else:
  387. if task.once and task.local:
  388. res = run_task(task, None, cli, env)
  389. return res
  390. for host in hosts:
  391. res = run_task(task, host, cli, env)
  392. if task.once:
  393. break
  394. return res
  395. def base_cli(args=None):
  396. parser = argparse.ArgumentParser()
  397. parser.add_argument('names', nargs='*',
  398. help='Hosts and commands to run them on')
  399. parser.add_argument('-c', '--config', default='bk.yaml',
  400. help='Config file')
  401. parser.add_argument('-r', '--run', nargs='*', default=[],
  402. help='Run custom task')
  403. parser.add_argument('-d', '--dry-run', action='store_true',
  404. help='Do not run actual tasks, just print them')
  405. parser.add_argument('-e', '--env', nargs='*', default=[],
  406. help='Add value to execution environment '
  407. '(ex: -e foo=bar "name=John Doe")')
  408. parser.add_argument('-s', '--sudo', default='auto',
  409. help='Enable sudo (auto|yes|no')
  410. parser.add_argument('-v', '--verbose', action='count',
  411. default=0, help='Increase verbosity')
  412. cli = parser.parse_args(args=args)
  413. return ObjectDict(vars(cli))
  414. def load(path, prefix=None):
  415. load_sections = ('networks', 'tasks', 'auth', 'env')
  416. logger.info('Load config %s' % path)
  417. cfg = yaml_load(open(path))
  418. cfg = ConfigRoot.parse(cfg)
  419. # Define useful defaults
  420. cfg.networks = cfg.networks or ObjectDict()
  421. cfg.tasks = cfg.tasks or ObjectDict()
  422. if prefix:
  423. fn = lambda x: '/'.join(prefix + [x])
  424. # Apply prefix
  425. for section in load_sections:
  426. if not cfg.get(section):
  427. continue
  428. items = cfg[section].items()
  429. cfg[section] = {fn(k): v for k, v in items if not k.startswith('_')}
  430. # Recursive load
  431. if cfg.load:
  432. cfg_path = os.path.dirname(path)
  433. for item in cfg.load:
  434. if item.get('as'):
  435. child_prefix = item['as']
  436. else:
  437. child_prefix, _ = os.path.splitext(item.file)
  438. child_path = os.path.join(cfg_path, item.file)
  439. child_cfg = load(child_path, child_prefix.split('/'))
  440. for section in load_sections:
  441. if not cfg.get(section):
  442. cfg[section] = {}
  443. cfg[section].update(child_cfg.get(section, {}))
  444. return cfg
  445. def main():
  446. cli = base_cli()
  447. if cli.verbose:
  448. level = 'INFO' if cli.verbose == 1 else 'DEBUG'
  449. logger.setLevel(level)
  450. logger.info('Log level set to: %s' % level)
  451. # Load config
  452. cfg = load(cli.config)
  453. cli.cfg = cfg
  454. # Make sure we don't have overlap between hosts and tasks
  455. items = list(cfg.networks) + list(cfg.tasks)
  456. msg = 'Name collision between tasks and networks'
  457. assert len(set(items)) == len(items), msg
  458. # Build task list
  459. tasks = []
  460. networks = []
  461. for name in cli.names:
  462. if name in cfg.networks:
  463. host = cfg.networks[name]
  464. networks.append(host)
  465. elif name in cfg.tasks:
  466. task = cfg.tasks[name]
  467. tasks.append(task)
  468. else:
  469. msg = 'Name "%s" not understood' % name
  470. matches = spell(cfg.networks, name) | spell(cfg.tasks, name)
  471. if matches:
  472. msg += ', try: %s' % ' or '.join(matches)
  473. logger.error(msg)
  474. sys.exit()
  475. for custom_task in cli.run:
  476. task = Command.parse(yaml_load(custom_task))
  477. task.desc = 'Custom command'
  478. tasks.append(task)
  479. hosts = list(chain.from_iterable(n.hosts for n in networks))
  480. try:
  481. for task in tasks:
  482. run_batch(task, hosts, cli)
  483. except invoke.exceptions.Failure as e:
  484. logger.error(str(e))
  485. if __name__ == '__main__':
  486. main()