openshift_facts.py 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. #!/usr/bin/python
  2. # pylint: disable=too-many-lines
  3. # -*- coding: utf-8 -*-
  4. # vim: expandtab:tabstop=4:shiftwidth=4
  5. # Reason: Disable pylint too-many-lines because we don't want to split up this file.
  6. # Status: Permanently disabled to keep this module as self-contained as possible.
  7. """Ansible module for retrieving and setting openshift related facts"""
  8. DOCUMENTATION = '''
  9. ---
  10. module: openshift_facts
  11. short_description: Cluster Facts
  12. author: Jason DeTiberus
  13. requirements: [ ]
  14. '''
  15. EXAMPLES = '''
  16. '''
  17. import ConfigParser
  18. import copy
  19. import os
  20. import StringIO
  21. import yaml
  22. from distutils.util import strtobool
  23. from distutils.version import LooseVersion
  24. import struct
  25. import socket
  26. def first_ip(network):
  27. """ Return the first IPv4 address in network
  28. Args:
  29. network (str): network in CIDR format
  30. Returns:
  31. str: first IPv4 address
  32. """
  33. atoi = lambda addr: struct.unpack("!I", socket.inet_aton(addr))[0]
  34. itoa = lambda addr: socket.inet_ntoa(struct.pack("!I", addr))
  35. (address, netmask) = network.split('/')
  36. netmask_i = (0xffffffff << (32 - atoi(netmask))) & 0xffffffff
  37. return itoa((atoi(address) & netmask_i) + 1)
  38. def hostname_valid(hostname):
  39. """ Test if specified hostname should be considered valid
  40. Args:
  41. hostname (str): hostname to test
  42. Returns:
  43. bool: True if valid, otherwise False
  44. """
  45. if (not hostname or
  46. hostname.startswith('localhost') or
  47. hostname.endswith('localdomain') or
  48. len(hostname.split('.')) < 2):
  49. return False
  50. return True
  51. def choose_hostname(hostnames=None, fallback=''):
  52. """ Choose a hostname from the provided hostnames
  53. Given a list of hostnames and a fallback value, choose a hostname to
  54. use. This function will prefer fqdns if they exist (excluding any that
  55. begin with localhost or end with localdomain) over ip addresses.
  56. Args:
  57. hostnames (list): list of hostnames
  58. fallback (str): default value to set if hostnames does not contain
  59. a valid hostname
  60. Returns:
  61. str: chosen hostname
  62. """
  63. hostname = fallback
  64. if hostnames is None:
  65. return hostname
  66. ip_regex = r'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z'
  67. ips = [i for i in hostnames
  68. if (i is not None and isinstance(i, basestring)
  69. and re.match(ip_regex, i))]
  70. hosts = [i for i in hostnames
  71. if i is not None and i != '' and i not in ips]
  72. for host_list in (hosts, ips):
  73. for host in host_list:
  74. if hostname_valid(host):
  75. return host
  76. return hostname
  77. def query_metadata(metadata_url, headers=None, expect_json=False):
  78. """ Return metadata from the provided metadata_url
  79. Args:
  80. metadata_url (str): metadata url
  81. headers (dict): headers to set for metadata request
  82. expect_json (bool): does the metadata_url return json
  83. Returns:
  84. dict or list: metadata request result
  85. """
  86. result, info = fetch_url(module, metadata_url, headers=headers)
  87. if info['status'] != 200:
  88. raise OpenShiftFactsMetadataUnavailableError("Metadata unavailable")
  89. if expect_json:
  90. return module.from_json(result.read())
  91. else:
  92. return [line.strip() for line in result.readlines()]
  93. def walk_metadata(metadata_url, headers=None, expect_json=False):
  94. """ Walk the metadata tree and return a dictionary of the entire tree
  95. Args:
  96. metadata_url (str): metadata url
  97. headers (dict): headers to set for metadata request
  98. expect_json (bool): does the metadata_url return json
  99. Returns:
  100. dict: the result of walking the metadata tree
  101. """
  102. metadata = dict()
  103. for line in query_metadata(metadata_url, headers, expect_json):
  104. if line.endswith('/') and not line == 'public-keys/':
  105. key = line[:-1]
  106. metadata[key] = walk_metadata(metadata_url + line,
  107. headers, expect_json)
  108. else:
  109. results = query_metadata(metadata_url + line, headers,
  110. expect_json)
  111. if len(results) == 1:
  112. # disable pylint maybe-no-member because overloaded use of
  113. # the module name causes pylint to not detect that results
  114. # is an array or hash
  115. # pylint: disable=maybe-no-member
  116. metadata[line] = results.pop()
  117. else:
  118. metadata[line] = results
  119. return metadata
  120. def get_provider_metadata(metadata_url, supports_recursive=False,
  121. headers=None, expect_json=False):
  122. """ Retrieve the provider metadata
  123. Args:
  124. metadata_url (str): metadata url
  125. supports_recursive (bool): does the provider metadata api support
  126. recursion
  127. headers (dict): headers to set for metadata request
  128. expect_json (bool): does the metadata_url return json
  129. Returns:
  130. dict: the provider metadata
  131. """
  132. try:
  133. if supports_recursive:
  134. metadata = query_metadata(metadata_url, headers,
  135. expect_json)
  136. else:
  137. metadata = walk_metadata(metadata_url, headers,
  138. expect_json)
  139. except OpenShiftFactsMetadataUnavailableError:
  140. metadata = None
  141. return metadata
  142. def normalize_gce_facts(metadata, facts):
  143. """ Normalize gce facts
  144. Args:
  145. metadata (dict): provider metadata
  146. facts (dict): facts to update
  147. Returns:
  148. dict: the result of adding the normalized metadata to the provided
  149. facts dict
  150. """
  151. for interface in metadata['instance']['networkInterfaces']:
  152. int_info = dict(ips=[interface['ip']], network_type='gce')
  153. int_info['public_ips'] = [ac['externalIp'] for ac
  154. in interface['accessConfigs']]
  155. int_info['public_ips'].extend(interface['forwardedIps'])
  156. _, _, network_id = interface['network'].rpartition('/')
  157. int_info['network_id'] = network_id
  158. facts['network']['interfaces'].append(int_info)
  159. _, _, zone = metadata['instance']['zone'].rpartition('/')
  160. facts['zone'] = zone
  161. # GCE currently only supports a single interface
  162. facts['network']['ip'] = facts['network']['interfaces'][0]['ips'][0]
  163. pub_ip = facts['network']['interfaces'][0]['public_ips'][0]
  164. facts['network']['public_ip'] = pub_ip
  165. facts['network']['hostname'] = metadata['instance']['hostname']
  166. # TODO: attempt to resolve public_hostname
  167. facts['network']['public_hostname'] = facts['network']['public_ip']
  168. return facts
  169. def normalize_aws_facts(metadata, facts):
  170. """ Normalize aws facts
  171. Args:
  172. metadata (dict): provider metadata
  173. facts (dict): facts to update
  174. Returns:
  175. dict: the result of adding the normalized metadata to the provided
  176. facts dict
  177. """
  178. for interface in sorted(
  179. metadata['network']['interfaces']['macs'].values(),
  180. key=lambda x: x['device-number']
  181. ):
  182. int_info = dict()
  183. var_map = {'ips': 'local-ipv4s', 'public_ips': 'public-ipv4s'}
  184. for ips_var, int_var in var_map.iteritems():
  185. ips = interface.get(int_var)
  186. if isinstance(ips, basestring):
  187. int_info[ips_var] = [ips]
  188. else:
  189. int_info[ips_var] = ips
  190. if 'vpc-id' in interface:
  191. int_info['network_type'] = 'vpc'
  192. else:
  193. int_info['network_type'] = 'classic'
  194. if int_info['network_type'] == 'vpc':
  195. int_info['network_id'] = interface['subnet-id']
  196. else:
  197. int_info['network_id'] = None
  198. facts['network']['interfaces'].append(int_info)
  199. facts['zone'] = metadata['placement']['availability-zone']
  200. # TODO: actually attempt to determine default local and public ips
  201. # by using the ansible default ip fact and the ipv4-associations
  202. # from the ec2 metadata
  203. facts['network']['ip'] = metadata.get('local-ipv4')
  204. facts['network']['public_ip'] = metadata.get('public-ipv4')
  205. # TODO: verify that local hostname makes sense and is resolvable
  206. facts['network']['hostname'] = metadata.get('local-hostname')
  207. # TODO: verify that public hostname makes sense and is resolvable
  208. facts['network']['public_hostname'] = metadata.get('public-hostname')
  209. return facts
  210. def normalize_openstack_facts(metadata, facts):
  211. """ Normalize openstack facts
  212. Args:
  213. metadata (dict): provider metadata
  214. facts (dict): facts to update
  215. Returns:
  216. dict: the result of adding the normalized metadata to the provided
  217. facts dict
  218. """
  219. # openstack ec2 compat api does not support network interfaces and
  220. # the version tested on did not include the info in the openstack
  221. # metadata api, should be updated if neutron exposes this.
  222. facts['zone'] = metadata['availability_zone']
  223. local_ipv4 = metadata['ec2_compat']['local-ipv4'].split(',')[0]
  224. facts['network']['ip'] = local_ipv4
  225. facts['network']['public_ip'] = metadata['ec2_compat']['public-ipv4']
  226. # TODO: verify local hostname makes sense and is resolvable
  227. facts['network']['hostname'] = metadata['hostname']
  228. # TODO: verify that public hostname makes sense and is resolvable
  229. pub_h = metadata['ec2_compat']['public-hostname']
  230. facts['network']['public_hostname'] = pub_h
  231. return facts
  232. def normalize_provider_facts(provider, metadata):
  233. """ Normalize provider facts
  234. Args:
  235. provider (str): host provider
  236. metadata (dict): provider metadata
  237. Returns:
  238. dict: the normalized provider facts
  239. """
  240. if provider is None or metadata is None:
  241. return {}
  242. # TODO: test for ipv6_enabled where possible (gce, aws do not support)
  243. # and configure ipv6 facts if available
  244. # TODO: add support for setting user_data if available
  245. facts = dict(name=provider, metadata=metadata,
  246. network=dict(interfaces=[], ipv6_enabled=False))
  247. if os.path.exists('/etc/cloud.conf'):
  248. for arg in ('api_server_args', 'controller_args', 'kubelet_args'):
  249. facts[arg] = {'cloud-provider': [provider],
  250. 'cloud-config': ['/etc/cloud.conf']}
  251. if provider == 'gce':
  252. facts = normalize_gce_facts(metadata, facts)
  253. elif provider == 'ec2':
  254. facts = normalize_aws_facts(metadata, facts)
  255. elif provider == 'openstack':
  256. facts = normalize_openstack_facts(metadata, facts)
  257. return facts
  258. def set_flannel_facts_if_unset(facts):
  259. """ Set flannel facts if not already present in facts dict
  260. dict: the facts dict updated with the flannel facts if
  261. missing
  262. Args:
  263. facts (dict): existing facts
  264. Returns:
  265. dict: the facts dict updated with the flannel
  266. facts if they were not already present
  267. """
  268. if 'common' in facts:
  269. if 'use_flannel' not in facts['common']:
  270. use_flannel = False
  271. facts['common']['use_flannel'] = use_flannel
  272. return facts
  273. def set_nuage_facts_if_unset(facts):
  274. """ Set nuage facts if not already present in facts dict
  275. dict: the facts dict updated with the nuage facts if
  276. missing
  277. Args:
  278. facts (dict): existing facts
  279. Returns:
  280. dict: the facts dict updated with the nuage
  281. facts if they were not already present
  282. """
  283. if 'common' in facts:
  284. if 'use_nuage' not in facts['common']:
  285. use_nuage = False
  286. facts['common']['use_nuage'] = use_nuage
  287. return facts
  288. def set_node_schedulability(facts):
  289. """ Set schedulable facts if not already present in facts dict
  290. Args:
  291. facts (dict): existing facts
  292. Returns:
  293. dict: the facts dict updated with the generated schedulable
  294. facts if they were not already present
  295. """
  296. if 'node' in facts:
  297. if 'schedulable' not in facts['node']:
  298. if 'master' in facts:
  299. facts['node']['schedulable'] = False
  300. else:
  301. facts['node']['schedulable'] = True
  302. return facts
  303. def set_master_selectors(facts):
  304. """ Set selectors facts if not already present in facts dict
  305. Args:
  306. facts (dict): existing facts
  307. Returns:
  308. dict: the facts dict updated with the generated selectors
  309. facts if they were not already present
  310. """
  311. if 'master' in facts:
  312. if 'infra_nodes' in facts['master']:
  313. deployment_type = facts['common']['deployment_type']
  314. if deployment_type == 'online':
  315. selector = "type=infra"
  316. else:
  317. selector = "region=infra"
  318. if 'router_selector' not in facts['master']:
  319. facts['master']['router_selector'] = selector
  320. if 'registry_selector' not in facts['master']:
  321. facts['master']['registry_selector'] = selector
  322. return facts
  323. def set_metrics_facts_if_unset(facts):
  324. """ Set cluster metrics facts if not already present in facts dict
  325. dict: the facts dict updated with the generated cluster metrics facts if
  326. missing
  327. Args:
  328. facts (dict): existing facts
  329. Returns:
  330. dict: the facts dict updated with the generated cluster metrics
  331. facts if they were not already present
  332. """
  333. if 'common' in facts:
  334. if 'use_cluster_metrics' not in facts['common']:
  335. use_cluster_metrics = False
  336. facts['common']['use_cluster_metrics'] = use_cluster_metrics
  337. return facts
  338. def set_project_cfg_facts_if_unset(facts):
  339. """ Set Project Configuration facts if not already present in facts dict
  340. dict:
  341. Args:
  342. facts (dict): existing facts
  343. Returns:
  344. dict: the facts dict updated with the generated Project Configuration
  345. facts if they were not already present
  346. """
  347. config = {
  348. 'default_node_selector': '',
  349. 'project_request_message': '',
  350. 'project_request_template': '',
  351. 'mcs_allocator_range': 's0:/2',
  352. 'mcs_labels_per_project': 5,
  353. 'uid_allocator_range': '1000000000-1999999999/10000'
  354. }
  355. if 'master' in facts:
  356. for key, value in config.items():
  357. if key not in facts['master']:
  358. facts['master'][key] = value
  359. return facts
  360. def set_identity_providers_if_unset(facts):
  361. """ Set identity_providers fact if not already present in facts dict
  362. Args:
  363. facts (dict): existing facts
  364. Returns:
  365. dict: the facts dict updated with the generated identity providers
  366. facts if they were not already present
  367. """
  368. if 'master' in facts:
  369. deployment_type = facts['common']['deployment_type']
  370. if 'identity_providers' not in facts['master']:
  371. identity_provider = dict(
  372. name='allow_all', challenge=True, login=True,
  373. kind='AllowAllPasswordIdentityProvider'
  374. )
  375. if deployment_type in ['enterprise', 'atomic-enterprise', 'openshift-enterprise']:
  376. identity_provider = dict(
  377. name='deny_all', challenge=True, login=True,
  378. kind='DenyAllPasswordIdentityProvider'
  379. )
  380. facts['master']['identity_providers'] = [identity_provider]
  381. return facts
  382. def set_url_facts_if_unset(facts):
  383. """ Set url facts if not already present in facts dict
  384. Args:
  385. facts (dict): existing facts
  386. Returns:
  387. dict: the facts dict updated with the generated url facts if they
  388. were not already present
  389. """
  390. if 'master' in facts:
  391. hostname = facts['common']['hostname']
  392. cluster_hostname = facts['master'].get('cluster_hostname')
  393. cluster_public_hostname = facts['master'].get('cluster_public_hostname')
  394. public_hostname = facts['common']['public_hostname']
  395. api_hostname = cluster_hostname if cluster_hostname else hostname
  396. api_public_hostname = cluster_public_hostname if cluster_public_hostname else public_hostname
  397. console_path = facts['master']['console_path']
  398. etcd_hosts = facts['master']['etcd_hosts']
  399. use_ssl = dict(
  400. api=facts['master']['api_use_ssl'],
  401. public_api=facts['master']['api_use_ssl'],
  402. loopback_api=facts['master']['api_use_ssl'],
  403. console=facts['master']['console_use_ssl'],
  404. public_console=facts['master']['console_use_ssl'],
  405. etcd=facts['master']['etcd_use_ssl']
  406. )
  407. ports = dict(
  408. api=facts['master']['api_port'],
  409. public_api=facts['master']['api_port'],
  410. loopback_api=facts['master']['api_port'],
  411. console=facts['master']['console_port'],
  412. public_console=facts['master']['console_port'],
  413. etcd=facts['master']['etcd_port'],
  414. )
  415. etcd_urls = []
  416. if etcd_hosts != '':
  417. facts['master']['etcd_port'] = ports['etcd']
  418. facts['master']['embedded_etcd'] = False
  419. for host in etcd_hosts:
  420. etcd_urls.append(format_url(use_ssl['etcd'], host,
  421. ports['etcd']))
  422. else:
  423. etcd_urls = [format_url(use_ssl['etcd'], hostname,
  424. ports['etcd'])]
  425. facts['master'].setdefault('etcd_urls', etcd_urls)
  426. prefix_hosts = [('api', api_hostname),
  427. ('public_api', api_public_hostname),
  428. ('loopback_api', hostname)]
  429. for prefix, host in prefix_hosts:
  430. facts['master'].setdefault(prefix + '_url', format_url(use_ssl[prefix],
  431. host,
  432. ports[prefix]))
  433. r_lhn = "{0}:{1}".format(hostname, ports['api']).replace('.', '-')
  434. r_lhu = "system:openshift-master/{0}:{1}".format(api_hostname, ports['api']).replace('.', '-')
  435. facts['master'].setdefault('loopback_cluster_name', r_lhn)
  436. facts['master'].setdefault('loopback_context_name', "default/{0}/system:openshift-master".format(r_lhn))
  437. facts['master'].setdefault('loopback_user', r_lhu)
  438. prefix_hosts = [('console', api_hostname), ('public_console', api_public_hostname)]
  439. for prefix, host in prefix_hosts:
  440. facts['master'].setdefault(prefix + '_url', format_url(use_ssl[prefix],
  441. host,
  442. ports[prefix],
  443. console_path))
  444. return facts
  445. def set_aggregate_facts(facts):
  446. """ Set aggregate facts
  447. Args:
  448. facts (dict): existing facts
  449. Returns:
  450. dict: the facts dict updated with aggregated facts
  451. """
  452. all_hostnames = set()
  453. internal_hostnames = set()
  454. if 'common' in facts:
  455. all_hostnames.add(facts['common']['hostname'])
  456. all_hostnames.add(facts['common']['public_hostname'])
  457. all_hostnames.add(facts['common']['ip'])
  458. all_hostnames.add(facts['common']['public_ip'])
  459. internal_hostnames.add(facts['common']['hostname'])
  460. internal_hostnames.add(facts['common']['ip'])
  461. cluster_domain = facts['common']['dns_domain']
  462. if 'master' in facts:
  463. if 'cluster_hostname' in facts['master']:
  464. all_hostnames.add(facts['master']['cluster_hostname'])
  465. if 'cluster_public_hostname' in facts['master']:
  466. all_hostnames.add(facts['master']['cluster_public_hostname'])
  467. svc_names = ['openshift', 'openshift.default', 'openshift.default.svc',
  468. 'openshift.default.svc.' + cluster_domain, 'kubernetes', 'kubernetes.default',
  469. 'kubernetes.default.svc', 'kubernetes.default.svc.' + cluster_domain]
  470. all_hostnames.update(svc_names)
  471. internal_hostnames.update(svc_names)
  472. first_svc_ip = first_ip(facts['master']['portal_net'])
  473. all_hostnames.add(first_svc_ip)
  474. internal_hostnames.add(first_svc_ip)
  475. facts['common']['all_hostnames'] = list(all_hostnames)
  476. facts['common']['internal_hostnames'] = list(internal_hostnames)
  477. return facts
  478. def set_etcd_facts_if_unset(facts):
  479. """
  480. If using embedded etcd, loads the data directory from master-config.yaml.
  481. If using standalone etcd, loads ETCD_DATA_DIR from etcd.conf.
  482. If anything goes wrong parsing these, the fact will not be set.
  483. """
  484. if 'master' in facts and facts['master']['embedded_etcd']:
  485. etcd_facts = facts['etcd'] if 'etcd' in facts else dict()
  486. if 'etcd_data_dir' not in etcd_facts:
  487. try:
  488. # Parse master config to find actual etcd data dir:
  489. master_cfg_path = os.path.join(facts['common']['config_base'],
  490. 'master/master-config.yaml')
  491. master_cfg_f = open(master_cfg_path, 'r')
  492. config = yaml.safe_load(master_cfg_f.read())
  493. master_cfg_f.close()
  494. etcd_facts['etcd_data_dir'] = \
  495. config['etcdConfig']['storageDirectory']
  496. facts['etcd'] = etcd_facts
  497. # We don't want exceptions bubbling up here:
  498. # pylint: disable=broad-except
  499. except Exception:
  500. pass
  501. else:
  502. etcd_facts = facts['etcd'] if 'etcd' in facts else dict()
  503. # Read ETCD_DATA_DIR from /etc/etcd/etcd.conf:
  504. try:
  505. # Add a fake section for parsing:
  506. ini_str = '[root]\n' + open('/etc/etcd/etcd.conf', 'r').read()
  507. ini_fp = StringIO.StringIO(ini_str)
  508. config = ConfigParser.RawConfigParser()
  509. config.readfp(ini_fp)
  510. etcd_data_dir = config.get('root', 'ETCD_DATA_DIR')
  511. if etcd_data_dir.startswith('"') and etcd_data_dir.endswith('"'):
  512. etcd_data_dir = etcd_data_dir[1:-1]
  513. etcd_facts['etcd_data_dir'] = etcd_data_dir
  514. facts['etcd'] = etcd_facts
  515. # We don't want exceptions bubbling up here:
  516. # pylint: disable=broad-except
  517. except Exception:
  518. pass
  519. return facts
  520. def set_deployment_facts_if_unset(facts):
  521. """ Set Facts that vary based on deployment_type. This currently
  522. includes common.service_type, common.config_base, master.registry_url,
  523. node.registry_url, node.storage_plugin_deps
  524. Args:
  525. facts (dict): existing facts
  526. Returns:
  527. dict: the facts dict updated with the generated deployment_type
  528. facts
  529. """
  530. # disabled to avoid breaking up facts related to deployment type into
  531. # multiple methods for now.
  532. # pylint: disable=too-many-statements, too-many-branches
  533. if 'common' in facts:
  534. deployment_type = facts['common']['deployment_type']
  535. if 'service_type' not in facts['common']:
  536. service_type = 'atomic-openshift'
  537. if deployment_type == 'origin':
  538. service_type = 'origin'
  539. elif deployment_type in ['enterprise']:
  540. service_type = 'openshift'
  541. facts['common']['service_type'] = service_type
  542. if 'config_base' not in facts['common']:
  543. config_base = '/etc/origin'
  544. if deployment_type in ['enterprise']:
  545. config_base = '/etc/openshift'
  546. # Handle upgrade scenarios when symlinks don't yet exist:
  547. if not os.path.exists(config_base) and os.path.exists('/etc/openshift'):
  548. config_base = '/etc/openshift'
  549. facts['common']['config_base'] = config_base
  550. if 'data_dir' not in facts['common']:
  551. data_dir = '/var/lib/origin'
  552. if deployment_type in ['enterprise']:
  553. data_dir = '/var/lib/openshift'
  554. # Handle upgrade scenarios when symlinks don't yet exist:
  555. if not os.path.exists(data_dir) and os.path.exists('/var/lib/openshift'):
  556. data_dir = '/var/lib/openshift'
  557. facts['common']['data_dir'] = data_dir
  558. # remove duplicate and empty strings from registry lists
  559. for cat in ['additional', 'blocked', 'insecure']:
  560. key = 'docker_{0}_registries'.format(cat)
  561. if key in facts['common']:
  562. facts['common'][key] = list(set(facts['common'][key]) - set(['']))
  563. if deployment_type in ['enterprise', 'atomic-enterprise', 'openshift-enterprise']:
  564. addtl_regs = facts['common'].get('docker_additional_registries', [])
  565. ent_reg = 'registry.access.redhat.com'
  566. if ent_reg not in addtl_regs:
  567. facts['common']['docker_additional_registries'] = addtl_regs + [ent_reg]
  568. for role in ('master', 'node'):
  569. if role in facts:
  570. deployment_type = facts['common']['deployment_type']
  571. if 'registry_url' not in facts[role]:
  572. registry_url = 'openshift/origin-${component}:${version}'
  573. if deployment_type in ['enterprise', 'online', 'openshift-enterprise']:
  574. registry_url = 'openshift3/ose-${component}:${version}'
  575. elif deployment_type == 'atomic-enterprise':
  576. registry_url = 'aep3_beta/aep-${component}:${version}'
  577. facts[role]['registry_url'] = registry_url
  578. if 'master' in facts:
  579. deployment_type = facts['common']['deployment_type']
  580. openshift_features = ['Builder', 'S2IBuilder', 'WebConsole']
  581. if 'disabled_features' in facts['master']:
  582. if deployment_type == 'atomic-enterprise':
  583. curr_disabled_features = set(facts['master']['disabled_features'])
  584. facts['master']['disabled_features'] = list(curr_disabled_features.union(openshift_features))
  585. else:
  586. if deployment_type == 'atomic-enterprise':
  587. facts['master']['disabled_features'] = openshift_features
  588. if 'node' in facts:
  589. deployment_type = facts['common']['deployment_type']
  590. if 'storage_plugin_deps' not in facts['node']:
  591. if deployment_type in ['openshift-enterprise', 'atomic-enterprise', 'origin']:
  592. facts['node']['storage_plugin_deps'] = ['ceph', 'glusterfs', 'iscsi']
  593. else:
  594. facts['node']['storage_plugin_deps'] = []
  595. return facts
  596. def set_version_facts_if_unset(facts):
  597. """ Set version facts. This currently includes common.version and
  598. common.version_gte_3_1_or_1_1.
  599. Args:
  600. facts (dict): existing facts
  601. Returns:
  602. dict: the facts dict updated with version facts.
  603. """
  604. if 'common' in facts:
  605. deployment_type = facts['common']['deployment_type']
  606. facts['common']['version'] = version = get_openshift_version()
  607. if version is not None:
  608. if deployment_type == 'origin':
  609. version_gte_3_1_or_1_1 = LooseVersion(version) >= LooseVersion('1.1.0')
  610. version_gte_3_1_1_or_1_1_1 = LooseVersion(version) >= LooseVersion('1.1.1')
  611. version_gte_3_2_or_1_2 = LooseVersion(version) >= LooseVersion('1.2.0')
  612. else:
  613. version_gte_3_1_or_1_1 = LooseVersion(version) >= LooseVersion('3.0.2.905')
  614. version_gte_3_1_1_or_1_1_1 = LooseVersion(version) >= LooseVersion('3.1.1')
  615. version_gte_3_2_or_1_2 = LooseVersion(version) >= LooseVersion('3.1.1.901')
  616. else:
  617. version_gte_3_1_or_1_1 = True
  618. version_gte_3_1_1_or_1_1_1 = True
  619. version_gte_3_2_or_1_2 = True
  620. facts['common']['version_gte_3_1_or_1_1'] = version_gte_3_1_or_1_1
  621. facts['common']['version_gte_3_1_1_or_1_1_1'] = version_gte_3_1_1_or_1_1_1
  622. facts['common']['version_gte_3_2_or_1_2'] = version_gte_3_2_or_1_2
  623. return facts
  624. def set_manageiq_facts_if_unset(facts):
  625. """ Set manageiq facts. This currently includes common.use_manageiq.
  626. Args:
  627. facts (dict): existing facts
  628. Returns:
  629. dict: the facts dict updated with version facts.
  630. Raises:
  631. OpenShiftFactsInternalError:
  632. """
  633. if 'common' not in facts:
  634. if 'version_gte_3_1_or_1_1' not in facts['common']:
  635. raise OpenShiftFactsInternalError(
  636. "Invalid invocation: The required facts are not set"
  637. )
  638. if 'use_manageiq' not in facts['common']:
  639. facts['common']['use_manageiq'] = facts['common']['version_gte_3_1_or_1_1']
  640. return facts
  641. def set_sdn_facts_if_unset(facts, system_facts):
  642. """ Set sdn facts if not already present in facts dict
  643. Args:
  644. facts (dict): existing facts
  645. system_facts (dict): ansible_facts
  646. Returns:
  647. dict: the facts dict updated with the generated sdn facts if they
  648. were not already present
  649. """
  650. if 'common' in facts:
  651. use_sdn = facts['common']['use_openshift_sdn']
  652. if not (use_sdn == '' or isinstance(use_sdn, bool)):
  653. use_sdn = bool(strtobool(str(use_sdn)))
  654. facts['common']['use_openshift_sdn'] = use_sdn
  655. if 'sdn_network_plugin_name' not in facts['common']:
  656. plugin = 'redhat/openshift-ovs-subnet' if use_sdn else ''
  657. facts['common']['sdn_network_plugin_name'] = plugin
  658. if 'master' in facts:
  659. if 'sdn_cluster_network_cidr' not in facts['master']:
  660. facts['master']['sdn_cluster_network_cidr'] = '10.1.0.0/16'
  661. if 'sdn_host_subnet_length' not in facts['master']:
  662. facts['master']['sdn_host_subnet_length'] = '8'
  663. if 'node' in facts and 'sdn_mtu' not in facts['node']:
  664. node_ip = facts['common']['ip']
  665. # default MTU if interface MTU cannot be detected
  666. facts['node']['sdn_mtu'] = '1450'
  667. for val in system_facts.itervalues():
  668. if isinstance(val, dict) and 'mtu' in val:
  669. mtu = val['mtu']
  670. if 'ipv4' in val and val['ipv4'].get('address') == node_ip:
  671. facts['node']['sdn_mtu'] = str(mtu - 50)
  672. return facts
  673. def format_url(use_ssl, hostname, port, path=''):
  674. """ Format url based on ssl flag, hostname, port and path
  675. Args:
  676. use_ssl (bool): is ssl enabled
  677. hostname (str): hostname
  678. port (str): port
  679. path (str): url path
  680. Returns:
  681. str: The generated url string
  682. """
  683. scheme = 'https' if use_ssl else 'http'
  684. netloc = hostname
  685. if (use_ssl and port != '443') or (not use_ssl and port != '80'):
  686. netloc += ":%s" % port
  687. return urlparse.urlunparse((scheme, netloc, path, '', '', ''))
  688. def get_current_config(facts):
  689. """ Get current openshift config
  690. Args:
  691. facts (dict): existing facts
  692. Returns:
  693. dict: the facts dict updated with the current openshift config
  694. """
  695. current_config = dict()
  696. roles = [role for role in facts if role not in ['common', 'provider']]
  697. for role in roles:
  698. if 'roles' in current_config:
  699. current_config['roles'].append(role)
  700. else:
  701. current_config['roles'] = [role]
  702. # TODO: parse the /etc/sysconfig/openshift-{master,node} config to
  703. # determine the location of files.
  704. # TODO: I suspect this isn't working right now, but it doesn't prevent
  705. # anything from working properly as far as I can tell, perhaps because
  706. # we override the kubeconfig path everywhere we use it?
  707. # Query kubeconfig settings
  708. kubeconfig_dir = '/var/lib/origin/openshift.local.certificates'
  709. if role == 'node':
  710. kubeconfig_dir = os.path.join(
  711. kubeconfig_dir, "node-%s" % facts['common']['hostname']
  712. )
  713. kubeconfig_path = os.path.join(kubeconfig_dir, '.kubeconfig')
  714. if (os.path.isfile('/usr/bin/openshift')
  715. and os.path.isfile(kubeconfig_path)):
  716. try:
  717. _, output, _ = module.run_command(
  718. ["/usr/bin/openshift", "ex", "config", "view", "-o",
  719. "json", "--kubeconfig=%s" % kubeconfig_path],
  720. check_rc=False
  721. )
  722. config = json.loads(output)
  723. cad = 'certificate-authority-data'
  724. try:
  725. for cluster in config['clusters']:
  726. config['clusters'][cluster][cad] = 'masked'
  727. except KeyError:
  728. pass
  729. try:
  730. for user in config['users']:
  731. config['users'][user][cad] = 'masked'
  732. config['users'][user]['client-key-data'] = 'masked'
  733. except KeyError:
  734. pass
  735. current_config['kubeconfig'] = config
  736. # override pylint broad-except warning, since we do not want
  737. # to bubble up any exceptions if oc config view
  738. # fails
  739. # pylint: disable=broad-except
  740. except Exception:
  741. pass
  742. return current_config
  743. def get_openshift_version():
  744. """ Get current version of openshift on the host
  745. Returns:
  746. version: the current openshift version
  747. """
  748. version = None
  749. if os.path.isfile('/usr/bin/openshift'):
  750. _, output, _ = module.run_command(['/usr/bin/openshift', 'version'])
  751. versions = dict(e.split(' v') for e in output.splitlines() if ' v' in e)
  752. version = versions.get('openshift', '')
  753. #TODO: acknowledge the possility of a containerized install
  754. return version
  755. def apply_provider_facts(facts, provider_facts):
  756. """ Apply provider facts to supplied facts dict
  757. Args:
  758. facts (dict): facts dict to update
  759. provider_facts (dict): provider facts to apply
  760. roles: host roles
  761. Returns:
  762. dict: the merged facts
  763. """
  764. if not provider_facts:
  765. return facts
  766. common_vars = [('hostname', 'ip'), ('public_hostname', 'public_ip')]
  767. for h_var, ip_var in common_vars:
  768. ip_value = provider_facts['network'].get(ip_var)
  769. if ip_value:
  770. facts['common'][ip_var] = ip_value
  771. facts['common'][h_var] = choose_hostname(
  772. [provider_facts['network'].get(h_var)],
  773. facts['common'][ip_var]
  774. )
  775. facts['provider'] = provider_facts
  776. return facts
  777. # Disabling pylint too many branches. This function needs refactored
  778. # but is a very core part of openshift_facts.
  779. # pylint: disable=too-many-branches
  780. def merge_facts(orig, new, additive_facts_to_overwrite, protected_facts_to_overwrite):
  781. """ Recursively merge facts dicts
  782. Args:
  783. orig (dict): existing facts
  784. new (dict): facts to update
  785. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  786. '.' notation ex: ['master.named_certificates']
  787. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  788. '.' notation ex: ['master.master_count']
  789. Returns:
  790. dict: the merged facts
  791. """
  792. additive_facts = ['named_certificates']
  793. protected_facts = ['ha', 'master_count']
  794. facts = dict()
  795. for key, value in orig.iteritems():
  796. # Key exists in both old and new facts.
  797. if key in new:
  798. # Continue to recurse if old and new fact is a dictionary.
  799. if isinstance(value, dict) and isinstance(new[key], dict):
  800. # Collect the subset of additive facts to overwrite if
  801. # key matches. These will be passed to the subsequent
  802. # merge_facts call.
  803. relevant_additive_facts = []
  804. for item in additive_facts_to_overwrite:
  805. if '.' in item and item.startswith(key + '.'):
  806. relevant_additive_facts.append(item)
  807. # Collect the subset of protected facts to overwrite
  808. # if key matches. These will be passed to the
  809. # subsequent merge_facts call.
  810. relevant_protected_facts = []
  811. for item in protected_facts_to_overwrite:
  812. if '.' in item and item.startswith(key + '.'):
  813. relevant_protected_facts.append(item)
  814. facts[key] = merge_facts(value, new[key], relevant_additive_facts, relevant_protected_facts)
  815. # Key matches an additive fact and we are not overwriting
  816. # it so we will append the new value to the existing value.
  817. elif key in additive_facts and key not in [x.split('.')[-1] for x in additive_facts_to_overwrite]:
  818. if isinstance(value, list) and isinstance(new[key], list):
  819. new_fact = []
  820. for item in copy.deepcopy(value) + copy.deepcopy(new[key]):
  821. if item not in new_fact:
  822. new_fact.append(item)
  823. facts[key] = new_fact
  824. # Key matches a protected fact and we are not overwriting
  825. # it so we will determine if it is okay to change this
  826. # fact.
  827. elif key in protected_facts and key not in [x.split('.')[-1] for x in protected_facts_to_overwrite]:
  828. # The master count (int) can only increase unless it
  829. # has been passed as a protected fact to overwrite.
  830. if key == 'master_count':
  831. if int(value) <= int(new[key]):
  832. facts[key] = copy.deepcopy(new[key])
  833. else:
  834. module.fail_json(msg='openshift_facts received a lower value for openshift.master.master_count')
  835. # ha (bool) can not change unless it has been passed
  836. # as a protected fact to overwrite.
  837. if key == 'ha':
  838. if bool(value) != bool(new[key]):
  839. module.fail_json(msg='openshift_facts received a different value for openshift.master.ha')
  840. else:
  841. facts[key] = value
  842. # No other condition has been met. Overwrite the old fact
  843. # with the new value.
  844. else:
  845. facts[key] = copy.deepcopy(new[key])
  846. # Key isn't in new so add it to facts to keep it.
  847. else:
  848. facts[key] = copy.deepcopy(value)
  849. new_keys = set(new.keys()) - set(orig.keys())
  850. for key in new_keys:
  851. facts[key] = copy.deepcopy(new[key])
  852. return facts
  853. def merge_provider_facts(facts):
  854. """ Recursively merge provider facts dicts
  855. Args:
  856. facts (dict): existing facts
  857. Returns:
  858. dict: the facts dict updated with the provider config
  859. """
  860. if 'provider' not in facts:
  861. return facts
  862. if 'master' in facts:
  863. for arg in ('api_server_args', 'controller_args'):
  864. facts['master'][arg] = merge_facts(
  865. facts['provider'].get(arg, {}),
  866. facts['master'].get(arg, {}),
  867. [], [])
  868. if 'node' in facts:
  869. facts['node']['kubelet_args'] = merge_facts(
  870. facts['provider'].get('kubelet_args', {}),
  871. facts['node'].get('kubelet_args', {}),
  872. [], [])
  873. return facts
  874. def save_local_facts(filename, facts):
  875. """ Save local facts
  876. Args:
  877. filename (str): local facts file
  878. facts (dict): facts to set
  879. """
  880. try:
  881. fact_dir = os.path.dirname(filename)
  882. if not os.path.exists(fact_dir):
  883. os.makedirs(fact_dir)
  884. with open(filename, 'w') as fact_file:
  885. fact_file.write(module.jsonify(facts))
  886. os.chmod(filename, 0o600)
  887. except (IOError, OSError) as ex:
  888. raise OpenShiftFactsFileWriteError(
  889. "Could not create fact file: %s, error: %s" % (filename, ex)
  890. )
  891. def get_local_facts_from_file(filename):
  892. """ Retrieve local facts from fact file
  893. Args:
  894. filename (str): local facts file
  895. Returns:
  896. dict: the retrieved facts
  897. """
  898. local_facts = dict()
  899. try:
  900. # Handle conversion of INI style facts file to json style
  901. ini_facts = ConfigParser.SafeConfigParser()
  902. ini_facts.read(filename)
  903. for section in ini_facts.sections():
  904. local_facts[section] = dict()
  905. for key, value in ini_facts.items(section):
  906. local_facts[section][key] = value
  907. except (ConfigParser.MissingSectionHeaderError,
  908. ConfigParser.ParsingError):
  909. try:
  910. with open(filename, 'r') as facts_file:
  911. local_facts = json.load(facts_file)
  912. except (ValueError, IOError):
  913. pass
  914. return local_facts
  915. def set_container_facts_if_unset(facts):
  916. """ Set containerized facts.
  917. Args:
  918. facts (dict): existing facts
  919. Returns:
  920. dict: the facts dict updated with the generated containerization
  921. facts
  922. """
  923. deployment_type = facts['common']['deployment_type']
  924. if deployment_type in ['enterprise', 'openshift-enterprise']:
  925. master_image = 'openshift3/ose'
  926. cli_image = master_image
  927. node_image = 'openshift3/node'
  928. ovs_image = 'openshift3/openvswitch'
  929. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  930. elif deployment_type == 'atomic-enterprise':
  931. master_image = 'aep3_beta/aep'
  932. cli_image = master_image
  933. node_image = 'aep3_beta/node'
  934. ovs_image = 'aep3_beta/openvswitch'
  935. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  936. else:
  937. master_image = 'openshift/origin'
  938. cli_image = master_image
  939. node_image = 'openshift/node'
  940. ovs_image = 'openshift/openvswitch'
  941. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  942. facts['common']['is_atomic'] = os.path.isfile('/run/ostree-booted')
  943. if 'is_containerized' not in facts['common']:
  944. facts['common']['is_containerized'] = facts['common']['is_atomic']
  945. if 'cli_image' not in facts['common']:
  946. facts['common']['cli_image'] = cli_image
  947. if 'etcd' in facts and 'etcd_image' not in facts['etcd']:
  948. facts['etcd']['etcd_image'] = etcd_image
  949. if 'master' in facts and 'master_image' not in facts['master']:
  950. facts['master']['master_image'] = master_image
  951. if 'node' in facts:
  952. if 'node_image' not in facts['node']:
  953. facts['node']['node_image'] = node_image
  954. if 'ovs_image' not in facts['node']:
  955. facts['node']['ovs_image'] = ovs_image
  956. if facts['common']['is_containerized']:
  957. facts['common']['admin_binary'] = '/usr/local/bin/oadm'
  958. facts['common']['client_binary'] = '/usr/local/bin/oc'
  959. return facts
  960. def set_installed_variant_rpm_facts(facts):
  961. """ Set RPM facts of installed variant
  962. Args:
  963. facts (dict): existing facts
  964. Returns:
  965. dict: the facts dict updated with installed_variant_rpms
  966. """
  967. installed_rpms = []
  968. for base_rpm in ['openshift', 'atomic-openshift', 'origin']:
  969. optional_rpms = ['master', 'node', 'clients', 'sdn-ovs']
  970. variant_rpms = [base_rpm] + \
  971. ['{0}-{1}'.format(base_rpm, r) for r in optional_rpms] + \
  972. ['tuned-profiles-%s-node' % base_rpm]
  973. for rpm in variant_rpms:
  974. exit_code, _, _ = module.run_command(['rpm', '-q', rpm])
  975. if exit_code == 0:
  976. installed_rpms.append(rpm)
  977. facts['common']['installed_variant_rpms'] = installed_rpms
  978. return facts
  979. class OpenShiftFactsInternalError(Exception):
  980. """Origin Facts Error"""
  981. pass
  982. class OpenShiftFactsUnsupportedRoleError(Exception):
  983. """Origin Facts Unsupported Role Error"""
  984. pass
  985. class OpenShiftFactsFileWriteError(Exception):
  986. """Origin Facts File Write Error"""
  987. pass
  988. class OpenShiftFactsMetadataUnavailableError(Exception):
  989. """Origin Facts Metadata Unavailable Error"""
  990. pass
  991. class OpenShiftFacts(object):
  992. """ Origin Facts
  993. Attributes:
  994. facts (dict): facts for the host
  995. Args:
  996. module (AnsibleModule): an AnsibleModule object
  997. role (str): role for setting local facts
  998. filename (str): local facts file to use
  999. local_facts (dict): local facts to set
  1000. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1001. '.' notation ex: ['master.named_certificates']
  1002. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1003. '.' notation ex: ['master.master_count']
  1004. Raises:
  1005. OpenShiftFactsUnsupportedRoleError:
  1006. """
  1007. known_roles = ['common', 'master', 'node', 'etcd', 'hosted']
  1008. # Disabling too-many-arguments, this should be cleaned up as a TODO item.
  1009. # pylint: disable=too-many-arguments
  1010. def __init__(self, role, filename, local_facts,
  1011. additive_facts_to_overwrite=None,
  1012. openshift_env=None,
  1013. protected_facts_to_overwrite=None):
  1014. self.changed = False
  1015. self.filename = filename
  1016. if role not in self.known_roles:
  1017. raise OpenShiftFactsUnsupportedRoleError(
  1018. "Role %s is not supported by this module" % role
  1019. )
  1020. self.role = role
  1021. self.system_facts = ansible_facts(module)
  1022. self.facts = self.generate_facts(local_facts,
  1023. additive_facts_to_overwrite,
  1024. openshift_env,
  1025. protected_facts_to_overwrite)
  1026. def generate_facts(self,
  1027. local_facts,
  1028. additive_facts_to_overwrite,
  1029. openshift_env,
  1030. protected_facts_to_overwrite):
  1031. """ Generate facts
  1032. Args:
  1033. local_facts (dict): local_facts for overriding generated defaults
  1034. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1035. '.' notation ex: ['master.named_certificates']
  1036. openshift_env (dict): openshift_env facts for overriding generated defaults
  1037. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1038. '.' notation ex: ['master.master_count']
  1039. Returns:
  1040. dict: The generated facts
  1041. """
  1042. local_facts = self.init_local_facts(local_facts,
  1043. additive_facts_to_overwrite,
  1044. openshift_env,
  1045. protected_facts_to_overwrite)
  1046. roles = local_facts.keys()
  1047. defaults = self.get_defaults(roles)
  1048. provider_facts = self.init_provider_facts()
  1049. facts = apply_provider_facts(defaults, provider_facts)
  1050. facts = merge_facts(facts,
  1051. local_facts,
  1052. additive_facts_to_overwrite,
  1053. protected_facts_to_overwrite)
  1054. facts = merge_provider_facts(facts)
  1055. facts['current_config'] = get_current_config(facts)
  1056. facts = set_url_facts_if_unset(facts)
  1057. facts = set_project_cfg_facts_if_unset(facts)
  1058. facts = set_flannel_facts_if_unset(facts)
  1059. facts = set_nuage_facts_if_unset(facts)
  1060. facts = set_node_schedulability(facts)
  1061. facts = set_master_selectors(facts)
  1062. facts = set_metrics_facts_if_unset(facts)
  1063. facts = set_identity_providers_if_unset(facts)
  1064. facts = set_sdn_facts_if_unset(facts, self.system_facts)
  1065. facts = set_deployment_facts_if_unset(facts)
  1066. facts = set_version_facts_if_unset(facts)
  1067. facts = set_manageiq_facts_if_unset(facts)
  1068. facts = set_aggregate_facts(facts)
  1069. facts = set_etcd_facts_if_unset(facts)
  1070. facts = set_container_facts_if_unset(facts)
  1071. if not facts['common']['is_containerized']:
  1072. facts = set_installed_variant_rpm_facts(facts)
  1073. return dict(openshift=facts)
  1074. def get_defaults(self, roles):
  1075. """ Get default fact values
  1076. Args:
  1077. roles (list): list of roles for this host
  1078. Returns:
  1079. dict: The generated default facts
  1080. """
  1081. defaults = dict()
  1082. ip_addr = self.system_facts['default_ipv4']['address']
  1083. exit_code, output, _ = module.run_command(['hostname', '-f'])
  1084. hostname_f = output.strip() if exit_code == 0 else ''
  1085. hostname_values = [hostname_f, self.system_facts['nodename'],
  1086. self.system_facts['fqdn']]
  1087. hostname = choose_hostname(hostname_values, ip_addr)
  1088. common = dict(use_openshift_sdn=True, ip=ip_addr, public_ip=ip_addr,
  1089. deployment_type='origin', hostname=hostname,
  1090. public_hostname=hostname)
  1091. common['client_binary'] = 'oc'
  1092. common['admin_binary'] = 'oadm'
  1093. common['dns_domain'] = 'cluster.local'
  1094. common['install_examples'] = True
  1095. defaults['common'] = common
  1096. if 'master' in roles:
  1097. master = dict(api_use_ssl=True, api_port='8443', controllers_port='8444',
  1098. console_use_ssl=True, console_path='/console',
  1099. console_port='8443', etcd_use_ssl=True, etcd_hosts='',
  1100. etcd_port='4001', portal_net='172.30.0.0/16',
  1101. embedded_etcd=True, embedded_kube=True,
  1102. embedded_dns=True, dns_port='53',
  1103. bind_addr='0.0.0.0', session_max_seconds=3600,
  1104. session_name='ssn', session_secrets_file='',
  1105. access_token_max_seconds=86400,
  1106. auth_token_max_seconds=500,
  1107. oauth_grant_method='auto')
  1108. defaults['master'] = master
  1109. if 'node' in roles:
  1110. node = dict(labels={}, annotations={}, portal_net='172.30.0.0/16',
  1111. iptables_sync_period='5s', set_node_ip=False)
  1112. defaults['node'] = node
  1113. defaults['hosted'] = dict(
  1114. registry=dict(
  1115. storage=dict(
  1116. kind=None,
  1117. volume=dict(
  1118. name='registry',
  1119. size='5Gi'
  1120. ),
  1121. nfs=dict(
  1122. directory='/exports',
  1123. options='*(rw,root_squash)'),
  1124. host=None,
  1125. access_modes=['ReadWriteMany'],
  1126. create_pv=True
  1127. )
  1128. )
  1129. )
  1130. return defaults
  1131. def guess_host_provider(self):
  1132. """ Guess the host provider
  1133. Returns:
  1134. dict: The generated default facts for the detected provider
  1135. """
  1136. # TODO: cloud provider facts should probably be submitted upstream
  1137. product_name = self.system_facts['product_name']
  1138. product_version = self.system_facts['product_version']
  1139. virt_type = self.system_facts['virtualization_type']
  1140. virt_role = self.system_facts['virtualization_role']
  1141. provider = None
  1142. metadata = None
  1143. # TODO: this is not exposed through module_utils/facts.py in ansible,
  1144. # need to create PR for ansible to expose it
  1145. bios_vendor = get_file_content(
  1146. '/sys/devices/virtual/dmi/id/bios_vendor'
  1147. )
  1148. if bios_vendor == 'Google':
  1149. provider = 'gce'
  1150. metadata_url = ('http://metadata.google.internal/'
  1151. 'computeMetadata/v1/?recursive=true')
  1152. headers = {'Metadata-Flavor': 'Google'}
  1153. metadata = get_provider_metadata(metadata_url, True, headers,
  1154. True)
  1155. # Filter sshKeys and serviceAccounts from gce metadata
  1156. if metadata:
  1157. metadata['project']['attributes'].pop('sshKeys', None)
  1158. metadata['instance'].pop('serviceAccounts', None)
  1159. elif (virt_type == 'xen' and virt_role == 'guest'
  1160. and re.match(r'.*\.amazon$', product_version)):
  1161. provider = 'ec2'
  1162. metadata_url = 'http://169.254.169.254/latest/meta-data/'
  1163. metadata = get_provider_metadata(metadata_url)
  1164. elif re.search(r'OpenStack', product_name):
  1165. provider = 'openstack'
  1166. metadata_url = ('http://169.254.169.254/openstack/latest/'
  1167. 'meta_data.json')
  1168. metadata = get_provider_metadata(metadata_url, True, None,
  1169. True)
  1170. if metadata:
  1171. ec2_compat_url = 'http://169.254.169.254/latest/meta-data/'
  1172. metadata['ec2_compat'] = get_provider_metadata(
  1173. ec2_compat_url
  1174. )
  1175. # disable pylint maybe-no-member because overloaded use of
  1176. # the module name causes pylint to not detect that results
  1177. # is an array or hash
  1178. # pylint: disable=maybe-no-member
  1179. # Filter public_keys and random_seed from openstack metadata
  1180. metadata.pop('public_keys', None)
  1181. metadata.pop('random_seed', None)
  1182. if not metadata['ec2_compat']:
  1183. metadata = None
  1184. return dict(name=provider, metadata=metadata)
  1185. def init_provider_facts(self):
  1186. """ Initialize the provider facts
  1187. Returns:
  1188. dict: The normalized provider facts
  1189. """
  1190. provider_info = self.guess_host_provider()
  1191. provider_facts = normalize_provider_facts(
  1192. provider_info.get('name'),
  1193. provider_info.get('metadata')
  1194. )
  1195. return provider_facts
  1196. # Disabling too-many-branches. This should be cleaned up as a TODO item.
  1197. #pylint: disable=too-many-branches
  1198. def init_local_facts(self, facts=None,
  1199. additive_facts_to_overwrite=None,
  1200. openshift_env=None,
  1201. protected_facts_to_overwrite=None):
  1202. """ Initialize the local facts
  1203. Args:
  1204. facts (dict): local facts to set
  1205. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1206. '.' notation ex: ['master.named_certificates']
  1207. openshift_env (dict): openshift env facts to set
  1208. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1209. '.' notation ex: ['master.master_count']
  1210. Returns:
  1211. dict: The result of merging the provided facts with existing
  1212. local facts
  1213. """
  1214. changed = False
  1215. facts_to_set = dict()
  1216. if facts is not None:
  1217. facts_to_set[self.role] = facts
  1218. if openshift_env != {} and openshift_env != None:
  1219. for fact, value in openshift_env.iteritems():
  1220. oo_env_facts = dict()
  1221. current_level = oo_env_facts
  1222. keys = fact.split('_')[1:]
  1223. if keys[0] != self.role:
  1224. continue
  1225. for key in keys:
  1226. if key == keys[-1]:
  1227. current_level[key] = value
  1228. elif key not in current_level:
  1229. current_level[key] = dict()
  1230. current_level = current_level[key]
  1231. facts_to_set = merge_facts(orig=facts_to_set,
  1232. new=oo_env_facts,
  1233. additive_facts_to_overwrite=[],
  1234. protected_facts_to_overwrite=[])
  1235. local_facts = get_local_facts_from_file(self.filename)
  1236. for arg in ['labels', 'annotations']:
  1237. if arg in facts_to_set and isinstance(facts_to_set[arg],
  1238. basestring):
  1239. facts_to_set[arg] = module.from_json(facts_to_set[arg])
  1240. new_local_facts = merge_facts(local_facts,
  1241. facts_to_set,
  1242. additive_facts_to_overwrite,
  1243. protected_facts_to_overwrite)
  1244. for facts in new_local_facts.values():
  1245. keys_to_delete = []
  1246. if isinstance(facts, dict):
  1247. for fact, value in facts.iteritems():
  1248. if value == "" or value is None:
  1249. keys_to_delete.append(fact)
  1250. for key in keys_to_delete:
  1251. del facts[key]
  1252. if new_local_facts != local_facts:
  1253. self.validate_local_facts(new_local_facts)
  1254. changed = True
  1255. if not module.check_mode:
  1256. save_local_facts(self.filename, new_local_facts)
  1257. self.changed = changed
  1258. return new_local_facts
  1259. def validate_local_facts(self, facts=None):
  1260. """ Validate local facts
  1261. Args:
  1262. facts (dict): local facts to validate
  1263. """
  1264. invalid_facts = dict()
  1265. invalid_facts = self.validate_master_facts(facts, invalid_facts)
  1266. if invalid_facts:
  1267. msg = 'Invalid facts detected:\n'
  1268. for key in invalid_facts.keys():
  1269. msg += '{0}: {1}\n'.format(key, invalid_facts[key])
  1270. module.fail_json(msg=msg,
  1271. changed=self.changed)
  1272. # disabling pylint errors for line-too-long since we're dealing
  1273. # with best effort reduction of error messages here.
  1274. # disabling errors for too-many-branches since we require checking
  1275. # many conditions.
  1276. # pylint: disable=line-too-long, too-many-branches
  1277. @staticmethod
  1278. def validate_master_facts(facts, invalid_facts):
  1279. """ Validate master facts
  1280. Args:
  1281. facts (dict): local facts to validate
  1282. invalid_facts (dict): collected invalid_facts
  1283. Returns:
  1284. dict: Invalid facts
  1285. """
  1286. if 'master' in facts:
  1287. # openshift.master.session_auth_secrets
  1288. if 'session_auth_secrets' in facts['master']:
  1289. session_auth_secrets = facts['master']['session_auth_secrets']
  1290. if not issubclass(type(session_auth_secrets), list):
  1291. invalid_facts['session_auth_secrets'] = 'Expects session_auth_secrets is a list.'
  1292. elif 'session_encryption_secrets' not in facts['master']:
  1293. invalid_facts['session_auth_secrets'] = ('openshift_master_session_encryption secrets must be set '
  1294. 'if openshift_master_session_auth_secrets is provided.')
  1295. elif len(session_auth_secrets) != len(facts['master']['session_encryption_secrets']):
  1296. invalid_facts['session_auth_secrets'] = ('openshift_master_session_auth_secrets and '
  1297. 'openshift_master_session_encryption_secrets must be '
  1298. 'equal length.')
  1299. else:
  1300. for secret in session_auth_secrets:
  1301. if len(secret) < 32:
  1302. invalid_facts['session_auth_secrets'] = ('Invalid secret in session_auth_secrets. '
  1303. 'Secrets must be at least 32 characters in length.')
  1304. # openshift.master.session_encryption_secrets
  1305. if 'session_encryption_secrets' in facts['master']:
  1306. session_encryption_secrets = facts['master']['session_encryption_secrets']
  1307. if not issubclass(type(session_encryption_secrets), list):
  1308. invalid_facts['session_encryption_secrets'] = 'Expects session_encryption_secrets is a list.'
  1309. elif 'session_auth_secrets' not in facts['master']:
  1310. invalid_facts['session_encryption_secrets'] = ('openshift_master_session_auth_secrets must be '
  1311. 'set if openshift_master_session_encryption_secrets '
  1312. 'is provided.')
  1313. else:
  1314. for secret in session_encryption_secrets:
  1315. if len(secret) not in [16, 24, 32]:
  1316. invalid_facts['session_encryption_secrets'] = ('Invalid secret in session_encryption_secrets. '
  1317. 'Secrets must be 16, 24, or 32 characters in length.')
  1318. return invalid_facts
  1319. def main():
  1320. """ main """
  1321. # disabling pylint errors for global-variable-undefined and invalid-name
  1322. # for 'global module' usage, since it is required to use ansible_facts
  1323. # pylint: disable=global-variable-undefined, invalid-name
  1324. global module
  1325. module = AnsibleModule(
  1326. argument_spec=dict(
  1327. role=dict(default='common', required=False,
  1328. choices=OpenShiftFacts.known_roles),
  1329. local_facts=dict(default=None, type='dict', required=False),
  1330. additive_facts_to_overwrite=dict(default=[], type='list', required=False),
  1331. openshift_env=dict(default={}, type='dict', required=False),
  1332. protected_facts_to_overwrite=dict(default=[], type='list', required=False),
  1333. ),
  1334. supports_check_mode=True,
  1335. add_file_common_args=True,
  1336. )
  1337. role = module.params['role']
  1338. local_facts = module.params['local_facts']
  1339. additive_facts_to_overwrite = module.params['additive_facts_to_overwrite']
  1340. openshift_env = module.params['openshift_env']
  1341. protected_facts_to_overwrite = module.params['protected_facts_to_overwrite']
  1342. fact_file = '/etc/ansible/facts.d/openshift.fact'
  1343. openshift_facts = OpenShiftFacts(role,
  1344. fact_file,
  1345. local_facts,
  1346. additive_facts_to_overwrite,
  1347. openshift_env,
  1348. protected_facts_to_overwrite)
  1349. file_params = module.params.copy()
  1350. file_params['path'] = fact_file
  1351. file_args = module.load_file_common_arguments(file_params)
  1352. changed = module.set_fs_attributes_if_different(file_args,
  1353. openshift_facts.changed)
  1354. return module.exit_json(changed=changed,
  1355. ansible_facts=openshift_facts.facts)
  1356. # ignore pylint errors related to the module_utils import
  1357. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
  1358. # import module snippets
  1359. from ansible.module_utils.basic import *
  1360. from ansible.module_utils.facts import *
  1361. from ansible.module_utils.urls import *
  1362. if __name__ == '__main__':
  1363. main()