baker.py 16 KB

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