openshift_facts.py 90 KB

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