openshift_facts.py 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299
  1. #!/usr/bin/python
  2. # pylint: disable=too-many-lines
  3. # -*- coding: utf-8 -*-
  4. # Reason: Disable pylint too-many-lines because we don't want to split up this file.
  5. # Status: Permanently disabled to keep this module as self-contained as possible.
  6. """Ansible module for retrieving and setting openshift related facts"""
  7. # pylint: disable=no-name-in-module, import-error, wrong-import-order
  8. import copy
  9. import errno
  10. import json
  11. import re
  12. import io
  13. import os
  14. import yaml
  15. import struct
  16. import socket
  17. from distutils.util import strtobool
  18. from distutils.version import LooseVersion
  19. from ansible.module_utils.six import string_types, text_type
  20. from ansible.module_utils.six.moves import configparser
  21. # ignore pylint errors related to the module_utils import
  22. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
  23. # import module snippets
  24. from ansible.module_utils.basic import * # noqa: F403
  25. from ansible.module_utils.facts import * # noqa: F403
  26. from ansible.module_utils.urls import * # noqa: F403
  27. from ansible.module_utils.six import iteritems, itervalues
  28. from ansible.module_utils.six.moves.urllib.parse import urlparse, urlunparse
  29. from ansible.module_utils._text import to_native
  30. HAVE_DBUS = False
  31. try:
  32. from dbus import SystemBus, Interface
  33. from dbus.exceptions import DBusException
  34. HAVE_DBUS = True
  35. except ImportError:
  36. pass
  37. DOCUMENTATION = '''
  38. ---
  39. module: openshift_facts
  40. short_description: Cluster Facts
  41. author: Jason DeTiberus
  42. requirements: [ ]
  43. '''
  44. EXAMPLES = '''
  45. '''
  46. # TODO: We should add a generic migration function that takes source and destination
  47. # paths and does the right thing rather than one function for common, one for node, etc.
  48. def migrate_common_facts(facts):
  49. """ Migrate facts from various roles into common """
  50. params = {
  51. 'node': ('portal_net'),
  52. 'master': ('portal_net')
  53. }
  54. if 'common' not in facts:
  55. facts['common'] = {}
  56. # pylint: disable=consider-iterating-dictionary
  57. for role in params.keys():
  58. if role in facts:
  59. for param in params[role]:
  60. if param in facts[role]:
  61. facts['common'][param] = facts[role].pop(param)
  62. return facts
  63. def migrate_node_facts(facts):
  64. """ Migrate facts from various roles into node """
  65. params = {
  66. 'common': ('dns_ip'),
  67. }
  68. if 'node' not in facts:
  69. facts['node'] = {}
  70. # pylint: disable=consider-iterating-dictionary
  71. for role in params.keys():
  72. if role in facts:
  73. for param in params[role]:
  74. if param in facts[role]:
  75. facts['node'][param] = facts[role].pop(param)
  76. return facts
  77. def migrate_hosted_facts(facts):
  78. """ Apply migrations for master facts """
  79. if 'master' in facts:
  80. if 'router_selector' in facts['master']:
  81. if 'hosted' not in facts:
  82. facts['hosted'] = {}
  83. if 'router' not in facts['hosted']:
  84. facts['hosted']['router'] = {}
  85. facts['hosted']['router']['selector'] = facts['master'].pop('router_selector')
  86. if 'registry_selector' in facts['master']:
  87. if 'hosted' not in facts:
  88. facts['hosted'] = {}
  89. if 'registry' not in facts['hosted']:
  90. facts['hosted']['registry'] = {}
  91. facts['hosted']['registry']['selector'] = facts['master'].pop('registry_selector')
  92. return facts
  93. def migrate_admission_plugin_facts(facts):
  94. """ Apply migrations for admission plugin facts """
  95. if 'master' in facts:
  96. if 'kube_admission_plugin_config' in facts['master']:
  97. if 'admission_plugin_config' not in facts['master']:
  98. facts['master']['admission_plugin_config'] = dict()
  99. # Merge existing kube_admission_plugin_config with admission_plugin_config.
  100. facts['master']['admission_plugin_config'] = merge_facts(facts['master']['admission_plugin_config'],
  101. facts['master']['kube_admission_plugin_config'],
  102. additive_facts_to_overwrite=[],
  103. protected_facts_to_overwrite=[])
  104. # Remove kube_admission_plugin_config fact
  105. facts['master'].pop('kube_admission_plugin_config', None)
  106. return facts
  107. def migrate_local_facts(facts):
  108. """ Apply migrations of local facts """
  109. migrated_facts = copy.deepcopy(facts)
  110. migrated_facts = migrate_common_facts(migrated_facts)
  111. migrated_facts = migrate_node_facts(migrated_facts)
  112. migrated_facts = migrate_hosted_facts(migrated_facts)
  113. migrated_facts = migrate_admission_plugin_facts(migrated_facts)
  114. return migrated_facts
  115. def first_ip(network):
  116. """ Return the first IPv4 address in network
  117. Args:
  118. network (str): network in CIDR format
  119. Returns:
  120. str: first IPv4 address
  121. """
  122. atoi = lambda addr: struct.unpack("!I", socket.inet_aton(addr))[0] # noqa: E731
  123. itoa = lambda addr: socket.inet_ntoa(struct.pack("!I", addr)) # noqa: E731
  124. (address, netmask) = network.split('/')
  125. netmask_i = (0xffffffff << (32 - atoi(netmask))) & 0xffffffff
  126. return itoa((atoi(address) & netmask_i) + 1)
  127. def hostname_valid(hostname):
  128. """ Test if specified hostname should be considered valid
  129. Args:
  130. hostname (str): hostname to test
  131. Returns:
  132. bool: True if valid, otherwise False
  133. """
  134. if (not hostname or
  135. hostname.startswith('localhost') or
  136. hostname.endswith('localdomain') or
  137. # OpenShift will not allow a node with more than 63 chars in name.
  138. len(hostname) > 63):
  139. return False
  140. return True
  141. def choose_hostname(hostnames=None, fallback=''):
  142. """ Choose a hostname from the provided hostnames
  143. Given a list of hostnames and a fallback value, choose a hostname to
  144. use. This function will prefer fqdns if they exist (excluding any that
  145. begin with localhost or end with localdomain) over ip addresses.
  146. Args:
  147. hostnames (list): list of hostnames
  148. fallback (str): default value to set if hostnames does not contain
  149. a valid hostname
  150. Returns:
  151. str: chosen hostname
  152. """
  153. hostname = fallback
  154. if hostnames is None:
  155. return hostname
  156. ip_regex = r'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z'
  157. ips = [i for i in hostnames if i is not None and isinstance(i, string_types) and re.match(ip_regex, i)]
  158. hosts = [i for i in hostnames if i is not None and i != '' and i not in ips]
  159. for host_list in (hosts, ips):
  160. for host in host_list:
  161. if hostname_valid(host):
  162. return host
  163. return hostname
  164. def query_metadata(metadata_url, headers=None, expect_json=False):
  165. """ Return metadata from the provided metadata_url
  166. Args:
  167. metadata_url (str): metadata url
  168. headers (dict): headers to set for metadata request
  169. expect_json (bool): does the metadata_url return json
  170. Returns:
  171. dict or list: metadata request result
  172. """
  173. result, info = fetch_url(module, metadata_url, headers=headers) # noqa: F405
  174. if info['status'] != 200:
  175. raise OpenShiftFactsMetadataUnavailableError("Metadata unavailable")
  176. if expect_json:
  177. return module.from_json(to_native(result.read())) # noqa: F405
  178. else:
  179. return [to_native(line.strip()) for line in result.readlines()]
  180. def walk_metadata(metadata_url, headers=None, expect_json=False):
  181. """ Walk the metadata tree and return a dictionary of the entire tree
  182. Args:
  183. metadata_url (str): metadata url
  184. headers (dict): headers to set for metadata request
  185. expect_json (bool): does the metadata_url return json
  186. Returns:
  187. dict: the result of walking the metadata tree
  188. """
  189. metadata = dict()
  190. for line in query_metadata(metadata_url, headers, expect_json):
  191. if line.endswith('/') and not line == 'public-keys/':
  192. key = line[:-1]
  193. metadata[key] = walk_metadata(metadata_url + line,
  194. headers, expect_json)
  195. else:
  196. results = query_metadata(metadata_url + line, headers,
  197. expect_json)
  198. if len(results) == 1:
  199. # disable pylint maybe-no-member because overloaded use of
  200. # the module name causes pylint to not detect that results
  201. # is an array or hash
  202. # pylint: disable=maybe-no-member
  203. metadata[line] = results.pop()
  204. else:
  205. metadata[line] = results
  206. return metadata
  207. def get_provider_metadata(metadata_url, supports_recursive=False,
  208. headers=None, expect_json=False):
  209. """ Retrieve the provider metadata
  210. Args:
  211. metadata_url (str): metadata url
  212. supports_recursive (bool): does the provider metadata api support
  213. recursion
  214. headers (dict): headers to set for metadata request
  215. expect_json (bool): does the metadata_url return json
  216. Returns:
  217. dict: the provider metadata
  218. """
  219. try:
  220. if supports_recursive:
  221. metadata = query_metadata(metadata_url, headers,
  222. expect_json)
  223. else:
  224. metadata = walk_metadata(metadata_url, headers,
  225. expect_json)
  226. except OpenShiftFactsMetadataUnavailableError:
  227. metadata = None
  228. return metadata
  229. def normalize_gce_facts(metadata, facts):
  230. """ Normalize gce facts
  231. Args:
  232. metadata (dict): provider metadata
  233. facts (dict): facts to update
  234. Returns:
  235. dict: the result of adding the normalized metadata to the provided
  236. facts dict
  237. """
  238. for interface in metadata['instance']['networkInterfaces']:
  239. int_info = dict(ips=[interface['ip']], network_type='gce')
  240. int_info['public_ips'] = [ac['externalIp'] for ac
  241. in interface['accessConfigs']]
  242. int_info['public_ips'].extend(interface['forwardedIps'])
  243. _, _, network_id = interface['network'].rpartition('/')
  244. int_info['network_id'] = network_id
  245. facts['network']['interfaces'].append(int_info)
  246. _, _, zone = metadata['instance']['zone'].rpartition('/')
  247. facts['zone'] = zone
  248. # GCE currently only supports a single interface
  249. facts['network']['ip'] = facts['network']['interfaces'][0]['ips'][0]
  250. pub_ip = facts['network']['interfaces'][0]['public_ips'][0]
  251. facts['network']['public_ip'] = pub_ip
  252. # Split instance hostname from GCE metadata to use the short instance name
  253. facts['network']['hostname'] = metadata['instance']['hostname'].split('.')[0]
  254. # TODO: attempt to resolve public_hostname
  255. facts['network']['public_hostname'] = facts['network']['public_ip']
  256. return facts
  257. def normalize_aws_facts(metadata, facts):
  258. """ Normalize aws facts
  259. Args:
  260. metadata (dict): provider metadata
  261. facts (dict): facts to update
  262. Returns:
  263. dict: the result of adding the normalized metadata to the provided
  264. facts dict
  265. """
  266. for interface in sorted(
  267. metadata['network']['interfaces']['macs'].values(),
  268. key=lambda x: x['device-number']
  269. ):
  270. int_info = dict()
  271. var_map = {'ips': 'local-ipv4s', 'public_ips': 'public-ipv4s'}
  272. for ips_var, int_var in iteritems(var_map):
  273. ips = interface.get(int_var)
  274. if isinstance(ips, string_types):
  275. int_info[ips_var] = [ips]
  276. else:
  277. int_info[ips_var] = ips
  278. if 'vpc-id' in interface:
  279. int_info['network_type'] = 'vpc'
  280. else:
  281. int_info['network_type'] = 'classic'
  282. if int_info['network_type'] == 'vpc':
  283. int_info['network_id'] = interface['subnet-id']
  284. else:
  285. int_info['network_id'] = None
  286. facts['network']['interfaces'].append(int_info)
  287. facts['zone'] = metadata['placement']['availability-zone']
  288. # TODO: actually attempt to determine default local and public ips
  289. # by using the ansible default ip fact and the ipv4-associations
  290. # from the ec2 metadata
  291. facts['network']['ip'] = metadata.get('local-ipv4')
  292. facts['network']['public_ip'] = metadata.get('public-ipv4')
  293. # TODO: verify that local hostname makes sense and is resolvable
  294. facts['network']['hostname'] = metadata.get('local-hostname')
  295. # TODO: verify that public hostname makes sense and is resolvable
  296. facts['network']['public_hostname'] = metadata.get('public-hostname')
  297. return facts
  298. def normalize_openstack_facts(metadata, facts):
  299. """ Normalize openstack facts
  300. Args:
  301. metadata (dict): provider metadata
  302. facts (dict): facts to update
  303. Returns:
  304. dict: the result of adding the normalized metadata to the provided
  305. facts dict
  306. """
  307. # openstack ec2 compat api does not support network interfaces and
  308. # the version tested on did not include the info in the openstack
  309. # metadata api, should be updated if neutron exposes this.
  310. facts['zone'] = metadata['availability_zone']
  311. local_ipv4 = metadata['ec2_compat']['local-ipv4'].split(',')[0]
  312. facts['network']['ip'] = local_ipv4
  313. facts['network']['public_ip'] = metadata['ec2_compat']['public-ipv4']
  314. for f_var, h_var, ip_var in [('hostname', 'hostname', 'local-ipv4'),
  315. ('public_hostname', 'public-hostname', 'public-ipv4')]:
  316. try:
  317. if socket.gethostbyname(metadata['ec2_compat'][h_var]) == metadata['ec2_compat'][ip_var]:
  318. facts['network'][f_var] = metadata['ec2_compat'][h_var]
  319. else:
  320. facts['network'][f_var] = metadata['ec2_compat'][ip_var]
  321. except socket.gaierror:
  322. facts['network'][f_var] = metadata['ec2_compat'][ip_var]
  323. return facts
  324. def normalize_provider_facts(provider, metadata):
  325. """ Normalize provider facts
  326. Args:
  327. provider (str): host provider
  328. metadata (dict): provider metadata
  329. Returns:
  330. dict: the normalized provider facts
  331. """
  332. if provider is None or metadata is None:
  333. return {}
  334. # TODO: test for ipv6_enabled where possible (gce, aws do not support)
  335. # and configure ipv6 facts if available
  336. # TODO: add support for setting user_data if available
  337. facts = dict(name=provider, metadata=metadata,
  338. network=dict(interfaces=[], ipv6_enabled=False))
  339. if provider == 'gce':
  340. facts = normalize_gce_facts(metadata, facts)
  341. elif provider == 'aws':
  342. facts = normalize_aws_facts(metadata, facts)
  343. elif provider == 'openstack':
  344. facts = normalize_openstack_facts(metadata, facts)
  345. return facts
  346. # pylint: disable=too-many-branches
  347. def set_selectors(facts):
  348. """ Set selectors facts if not already present in facts dict
  349. Args:
  350. facts (dict): existing facts
  351. Returns:
  352. dict: the facts dict updated with the generated selectors
  353. facts if they were not already present
  354. """
  355. selector = "region=infra"
  356. if 'hosted' not in facts:
  357. facts['hosted'] = {}
  358. if 'router' not in facts['hosted']:
  359. facts['hosted']['router'] = {}
  360. if 'selector' not in facts['hosted']['router'] or facts['hosted']['router']['selector'] in [None, 'None']:
  361. facts['hosted']['router']['selector'] = selector
  362. if 'registry' not in facts['hosted']:
  363. facts['hosted']['registry'] = {}
  364. if 'selector' not in facts['hosted']['registry'] or facts['hosted']['registry']['selector'] in [None, 'None']:
  365. facts['hosted']['registry']['selector'] = selector
  366. if 'metrics' not in facts['hosted']:
  367. facts['hosted']['metrics'] = {}
  368. if 'selector' not in facts['hosted']['metrics'] or facts['hosted']['metrics']['selector'] in [None, 'None']:
  369. facts['hosted']['metrics']['selector'] = None
  370. if 'logging' not in facts or not isinstance(facts['logging'], dict):
  371. facts['logging'] = {}
  372. if 'selector' not in facts['logging'] or facts['logging']['selector'] in [None, 'None']:
  373. facts['logging']['selector'] = None
  374. if 'etcd' not in facts['hosted']:
  375. facts['hosted']['etcd'] = {}
  376. if 'selector' not in facts['hosted']['etcd'] or facts['hosted']['etcd']['selector'] in [None, 'None']:
  377. facts['hosted']['etcd']['selector'] = None
  378. if 'prometheus' not in facts:
  379. facts['prometheus'] = {}
  380. if 'selector' not in facts['prometheus'] or facts['prometheus']['selector'] in [None, 'None']:
  381. facts['prometheus']['selector'] = None
  382. if 'alertmanager' not in facts['prometheus']:
  383. facts['prometheus']['alertmanager'] = {}
  384. # pylint: disable=line-too-long
  385. if 'selector' not in facts['prometheus']['alertmanager'] or facts['prometheus']['alertmanager']['selector'] in [None, 'None']:
  386. facts['prometheus']['alertmanager']['selector'] = None
  387. if 'alertbuffer' not in facts['prometheus']:
  388. facts['prometheus']['alertbuffer'] = {}
  389. # pylint: disable=line-too-long
  390. if 'selector' not in facts['prometheus']['alertbuffer'] or facts['prometheus']['alertbuffer']['selector'] in [None, 'None']:
  391. facts['prometheus']['alertbuffer']['selector'] = None
  392. return facts
  393. def set_identity_providers_if_unset(facts):
  394. """ Set identity_providers fact if not already present in facts dict
  395. Args:
  396. facts (dict): existing facts
  397. Returns:
  398. dict: the facts dict updated with the generated identity providers
  399. facts if they were not already present
  400. """
  401. if 'master' in facts:
  402. deployment_type = facts['common']['deployment_type']
  403. if 'identity_providers' not in facts['master']:
  404. identity_provider = dict(
  405. name='allow_all', challenge=True, login=True,
  406. kind='AllowAllPasswordIdentityProvider'
  407. )
  408. if deployment_type == 'openshift-enterprise':
  409. identity_provider = dict(
  410. name='deny_all', challenge=True, login=True,
  411. kind='DenyAllPasswordIdentityProvider'
  412. )
  413. facts['master']['identity_providers'] = [identity_provider]
  414. return facts
  415. def set_url_facts_if_unset(facts):
  416. """ Set url facts if not already present in facts dict
  417. Args:
  418. facts (dict): existing facts
  419. Returns:
  420. dict: the facts dict updated with the generated url facts if they
  421. were not already present
  422. """
  423. if 'master' in facts:
  424. hostname = facts['common']['hostname']
  425. cluster_hostname = facts['master'].get('cluster_hostname')
  426. cluster_public_hostname = facts['master'].get('cluster_public_hostname')
  427. public_hostname = facts['common']['public_hostname']
  428. api_hostname = cluster_hostname if cluster_hostname else hostname
  429. api_public_hostname = cluster_public_hostname if cluster_public_hostname else public_hostname
  430. console_path = facts['master']['console_path']
  431. etcd_hosts = facts['master']['etcd_hosts']
  432. use_ssl = dict(
  433. api=facts['master']['api_use_ssl'],
  434. public_api=facts['master']['api_use_ssl'],
  435. loopback_api=facts['master']['api_use_ssl'],
  436. console=facts['master']['console_use_ssl'],
  437. public_console=facts['master']['console_use_ssl'],
  438. etcd=facts['master']['etcd_use_ssl']
  439. )
  440. ports = dict(
  441. api=facts['master']['api_port'],
  442. public_api=facts['master']['api_port'],
  443. loopback_api=facts['master']['api_port'],
  444. console=facts['master']['console_port'],
  445. public_console=facts['master']['console_port'],
  446. etcd=facts['master']['etcd_port'],
  447. )
  448. etcd_urls = []
  449. if etcd_hosts != '':
  450. facts['master']['etcd_port'] = ports['etcd']
  451. facts['master']['embedded_etcd'] = False
  452. for host in etcd_hosts:
  453. etcd_urls.append(format_url(use_ssl['etcd'], host,
  454. ports['etcd']))
  455. else:
  456. etcd_urls = [format_url(use_ssl['etcd'], hostname,
  457. ports['etcd'])]
  458. facts['master'].setdefault('etcd_urls', etcd_urls)
  459. prefix_hosts = [('api', api_hostname),
  460. ('public_api', api_public_hostname),
  461. ('loopback_api', hostname)]
  462. for prefix, host in prefix_hosts:
  463. facts['master'].setdefault(prefix + '_url', format_url(use_ssl[prefix],
  464. host,
  465. ports[prefix]))
  466. r_lhn = "{0}:{1}".format(hostname, ports['api']).replace('.', '-')
  467. r_lhu = "system:openshift-master/{0}:{1}".format(api_hostname, ports['api']).replace('.', '-')
  468. facts['master'].setdefault('loopback_cluster_name', r_lhn)
  469. facts['master'].setdefault('loopback_context_name', "default/{0}/system:openshift-master".format(r_lhn))
  470. facts['master'].setdefault('loopback_user', r_lhu)
  471. prefix_hosts = [('console', api_hostname), ('public_console', api_public_hostname)]
  472. for prefix, host in prefix_hosts:
  473. facts['master'].setdefault(prefix + '_url', format_url(use_ssl[prefix],
  474. host,
  475. ports[prefix],
  476. console_path))
  477. return facts
  478. def set_aggregate_facts(facts):
  479. """ Set aggregate facts
  480. Args:
  481. facts (dict): existing facts
  482. Returns:
  483. dict: the facts dict updated with aggregated facts
  484. """
  485. all_hostnames = set()
  486. internal_hostnames = set()
  487. kube_svc_ip = first_ip(facts['common']['portal_net'])
  488. if 'common' in facts:
  489. all_hostnames.add(facts['common']['hostname'])
  490. all_hostnames.add(facts['common']['public_hostname'])
  491. all_hostnames.add(facts['common']['ip'])
  492. all_hostnames.add(facts['common']['public_ip'])
  493. facts['common']['kube_svc_ip'] = kube_svc_ip
  494. internal_hostnames.add(facts['common']['hostname'])
  495. internal_hostnames.add(facts['common']['ip'])
  496. cluster_domain = facts['common']['dns_domain']
  497. if 'master' in facts:
  498. if 'cluster_hostname' in facts['master']:
  499. all_hostnames.add(facts['master']['cluster_hostname'])
  500. if 'cluster_public_hostname' in facts['master']:
  501. all_hostnames.add(facts['master']['cluster_public_hostname'])
  502. svc_names = ['openshift', 'openshift.default', 'openshift.default.svc',
  503. 'openshift.default.svc.' + cluster_domain, 'kubernetes', 'kubernetes.default',
  504. 'kubernetes.default.svc', 'kubernetes.default.svc.' + cluster_domain]
  505. all_hostnames.update(svc_names)
  506. internal_hostnames.update(svc_names)
  507. all_hostnames.add(kube_svc_ip)
  508. internal_hostnames.add(kube_svc_ip)
  509. facts['common']['all_hostnames'] = list(all_hostnames)
  510. facts['common']['internal_hostnames'] = list(internal_hostnames)
  511. return facts
  512. def set_etcd_facts_if_unset(facts):
  513. """
  514. If using embedded etcd, loads the data directory from master-config.yaml.
  515. If using standalone etcd, loads ETCD_DATA_DIR from etcd.conf.
  516. If anything goes wrong parsing these, the fact will not be set.
  517. """
  518. if 'master' in facts and safe_get_bool(facts['master']['embedded_etcd']):
  519. etcd_facts = facts['etcd'] if 'etcd' in facts else dict()
  520. if 'etcd_data_dir' not in etcd_facts:
  521. try:
  522. # Parse master config to find actual etcd data dir:
  523. master_cfg_path = os.path.join(facts['common']['config_base'],
  524. 'master/master-config.yaml')
  525. master_cfg_f = open(master_cfg_path, 'r')
  526. config = yaml.safe_load(master_cfg_f.read())
  527. master_cfg_f.close()
  528. etcd_facts['etcd_data_dir'] = \
  529. config['etcdConfig']['storageDirectory']
  530. facts['etcd'] = etcd_facts
  531. # We don't want exceptions bubbling up here:
  532. # pylint: disable=broad-except
  533. except Exception:
  534. pass
  535. else:
  536. etcd_facts = facts['etcd'] if 'etcd' in facts else dict()
  537. # Read ETCD_DATA_DIR from /etc/etcd/etcd.conf:
  538. try:
  539. # Add a fake section for parsing:
  540. ini_str = text_type('[root]\n' + open('/etc/etcd/etcd.conf', 'r').read(), 'utf-8')
  541. ini_fp = io.StringIO(ini_str)
  542. config = configparser.RawConfigParser()
  543. config.readfp(ini_fp)
  544. etcd_data_dir = config.get('root', 'ETCD_DATA_DIR')
  545. if etcd_data_dir.startswith('"') and etcd_data_dir.endswith('"'):
  546. etcd_data_dir = etcd_data_dir[1:-1]
  547. etcd_facts['etcd_data_dir'] = etcd_data_dir
  548. facts['etcd'] = etcd_facts
  549. # We don't want exceptions bubbling up here:
  550. # pylint: disable=broad-except
  551. except Exception:
  552. pass
  553. return facts
  554. def set_deployment_facts_if_unset(facts):
  555. """ Set Facts that vary based on deployment_type. This currently
  556. includes common.service_type, master.registry_url, node.registry_url,
  557. node.storage_plugin_deps
  558. Args:
  559. facts (dict): existing facts
  560. Returns:
  561. dict: the facts dict updated with the generated deployment_type
  562. facts
  563. """
  564. # disabled to avoid breaking up facts related to deployment type into
  565. # multiple methods for now.
  566. # pylint: disable=too-many-statements, too-many-branches
  567. if 'common' in facts:
  568. deployment_type = facts['common']['deployment_type']
  569. if 'service_type' not in facts['common']:
  570. service_type = 'atomic-openshift'
  571. if deployment_type == 'origin':
  572. service_type = 'origin'
  573. facts['common']['service_type'] = service_type
  574. for role in ('master', 'node'):
  575. if role in facts:
  576. deployment_type = facts['common']['deployment_type']
  577. if 'registry_url' not in facts[role]:
  578. registry_url = 'openshift/origin-${component}:${version}'
  579. if deployment_type == 'openshift-enterprise':
  580. registry_url = 'openshift3/ose-${component}:${version}'
  581. facts[role]['registry_url'] = registry_url
  582. if 'master' in facts:
  583. deployment_type = facts['common']['deployment_type']
  584. openshift_features = ['Builder', 'S2IBuilder', 'WebConsole']
  585. if 'disabled_features' not in facts['master']:
  586. if facts['common']['deployment_subtype'] == 'registry':
  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. facts['node']['storage_plugin_deps'] = ['ceph', 'glusterfs', 'iscsi']
  592. return facts
  593. # pylint: disable=too-many-statements
  594. def set_version_facts_if_unset(facts):
  595. """ Set version facts. This currently includes common.version and
  596. common.version_gte_3_x
  597. Args:
  598. facts (dict): existing facts
  599. Returns:
  600. dict: the facts dict updated with version facts.
  601. """
  602. if 'common' in facts:
  603. openshift_version = get_openshift_version(facts)
  604. if openshift_version and openshift_version != "latest":
  605. version = LooseVersion(openshift_version)
  606. facts['common']['version'] = openshift_version
  607. facts['common']['short_version'] = '.'.join([str(x) for x in version.version[0:2]])
  608. version_gte_3_6 = version >= LooseVersion('3.6')
  609. version_gte_3_7 = version >= LooseVersion('3.7')
  610. version_gte_3_8 = version >= LooseVersion('3.8')
  611. version_gte_3_9 = version >= LooseVersion('3.9')
  612. else:
  613. # 'Latest' version is set to True, 'Next' versions set to False
  614. version_gte_3_6 = True
  615. version_gte_3_7 = True
  616. version_gte_3_8 = False
  617. version_gte_3_9 = False
  618. facts['common']['version_gte_3_6'] = version_gte_3_6
  619. facts['common']['version_gte_3_7'] = version_gte_3_7
  620. facts['common']['version_gte_3_8'] = version_gte_3_8
  621. facts['common']['version_gte_3_9'] = version_gte_3_9
  622. if version_gte_3_9:
  623. examples_content_version = 'v3.9'
  624. elif version_gte_3_8:
  625. examples_content_version = 'v3.8'
  626. elif version_gte_3_7:
  627. examples_content_version = 'v3.7'
  628. elif version_gte_3_6:
  629. examples_content_version = 'v3.6'
  630. else:
  631. examples_content_version = 'v1.5'
  632. facts['common']['examples_content_version'] = examples_content_version
  633. return facts
  634. def set_sdn_facts_if_unset(facts, system_facts):
  635. """ Set sdn facts if not already present in facts dict
  636. Args:
  637. facts (dict): existing facts
  638. system_facts (dict): ansible_facts
  639. Returns:
  640. dict: the facts dict updated with the generated sdn facts if they
  641. were not already present
  642. """
  643. if 'master' in facts:
  644. # set defaults for sdn_cluster_network_cidr and sdn_host_subnet_length
  645. # these might be overridden if they exist in the master config file
  646. sdn_cluster_network_cidr = '10.128.0.0/14'
  647. sdn_host_subnet_length = '9'
  648. master_cfg_path = os.path.join(facts['common']['config_base'],
  649. 'master/master-config.yaml')
  650. if os.path.isfile(master_cfg_path):
  651. with open(master_cfg_path, 'r') as master_cfg_f:
  652. config = yaml.safe_load(master_cfg_f.read())
  653. if 'networkConfig' in config:
  654. if 'clusterNetworkCIDR' in config['networkConfig']:
  655. sdn_cluster_network_cidr = \
  656. config['networkConfig']['clusterNetworkCIDR']
  657. if 'hostSubnetLength' in config['networkConfig']:
  658. sdn_host_subnet_length = \
  659. config['networkConfig']['hostSubnetLength']
  660. if 'sdn_cluster_network_cidr' not in facts['master']:
  661. facts['master']['sdn_cluster_network_cidr'] = sdn_cluster_network_cidr
  662. if 'sdn_host_subnet_length' not in facts['master']:
  663. facts['master']['sdn_host_subnet_length'] = sdn_host_subnet_length
  664. if 'node' in facts and 'sdn_mtu' not in facts['node']:
  665. node_ip = facts['common']['ip']
  666. # default MTU if interface MTU cannot be detected
  667. facts['node']['sdn_mtu'] = '1450'
  668. for val in itervalues(system_facts):
  669. if isinstance(val, dict) and 'mtu' in val:
  670. mtu = val['mtu']
  671. if 'ipv4' in val and val['ipv4'].get('address') == node_ip:
  672. facts['node']['sdn_mtu'] = str(mtu - 50)
  673. return facts
  674. def set_nodename(facts):
  675. """ set nodename """
  676. if 'node' in facts and 'common' in facts:
  677. if 'cloudprovider' in facts and facts['cloudprovider']['kind'] == 'gce':
  678. facts['node']['nodename'] = facts['provider']['metadata']['instance']['hostname'].split('.')[0]
  679. # TODO: The openstack cloudprovider nodename setting was too opinionaed.
  680. # It needs to be generalized before it can be enabled again.
  681. # elif 'cloudprovider' in facts and facts['cloudprovider']['kind'] == 'openstack':
  682. # facts['node']['nodename'] = facts['provider']['metadata']['hostname'].replace('.novalocal', '')
  683. else:
  684. facts['node']['nodename'] = facts['common']['hostname'].lower()
  685. return facts
  686. def migrate_oauth_template_facts(facts):
  687. """
  688. Migrate an old oauth template fact to a newer format if it's present.
  689. The legacy 'oauth_template' fact was just a filename, and assumed you were
  690. setting the 'login' template.
  691. The new pluralized 'oauth_templates' fact is a dict mapping the template
  692. name to a filename.
  693. Simplify the code after this by merging the old fact into the new.
  694. """
  695. if 'master' in facts and 'oauth_template' in facts['master']:
  696. if 'oauth_templates' not in facts['master']:
  697. facts['master']['oauth_templates'] = {"login": facts['master']['oauth_template']}
  698. elif 'login' not in facts['master']['oauth_templates']:
  699. facts['master']['oauth_templates']['login'] = facts['master']['oauth_template']
  700. return facts
  701. def format_url(use_ssl, hostname, port, path=''):
  702. """ Format url based on ssl flag, hostname, port and path
  703. Args:
  704. use_ssl (bool): is ssl enabled
  705. hostname (str): hostname
  706. port (str): port
  707. path (str): url path
  708. Returns:
  709. str: The generated url string
  710. """
  711. scheme = 'https' if use_ssl else 'http'
  712. netloc = hostname
  713. if (use_ssl and port != '443') or (not use_ssl and port != '80'):
  714. netloc += ":%s" % port
  715. try:
  716. url = urlparse.urlunparse((scheme, netloc, path, '', '', ''))
  717. except AttributeError:
  718. # pylint: disable=undefined-variable
  719. url = urlunparse((scheme, netloc, path, '', '', ''))
  720. return url
  721. def get_current_config(facts):
  722. """ Get current openshift config
  723. Args:
  724. facts (dict): existing facts
  725. Returns:
  726. dict: the facts dict updated with the current openshift config
  727. """
  728. current_config = dict()
  729. roles = [role for role in facts if role not in ['common', 'provider']]
  730. for role in roles:
  731. if 'roles' in current_config:
  732. current_config['roles'].append(role)
  733. else:
  734. current_config['roles'] = [role]
  735. # TODO: parse the /etc/sysconfig/openshift-{master,node} config to
  736. # determine the location of files.
  737. # TODO: I suspect this isn't working right now, but it doesn't prevent
  738. # anything from working properly as far as I can tell, perhaps because
  739. # we override the kubeconfig path everywhere we use it?
  740. # Query kubeconfig settings
  741. kubeconfig_dir = '/var/lib/origin/openshift.local.certificates'
  742. if role == 'node':
  743. kubeconfig_dir = os.path.join(
  744. kubeconfig_dir, "node-%s" % facts['common']['hostname']
  745. )
  746. kubeconfig_path = os.path.join(kubeconfig_dir, '.kubeconfig')
  747. if os.path.isfile('/usr/bin/openshift') and os.path.isfile(kubeconfig_path):
  748. try:
  749. _, output, _ = module.run_command( # noqa: F405
  750. ["/usr/bin/openshift", "ex", "config", "view", "-o",
  751. "json", "--kubeconfig=%s" % kubeconfig_path],
  752. check_rc=False
  753. )
  754. config = json.loads(output)
  755. cad = 'certificate-authority-data'
  756. try:
  757. for cluster in config['clusters']:
  758. config['clusters'][cluster][cad] = 'masked'
  759. except KeyError:
  760. pass
  761. try:
  762. for user in config['users']:
  763. config['users'][user][cad] = 'masked'
  764. config['users'][user]['client-key-data'] = 'masked'
  765. except KeyError:
  766. pass
  767. current_config['kubeconfig'] = config
  768. # override pylint broad-except warning, since we do not want
  769. # to bubble up any exceptions if oc config view
  770. # fails
  771. # pylint: disable=broad-except
  772. except Exception:
  773. pass
  774. return current_config
  775. def build_kubelet_args(facts):
  776. """Build node kubelet_args
  777. In the node-config.yaml file, kubeletArgument sub-keys have their
  778. values provided as a list. Hence the gratuitous use of ['foo'] below.
  779. """
  780. cloud_cfg_path = os.path.join(
  781. facts['common']['config_base'],
  782. 'cloudprovider')
  783. # We only have to do this stuff on hosts that are nodes
  784. if 'node' in facts:
  785. # Any changes to the kubeletArguments parameter are stored
  786. # here first.
  787. kubelet_args = {}
  788. if 'cloudprovider' in facts:
  789. # EVERY cloud is special <3
  790. if 'kind' in facts['cloudprovider']:
  791. if facts['cloudprovider']['kind'] == 'aws':
  792. kubelet_args['cloud-provider'] = ['aws']
  793. kubelet_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  794. if facts['cloudprovider']['kind'] == 'openstack':
  795. kubelet_args['cloud-provider'] = ['openstack']
  796. kubelet_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  797. if facts['cloudprovider']['kind'] == 'gce':
  798. kubelet_args['cloud-provider'] = ['gce']
  799. kubelet_args['cloud-config'] = [cloud_cfg_path + '/gce.conf']
  800. # Automatically add node-labels to the kubeletArguments
  801. # parameter. See BZ1359848 for additional details.
  802. #
  803. # Ref: https://bugzilla.redhat.com/show_bug.cgi?id=1359848
  804. if 'labels' in facts['node'] and isinstance(facts['node']['labels'], dict):
  805. # tl;dr: os_node_labels="{'foo': 'bar', 'a': 'b'}" turns
  806. # into ['foo=bar', 'a=b']
  807. #
  808. # On the openshift_node_labels inventory variable we loop
  809. # over each key-value tuple (from .items()) and join the
  810. # key to the value with an '=' character, this produces a
  811. # list.
  812. #
  813. # map() seems to be returning an itertools.imap object
  814. # instead of a list. We cast it to a list ourselves.
  815. # pylint: disable=unnecessary-lambda
  816. labels_str = list(map(lambda x: '='.join(x), facts['node']['labels'].items()))
  817. if labels_str != '':
  818. kubelet_args['node-labels'] = labels_str
  819. # If we've added items to the kubelet_args dict then we need
  820. # to merge the new items back into the main facts object.
  821. if kubelet_args != {}:
  822. facts = merge_facts({'node': {'kubelet_args': kubelet_args}}, facts, [], [])
  823. return facts
  824. def build_controller_args(facts):
  825. """ Build master controller_args """
  826. cloud_cfg_path = os.path.join(facts['common']['config_base'],
  827. 'cloudprovider')
  828. if 'master' in facts:
  829. controller_args = {}
  830. if 'cloudprovider' in facts:
  831. if 'kind' in facts['cloudprovider']:
  832. if facts['cloudprovider']['kind'] == 'aws':
  833. controller_args['cloud-provider'] = ['aws']
  834. controller_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  835. if facts['cloudprovider']['kind'] == 'openstack':
  836. controller_args['cloud-provider'] = ['openstack']
  837. controller_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  838. if facts['cloudprovider']['kind'] == 'gce':
  839. controller_args['cloud-provider'] = ['gce']
  840. controller_args['cloud-config'] = [cloud_cfg_path + '/gce.conf']
  841. if controller_args != {}:
  842. facts = merge_facts({'master': {'controller_args': controller_args}}, facts, [], [])
  843. return facts
  844. def build_api_server_args(facts):
  845. """ Build master api_server_args """
  846. cloud_cfg_path = os.path.join(facts['common']['config_base'],
  847. 'cloudprovider')
  848. if 'master' in facts:
  849. api_server_args = {}
  850. if 'cloudprovider' in facts:
  851. if 'kind' in facts['cloudprovider']:
  852. if facts['cloudprovider']['kind'] == 'aws':
  853. api_server_args['cloud-provider'] = ['aws']
  854. api_server_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  855. if facts['cloudprovider']['kind'] == 'openstack':
  856. api_server_args['cloud-provider'] = ['openstack']
  857. api_server_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  858. if facts['cloudprovider']['kind'] == 'gce':
  859. api_server_args['cloud-provider'] = ['gce']
  860. api_server_args['cloud-config'] = [cloud_cfg_path + '/gce.conf']
  861. if api_server_args != {}:
  862. facts = merge_facts({'master': {'api_server_args': api_server_args}}, facts, [], [])
  863. return facts
  864. def is_service_running(service):
  865. """ Queries systemd through dbus to see if the service is running """
  866. service_running = False
  867. try:
  868. bus = SystemBus()
  869. systemd = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
  870. manager = Interface(systemd, dbus_interface='org.freedesktop.systemd1.Manager')
  871. service_unit = service if service.endswith('.service') else manager.GetUnit('{0}.service'.format(service))
  872. service_proxy = bus.get_object('org.freedesktop.systemd1', str(service_unit))
  873. service_properties = Interface(service_proxy, dbus_interface='org.freedesktop.DBus.Properties')
  874. service_load_state = service_properties.Get('org.freedesktop.systemd1.Unit', 'LoadState')
  875. service_active_state = service_properties.Get('org.freedesktop.systemd1.Unit', 'ActiveState')
  876. if service_load_state == 'loaded' and service_active_state == 'active':
  877. service_running = True
  878. except DBusException:
  879. # TODO: do not swallow exception, as it may be hiding useful debugging
  880. # information.
  881. pass
  882. return service_running
  883. def rpm_rebuilddb():
  884. """
  885. Runs rpm --rebuilddb to ensure the db is in good shape.
  886. """
  887. module.run_command(['/usr/bin/rpm', '--rebuilddb']) # noqa: F405
  888. def get_version_output(binary, version_cmd):
  889. """ runs and returns the version output for a command """
  890. cmd = []
  891. for item in (binary, version_cmd):
  892. if isinstance(item, list):
  893. cmd.extend(item)
  894. else:
  895. cmd.append(item)
  896. if os.path.isfile(cmd[0]):
  897. _, output, _ = module.run_command(cmd) # noqa: F405
  898. return output
  899. # We may need this in the future.
  900. def get_docker_version_info():
  901. """ Parses and returns the docker version info """
  902. result = None
  903. if is_service_running('docker') or is_service_running('container-engine'):
  904. version_info = yaml.safe_load(get_version_output('/usr/bin/docker', 'version'))
  905. if 'Server' in version_info:
  906. result = {
  907. 'api_version': version_info['Server']['API version'],
  908. 'version': version_info['Server']['Version']
  909. }
  910. return result
  911. def get_openshift_version(facts):
  912. """ Get current version of openshift on the host.
  913. Checks a variety of ways ranging from fastest to slowest.
  914. Args:
  915. facts (dict): existing facts
  916. optional cli_image for pulling the version number
  917. Returns:
  918. version: the current openshift version
  919. """
  920. version = None
  921. # No need to run this method repeatedly on a system if we already know the
  922. # version
  923. # TODO: We need a way to force reload this after upgrading bits.
  924. if 'common' in facts:
  925. if 'version' in facts['common'] and facts['common']['version'] is not None:
  926. return chomp_commit_offset(facts['common']['version'])
  927. if os.path.isfile('/usr/bin/openshift'):
  928. _, output, _ = module.run_command(['/usr/bin/openshift', 'version']) # noqa: F405
  929. version = parse_openshift_version(output)
  930. elif 'common' in facts and 'is_containerized' in facts['common']:
  931. version = get_container_openshift_version(facts)
  932. # Handle containerized masters that have not yet been configured as a node.
  933. # This can be very slow and may get re-run multiple times, so we only use this
  934. # if other methods failed to find a version.
  935. if not version and os.path.isfile('/usr/local/bin/openshift'):
  936. _, output, _ = module.run_command(['/usr/local/bin/openshift', 'version']) # noqa: F405
  937. version = parse_openshift_version(output)
  938. return chomp_commit_offset(version)
  939. def chomp_commit_offset(version):
  940. """Chomp any "+git.foo" commit offset string from the given `version`
  941. and return the modified version string.
  942. Ex:
  943. - chomp_commit_offset(None) => None
  944. - chomp_commit_offset(1337) => "1337"
  945. - chomp_commit_offset("v3.4.0.15+git.derp") => "v3.4.0.15"
  946. - chomp_commit_offset("v3.4.0.15") => "v3.4.0.15"
  947. - chomp_commit_offset("v1.3.0+52492b4") => "v1.3.0"
  948. """
  949. if version is None:
  950. return version
  951. else:
  952. # Stringify, just in case it's a Number type. Split by '+' and
  953. # return the first split. No concerns about strings without a
  954. # '+', .split() returns an array of the original string.
  955. return str(version).split('+')[0]
  956. def get_container_openshift_version(facts):
  957. """
  958. If containerized, see if we can determine the installed version via the
  959. systemd environment files.
  960. """
  961. for filename in ['/etc/sysconfig/%s-master-controllers', '/etc/sysconfig/%s-node']:
  962. env_path = filename % facts['common']['service_type']
  963. if not os.path.exists(env_path):
  964. continue
  965. with open(env_path) as env_file:
  966. for line in env_file:
  967. if line.startswith("IMAGE_VERSION="):
  968. tag = line[len("IMAGE_VERSION="):].strip()
  969. # Remove leading "v" and any trailing release info, we just want
  970. # a version number here:
  971. no_v_version = tag[1:] if tag[0] == 'v' else tag
  972. version = no_v_version.split("-")[0]
  973. return version
  974. return None
  975. def parse_openshift_version(output):
  976. """ Apply provider facts to supplied facts dict
  977. Args:
  978. string: output of 'openshift version'
  979. Returns:
  980. string: the version number
  981. """
  982. versions = dict(e.split(' v') for e in output.splitlines() if ' v' in e)
  983. ver = versions.get('openshift', '')
  984. # Remove trailing build number and commit hash from older versions, we need to return a straight
  985. # w.x.y.z version here for use as openshift_version throughout the playbooks/roles. (i.e. 3.1.1.6-64-g80b61da)
  986. ver = ver.split('-')[0]
  987. return ver
  988. def apply_provider_facts(facts, provider_facts):
  989. """ Apply provider facts to supplied facts dict
  990. Args:
  991. facts (dict): facts dict to update
  992. provider_facts (dict): provider facts to apply
  993. roles: host roles
  994. Returns:
  995. dict: the merged facts
  996. """
  997. if not provider_facts:
  998. return facts
  999. common_vars = [('hostname', 'ip'), ('public_hostname', 'public_ip')]
  1000. for h_var, ip_var in common_vars:
  1001. ip_value = provider_facts['network'].get(ip_var)
  1002. if ip_value:
  1003. facts['common'][ip_var] = ip_value
  1004. facts['common'][h_var] = choose_hostname(
  1005. [provider_facts['network'].get(h_var)],
  1006. facts['common'][h_var]
  1007. )
  1008. facts['provider'] = provider_facts
  1009. return facts
  1010. # Disabling pylint too many branches. This function needs refactored
  1011. # but is a very core part of openshift_facts.
  1012. # pylint: disable=too-many-branches, too-many-nested-blocks
  1013. def merge_facts(orig, new, additive_facts_to_overwrite, protected_facts_to_overwrite):
  1014. """ Recursively merge facts dicts
  1015. Args:
  1016. orig (dict): existing facts
  1017. new (dict): facts to update
  1018. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1019. '.' notation ex: ['master.named_certificates']
  1020. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1021. '.' notation ex: ['master.master_count']
  1022. Returns:
  1023. dict: the merged facts
  1024. """
  1025. additive_facts = ['named_certificates']
  1026. protected_facts = ['ha']
  1027. # Facts we do not ever want to merge. These originate in inventory variables
  1028. # and contain JSON dicts. We don't ever want to trigger a merge
  1029. # here, just completely overwrite with the new if they are present there.
  1030. inventory_json_facts = ['admission_plugin_config',
  1031. 'kube_admission_plugin_config',
  1032. 'image_policy_config',
  1033. "builddefaults",
  1034. "buildoverrides"]
  1035. facts = dict()
  1036. for key, value in iteritems(orig):
  1037. # Key exists in both old and new facts.
  1038. if key in new:
  1039. if key in inventory_json_facts:
  1040. # Watchout for JSON facts that sometimes load as strings.
  1041. # (can happen if the JSON contains a boolean)
  1042. if isinstance(new[key], string_types):
  1043. facts[key] = yaml.safe_load(new[key])
  1044. else:
  1045. facts[key] = copy.deepcopy(new[key])
  1046. # Continue to recurse if old and new fact is a dictionary.
  1047. elif isinstance(value, dict) and isinstance(new[key], dict):
  1048. # Collect the subset of additive facts to overwrite if
  1049. # key matches. These will be passed to the subsequent
  1050. # merge_facts call.
  1051. relevant_additive_facts = []
  1052. for item in additive_facts_to_overwrite:
  1053. if '.' in item and item.startswith(key + '.'):
  1054. relevant_additive_facts.append(item)
  1055. # Collect the subset of protected facts to overwrite
  1056. # if key matches. These will be passed to the
  1057. # subsequent merge_facts call.
  1058. relevant_protected_facts = []
  1059. for item in protected_facts_to_overwrite:
  1060. if '.' in item and item.startswith(key + '.'):
  1061. relevant_protected_facts.append(item)
  1062. facts[key] = merge_facts(value, new[key], relevant_additive_facts, relevant_protected_facts)
  1063. # Key matches an additive fact and we are not overwriting
  1064. # it so we will append the new value to the existing value.
  1065. elif key in additive_facts and key not in [x.split('.')[-1] for x in additive_facts_to_overwrite]:
  1066. if isinstance(value, list) and isinstance(new[key], list):
  1067. new_fact = []
  1068. for item in copy.deepcopy(value) + copy.deepcopy(new[key]):
  1069. if item not in new_fact:
  1070. new_fact.append(item)
  1071. facts[key] = new_fact
  1072. # Key matches a protected fact and we are not overwriting
  1073. # it so we will determine if it is okay to change this
  1074. # fact.
  1075. elif key in protected_facts and key not in [x.split('.')[-1] for x in protected_facts_to_overwrite]:
  1076. # ha (bool) can not change unless it has been passed
  1077. # as a protected fact to overwrite.
  1078. if key == 'ha':
  1079. if safe_get_bool(value) != safe_get_bool(new[key]):
  1080. # pylint: disable=line-too-long
  1081. module.fail_json(msg='openshift_facts received a different value for openshift.master.ha') # noqa: F405
  1082. else:
  1083. facts[key] = value
  1084. # No other condition has been met. Overwrite the old fact
  1085. # with the new value.
  1086. else:
  1087. facts[key] = copy.deepcopy(new[key])
  1088. # Key isn't in new so add it to facts to keep it.
  1089. else:
  1090. facts[key] = copy.deepcopy(value)
  1091. new_keys = set(new.keys()) - set(orig.keys())
  1092. for key in new_keys:
  1093. # Watchout for JSON facts that sometimes load as strings.
  1094. # (can happen if the JSON contains a boolean)
  1095. if key in inventory_json_facts and isinstance(new[key], string_types):
  1096. facts[key] = yaml.safe_load(new[key])
  1097. else:
  1098. facts[key] = copy.deepcopy(new[key])
  1099. return facts
  1100. def save_local_facts(filename, facts):
  1101. """ Save local facts
  1102. Args:
  1103. filename (str): local facts file
  1104. facts (dict): facts to set
  1105. """
  1106. try:
  1107. fact_dir = os.path.dirname(filename)
  1108. try:
  1109. os.makedirs(fact_dir) # try to make the directory
  1110. except OSError as exception:
  1111. if exception.errno != errno.EEXIST: # but it is okay if it is already there
  1112. raise # pass any other exceptions up the chain
  1113. with open(filename, 'w') as fact_file:
  1114. fact_file.write(module.jsonify(facts)) # noqa: F405
  1115. os.chmod(filename, 0o600)
  1116. except (IOError, OSError) as ex:
  1117. raise OpenShiftFactsFileWriteError(
  1118. "Could not create fact file: %s, error: %s" % (filename, ex)
  1119. )
  1120. def get_local_facts_from_file(filename):
  1121. """ Retrieve local facts from fact file
  1122. Args:
  1123. filename (str): local facts file
  1124. Returns:
  1125. dict: the retrieved facts
  1126. """
  1127. local_facts = dict()
  1128. try:
  1129. # Handle conversion of INI style facts file to json style
  1130. ini_facts = configparser.SafeConfigParser()
  1131. ini_facts.read(filename)
  1132. for section in ini_facts.sections():
  1133. local_facts[section] = dict()
  1134. for key, value in ini_facts.items(section):
  1135. local_facts[section][key] = value
  1136. except (configparser.MissingSectionHeaderError,
  1137. configparser.ParsingError):
  1138. try:
  1139. with open(filename, 'r') as facts_file:
  1140. local_facts = json.load(facts_file)
  1141. except (ValueError, IOError):
  1142. pass
  1143. return local_facts
  1144. def sort_unique(alist):
  1145. """ Sorts and de-dupes a list
  1146. Args:
  1147. list: a list
  1148. Returns:
  1149. list: a sorted de-duped list
  1150. """
  1151. return sorted(list(set(alist)))
  1152. def safe_get_bool(fact):
  1153. """ Get a boolean fact safely.
  1154. Args:
  1155. facts: fact to convert
  1156. Returns:
  1157. bool: given fact as a bool
  1158. """
  1159. return bool(strtobool(str(fact)))
  1160. def set_proxy_facts(facts):
  1161. """ Set global proxy facts
  1162. Args:
  1163. facts(dict): existing facts
  1164. Returns:
  1165. facts(dict): Updated facts with missing values
  1166. """
  1167. if 'common' in facts:
  1168. common = facts['common']
  1169. if 'http_proxy' in common or 'https_proxy' in common or 'no_proxy' in common:
  1170. if 'no_proxy' in common and isinstance(common['no_proxy'], string_types):
  1171. common['no_proxy'] = common['no_proxy'].split(",")
  1172. elif 'no_proxy' not in common:
  1173. common['no_proxy'] = []
  1174. # See https://bugzilla.redhat.com/show_bug.cgi?id=1466783
  1175. # masters behind a proxy need to connect to etcd via IP
  1176. if 'no_proxy_etcd_host_ips' in common:
  1177. if isinstance(common['no_proxy_etcd_host_ips'], string_types):
  1178. common['no_proxy'].extend(common['no_proxy_etcd_host_ips'].split(','))
  1179. if 'generate_no_proxy_hosts' in common and safe_get_bool(common['generate_no_proxy_hosts']):
  1180. if 'no_proxy_internal_hostnames' in common:
  1181. common['no_proxy'].extend(common['no_proxy_internal_hostnames'].split(','))
  1182. # We always add local dns domain and ourselves no matter what
  1183. common['no_proxy'].append('.' + common['dns_domain'])
  1184. common['no_proxy'].append('.svc')
  1185. common['no_proxy'].append(common['hostname'])
  1186. common['no_proxy'] = ','.join(sort_unique(common['no_proxy']))
  1187. facts['common'] = common
  1188. return facts
  1189. def set_builddefaults_facts(facts):
  1190. """ Set build defaults including setting proxy values from http_proxy, https_proxy,
  1191. no_proxy to the more specific builddefaults and builddefaults_git vars.
  1192. 1. http_proxy, https_proxy, no_proxy
  1193. 2. builddefaults_*
  1194. 3. builddefaults_git_*
  1195. Args:
  1196. facts(dict): existing facts
  1197. Returns:
  1198. facts(dict): Updated facts with missing values
  1199. """
  1200. if 'builddefaults' in facts:
  1201. builddefaults = facts['builddefaults']
  1202. common = facts['common']
  1203. # Copy values from common to builddefaults
  1204. if 'http_proxy' not in builddefaults and 'http_proxy' in common:
  1205. builddefaults['http_proxy'] = common['http_proxy']
  1206. if 'https_proxy' not in builddefaults and 'https_proxy' in common:
  1207. builddefaults['https_proxy'] = common['https_proxy']
  1208. if 'no_proxy' not in builddefaults and 'no_proxy' in common:
  1209. builddefaults['no_proxy'] = common['no_proxy']
  1210. # Create git specific facts from generic values, if git specific values are
  1211. # not defined.
  1212. if 'git_http_proxy' not in builddefaults and 'http_proxy' in builddefaults:
  1213. builddefaults['git_http_proxy'] = builddefaults['http_proxy']
  1214. if 'git_https_proxy' not in builddefaults and 'https_proxy' in builddefaults:
  1215. builddefaults['git_https_proxy'] = builddefaults['https_proxy']
  1216. if 'git_no_proxy' not in builddefaults and 'no_proxy' in builddefaults:
  1217. builddefaults['git_no_proxy'] = builddefaults['no_proxy']
  1218. # If we're actually defining a builddefaults config then create admission_plugin_config
  1219. # then merge builddefaults[config] structure into admission_plugin_config
  1220. # 'config' is the 'openshift_builddefaults_json' inventory variable
  1221. if 'config' in builddefaults:
  1222. if 'admission_plugin_config' not in facts['master']:
  1223. # Scaffold out the full expected datastructure
  1224. facts['master']['admission_plugin_config'] = {'BuildDefaults': {'configuration': {'env': {}}}}
  1225. facts['master']['admission_plugin_config'].update(builddefaults['config'])
  1226. if 'env' in facts['master']['admission_plugin_config']['BuildDefaults']['configuration']:
  1227. delete_empty_keys(facts['master']['admission_plugin_config']['BuildDefaults']['configuration']['env'])
  1228. return facts
  1229. def delete_empty_keys(keylist):
  1230. """ Delete dictionary elements from keylist where "value" is empty.
  1231. Args:
  1232. keylist(list): A list of builddefault configuration envs.
  1233. Returns:
  1234. none
  1235. Example:
  1236. keylist = [{'name': 'HTTP_PROXY', 'value': 'http://file.rdu.redhat.com:3128'},
  1237. {'name': 'HTTPS_PROXY', 'value': 'http://file.rdu.redhat.com:3128'},
  1238. {'name': 'NO_PROXY', 'value': ''}]
  1239. After calling delete_empty_keys the provided list is modified to become:
  1240. [{'name': 'HTTP_PROXY', 'value': 'http://file.rdu.redhat.com:3128'},
  1241. {'name': 'HTTPS_PROXY', 'value': 'http://file.rdu.redhat.com:3128'}]
  1242. """
  1243. count = 0
  1244. for i in range(0, len(keylist)):
  1245. if len(keylist[i - count]['value']) == 0:
  1246. del keylist[i - count]
  1247. count += 1
  1248. def set_buildoverrides_facts(facts):
  1249. """ Set build overrides
  1250. Args:
  1251. facts(dict): existing facts
  1252. Returns:
  1253. facts(dict): Updated facts with missing values
  1254. """
  1255. if 'buildoverrides' in facts:
  1256. buildoverrides = facts['buildoverrides']
  1257. # If we're actually defining a buildoverrides config then create admission_plugin_config
  1258. # then merge buildoverrides[config] structure into admission_plugin_config
  1259. if 'config' in buildoverrides:
  1260. if 'admission_plugin_config' not in facts['master']:
  1261. facts['master']['admission_plugin_config'] = dict()
  1262. facts['master']['admission_plugin_config'].update(buildoverrides['config'])
  1263. return facts
  1264. # pylint: disable=too-many-statements
  1265. def set_container_facts_if_unset(facts):
  1266. """ Set containerized facts.
  1267. Args:
  1268. facts (dict): existing facts
  1269. Returns:
  1270. dict: the facts dict updated with the generated containerization
  1271. facts
  1272. """
  1273. deployment_type = facts['common']['deployment_type']
  1274. if deployment_type == 'openshift-enterprise':
  1275. master_image = 'openshift3/ose'
  1276. node_image = 'openshift3/node'
  1277. ovs_image = 'openshift3/openvswitch'
  1278. pod_image = 'openshift3/ose-pod'
  1279. router_image = 'openshift3/ose-haproxy-router'
  1280. registry_image = 'openshift3/ose-docker-registry'
  1281. deployer_image = 'openshift3/ose-deployer'
  1282. else:
  1283. master_image = 'openshift/origin'
  1284. node_image = 'openshift/node'
  1285. ovs_image = 'openshift/openvswitch'
  1286. pod_image = 'openshift/origin-pod'
  1287. router_image = 'openshift/origin-haproxy-router'
  1288. registry_image = 'openshift/origin-docker-registry'
  1289. deployer_image = 'openshift/origin-deployer'
  1290. facts['common']['is_atomic'] = os.path.isfile('/run/ostree-booted')
  1291. if 'is_containerized' not in facts['common']:
  1292. facts['common']['is_containerized'] = facts['common']['is_atomic']
  1293. if 'pod_image' not in facts['common']:
  1294. facts['common']['pod_image'] = pod_image
  1295. if 'router_image' not in facts['common']:
  1296. facts['common']['router_image'] = router_image
  1297. if 'registry_image' not in facts['common']:
  1298. facts['common']['registry_image'] = registry_image
  1299. if 'deployer_image' not in facts['common']:
  1300. facts['common']['deployer_image'] = deployer_image
  1301. if 'master' in facts and 'master_image' not in facts['master']:
  1302. facts['master']['master_image'] = master_image
  1303. facts['master']['master_system_image'] = master_image
  1304. if 'node' in facts:
  1305. if 'node_image' not in facts['node']:
  1306. facts['node']['node_image'] = node_image
  1307. facts['node']['node_system_image'] = node_image
  1308. if 'ovs_image' not in facts['node']:
  1309. facts['node']['ovs_image'] = ovs_image
  1310. facts['node']['ovs_system_image'] = ovs_image
  1311. if safe_get_bool(facts['common']['is_containerized']):
  1312. facts['common']['admin_binary'] = '/usr/local/bin/oadm'
  1313. facts['common']['client_binary'] = '/usr/local/bin/oc'
  1314. return facts
  1315. def set_installed_variant_rpm_facts(facts):
  1316. """ Set RPM facts of installed variant
  1317. Args:
  1318. facts (dict): existing facts
  1319. Returns:
  1320. dict: the facts dict updated with installed_variant_rpms
  1321. """
  1322. installed_rpms = []
  1323. for base_rpm in ['openshift', 'atomic-openshift', 'origin']:
  1324. optional_rpms = ['master', 'node', 'clients', 'sdn-ovs']
  1325. variant_rpms = [base_rpm] + \
  1326. ['{0}-{1}'.format(base_rpm, r) for r in optional_rpms] + \
  1327. ['tuned-profiles-%s-node' % base_rpm]
  1328. for rpm in variant_rpms:
  1329. exit_code, _, _ = module.run_command(['rpm', '-q', rpm]) # noqa: F405
  1330. if exit_code == 0:
  1331. installed_rpms.append(rpm)
  1332. facts['common']['installed_variant_rpms'] = installed_rpms
  1333. return facts
  1334. class OpenShiftFactsInternalError(Exception):
  1335. """Origin Facts Error"""
  1336. pass
  1337. class OpenShiftFactsUnsupportedRoleError(Exception):
  1338. """Origin Facts Unsupported Role Error"""
  1339. pass
  1340. class OpenShiftFactsFileWriteError(Exception):
  1341. """Origin Facts File Write Error"""
  1342. pass
  1343. class OpenShiftFactsMetadataUnavailableError(Exception):
  1344. """Origin Facts Metadata Unavailable Error"""
  1345. pass
  1346. class OpenShiftFacts(object):
  1347. """ Origin Facts
  1348. Attributes:
  1349. facts (dict): facts for the host
  1350. Args:
  1351. module (AnsibleModule): an AnsibleModule object
  1352. role (str): role for setting local facts
  1353. filename (str): local facts file to use
  1354. local_facts (dict): local facts to set
  1355. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1356. '.' notation ex: ['master.named_certificates']
  1357. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1358. '.' notation ex: ['master.master_count']
  1359. Raises:
  1360. OpenShiftFactsUnsupportedRoleError:
  1361. """
  1362. known_roles = ['builddefaults',
  1363. 'buildoverrides',
  1364. 'cloudprovider',
  1365. 'common',
  1366. 'etcd',
  1367. 'hosted',
  1368. 'master',
  1369. 'node',
  1370. 'logging',
  1371. 'loggingops',
  1372. 'metrics',
  1373. 'prometheus']
  1374. # Disabling too-many-arguments, this should be cleaned up as a TODO item.
  1375. # pylint: disable=too-many-arguments,no-value-for-parameter
  1376. def __init__(self, role, filename, local_facts,
  1377. additive_facts_to_overwrite=None,
  1378. openshift_env=None,
  1379. openshift_env_structures=None,
  1380. protected_facts_to_overwrite=None):
  1381. self.changed = False
  1382. self.filename = filename
  1383. if role not in self.known_roles:
  1384. raise OpenShiftFactsUnsupportedRoleError(
  1385. "Role %s is not supported by this module" % role
  1386. )
  1387. self.role = role
  1388. # Collect system facts and preface each fact with 'ansible_'.
  1389. try:
  1390. # pylint: disable=too-many-function-args,invalid-name
  1391. self.system_facts = ansible_facts(module, ['hardware', 'network', 'virtual', 'facter']) # noqa: F405
  1392. additional_facts = {}
  1393. for (k, v) in self.system_facts.items():
  1394. additional_facts["ansible_%s" % k.replace('-', '_')] = v
  1395. self.system_facts.update(additional_facts)
  1396. except UnboundLocalError:
  1397. # ansible-2.2,2.3
  1398. self.system_facts = get_all_facts(module)['ansible_facts'] # noqa: F405
  1399. self.facts = self.generate_facts(local_facts,
  1400. additive_facts_to_overwrite,
  1401. openshift_env,
  1402. openshift_env_structures,
  1403. protected_facts_to_overwrite)
  1404. def generate_facts(self,
  1405. local_facts,
  1406. additive_facts_to_overwrite,
  1407. openshift_env,
  1408. openshift_env_structures,
  1409. protected_facts_to_overwrite):
  1410. """ Generate facts
  1411. Args:
  1412. local_facts (dict): local_facts for overriding generated defaults
  1413. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1414. '.' notation ex: ['master.named_certificates']
  1415. openshift_env (dict): openshift_env facts for overriding generated defaults
  1416. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1417. '.' notation ex: ['master.master_count']
  1418. Returns:
  1419. dict: The generated facts
  1420. """
  1421. local_facts = self.init_local_facts(local_facts,
  1422. additive_facts_to_overwrite,
  1423. openshift_env,
  1424. openshift_env_structures,
  1425. protected_facts_to_overwrite)
  1426. roles = local_facts.keys()
  1427. if 'common' in local_facts and 'deployment_type' in local_facts['common']:
  1428. deployment_type = local_facts['common']['deployment_type']
  1429. else:
  1430. deployment_type = 'origin'
  1431. if 'common' in local_facts and 'deployment_subtype' in local_facts['common']:
  1432. deployment_subtype = local_facts['common']['deployment_subtype']
  1433. else:
  1434. deployment_subtype = 'basic'
  1435. defaults = self.get_defaults(roles, deployment_type, deployment_subtype)
  1436. provider_facts = self.init_provider_facts()
  1437. facts = apply_provider_facts(defaults, provider_facts)
  1438. facts = merge_facts(facts,
  1439. local_facts,
  1440. additive_facts_to_overwrite,
  1441. protected_facts_to_overwrite)
  1442. facts = migrate_oauth_template_facts(facts)
  1443. facts['current_config'] = get_current_config(facts)
  1444. facts = set_url_facts_if_unset(facts)
  1445. facts = set_selectors(facts)
  1446. facts = set_identity_providers_if_unset(facts)
  1447. facts = set_deployment_facts_if_unset(facts)
  1448. facts = set_sdn_facts_if_unset(facts, self.system_facts)
  1449. facts = set_container_facts_if_unset(facts)
  1450. facts = build_kubelet_args(facts)
  1451. facts = build_controller_args(facts)
  1452. facts = build_api_server_args(facts)
  1453. facts = set_version_facts_if_unset(facts)
  1454. facts = set_aggregate_facts(facts)
  1455. facts = set_etcd_facts_if_unset(facts)
  1456. facts = set_proxy_facts(facts)
  1457. facts = set_builddefaults_facts(facts)
  1458. facts = set_buildoverrides_facts(facts)
  1459. if not safe_get_bool(facts['common']['is_containerized']):
  1460. facts = set_installed_variant_rpm_facts(facts)
  1461. facts = set_nodename(facts)
  1462. return dict(openshift=facts)
  1463. def get_defaults(self, roles, deployment_type, deployment_subtype):
  1464. """ Get default fact values
  1465. Args:
  1466. roles (list): list of roles for this host
  1467. Returns:
  1468. dict: The generated default facts
  1469. """
  1470. defaults = {}
  1471. ip_addr = self.system_facts['ansible_default_ipv4']['address']
  1472. exit_code, output, _ = module.run_command(['hostname', '-f']) # noqa: F405
  1473. hostname_f = output.strip() if exit_code == 0 else ''
  1474. hostname_values = [hostname_f, self.system_facts['ansible_nodename'],
  1475. self.system_facts['ansible_fqdn']]
  1476. hostname = choose_hostname(hostname_values, ip_addr).lower()
  1477. defaults['common'] = dict(ip=ip_addr,
  1478. public_ip=ip_addr,
  1479. deployment_type=deployment_type,
  1480. deployment_subtype=deployment_subtype,
  1481. hostname=hostname,
  1482. public_hostname=hostname,
  1483. portal_net='172.30.0.0/16',
  1484. client_binary='oc', admin_binary='oadm',
  1485. dns_domain='cluster.local',
  1486. config_base='/etc/origin')
  1487. if 'master' in roles:
  1488. defaults['master'] = dict(api_use_ssl=True, api_port='8443',
  1489. controllers_port='8444',
  1490. console_use_ssl=True,
  1491. console_path='/console',
  1492. console_port='8443', etcd_use_ssl=True,
  1493. etcd_hosts='', etcd_port='4001',
  1494. portal_net='172.30.0.0/16',
  1495. embedded_etcd=True, embedded_kube=True,
  1496. embedded_dns=True,
  1497. bind_addr='0.0.0.0',
  1498. session_max_seconds=3600,
  1499. session_name='ssn',
  1500. session_secrets_file='',
  1501. access_token_max_seconds=86400,
  1502. auth_token_max_seconds=500,
  1503. oauth_grant_method='auto',
  1504. dynamic_provisioning_enabled=True,
  1505. max_requests_inflight=500)
  1506. if 'node' in roles:
  1507. defaults['node'] = dict(labels={}, annotations={},
  1508. iptables_sync_period='30s',
  1509. local_quota_per_fsgroup="",
  1510. set_node_ip=False)
  1511. if 'cloudprovider' in roles:
  1512. defaults['cloudprovider'] = dict(kind=None)
  1513. if 'hosted' in roles or self.role == 'hosted':
  1514. defaults['hosted'] = dict(
  1515. etcd=dict(
  1516. storage=dict(
  1517. kind=None,
  1518. volume=dict(
  1519. name='etcd',
  1520. size='1Gi'
  1521. ),
  1522. nfs=dict(
  1523. directory='/exports',
  1524. options='*(rw,root_squash)'
  1525. ),
  1526. host=None,
  1527. access=dict(
  1528. modes=['ReadWriteOnce']
  1529. ),
  1530. create_pv=True,
  1531. create_pvc=False
  1532. )
  1533. ),
  1534. registry=dict(
  1535. storage=dict(
  1536. kind=None,
  1537. volume=dict(
  1538. name='registry',
  1539. size='5Gi'
  1540. ),
  1541. nfs=dict(
  1542. directory='/exports',
  1543. options='*(rw,root_squash)'),
  1544. glusterfs=dict(
  1545. endpoints='glusterfs-registry-endpoints',
  1546. path='glusterfs-registry-volume',
  1547. ips=[],
  1548. readOnly=False,
  1549. swap=False,
  1550. swapcopy=True),
  1551. host=None,
  1552. access=dict(
  1553. modes=['ReadWriteMany']
  1554. ),
  1555. create_pv=True,
  1556. create_pvc=True
  1557. )
  1558. ),
  1559. router=dict()
  1560. )
  1561. defaults['logging'] = dict(
  1562. storage=dict(
  1563. kind=None,
  1564. volume=dict(
  1565. name='logging-es',
  1566. size='10Gi'
  1567. ),
  1568. nfs=dict(
  1569. directory='/exports',
  1570. options='*(rw,root_squash)'
  1571. ),
  1572. host=None,
  1573. access=dict(
  1574. modes=['ReadWriteOnce']
  1575. ),
  1576. create_pv=True,
  1577. create_pvc=False
  1578. )
  1579. )
  1580. defaults['loggingops'] = dict(
  1581. storage=dict(
  1582. kind=None,
  1583. volume=dict(
  1584. name='logging-es-ops',
  1585. size='10Gi'
  1586. ),
  1587. nfs=dict(
  1588. directory='/exports',
  1589. options='*(rw,root_squash)'
  1590. ),
  1591. host=None,
  1592. access=dict(
  1593. modes=['ReadWriteOnce']
  1594. ),
  1595. create_pv=True,
  1596. create_pvc=False
  1597. )
  1598. )
  1599. defaults['metrics'] = dict(
  1600. deploy=False,
  1601. duration=7,
  1602. resolution='10s',
  1603. storage=dict(
  1604. kind=None,
  1605. volume=dict(
  1606. name='metrics',
  1607. size='10Gi'
  1608. ),
  1609. nfs=dict(
  1610. directory='/exports',
  1611. options='*(rw,root_squash)'
  1612. ),
  1613. host=None,
  1614. access=dict(
  1615. modes=['ReadWriteOnce']
  1616. ),
  1617. create_pv=True,
  1618. create_pvc=False
  1619. )
  1620. )
  1621. defaults['prometheus'] = dict(
  1622. storage=dict(
  1623. kind=None,
  1624. volume=dict(
  1625. name='prometheus',
  1626. size='10Gi'
  1627. ),
  1628. nfs=dict(
  1629. directory='/exports',
  1630. options='*(rw,root_squash)'
  1631. ),
  1632. host=None,
  1633. access=dict(
  1634. modes=['ReadWriteOnce']
  1635. ),
  1636. create_pv=True,
  1637. create_pvc=False
  1638. )
  1639. )
  1640. defaults['prometheus']['alertmanager'] = dict(
  1641. storage=dict(
  1642. kind=None,
  1643. volume=dict(
  1644. name='prometheus-alertmanager',
  1645. size='10Gi'
  1646. ),
  1647. nfs=dict(
  1648. directory='/exports',
  1649. options='*(rw,root_squash)'
  1650. ),
  1651. host=None,
  1652. access=dict(
  1653. modes=['ReadWriteOnce']
  1654. ),
  1655. create_pv=True,
  1656. create_pvc=False
  1657. )
  1658. )
  1659. defaults['prometheus']['alertbuffer'] = dict(
  1660. storage=dict(
  1661. kind=None,
  1662. volume=dict(
  1663. name='prometheus-alertbuffer',
  1664. size='10Gi'
  1665. ),
  1666. nfs=dict(
  1667. directory='/exports',
  1668. options='*(rw,root_squash)'
  1669. ),
  1670. host=None,
  1671. access=dict(
  1672. modes=['ReadWriteOnce']
  1673. ),
  1674. create_pv=True,
  1675. create_pvc=False
  1676. )
  1677. )
  1678. return defaults
  1679. def guess_host_provider(self):
  1680. """ Guess the host provider
  1681. Returns:
  1682. dict: The generated default facts for the detected provider
  1683. """
  1684. # TODO: cloud provider facts should probably be submitted upstream
  1685. product_name = self.system_facts['ansible_product_name']
  1686. product_version = self.system_facts['ansible_product_version']
  1687. virt_type = self.system_facts['ansible_virtualization_type']
  1688. virt_role = self.system_facts['ansible_virtualization_role']
  1689. bios_vendor = self.system_facts['ansible_system_vendor']
  1690. provider = None
  1691. metadata = None
  1692. if bios_vendor == 'Google':
  1693. provider = 'gce'
  1694. metadata_url = ('http://metadata.google.internal/'
  1695. 'computeMetadata/v1/?recursive=true')
  1696. headers = {'Metadata-Flavor': 'Google'}
  1697. metadata = get_provider_metadata(metadata_url, True, headers,
  1698. True)
  1699. # Filter sshKeys and serviceAccounts from gce metadata
  1700. if metadata:
  1701. metadata['project']['attributes'].pop('sshKeys', None)
  1702. metadata['instance'].pop('serviceAccounts', None)
  1703. elif virt_type == 'xen' and virt_role == 'guest' and re.match(r'.*\.amazon$', product_version):
  1704. provider = 'aws'
  1705. metadata_url = 'http://169.254.169.254/latest/meta-data/'
  1706. metadata = get_provider_metadata(metadata_url)
  1707. elif re.search(r'OpenStack', product_name):
  1708. provider = 'openstack'
  1709. metadata_url = ('http://169.254.169.254/openstack/latest/'
  1710. 'meta_data.json')
  1711. metadata = get_provider_metadata(metadata_url, True, None,
  1712. True)
  1713. if metadata:
  1714. ec2_compat_url = 'http://169.254.169.254/latest/meta-data/'
  1715. metadata['ec2_compat'] = get_provider_metadata(
  1716. ec2_compat_url
  1717. )
  1718. # disable pylint maybe-no-member because overloaded use of
  1719. # the module name causes pylint to not detect that results
  1720. # is an array or hash
  1721. # pylint: disable=maybe-no-member
  1722. # Filter public_keys and random_seed from openstack metadata
  1723. metadata.pop('public_keys', None)
  1724. metadata.pop('random_seed', None)
  1725. if not metadata['ec2_compat']:
  1726. metadata = None
  1727. return dict(name=provider, metadata=metadata)
  1728. def init_provider_facts(self):
  1729. """ Initialize the provider facts
  1730. Returns:
  1731. dict: The normalized provider facts
  1732. """
  1733. provider_info = self.guess_host_provider()
  1734. provider_facts = normalize_provider_facts(
  1735. provider_info.get('name'),
  1736. provider_info.get('metadata')
  1737. )
  1738. return provider_facts
  1739. @staticmethod
  1740. def split_openshift_env_fact_keys(openshift_env_fact, openshift_env_structures):
  1741. """ Split openshift_env facts based on openshift_env structures.
  1742. Args:
  1743. openshift_env_fact (string): the openshift_env fact to split
  1744. ex: 'openshift_cloudprovider_openstack_auth_url'
  1745. openshift_env_structures (list): a list of structures to determine fact keys
  1746. ex: ['openshift.cloudprovider.openstack.*']
  1747. Returns:
  1748. list: a list of keys that represent the fact
  1749. ex: ['openshift', 'cloudprovider', 'openstack', 'auth_url']
  1750. """
  1751. # By default, we'll split an openshift_env fact by underscores.
  1752. fact_keys = openshift_env_fact.split('_')
  1753. # Determine if any of the provided variable structures match the fact.
  1754. matching_structure = None
  1755. if openshift_env_structures is not None:
  1756. for structure in openshift_env_structures:
  1757. if re.match(structure, openshift_env_fact):
  1758. matching_structure = structure
  1759. # Fact didn't match any variable structures so return the default fact keys.
  1760. if matching_structure is None:
  1761. return fact_keys
  1762. final_keys = []
  1763. structure_keys = matching_structure.split('.')
  1764. for structure_key in structure_keys:
  1765. # Matched current key. Add to final keys.
  1766. if structure_key == fact_keys[structure_keys.index(structure_key)]:
  1767. final_keys.append(structure_key)
  1768. # Wildcard means we will be taking everything from here to the end of the fact.
  1769. elif structure_key == '*':
  1770. final_keys.append('_'.join(fact_keys[structure_keys.index(structure_key):]))
  1771. # Shouldn't have gotten here, return the fact keys.
  1772. else:
  1773. return fact_keys
  1774. return final_keys
  1775. # Disabling too-many-branches and too-many-locals.
  1776. # This should be cleaned up as a TODO item.
  1777. # pylint: disable=too-many-branches, too-many-locals
  1778. def init_local_facts(self, facts=None,
  1779. additive_facts_to_overwrite=None,
  1780. openshift_env=None,
  1781. openshift_env_structures=None,
  1782. protected_facts_to_overwrite=None):
  1783. """ Initialize the local facts
  1784. Args:
  1785. facts (dict): local facts to set
  1786. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1787. '.' notation ex: ['master.named_certificates']
  1788. openshift_env (dict): openshift env facts to set
  1789. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1790. '.' notation ex: ['master.master_count']
  1791. Returns:
  1792. dict: The result of merging the provided facts with existing
  1793. local facts
  1794. """
  1795. changed = False
  1796. facts_to_set = dict()
  1797. if facts is not None:
  1798. facts_to_set[self.role] = facts
  1799. if openshift_env != {} and openshift_env is not None:
  1800. for fact, value in iteritems(openshift_env):
  1801. oo_env_facts = dict()
  1802. current_level = oo_env_facts
  1803. keys = self.split_openshift_env_fact_keys(fact, openshift_env_structures)[1:]
  1804. if len(keys) > 0 and keys[0] != self.role:
  1805. continue
  1806. # Build a dictionary from the split fact keys.
  1807. # After this loop oo_env_facts is the resultant dictionary.
  1808. # For example:
  1809. # fact = "openshift_metrics_install_metrics"
  1810. # value = 'true'
  1811. # keys = ['metrics', 'install', 'metrics']
  1812. # result = {'metrics': {'install': {'metrics': 'true'}}}
  1813. for i, _ in enumerate(keys):
  1814. # This is the last key. Set the value.
  1815. if i == (len(keys) - 1):
  1816. current_level[keys[i]] = value
  1817. # This is a key other than the last key. Set as
  1818. # dictionary and continue.
  1819. else:
  1820. current_level[keys[i]] = dict()
  1821. current_level = current_level[keys[i]]
  1822. facts_to_set = merge_facts(orig=facts_to_set,
  1823. new=oo_env_facts,
  1824. additive_facts_to_overwrite=[],
  1825. protected_facts_to_overwrite=[])
  1826. local_facts = get_local_facts_from_file(self.filename)
  1827. migrated_facts = migrate_local_facts(local_facts)
  1828. new_local_facts = merge_facts(migrated_facts,
  1829. facts_to_set,
  1830. additive_facts_to_overwrite,
  1831. protected_facts_to_overwrite)
  1832. new_local_facts = self.remove_empty_facts(new_local_facts)
  1833. if new_local_facts != local_facts:
  1834. self.validate_local_facts(new_local_facts)
  1835. changed = True
  1836. if not module.check_mode: # noqa: F405
  1837. save_local_facts(self.filename, new_local_facts)
  1838. self.changed = changed
  1839. return new_local_facts
  1840. def remove_empty_facts(self, facts=None):
  1841. """ Remove empty facts
  1842. Args:
  1843. facts (dict): facts to clean
  1844. """
  1845. facts_to_remove = []
  1846. for fact, value in iteritems(facts):
  1847. if isinstance(facts[fact], dict):
  1848. facts[fact] = self.remove_empty_facts(facts[fact])
  1849. else:
  1850. if value == "" or value == [""] or value is None:
  1851. facts_to_remove.append(fact)
  1852. for fact in facts_to_remove:
  1853. del facts[fact]
  1854. return facts
  1855. def validate_local_facts(self, facts=None):
  1856. """ Validate local facts
  1857. Args:
  1858. facts (dict): local facts to validate
  1859. """
  1860. invalid_facts = dict()
  1861. invalid_facts = self.validate_master_facts(facts, invalid_facts)
  1862. if invalid_facts:
  1863. msg = 'Invalid facts detected:\n'
  1864. # pylint: disable=consider-iterating-dictionary
  1865. for key in invalid_facts.keys():
  1866. msg += '{0}: {1}\n'.format(key, invalid_facts[key])
  1867. module.fail_json(msg=msg, changed=self.changed) # noqa: F405
  1868. # disabling pylint errors for line-too-long since we're dealing
  1869. # with best effort reduction of error messages here.
  1870. # disabling errors for too-many-branches since we require checking
  1871. # many conditions.
  1872. # pylint: disable=line-too-long, too-many-branches
  1873. @staticmethod
  1874. def validate_master_facts(facts, invalid_facts):
  1875. """ Validate master facts
  1876. Args:
  1877. facts (dict): local facts to validate
  1878. invalid_facts (dict): collected invalid_facts
  1879. Returns:
  1880. dict: Invalid facts
  1881. """
  1882. if 'master' in facts:
  1883. # openshift.master.session_auth_secrets
  1884. if 'session_auth_secrets' in facts['master']:
  1885. session_auth_secrets = facts['master']['session_auth_secrets']
  1886. if not issubclass(type(session_auth_secrets), list):
  1887. invalid_facts['session_auth_secrets'] = 'Expects session_auth_secrets is a list.'
  1888. elif 'session_encryption_secrets' not in facts['master']:
  1889. invalid_facts['session_auth_secrets'] = ('openshift_master_session_encryption secrets must be set '
  1890. 'if openshift_master_session_auth_secrets is provided.')
  1891. elif len(session_auth_secrets) != len(facts['master']['session_encryption_secrets']):
  1892. invalid_facts['session_auth_secrets'] = ('openshift_master_session_auth_secrets and '
  1893. 'openshift_master_session_encryption_secrets must be '
  1894. 'equal length.')
  1895. else:
  1896. for secret in session_auth_secrets:
  1897. if len(secret) < 32:
  1898. invalid_facts['session_auth_secrets'] = ('Invalid secret in session_auth_secrets. '
  1899. 'Secrets must be at least 32 characters in length.')
  1900. # openshift.master.session_encryption_secrets
  1901. if 'session_encryption_secrets' in facts['master']:
  1902. session_encryption_secrets = facts['master']['session_encryption_secrets']
  1903. if not issubclass(type(session_encryption_secrets), list):
  1904. invalid_facts['session_encryption_secrets'] = 'Expects session_encryption_secrets is a list.'
  1905. elif 'session_auth_secrets' not in facts['master']:
  1906. invalid_facts['session_encryption_secrets'] = ('openshift_master_session_auth_secrets must be '
  1907. 'set if openshift_master_session_encryption_secrets '
  1908. 'is provided.')
  1909. else:
  1910. for secret in session_encryption_secrets:
  1911. if len(secret) not in [16, 24, 32]:
  1912. invalid_facts['session_encryption_secrets'] = ('Invalid secret in session_encryption_secrets. '
  1913. 'Secrets must be 16, 24, or 32 characters in length.')
  1914. return invalid_facts
  1915. def main():
  1916. """ main """
  1917. # disabling pylint errors for global-variable-undefined and invalid-name
  1918. # for 'global module' usage, since it is required to use ansible_facts
  1919. # pylint: disable=global-variable-undefined, invalid-name
  1920. global module
  1921. module = AnsibleModule( # noqa: F405
  1922. argument_spec=dict(
  1923. role=dict(default='common', required=False,
  1924. choices=OpenShiftFacts.known_roles),
  1925. local_facts=dict(default=None, type='dict', required=False),
  1926. additive_facts_to_overwrite=dict(default=[], type='list', required=False),
  1927. openshift_env=dict(default={}, type='dict', required=False),
  1928. openshift_env_structures=dict(default=[], type='list', required=False),
  1929. protected_facts_to_overwrite=dict(default=[], type='list', required=False)
  1930. ),
  1931. supports_check_mode=True,
  1932. add_file_common_args=True,
  1933. )
  1934. if not HAVE_DBUS:
  1935. module.fail_json(msg="This module requires dbus python bindings") # noqa: F405
  1936. module.params['gather_subset'] = ['hardware', 'network', 'virtual', 'facter'] # noqa: F405
  1937. module.params['gather_timeout'] = 10 # noqa: F405
  1938. module.params['filter'] = '*' # noqa: F405
  1939. role = module.params['role'] # noqa: F405
  1940. local_facts = module.params['local_facts'] # noqa: F405
  1941. additive_facts_to_overwrite = module.params['additive_facts_to_overwrite'] # noqa: F405
  1942. openshift_env = module.params['openshift_env'] # noqa: F405
  1943. openshift_env_structures = module.params['openshift_env_structures'] # noqa: F405
  1944. protected_facts_to_overwrite = module.params['protected_facts_to_overwrite'] # noqa: F405
  1945. fact_file = '/etc/ansible/facts.d/openshift.fact'
  1946. openshift_facts = OpenShiftFacts(role,
  1947. fact_file,
  1948. local_facts,
  1949. additive_facts_to_overwrite,
  1950. openshift_env,
  1951. openshift_env_structures,
  1952. protected_facts_to_overwrite)
  1953. file_params = module.params.copy() # noqa: F405
  1954. file_params['path'] = fact_file
  1955. file_args = module.load_file_common_arguments(file_params) # noqa: F405
  1956. changed = module.set_fs_attributes_if_different(file_args, # noqa: F405
  1957. openshift_facts.changed)
  1958. return module.exit_json(changed=changed, # noqa: F405
  1959. ansible_facts=openshift_facts.facts)
  1960. if __name__ == '__main__':
  1961. main()