oo_filters.py 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. # vim: expandtab:tabstop=4:shiftwidth=4
  4. """
  5. Custom filters for use in openshift-ansible
  6. """
  7. from ansible import errors
  8. from operator import itemgetter
  9. import OpenSSL.crypto
  10. import os
  11. import pdb
  12. import re
  13. import json
  14. import yaml
  15. from ansible.utils.unicode import to_unicode
  16. # Disabling too-many-public-methods, since filter methods are necessarily
  17. # public
  18. # pylint: disable=too-many-public-methods
  19. class FilterModule(object):
  20. """ Custom ansible filters """
  21. @staticmethod
  22. def oo_pdb(arg):
  23. """ This pops you into a pdb instance where arg is the data passed in
  24. from the filter.
  25. Ex: "{{ hostvars | oo_pdb }}"
  26. """
  27. pdb.set_trace()
  28. return arg
  29. @staticmethod
  30. def get_attr(data, attribute=None):
  31. """ This looks up dictionary attributes of the form a.b.c and returns
  32. the value.
  33. Ex: data = {'a': {'b': {'c': 5}}}
  34. attribute = "a.b.c"
  35. returns 5
  36. """
  37. if not attribute:
  38. raise errors.AnsibleFilterError("|failed expects attribute to be set")
  39. ptr = data
  40. for attr in attribute.split('.'):
  41. ptr = ptr[attr]
  42. return ptr
  43. @staticmethod
  44. def oo_flatten(data):
  45. """ This filter plugin will flatten a list of lists
  46. """
  47. if not isinstance(data, list):
  48. raise errors.AnsibleFilterError("|failed expects to flatten a List")
  49. return [item for sublist in data for item in sublist]
  50. @staticmethod
  51. def oo_merge_dicts(first_dict, second_dict):
  52. """ Merge two dictionaries where second_dict values take precedence.
  53. Ex: first_dict={'a': 1, 'b': 2}
  54. second_dict={'b': 3, 'c': 4}
  55. returns {'a': 1, 'b': 3, 'c': 4}
  56. """
  57. if not isinstance(first_dict, dict) or not isinstance(second_dict, dict):
  58. raise errors.AnsibleFilterError("|failed expects to merge two dicts")
  59. merged = first_dict.copy()
  60. merged.update(second_dict)
  61. return merged
  62. @staticmethod
  63. def oo_collect(data, attribute=None, filters=None):
  64. """ This takes a list of dict and collects all attributes specified into a
  65. list. If filter is specified then we will include all items that
  66. match _ALL_ of filters. If a dict entry is missing the key in a
  67. filter it will be excluded from the match.
  68. Ex: data = [ {'a':1, 'b':5, 'z': 'z'}, # True, return
  69. {'a':2, 'z': 'z'}, # True, return
  70. {'a':3, 'z': 'z'}, # True, return
  71. {'a':4, 'z': 'b'}, # FAILED, obj['z'] != obj['z']
  72. ]
  73. attribute = 'a'
  74. filters = {'z': 'z'}
  75. returns [1, 2, 3]
  76. """
  77. if not isinstance(data, list):
  78. raise errors.AnsibleFilterError("|failed expects to filter on a List")
  79. if not attribute:
  80. raise errors.AnsibleFilterError("|failed expects attribute to be set")
  81. if filters is not None:
  82. if not isinstance(filters, dict):
  83. raise errors.AnsibleFilterError("|failed expects filter to be a"
  84. " dict")
  85. retval = [FilterModule.get_attr(d, attribute) for d in data if (
  86. all([d.get(key, None) == filters[key] for key in filters]))]
  87. else:
  88. retval = [FilterModule.get_attr(d, attribute) for d in data]
  89. return retval
  90. @staticmethod
  91. def oo_select_keys_from_list(data, keys):
  92. """ This returns a list, which contains the value portions for the keys
  93. Ex: data = { 'a':1, 'b':2, 'c':3 }
  94. keys = ['a', 'c']
  95. returns [1, 3]
  96. """
  97. if not isinstance(data, list):
  98. raise errors.AnsibleFilterError("|failed expects to filter on a list")
  99. if not isinstance(keys, list):
  100. raise errors.AnsibleFilterError("|failed expects first param is a list")
  101. # Gather up the values for the list of keys passed in
  102. retval = [FilterModule.oo_select_keys(item, keys) for item in data]
  103. return FilterModule.oo_flatten(retval)
  104. @staticmethod
  105. def oo_select_keys(data, keys):
  106. """ This returns a list, which contains the value portions for the keys
  107. Ex: data = { 'a':1, 'b':2, 'c':3 }
  108. keys = ['a', 'c']
  109. returns [1, 3]
  110. """
  111. if not isinstance(data, dict):
  112. raise errors.AnsibleFilterError("|failed expects to filter on a dict")
  113. if not isinstance(keys, list):
  114. raise errors.AnsibleFilterError("|failed expects first param is a list")
  115. # Gather up the values for the list of keys passed in
  116. retval = [data[key] for key in keys if data.has_key(key)]
  117. return retval
  118. @staticmethod
  119. def oo_prepend_strings_in_list(data, prepend):
  120. """ This takes a list of strings and prepends a string to each item in the
  121. list
  122. Ex: data = ['cart', 'tree']
  123. prepend = 'apple-'
  124. returns ['apple-cart', 'apple-tree']
  125. """
  126. if not isinstance(data, list):
  127. raise errors.AnsibleFilterError("|failed expects first param is a list")
  128. if not all(isinstance(x, basestring) for x in data):
  129. raise errors.AnsibleFilterError("|failed expects first param is a list"
  130. " of strings")
  131. retval = [prepend + s for s in data]
  132. return retval
  133. @staticmethod
  134. def oo_combine_key_value(data, joiner='='):
  135. """Take a list of dict in the form of { 'key': 'value'} and
  136. arrange them as a list of strings ['key=value']
  137. """
  138. if not isinstance(data, list):
  139. raise errors.AnsibleFilterError("|failed expects first param is a list")
  140. rval = []
  141. for item in data:
  142. rval.append("%s%s%s" % (item['key'], joiner, item['value']))
  143. return rval
  144. @staticmethod
  145. def oo_combine_dict(data, in_joiner='=', out_joiner=' '):
  146. """Take a dict in the form of { 'key': 'value', 'key': 'value' } and
  147. arrange them as a string 'key=value key=value'
  148. """
  149. if not isinstance(data, dict):
  150. raise errors.AnsibleFilterError("|failed expects first param is a dict")
  151. return out_joiner.join([in_joiner.join([k, v]) for k, v in data.items()])
  152. @staticmethod
  153. def oo_ami_selector(data, image_name):
  154. """ This takes a list of amis and an image name and attempts to return
  155. the latest ami.
  156. """
  157. if not isinstance(data, list):
  158. raise errors.AnsibleFilterError("|failed expects first param is a list")
  159. if not data:
  160. return None
  161. else:
  162. if image_name is None or not image_name.endswith('_*'):
  163. ami = sorted(data, key=itemgetter('name'), reverse=True)[0]
  164. return ami['ami_id']
  165. else:
  166. ami_info = [(ami, ami['name'].split('_')[-1]) for ami in data]
  167. ami = sorted(ami_info, key=itemgetter(1), reverse=True)[0][0]
  168. return ami['ami_id']
  169. @staticmethod
  170. def oo_ec2_volume_definition(data, host_type, docker_ephemeral=False):
  171. """ This takes a dictionary of volume definitions and returns a valid ec2
  172. volume definition based on the host_type and the values in the
  173. dictionary.
  174. The dictionary should look similar to this:
  175. { 'master':
  176. { 'root':
  177. { 'volume_size': 10, 'device_type': 'gp2',
  178. 'iops': 500
  179. },
  180. 'docker':
  181. { 'volume_size': 40, 'device_type': 'gp2',
  182. 'iops': 500, 'ephemeral': 'true'
  183. }
  184. },
  185. 'node':
  186. { 'root':
  187. { 'volume_size': 10, 'device_type': 'io1',
  188. 'iops': 1000
  189. },
  190. 'docker':
  191. { 'volume_size': 40, 'device_type': 'gp2',
  192. 'iops': 500, 'ephemeral': 'true'
  193. }
  194. }
  195. }
  196. """
  197. if not isinstance(data, dict):
  198. raise errors.AnsibleFilterError("|failed expects first param is a dict")
  199. if host_type not in ['master', 'node', 'etcd']:
  200. raise errors.AnsibleFilterError("|failed expects etcd, master or node"
  201. " as the host type")
  202. root_vol = data[host_type]['root']
  203. root_vol['device_name'] = '/dev/sda1'
  204. root_vol['delete_on_termination'] = True
  205. if root_vol['device_type'] != 'io1':
  206. root_vol.pop('iops', None)
  207. if host_type in ['master', 'node'] and 'docker' in data[host_type]:
  208. docker_vol = data[host_type]['docker']
  209. docker_vol['device_name'] = '/dev/xvdb'
  210. docker_vol['delete_on_termination'] = True
  211. if docker_vol['device_type'] != 'io1':
  212. docker_vol.pop('iops', None)
  213. if docker_ephemeral:
  214. docker_vol.pop('device_type', None)
  215. docker_vol.pop('delete_on_termination', None)
  216. docker_vol['ephemeral'] = 'ephemeral0'
  217. return [root_vol, docker_vol]
  218. elif host_type == 'etcd' and 'etcd' in data[host_type]:
  219. etcd_vol = data[host_type]['etcd']
  220. etcd_vol['device_name'] = '/dev/xvdb'
  221. etcd_vol['delete_on_termination'] = True
  222. if etcd_vol['device_type'] != 'io1':
  223. etcd_vol.pop('iops', None)
  224. return [root_vol, etcd_vol]
  225. return [root_vol]
  226. @staticmethod
  227. def oo_split(string, separator=','):
  228. """ This splits the input string into a list
  229. """
  230. return string.split(separator)
  231. @staticmethod
  232. def oo_haproxy_backend_masters(hosts):
  233. """ This takes an array of dicts and returns an array of dicts
  234. to be used as a backend for the haproxy role
  235. """
  236. servers = []
  237. for idx, host_info in enumerate(hosts):
  238. server = dict(name="master%s" % idx)
  239. server_ip = host_info['openshift']['common']['ip']
  240. server_port = host_info['openshift']['master']['api_port']
  241. server['address'] = "%s:%s" % (server_ip, server_port)
  242. server['opts'] = 'check'
  243. servers.append(server)
  244. return servers
  245. @staticmethod
  246. def oo_filter_list(data, filter_attr=None):
  247. """ This returns a list, which contains all items where filter_attr
  248. evaluates to true
  249. Ex: data = [ { a: 1, b: True },
  250. { a: 3, b: False },
  251. { a: 5, b: True } ]
  252. filter_attr = 'b'
  253. returns [ { a: 1, b: True },
  254. { a: 5, b: True } ]
  255. """
  256. if not isinstance(data, list):
  257. raise errors.AnsibleFilterError("|failed expects to filter on a list")
  258. if not isinstance(filter_attr, basestring):
  259. raise errors.AnsibleFilterError("|failed expects filter_attr is a str or unicode")
  260. # Gather up the values for the list of keys passed in
  261. return [x for x in data if x.has_key(filter_attr) and x[filter_attr]]
  262. @staticmethod
  263. def oo_nodes_with_label(nodes, label, value=None):
  264. """ Filters a list of nodes by label and value (if provided)
  265. It handles labels that are in the following variables by priority:
  266. openshift_node_labels, cli_openshift_node_labels, openshift['node']['labels']
  267. Examples:
  268. data = ['a': {'openshift_node_labels': {'color': 'blue', 'size': 'M'}},
  269. 'b': {'openshift_node_labels': {'color': 'green', 'size': 'L'}},
  270. 'c': {'openshift_node_labels': {'size': 'S'}}]
  271. label = 'color'
  272. returns = ['a': {'openshift_node_labels': {'color': 'blue', 'size': 'M'}},
  273. 'b': {'openshift_node_labels': {'color': 'green', 'size': 'L'}}]
  274. data = ['a': {'openshift_node_labels': {'color': 'blue', 'size': 'M'}},
  275. 'b': {'openshift_node_labels': {'color': 'green', 'size': 'L'}},
  276. 'c': {'openshift_node_labels': {'size': 'S'}}]
  277. label = 'color'
  278. value = 'green'
  279. returns = ['b': {'labels': {'color': 'green', 'size': 'L'}}]
  280. Args:
  281. nodes (list[dict]): list of node to node variables
  282. label (str): label to filter `nodes` by
  283. value (Optional[str]): value of `label` to filter by Defaults
  284. to None.
  285. Returns:
  286. list[dict]: nodes filtered by label and value (if provided)
  287. """
  288. if not isinstance(nodes, list):
  289. raise errors.AnsibleFilterError("failed expects to filter on a list")
  290. if not isinstance(label, basestring):
  291. raise errors.AnsibleFilterError("failed expects label to be a string")
  292. if value is not None and not isinstance(value, basestring):
  293. raise errors.AnsibleFilterError("failed expects value to be a string")
  294. def label_filter(node):
  295. """ filter function for testing if node should be returned """
  296. if not isinstance(node, dict):
  297. raise errors.AnsibleFilterError("failed expects to filter on a list of dicts")
  298. if 'openshift_node_labels' in node:
  299. labels = node['openshift_node_labels']
  300. elif 'cli_openshift_node_labels' in node:
  301. labels = node['cli_openshift_node_labels']
  302. elif 'openshift' in node and 'node' in node['openshift'] and 'labels' in node['openshift']['node']:
  303. labels = node['openshift']['node']['labels']
  304. else:
  305. return False
  306. if isinstance(labels, basestring):
  307. labels = yaml.safe_load(labels)
  308. if not isinstance(labels, dict):
  309. raise errors.AnsibleFilterError(
  310. "failed expected node labels to be a dict or serializable to a dict"
  311. )
  312. return label in labels and (value is None or labels[label] == value)
  313. return [n for n in nodes if label_filter(n)]
  314. @staticmethod
  315. def oo_parse_heat_stack_outputs(data):
  316. """ Formats the HEAT stack output into a usable form
  317. The goal is to transform something like this:
  318. +---------------+-------------------------------------------------+
  319. | Property | Value |
  320. +---------------+-------------------------------------------------+
  321. | capabilities | [] | |
  322. | creation_time | 2015-06-26T12:26:26Z | |
  323. | description | OpenShift cluster | |
  324. | … | … |
  325. | outputs | [ |
  326. | | { |
  327. | | "output_value": "value_A" |
  328. | | "description": "This is the value of Key_A" |
  329. | | "output_key": "Key_A" |
  330. | | }, |
  331. | | { |
  332. | | "output_value": [ |
  333. | | "value_B1", |
  334. | | "value_B2" |
  335. | | ], |
  336. | | "description": "This is the value of Key_B" |
  337. | | "output_key": "Key_B" |
  338. | | }, |
  339. | | ] |
  340. | parameters | { |
  341. | … | … |
  342. +---------------+-------------------------------------------------+
  343. into something like this:
  344. {
  345. "Key_A": "value_A",
  346. "Key_B": [
  347. "value_B1",
  348. "value_B2"
  349. ]
  350. }
  351. """
  352. # Extract the “outputs” JSON snippet from the pretty-printed array
  353. in_outputs = False
  354. outputs = ''
  355. line_regex = re.compile(r'\|\s*(.*?)\s*\|\s*(.*?)\s*\|')
  356. for line in data['stdout_lines']:
  357. match = line_regex.match(line)
  358. if match:
  359. if match.group(1) == 'outputs':
  360. in_outputs = True
  361. elif match.group(1) != '':
  362. in_outputs = False
  363. if in_outputs:
  364. outputs += match.group(2)
  365. outputs = json.loads(outputs)
  366. # Revamp the “outputs” to put it in the form of a “Key: value” map
  367. revamped_outputs = {}
  368. for output in outputs:
  369. revamped_outputs[output['output_key']] = output['output_value']
  370. return revamped_outputs
  371. @staticmethod
  372. # pylint: disable=too-many-branches
  373. def oo_parse_named_certificates(certificates, named_certs_dir, internal_hostnames):
  374. """ Parses names from list of certificate hashes.
  375. Ex: certificates = [{ "certfile": "/root/custom1.crt",
  376. "keyfile": "/root/custom1.key" },
  377. { "certfile": "custom2.crt",
  378. "keyfile": "custom2.key" }]
  379. returns [{ "certfile": "/etc/origin/master/named_certificates/custom1.crt",
  380. "keyfile": "/etc/origin/master/named_certificates/custom1.key",
  381. "names": [ "public-master-host.com",
  382. "other-master-host.com" ] },
  383. { "certfile": "/etc/origin/master/named_certificates/custom2.crt",
  384. "keyfile": "/etc/origin/master/named_certificates/custom2.key",
  385. "names": [ "some-hostname.com" ] }]
  386. """
  387. if not isinstance(named_certs_dir, basestring):
  388. raise errors.AnsibleFilterError("|failed expects named_certs_dir is str or unicode")
  389. if not isinstance(internal_hostnames, list):
  390. raise errors.AnsibleFilterError("|failed expects internal_hostnames is list")
  391. for certificate in certificates:
  392. if 'names' in certificate.keys():
  393. continue
  394. else:
  395. certificate['names'] = []
  396. if not os.path.isfile(certificate['certfile']) or not os.path.isfile(certificate['keyfile']):
  397. raise errors.AnsibleFilterError("|certificate and/or key does not exist '%s', '%s'" %
  398. (certificate['certfile'], certificate['keyfile']))
  399. try:
  400. st_cert = open(certificate['certfile'], 'rt').read()
  401. cert = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, st_cert)
  402. certificate['names'].append(str(cert.get_subject().commonName.decode()))
  403. for i in range(cert.get_extension_count()):
  404. if cert.get_extension(i).get_short_name() == 'subjectAltName':
  405. for name in str(cert.get_extension(i)).replace('DNS:', '').split(', '):
  406. certificate['names'].append(name)
  407. except:
  408. raise errors.AnsibleFilterError(("|failed to parse certificate '%s', " % certificate['certfile'] +
  409. "please specify certificate names in host inventory"))
  410. certificate['names'] = [name for name in certificate['names'] if name not in internal_hostnames]
  411. certificate['names'] = list(set(certificate['names']))
  412. if not certificate['names']:
  413. raise errors.AnsibleFilterError(("|failed to parse certificate '%s' or " % certificate['certfile'] +
  414. "detected a collision with internal hostname, please specify " +
  415. "certificate names in host inventory"))
  416. for certificate in certificates:
  417. # Update paths for configuration
  418. certificate['certfile'] = os.path.join(named_certs_dir, os.path.basename(certificate['certfile']))
  419. certificate['keyfile'] = os.path.join(named_certs_dir, os.path.basename(certificate['keyfile']))
  420. return certificates
  421. @staticmethod
  422. def oo_pretty_print_cluster(data):
  423. """ Read a subset of hostvars and build a summary of the cluster
  424. in the following layout:
  425. "c_id": {
  426. "master": {
  427. "default": [
  428. { "name": "c_id-master-12345", "public IP": "172.16.0.1", "private IP": "192.168.0.1" }
  429. ]
  430. "node": {
  431. "infra": [
  432. { "name": "c_id-node-infra-23456", "public IP": "172.16.0.2", "private IP": "192.168.0.2" }
  433. ],
  434. "compute": [
  435. { "name": "c_id-node-compute-23456", "public IP": "172.16.0.3", "private IP": "192.168.0.3" },
  436. ...
  437. ]
  438. }
  439. """
  440. def _get_tag_value(tags, key):
  441. """ Extract values of a map implemented as a set.
  442. Ex: tags = { 'tag_foo_value1', 'tag_bar_value2', 'tag_baz_value3' }
  443. key = 'bar'
  444. returns 'value2'
  445. """
  446. for tag in tags:
  447. if tag[:len(key)+4] == 'tag_' + key:
  448. return tag[len(key)+5:]
  449. raise KeyError(key)
  450. def _add_host(clusters,
  451. clusterid,
  452. host_type,
  453. sub_host_type,
  454. host):
  455. """ Add a new host in the clusters data structure """
  456. if clusterid not in clusters:
  457. clusters[clusterid] = {}
  458. if host_type not in clusters[clusterid]:
  459. clusters[clusterid][host_type] = {}
  460. if sub_host_type not in clusters[clusterid][host_type]:
  461. clusters[clusterid][host_type][sub_host_type] = []
  462. clusters[clusterid][host_type][sub_host_type].append(host)
  463. clusters = {}
  464. for host in data:
  465. try:
  466. _add_host(clusters=clusters,
  467. clusterid=_get_tag_value(host['group_names'], 'clusterid'),
  468. host_type=_get_tag_value(host['group_names'], 'host-type'),
  469. sub_host_type=_get_tag_value(host['group_names'], 'sub-host-type'),
  470. host={'name': host['inventory_hostname'],
  471. 'public IP': host['ansible_ssh_host'],
  472. 'private IP': host['ansible_default_ipv4']['address']})
  473. except KeyError:
  474. pass
  475. return clusters
  476. @staticmethod
  477. def oo_generate_secret(num_bytes):
  478. """ generate a session secret """
  479. if not isinstance(num_bytes, int):
  480. raise errors.AnsibleFilterError("|failed expects num_bytes is int")
  481. secret = os.urandom(num_bytes)
  482. return secret.encode('base-64').strip()
  483. @staticmethod
  484. def to_padded_yaml(data, level=0, indent=2, **kw):
  485. """ returns a yaml snippet padded to match the indent level you specify """
  486. if data in [None, ""]:
  487. return ""
  488. try:
  489. transformed = yaml.safe_dump(data, indent=indent, allow_unicode=True, default_flow_style=False, **kw)
  490. padded = "\n".join([" " * level * indent + line for line in transformed.splitlines()])
  491. return to_unicode("\n{0}".format(padded))
  492. except Exception as my_e:
  493. raise errors.AnsibleFilterError('Failed to convert: %s', my_e)
  494. @staticmethod
  495. def oo_openshift_env(hostvars):
  496. ''' Return facts which begin with "openshift_"
  497. Ex: hostvars = {'openshift_fact': 42,
  498. 'theyre_taking_the_hobbits_to': 'isengard'}
  499. returns = {'openshift_fact': 42}
  500. '''
  501. if not issubclass(type(hostvars), dict):
  502. raise errors.AnsibleFilterError("|failed expects hostvars is a dict")
  503. facts = {}
  504. regex = re.compile('^openshift_.*')
  505. for key in hostvars:
  506. if regex.match(key):
  507. facts[key] = hostvars[key]
  508. return facts
  509. @staticmethod
  510. # pylint: disable=too-many-branches
  511. def oo_persistent_volumes(hostvars, groups, persistent_volumes=None):
  512. """ Generate list of persistent volumes based on oo_openshift_env
  513. storage options set in host variables.
  514. """
  515. if not issubclass(type(hostvars), dict):
  516. raise errors.AnsibleFilterError("|failed expects hostvars is a dict")
  517. if not issubclass(type(groups), dict):
  518. raise errors.AnsibleFilterError("|failed expects groups is a dict")
  519. if persistent_volumes != None and not issubclass(type(persistent_volumes), list):
  520. raise errors.AnsibleFilterError("|failed expects persistent_volumes is a list")
  521. if persistent_volumes == None:
  522. persistent_volumes = []
  523. for component in hostvars['openshift']['hosted']:
  524. kind = hostvars['openshift']['hosted'][component]['storage']['kind']
  525. create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
  526. if kind != None and create_pv:
  527. if kind == 'nfs':
  528. host = hostvars['openshift']['hosted'][component]['storage']['host']
  529. if host == None:
  530. if len(groups['oo_nfs_to_config']) > 0:
  531. host = groups['oo_nfs_to_config'][0]
  532. else:
  533. raise errors.AnsibleFilterError("|failed no storage host detected")
  534. directory = hostvars['openshift']['hosted'][component]['storage']['nfs']['directory']
  535. volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
  536. path = directory + '/' + volume
  537. size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
  538. access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
  539. persistent_volume = dict(
  540. name="{0}-volume".format(volume),
  541. capacity=size,
  542. access_modes=access_modes,
  543. storage=dict(
  544. nfs=dict(
  545. server=host,
  546. path=path)))
  547. persistent_volumes.append(persistent_volume)
  548. else:
  549. msg = "|failed invalid storage kind '{0}' for component '{1}'".format(
  550. kind,
  551. component)
  552. raise errors.AnsibleFilterError(msg)
  553. return persistent_volumes
  554. @staticmethod
  555. def oo_persistent_volume_claims(hostvars, persistent_volume_claims=None):
  556. """ Generate list of persistent volume claims based on oo_openshift_env
  557. storage options set in host variables.
  558. """
  559. if not issubclass(type(hostvars), dict):
  560. raise errors.AnsibleFilterError("|failed expects hostvars is a dict")
  561. if persistent_volume_claims != None and not issubclass(type(persistent_volume_claims), list):
  562. raise errors.AnsibleFilterError("|failed expects persistent_volume_claims is a list")
  563. if persistent_volume_claims == None:
  564. persistent_volume_claims = []
  565. for component in hostvars['openshift']['hosted']:
  566. kind = hostvars['openshift']['hosted'][component]['storage']['kind']
  567. create_pv = hostvars['openshift']['hosted'][component]['storage']['create_pv']
  568. if kind != None and create_pv:
  569. volume = hostvars['openshift']['hosted'][component]['storage']['volume']['name']
  570. size = hostvars['openshift']['hosted'][component]['storage']['volume']['size']
  571. access_modes = hostvars['openshift']['hosted'][component]['storage']['access_modes']
  572. persistent_volume_claim = dict(
  573. name="{0}-claim".format(volume),
  574. capacity=size,
  575. access_modes=access_modes)
  576. persistent_volume_claims.append(persistent_volume_claim)
  577. return persistent_volume_claims
  578. @staticmethod
  579. def oo_31_rpm_rename_conversion(rpms, openshift_version=None):
  580. """ Filters a list of 3.0 rpms and return the corresponding 3.1 rpms
  581. names with proper version (if provided)
  582. If 3.1 rpms are passed in they will only be augmented with the
  583. correct version. This is important for hosts that are running both
  584. Masters and Nodes.
  585. """
  586. if not isinstance(rpms, list):
  587. raise errors.AnsibleFilterError("failed expects to filter on a list")
  588. if openshift_version is not None and not isinstance(openshift_version, basestring):
  589. raise errors.AnsibleFilterError("failed expects openshift_version to be a string")
  590. rpms_31 = []
  591. for rpm in rpms:
  592. if not 'atomic' in rpm:
  593. rpm = rpm.replace("openshift", "atomic-openshift")
  594. if openshift_version:
  595. rpm = rpm + openshift_version
  596. rpms_31.append(rpm)
  597. return rpms_31
  598. @staticmethod
  599. def oo_pods_match_component(pods, deployment_type, component):
  600. """ Filters a list of Pods and returns the ones matching the deployment_type and component
  601. """
  602. if not isinstance(pods, list):
  603. raise errors.AnsibleFilterError("failed expects to filter on a list")
  604. if not isinstance(deployment_type, basestring):
  605. raise errors.AnsibleFilterError("failed expects deployment_type to be a string")
  606. if not isinstance(component, basestring):
  607. raise errors.AnsibleFilterError("failed expects component to be a string")
  608. image_prefix = 'openshift/origin-'
  609. if deployment_type in ['enterprise', 'online', 'openshift-enterprise']:
  610. image_prefix = 'openshift3/ose-'
  611. elif deployment_type == 'atomic-enterprise':
  612. image_prefix = 'aep3_beta/aep-'
  613. matching_pods = []
  614. image_regex = image_prefix + component + r'.*'
  615. for pod in pods:
  616. for container in pod['spec']['containers']:
  617. if re.search(image_regex, container['image']):
  618. matching_pods.append(pod)
  619. break # stop here, don't add a pod more than once
  620. return matching_pods
  621. @staticmethod
  622. def oo_get_hosts_from_hostvars(hostvars, hosts):
  623. """ Return a list of hosts from hostvars """
  624. retval = []
  625. for host in hosts:
  626. try:
  627. retval.append(hostvars[host])
  628. except errors.AnsibleError as _:
  629. # host does not exist
  630. pass
  631. return retval
  632. @staticmethod
  633. def oo_image_tag_to_rpm_version(version):
  634. """ Convert an image tag string to an RPM version if necessary
  635. Empty strings and strings that are already in rpm version format
  636. are ignored.
  637. Ex. v3.2.0.10 -> -3.2.0.10
  638. """
  639. if not isinstance(version, basestring):
  640. raise errors.AnsibleFilterError("|failed expects a string or unicode")
  641. if version.startswith("v"):
  642. version = "-" + version.replace("v", "")
  643. return version
  644. def filters(self):
  645. """ returns a mapping of filters to methods """
  646. return {
  647. "oo_select_keys": self.oo_select_keys,
  648. "oo_select_keys_from_list": self.oo_select_keys_from_list,
  649. "oo_collect": self.oo_collect,
  650. "oo_flatten": self.oo_flatten,
  651. "oo_pdb": self.oo_pdb,
  652. "oo_prepend_strings_in_list": self.oo_prepend_strings_in_list,
  653. "oo_ami_selector": self.oo_ami_selector,
  654. "oo_ec2_volume_definition": self.oo_ec2_volume_definition,
  655. "oo_combine_key_value": self.oo_combine_key_value,
  656. "oo_combine_dict": self.oo_combine_dict,
  657. "oo_split": self.oo_split,
  658. "oo_filter_list": self.oo_filter_list,
  659. "oo_parse_heat_stack_outputs": self.oo_parse_heat_stack_outputs,
  660. "oo_parse_named_certificates": self.oo_parse_named_certificates,
  661. "oo_haproxy_backend_masters": self.oo_haproxy_backend_masters,
  662. "oo_pretty_print_cluster": self.oo_pretty_print_cluster,
  663. "oo_generate_secret": self.oo_generate_secret,
  664. "to_padded_yaml": self.to_padded_yaml,
  665. "oo_nodes_with_label": self.oo_nodes_with_label,
  666. "oo_openshift_env": self.oo_openshift_env,
  667. "oo_persistent_volumes": self.oo_persistent_volumes,
  668. "oo_persistent_volume_claims": self.oo_persistent_volume_claims,
  669. "oo_31_rpm_rename_conversion": self.oo_31_rpm_rename_conversion,
  670. "oo_pods_match_component": self.oo_pods_match_component,
  671. "oo_get_hosts_from_hostvars": self.oo_get_hosts_from_hostvars,
  672. "oo_image_tag_to_rpm_version": self.oo_image_tag_to_rpm_version,
  673. "oo_merge_dicts": self.oo_merge_dicts
  674. }