openshift_facts.py 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242
  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. In the node-config.yaml file, kubeletArgument sub-keys have their
  885. values provided as a list. Hence the gratuitous use of ['foo'] below.
  886. """
  887. cloud_cfg_path = os.path.join(
  888. facts['common']['config_base'],
  889. 'cloudprovider')
  890. # We only have to do this stuff on hosts that are nodes
  891. if 'node' in facts:
  892. # Any changes to the kubeletArguments parameter are stored
  893. # here first.
  894. kubelet_args = {}
  895. if 'cloudprovider' in facts:
  896. # EVERY cloud is special <3
  897. if 'kind' in facts['cloudprovider']:
  898. if facts['cloudprovider']['kind'] == 'aws':
  899. kubelet_args['cloud-provider'] = ['aws']
  900. kubelet_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  901. if facts['cloudprovider']['kind'] == 'openstack':
  902. kubelet_args['cloud-provider'] = ['openstack']
  903. kubelet_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  904. if facts['cloudprovider']['kind'] == 'gce':
  905. kubelet_args['cloud-provider'] = ['gce']
  906. # Automatically add node-labels to the kubeletArguments
  907. # parameter. See BZ1359848 for additional details.
  908. #
  909. # Ref: https://bugzilla.redhat.com/show_bug.cgi?id=1359848
  910. if 'labels' in facts['node'] and isinstance(facts['node']['labels'], dict):
  911. # tl;dr: os_node_labels="{'foo': 'bar', 'a': 'b'}" turns
  912. # into ['foo=bar', 'a=b']
  913. #
  914. # On the openshift_node_labels inventory variable we loop
  915. # over each key-value tuple (from .items()) and join the
  916. # key to the value with an '=' character, this produces a
  917. # list.
  918. #
  919. # map() seems to be returning an itertools.imap object
  920. # instead of a list. We cast it to a list ourselves.
  921. labels_str = list(map(lambda x: '='.join(x), facts['node']['labels'].items()))
  922. if labels_str != '':
  923. kubelet_args['node-labels'] = labels_str
  924. # If we've added items to the kubelet_args dict then we need
  925. # to merge the new items back into the main facts object.
  926. if kubelet_args != {}:
  927. facts = merge_facts({'node': {'kubelet_args': kubelet_args}}, facts, [], [])
  928. return facts
  929. def build_controller_args(facts):
  930. """ Build master controller_args """
  931. cloud_cfg_path = os.path.join(facts['common']['config_base'],
  932. 'cloudprovider')
  933. if 'master' in facts:
  934. controller_args = {}
  935. if 'cloudprovider' in facts:
  936. if 'kind' in facts['cloudprovider']:
  937. if facts['cloudprovider']['kind'] == 'aws':
  938. controller_args['cloud-provider'] = ['aws']
  939. controller_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  940. if facts['cloudprovider']['kind'] == 'openstack':
  941. controller_args['cloud-provider'] = ['openstack']
  942. controller_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  943. if facts['cloudprovider']['kind'] == 'gce':
  944. controller_args['cloud-provider'] = ['gce']
  945. if controller_args != {}:
  946. facts = merge_facts({'master': {'controller_args': controller_args}}, facts, [], [])
  947. return facts
  948. def build_api_server_args(facts):
  949. """ Build master api_server_args """
  950. cloud_cfg_path = os.path.join(facts['common']['config_base'],
  951. 'cloudprovider')
  952. if 'master' in facts:
  953. api_server_args = {}
  954. if 'cloudprovider' in facts:
  955. if 'kind' in facts['cloudprovider']:
  956. if facts['cloudprovider']['kind'] == 'aws':
  957. api_server_args['cloud-provider'] = ['aws']
  958. api_server_args['cloud-config'] = [cloud_cfg_path + '/aws.conf']
  959. if facts['cloudprovider']['kind'] == 'openstack':
  960. api_server_args['cloud-provider'] = ['openstack']
  961. api_server_args['cloud-config'] = [cloud_cfg_path + '/openstack.conf']
  962. if facts['cloudprovider']['kind'] == 'gce':
  963. api_server_args['cloud-provider'] = ['gce']
  964. if api_server_args != {}:
  965. facts = merge_facts({'master': {'api_server_args': api_server_args}}, facts, [], [])
  966. return facts
  967. def is_service_running(service):
  968. """ Queries systemd through dbus to see if the service is running """
  969. service_running = False
  970. bus = SystemBus()
  971. systemd = bus.get_object('org.freedesktop.systemd1', '/org/freedesktop/systemd1')
  972. manager = Interface(systemd, dbus_interface='org.freedesktop.systemd1.Manager')
  973. try:
  974. service_unit = service if service.endswith('.service') else manager.GetUnit('{0}.service'.format(service))
  975. service_proxy = bus.get_object('org.freedesktop.systemd1', str(service_unit))
  976. service_properties = Interface(service_proxy, dbus_interface='org.freedesktop.DBus.Properties')
  977. service_load_state = service_properties.Get('org.freedesktop.systemd1.Unit', 'LoadState')
  978. service_active_state = service_properties.Get('org.freedesktop.systemd1.Unit', 'ActiveState')
  979. if service_load_state == 'loaded' and service_active_state == 'active':
  980. service_running = True
  981. except DBusException:
  982. pass
  983. return service_running
  984. def get_version_output(binary, version_cmd):
  985. """ runs and returns the version output for a command """
  986. cmd = []
  987. for item in (binary, version_cmd):
  988. if isinstance(item, list):
  989. cmd.extend(item)
  990. else:
  991. cmd.append(item)
  992. if os.path.isfile(cmd[0]):
  993. _, output, _ = module.run_command(cmd)
  994. return output
  995. def get_docker_version_info():
  996. """ Parses and returns the docker version info """
  997. result = None
  998. if is_service_running('docker'):
  999. version_info = yaml.safe_load(get_version_output('/usr/bin/docker', 'version'))
  1000. if 'Server' in version_info:
  1001. result = {
  1002. 'api_version': version_info['Server']['API version'],
  1003. 'version': version_info['Server']['Version']
  1004. }
  1005. return result
  1006. def get_openshift_version(facts):
  1007. """ Get current version of openshift on the host.
  1008. Checks a variety of ways ranging from fastest to slowest.
  1009. Args:
  1010. facts (dict): existing facts
  1011. optional cli_image for pulling the version number
  1012. Returns:
  1013. version: the current openshift version
  1014. """
  1015. version = None
  1016. # No need to run this method repeatedly on a system if we already know the
  1017. # version
  1018. if 'common' in facts:
  1019. if 'version' in facts['common'] and facts['common']['version'] is not None:
  1020. return facts['common']['version']
  1021. if os.path.isfile('/usr/bin/openshift'):
  1022. _, output, _ = module.run_command(['/usr/bin/openshift', 'version'])
  1023. version = parse_openshift_version(output)
  1024. elif 'common' in facts and 'is_containerized' in facts['common']:
  1025. version = get_container_openshift_version(facts)
  1026. # Handle containerized masters that have not yet been configured as a node.
  1027. # This can be very slow and may get re-run multiple times, so we only use this
  1028. # if other methods failed to find a version.
  1029. if not version and os.path.isfile('/usr/local/bin/openshift'):
  1030. _, output, _ = module.run_command(['/usr/local/bin/openshift', 'version'])
  1031. version = parse_openshift_version(output)
  1032. return version
  1033. def get_container_openshift_version(facts):
  1034. """
  1035. If containerized, see if we can determine the installed version via the
  1036. systemd environment files.
  1037. """
  1038. for filename in ['/etc/sysconfig/%s-master', '/etc/sysconfig/%s-node']:
  1039. env_path = filename % facts['common']['service_type']
  1040. if not os.path.exists(env_path):
  1041. continue
  1042. with open(env_path) as env_file:
  1043. for line in env_file:
  1044. if line.startswith("IMAGE_VERSION="):
  1045. tag = line[len("IMAGE_VERSION="):].strip()
  1046. # Remove leading "v" and any trailing release info, we just want
  1047. # a version number here:
  1048. version = tag[1:].split("-")[0]
  1049. return version
  1050. return None
  1051. def parse_openshift_version(output):
  1052. """ Apply provider facts to supplied facts dict
  1053. Args:
  1054. string: output of 'openshift version'
  1055. Returns:
  1056. string: the version number
  1057. """
  1058. versions = dict(e.split(' v') for e in output.splitlines() if ' v' in e)
  1059. ver = versions.get('openshift', '')
  1060. # Remove trailing build number and commit hash from older versions, we need to return a straight
  1061. # w.x.y.z version here for use as openshift_version throughout the playbooks/roles. (i.e. 3.1.1.6-64-g80b61da)
  1062. ver = ver.split('-')[0]
  1063. return ver
  1064. def apply_provider_facts(facts, provider_facts):
  1065. """ Apply provider facts to supplied facts dict
  1066. Args:
  1067. facts (dict): facts dict to update
  1068. provider_facts (dict): provider facts to apply
  1069. roles: host roles
  1070. Returns:
  1071. dict: the merged facts
  1072. """
  1073. if not provider_facts:
  1074. return facts
  1075. common_vars = [('hostname', 'ip'), ('public_hostname', 'public_ip')]
  1076. for h_var, ip_var in common_vars:
  1077. ip_value = provider_facts['network'].get(ip_var)
  1078. if ip_value:
  1079. facts['common'][ip_var] = ip_value
  1080. facts['common'][h_var] = choose_hostname(
  1081. [provider_facts['network'].get(h_var)],
  1082. facts['common'][h_var]
  1083. )
  1084. facts['provider'] = provider_facts
  1085. return facts
  1086. # Disabling pylint too many branches. This function needs refactored
  1087. # but is a very core part of openshift_facts.
  1088. # pylint: disable=too-many-branches
  1089. def merge_facts(orig, new, additive_facts_to_overwrite, protected_facts_to_overwrite):
  1090. """ Recursively merge facts dicts
  1091. Args:
  1092. orig (dict): existing facts
  1093. new (dict): facts to update
  1094. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1095. '.' notation ex: ['master.named_certificates']
  1096. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1097. '.' notation ex: ['master.master_count']
  1098. Returns:
  1099. dict: the merged facts
  1100. """
  1101. additive_facts = ['named_certificates']
  1102. protected_facts = ['ha', 'master_count']
  1103. # Facts we do not ever want to merge. These originate in inventory variables
  1104. # and contain JSON dicts. We don't ever want to trigger a merge
  1105. # here, just completely overwrite with the new if they are present there.
  1106. inventory_json_facts = ['admission_plugin_config',
  1107. 'kube_admission_plugin_config',
  1108. 'image_policy_config']
  1109. facts = dict()
  1110. for key, value in orig.iteritems():
  1111. # Key exists in both old and new facts.
  1112. if key in new:
  1113. if key in inventory_json_facts:
  1114. # Watchout for JSON facts that sometimes load as strings.
  1115. # (can happen if the JSON contains a boolean)
  1116. if isinstance(new[key], basestring):
  1117. facts[key] = yaml.safe_load(new[key])
  1118. else:
  1119. facts[key] = copy.deepcopy(new[key])
  1120. # Continue to recurse if old and new fact is a dictionary.
  1121. elif isinstance(value, dict) and isinstance(new[key], dict):
  1122. # Collect the subset of additive facts to overwrite if
  1123. # key matches. These will be passed to the subsequent
  1124. # merge_facts call.
  1125. relevant_additive_facts = []
  1126. for item in additive_facts_to_overwrite:
  1127. if '.' in item and item.startswith(key + '.'):
  1128. relevant_additive_facts.append(item)
  1129. # Collect the subset of protected facts to overwrite
  1130. # if key matches. These will be passed to the
  1131. # subsequent merge_facts call.
  1132. relevant_protected_facts = []
  1133. for item in protected_facts_to_overwrite:
  1134. if '.' in item and item.startswith(key + '.'):
  1135. relevant_protected_facts.append(item)
  1136. facts[key] = merge_facts(value, new[key], relevant_additive_facts, relevant_protected_facts)
  1137. # Key matches an additive fact and we are not overwriting
  1138. # it so we will append the new value to the existing value.
  1139. elif key in additive_facts and key not in [x.split('.')[-1] for x in additive_facts_to_overwrite]:
  1140. if isinstance(value, list) and isinstance(new[key], list):
  1141. new_fact = []
  1142. for item in copy.deepcopy(value) + copy.deepcopy(new[key]):
  1143. if item not in new_fact:
  1144. new_fact.append(item)
  1145. facts[key] = new_fact
  1146. # Key matches a protected fact and we are not overwriting
  1147. # it so we will determine if it is okay to change this
  1148. # fact.
  1149. elif key in protected_facts and key not in [x.split('.')[-1] for x in protected_facts_to_overwrite]:
  1150. # The master count (int) can only increase unless it
  1151. # has been passed as a protected fact to overwrite.
  1152. if key == 'master_count':
  1153. if int(value) <= int(new[key]):
  1154. facts[key] = copy.deepcopy(new[key])
  1155. else:
  1156. module.fail_json(msg='openshift_facts received a lower value for openshift.master.master_count')
  1157. # ha (bool) can not change unless it has been passed
  1158. # as a protected fact to overwrite.
  1159. if key == 'ha':
  1160. if safe_get_bool(value) != safe_get_bool(new[key]):
  1161. module.fail_json(msg='openshift_facts received a different value for openshift.master.ha')
  1162. else:
  1163. facts[key] = value
  1164. # No other condition has been met. Overwrite the old fact
  1165. # with the new value.
  1166. else:
  1167. facts[key] = copy.deepcopy(new[key])
  1168. # Key isn't in new so add it to facts to keep it.
  1169. else:
  1170. facts[key] = copy.deepcopy(value)
  1171. new_keys = set(new.keys()) - set(orig.keys())
  1172. for key in new_keys:
  1173. # Watchout for JSON facts that sometimes load as strings.
  1174. # (can happen if the JSON contains a boolean)
  1175. if key in inventory_json_facts and isinstance(new[key], basestring):
  1176. facts[key] = yaml.safe_load(new[key])
  1177. else:
  1178. facts[key] = copy.deepcopy(new[key])
  1179. return facts
  1180. def save_local_facts(filename, facts):
  1181. """ Save local facts
  1182. Args:
  1183. filename (str): local facts file
  1184. facts (dict): facts to set
  1185. """
  1186. try:
  1187. fact_dir = os.path.dirname(filename)
  1188. if not os.path.exists(fact_dir):
  1189. os.makedirs(fact_dir)
  1190. with open(filename, 'w') as fact_file:
  1191. fact_file.write(module.jsonify(facts))
  1192. os.chmod(filename, 0o600)
  1193. except (IOError, OSError) as ex:
  1194. raise OpenShiftFactsFileWriteError(
  1195. "Could not create fact file: %s, error: %s" % (filename, ex)
  1196. )
  1197. def get_local_facts_from_file(filename):
  1198. """ Retrieve local facts from fact file
  1199. Args:
  1200. filename (str): local facts file
  1201. Returns:
  1202. dict: the retrieved facts
  1203. """
  1204. local_facts = dict()
  1205. try:
  1206. # Handle conversion of INI style facts file to json style
  1207. ini_facts = ConfigParser.SafeConfigParser()
  1208. ini_facts.read(filename)
  1209. for section in ini_facts.sections():
  1210. local_facts[section] = dict()
  1211. for key, value in ini_facts.items(section):
  1212. local_facts[section][key] = value
  1213. except (ConfigParser.MissingSectionHeaderError,
  1214. ConfigParser.ParsingError):
  1215. try:
  1216. with open(filename, 'r') as facts_file:
  1217. local_facts = json.load(facts_file)
  1218. except (ValueError, IOError):
  1219. pass
  1220. return local_facts
  1221. def sort_unique(alist):
  1222. """ Sorts and de-dupes a list
  1223. Args:
  1224. list: a list
  1225. Returns:
  1226. list: a sorted de-duped list
  1227. """
  1228. alist.sort()
  1229. out = list()
  1230. for i in alist:
  1231. if i not in out:
  1232. out.append(i)
  1233. return out
  1234. def safe_get_bool(fact):
  1235. """ Get a boolean fact safely.
  1236. Args:
  1237. facts: fact to convert
  1238. Returns:
  1239. bool: given fact as a bool
  1240. """
  1241. return bool(strtobool(str(fact)))
  1242. def set_proxy_facts(facts):
  1243. """ Set global proxy facts and promote defaults from http_proxy, https_proxy,
  1244. no_proxy to the more specific builddefaults and builddefaults_git vars.
  1245. 1. http_proxy, https_proxy, no_proxy
  1246. 2. builddefaults_*
  1247. 3. builddefaults_git_*
  1248. Args:
  1249. facts(dict): existing facts
  1250. Returns:
  1251. facts(dict): Updated facts with missing values
  1252. """
  1253. if 'common' in facts:
  1254. common = facts['common']
  1255. if 'http_proxy' in common or 'https_proxy' in common:
  1256. if 'no_proxy' in common and \
  1257. isinstance(common['no_proxy'], basestring):
  1258. common['no_proxy'] = common['no_proxy'].split(",")
  1259. elif 'no_proxy' not in common:
  1260. common['no_proxy'] = []
  1261. if 'generate_no_proxy_hosts' in common and \
  1262. safe_get_bool(common['generate_no_proxy_hosts']):
  1263. if 'no_proxy_internal_hostnames' in common:
  1264. common['no_proxy'].extend(common['no_proxy_internal_hostnames'].split(','))
  1265. common['no_proxy'].append('.' + common['dns_domain'])
  1266. # We always add ourselves no matter what
  1267. common['no_proxy'].append(common['hostname'])
  1268. common['no_proxy'] = sort_unique(common['no_proxy'])
  1269. facts['common'] = common
  1270. if 'builddefaults' in facts:
  1271. builddefaults = facts['builddefaults']
  1272. common = facts['common']
  1273. # Copy values from common to builddefaults
  1274. if 'http_proxy' not in builddefaults and 'http_proxy' in common:
  1275. builddefaults['http_proxy'] = common['http_proxy']
  1276. if 'https_proxy' not in builddefaults and 'https_proxy' in common:
  1277. builddefaults['https_proxy'] = common['https_proxy']
  1278. # make no_proxy into a list if it's not
  1279. if 'no_proxy' in builddefaults and isinstance(builddefaults['no_proxy'], basestring):
  1280. builddefaults['no_proxy'] = builddefaults['no_proxy'].split(",")
  1281. if 'no_proxy' not in builddefaults and 'no_proxy' in common:
  1282. builddefaults['no_proxy'] = common['no_proxy']
  1283. if 'git_http_proxy' not in builddefaults and 'http_proxy' in builddefaults:
  1284. builddefaults['git_http_proxy'] = builddefaults['http_proxy']
  1285. if 'git_https_proxy' not in builddefaults and 'https_proxy' in builddefaults:
  1286. builddefaults['git_https_proxy'] = builddefaults['https_proxy']
  1287. # If we're actually defining a proxy config then create kube_admission_plugin_config
  1288. # if it doesn't exist, then merge builddefaults[config] structure
  1289. # into kube_admission_plugin_config
  1290. if 'kube_admission_plugin_config' not in facts['master']:
  1291. facts['master']['kube_admission_plugin_config'] = dict()
  1292. if 'config' in builddefaults and ('http_proxy' in builddefaults or \
  1293. 'https_proxy' in builddefaults):
  1294. facts['master']['kube_admission_plugin_config'].update(builddefaults['config'])
  1295. facts['builddefaults'] = builddefaults
  1296. return facts
  1297. # pylint: disable=too-many-statements
  1298. def set_container_facts_if_unset(facts):
  1299. """ Set containerized facts.
  1300. Args:
  1301. facts (dict): existing facts
  1302. Returns:
  1303. dict: the facts dict updated with the generated containerization
  1304. facts
  1305. """
  1306. deployment_type = facts['common']['deployment_type']
  1307. if deployment_type in ['enterprise', 'openshift-enterprise']:
  1308. master_image = 'openshift3/ose'
  1309. cli_image = master_image
  1310. node_image = 'openshift3/node'
  1311. ovs_image = 'openshift3/openvswitch'
  1312. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  1313. pod_image = 'openshift3/ose-pod'
  1314. router_image = 'openshift3/ose-haproxy-router'
  1315. registry_image = 'openshift3/ose-docker-registry'
  1316. deployer_image = 'openshift3/ose-deployer'
  1317. elif deployment_type == 'atomic-enterprise':
  1318. master_image = 'aep3_beta/aep'
  1319. cli_image = master_image
  1320. node_image = 'aep3_beta/node'
  1321. ovs_image = 'aep3_beta/openvswitch'
  1322. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  1323. pod_image = 'aep3_beta/aep-pod'
  1324. router_image = 'aep3_beta/aep-haproxy-router'
  1325. registry_image = 'aep3_beta/aep-docker-registry'
  1326. deployer_image = 'aep3_beta/aep-deployer'
  1327. else:
  1328. master_image = 'openshift/origin'
  1329. cli_image = master_image
  1330. node_image = 'openshift/node'
  1331. ovs_image = 'openshift/openvswitch'
  1332. etcd_image = 'registry.access.redhat.com/rhel7/etcd'
  1333. pod_image = 'openshift/origin-pod'
  1334. router_image = 'openshift/origin-haproxy-router'
  1335. registry_image = 'openshift/origin-docker-registry'
  1336. deployer_image = 'openshift/origin-deployer'
  1337. facts['common']['is_atomic'] = os.path.isfile('/run/ostree-booted')
  1338. if 'is_containerized' not in facts['common']:
  1339. facts['common']['is_containerized'] = facts['common']['is_atomic']
  1340. if 'cli_image' not in facts['common']:
  1341. facts['common']['cli_image'] = cli_image
  1342. if 'pod_image' not in facts['common']:
  1343. facts['common']['pod_image'] = pod_image
  1344. if 'router_image' not in facts['common']:
  1345. facts['common']['router_image'] = router_image
  1346. if 'registry_image' not in facts['common']:
  1347. facts['common']['registry_image'] = registry_image
  1348. if 'deployer_image' not in facts['common']:
  1349. facts['common']['deployer_image'] = deployer_image
  1350. if 'etcd' in facts and 'etcd_image' not in facts['etcd']:
  1351. facts['etcd']['etcd_image'] = etcd_image
  1352. if 'master' in facts and 'master_image' not in facts['master']:
  1353. facts['master']['master_image'] = master_image
  1354. if 'node' in facts:
  1355. if 'node_image' not in facts['node']:
  1356. facts['node']['node_image'] = node_image
  1357. if 'ovs_image' not in facts['node']:
  1358. facts['node']['ovs_image'] = ovs_image
  1359. if safe_get_bool(facts['common']['is_containerized']):
  1360. facts['common']['admin_binary'] = '/usr/local/bin/oadm'
  1361. facts['common']['client_binary'] = '/usr/local/bin/oc'
  1362. return facts
  1363. def set_installed_variant_rpm_facts(facts):
  1364. """ Set RPM facts of installed variant
  1365. Args:
  1366. facts (dict): existing facts
  1367. Returns:
  1368. dict: the facts dict updated with installed_variant_rpms
  1369. """
  1370. installed_rpms = []
  1371. for base_rpm in ['openshift', 'atomic-openshift', 'origin']:
  1372. optional_rpms = ['master', 'node', 'clients', 'sdn-ovs']
  1373. variant_rpms = [base_rpm] + \
  1374. ['{0}-{1}'.format(base_rpm, r) for r in optional_rpms] + \
  1375. ['tuned-profiles-%s-node' % base_rpm]
  1376. for rpm in variant_rpms:
  1377. exit_code, _, _ = module.run_command(['rpm', '-q', rpm])
  1378. if exit_code == 0:
  1379. installed_rpms.append(rpm)
  1380. facts['common']['installed_variant_rpms'] = installed_rpms
  1381. return facts
  1382. class OpenShiftFactsInternalError(Exception):
  1383. """Origin Facts Error"""
  1384. pass
  1385. class OpenShiftFactsUnsupportedRoleError(Exception):
  1386. """Origin Facts Unsupported Role Error"""
  1387. pass
  1388. class OpenShiftFactsFileWriteError(Exception):
  1389. """Origin Facts File Write Error"""
  1390. pass
  1391. class OpenShiftFactsMetadataUnavailableError(Exception):
  1392. """Origin Facts Metadata Unavailable Error"""
  1393. pass
  1394. class OpenShiftFacts(object):
  1395. """ Origin Facts
  1396. Attributes:
  1397. facts (dict): facts for the host
  1398. Args:
  1399. module (AnsibleModule): an AnsibleModule object
  1400. role (str): role for setting local facts
  1401. filename (str): local facts file to use
  1402. local_facts (dict): local facts to set
  1403. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1404. '.' notation ex: ['master.named_certificates']
  1405. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1406. '.' notation ex: ['master.master_count']
  1407. Raises:
  1408. OpenShiftFactsUnsupportedRoleError:
  1409. """
  1410. known_roles = ['builddefaults',
  1411. 'clock',
  1412. 'cloudprovider',
  1413. 'common',
  1414. 'docker',
  1415. 'etcd',
  1416. 'hosted',
  1417. 'master',
  1418. 'node']
  1419. # Disabling too-many-arguments, this should be cleaned up as a TODO item.
  1420. # pylint: disable=too-many-arguments,no-value-for-parameter
  1421. def __init__(self, role, filename, local_facts,
  1422. additive_facts_to_overwrite=None,
  1423. openshift_env=None,
  1424. openshift_env_structures=None,
  1425. protected_facts_to_overwrite=None):
  1426. self.changed = False
  1427. self.filename = filename
  1428. if role not in self.known_roles:
  1429. raise OpenShiftFactsUnsupportedRoleError(
  1430. "Role %s is not supported by this module" % role
  1431. )
  1432. self.role = role
  1433. try:
  1434. # ansible-2.1
  1435. # pylint: disable=too-many-function-args,invalid-name
  1436. self.system_facts = ansible_facts(module, ['hardware', 'network', 'virtual', 'facter'])
  1437. for (k, v) in self.system_facts.items():
  1438. self.system_facts["ansible_%s" % k.replace('-', '_')] = v
  1439. except UnboundLocalError:
  1440. # ansible-2.2
  1441. self.system_facts = get_all_facts(module)['ansible_facts']
  1442. self.facts = self.generate_facts(local_facts,
  1443. additive_facts_to_overwrite,
  1444. openshift_env,
  1445. openshift_env_structures,
  1446. protected_facts_to_overwrite)
  1447. def generate_facts(self,
  1448. local_facts,
  1449. additive_facts_to_overwrite,
  1450. openshift_env,
  1451. openshift_env_structures,
  1452. protected_facts_to_overwrite):
  1453. """ Generate facts
  1454. Args:
  1455. local_facts (dict): local_facts for overriding generated defaults
  1456. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1457. '.' notation ex: ['master.named_certificates']
  1458. openshift_env (dict): openshift_env facts for overriding generated defaults
  1459. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1460. '.' notation ex: ['master.master_count']
  1461. Returns:
  1462. dict: The generated facts
  1463. """
  1464. local_facts = self.init_local_facts(local_facts,
  1465. additive_facts_to_overwrite,
  1466. openshift_env,
  1467. openshift_env_structures,
  1468. protected_facts_to_overwrite)
  1469. roles = local_facts.keys()
  1470. if 'common' in local_facts and 'deployment_type' in local_facts['common']:
  1471. deployment_type = local_facts['common']['deployment_type']
  1472. else:
  1473. deployment_type = 'origin'
  1474. if 'common' in local_facts and 'deployment_subtype' in local_facts['common']:
  1475. deployment_subtype = local_facts['common']['deployment_subtype']
  1476. else:
  1477. deployment_subtype = 'basic'
  1478. defaults = self.get_defaults(roles, deployment_type, deployment_subtype)
  1479. provider_facts = self.init_provider_facts()
  1480. facts = apply_provider_facts(defaults, provider_facts)
  1481. facts = merge_facts(facts,
  1482. local_facts,
  1483. additive_facts_to_overwrite,
  1484. protected_facts_to_overwrite)
  1485. facts = migrate_oauth_template_facts(facts)
  1486. facts['current_config'] = get_current_config(facts)
  1487. facts = set_url_facts_if_unset(facts)
  1488. facts = set_project_cfg_facts_if_unset(facts)
  1489. facts = set_flannel_facts_if_unset(facts)
  1490. facts = set_nuage_facts_if_unset(facts)
  1491. facts = set_node_schedulability(facts)
  1492. facts = set_selectors(facts)
  1493. facts = set_identity_providers_if_unset(facts)
  1494. facts = set_sdn_facts_if_unset(facts, self.system_facts)
  1495. facts = set_deployment_facts_if_unset(facts)
  1496. facts = set_container_facts_if_unset(facts)
  1497. facts = build_kubelet_args(facts)
  1498. facts = build_controller_args(facts)
  1499. facts = build_api_server_args(facts)
  1500. facts = set_version_facts_if_unset(facts)
  1501. facts = set_dnsmasq_facts_if_unset(facts)
  1502. facts = set_manageiq_facts_if_unset(facts)
  1503. facts = set_aggregate_facts(facts)
  1504. facts = set_etcd_facts_if_unset(facts)
  1505. facts = set_proxy_facts(facts)
  1506. if not safe_get_bool(facts['common']['is_containerized']):
  1507. facts = set_installed_variant_rpm_facts(facts)
  1508. facts = set_nodename(facts)
  1509. return dict(openshift=facts)
  1510. def get_defaults(self, roles, deployment_type, deployment_subtype):
  1511. """ Get default fact values
  1512. Args:
  1513. roles (list): list of roles for this host
  1514. Returns:
  1515. dict: The generated default facts
  1516. """
  1517. defaults = {}
  1518. ip_addr = self.system_facts['ansible_default_ipv4']['address']
  1519. exit_code, output, _ = module.run_command(['hostname', '-f'])
  1520. hostname_f = output.strip() if exit_code == 0 else ''
  1521. hostname_values = [hostname_f, self.system_facts['ansible_nodename'],
  1522. self.system_facts['ansible_fqdn']]
  1523. hostname = choose_hostname(hostname_values, ip_addr)
  1524. defaults['common'] = dict(use_openshift_sdn=True, ip=ip_addr,
  1525. public_ip=ip_addr,
  1526. deployment_type=deployment_type,
  1527. deployment_subtype=deployment_subtype,
  1528. hostname=hostname,
  1529. public_hostname=hostname,
  1530. portal_net='172.30.0.0/16',
  1531. client_binary='oc', admin_binary='oadm',
  1532. dns_domain='cluster.local',
  1533. install_examples=True,
  1534. debug_level=2)
  1535. if 'master' in roles:
  1536. scheduler_predicates = [
  1537. {"name": "MatchNodeSelector"},
  1538. {"name": "PodFitsResources"},
  1539. {"name": "PodFitsPorts"},
  1540. {"name": "NoDiskConflict"},
  1541. {"name": "NoVolumeZoneConflict"},
  1542. {"name": "MaxEBSVolumeCount"},
  1543. {"name": "MaxGCEPDVolumeCount"},
  1544. {"name": "Region", "argument": {"serviceAffinity" : {"labels" : ["region"]}}}
  1545. ]
  1546. scheduler_priorities = [
  1547. {"name": "LeastRequestedPriority", "weight": 1},
  1548. {"name": "SelectorSpreadPriority", "weight": 1},
  1549. {"name": "Zone", "weight" : 2, "argument": {"serviceAntiAffinity" : {"label": "zone"}}}
  1550. ]
  1551. defaults['master'] = dict(api_use_ssl=True, api_port='8443',
  1552. controllers_port='8444',
  1553. console_use_ssl=True,
  1554. console_path='/console',
  1555. console_port='8443', etcd_use_ssl=True,
  1556. etcd_hosts='', etcd_port='4001',
  1557. portal_net='172.30.0.0/16',
  1558. embedded_etcd=True, embedded_kube=True,
  1559. embedded_dns=True,
  1560. bind_addr='0.0.0.0',
  1561. session_max_seconds=3600,
  1562. session_name='ssn',
  1563. session_secrets_file='',
  1564. access_token_max_seconds=86400,
  1565. auth_token_max_seconds=500,
  1566. oauth_grant_method='auto',
  1567. scheduler_predicates=scheduler_predicates,
  1568. scheduler_priorities=scheduler_priorities,
  1569. dynamic_provisioning_enabled=True,
  1570. max_requests_inflight=500)
  1571. if 'node' in roles:
  1572. defaults['node'] = dict(labels={}, annotations={},
  1573. iptables_sync_period='30s',
  1574. local_quota_per_fsgroup="",
  1575. set_node_ip=False)
  1576. if 'docker' in roles:
  1577. docker = dict(disable_push_dockerhub=False,
  1578. hosted_registry_insecure=True,
  1579. options='--log-driver=json-file --log-opt max-size=50m')
  1580. version_info = get_docker_version_info()
  1581. if version_info is not None:
  1582. docker['api_version'] = version_info['api_version']
  1583. docker['version'] = version_info['version']
  1584. docker['gte_1_10'] = LooseVersion(version_info['version']) >= LooseVersion('1.10')
  1585. defaults['docker'] = docker
  1586. if 'clock' in roles:
  1587. exit_code, _, _ = module.run_command(['rpm', '-q', 'chrony'])
  1588. chrony_installed = bool(exit_code == 0)
  1589. defaults['clock'] = dict(
  1590. enabled=True,
  1591. chrony_installed=chrony_installed)
  1592. if 'cloudprovider' in roles:
  1593. defaults['cloudprovider'] = dict(kind=None)
  1594. if 'hosted' in roles or self.role == 'hosted':
  1595. defaults['hosted'] = dict(
  1596. metrics=dict(
  1597. deploy=False,
  1598. duration=7,
  1599. resolution='10s',
  1600. storage=dict(
  1601. kind=None,
  1602. volume=dict(
  1603. name='metrics',
  1604. size='10Gi'
  1605. ),
  1606. nfs=dict(
  1607. directory='/exports',
  1608. options='*(rw,root_squash)'
  1609. ),
  1610. host=None,
  1611. access_modes=['ReadWriteOnce'],
  1612. create_pv=True,
  1613. create_pvc=False
  1614. )
  1615. ),
  1616. logging=dict(
  1617. storage=dict(
  1618. kind=None,
  1619. volume=dict(
  1620. name='logging-es',
  1621. size='10Gi'
  1622. ),
  1623. nfs=dict(
  1624. directory='/exports',
  1625. options='*(rw,root_squash)'
  1626. ),
  1627. host=None,
  1628. access_modes=['ReadWriteOnce'],
  1629. create_pv=True,
  1630. create_pvc=False
  1631. )
  1632. ),
  1633. registry=dict(
  1634. storage=dict(
  1635. kind=None,
  1636. volume=dict(
  1637. name='registry',
  1638. size='5Gi'
  1639. ),
  1640. nfs=dict(
  1641. directory='/exports',
  1642. options='*(rw,root_squash)'),
  1643. host=None,
  1644. access_modes=['ReadWriteMany'],
  1645. create_pv=True,
  1646. create_pvc=True
  1647. )
  1648. ),
  1649. router=dict()
  1650. )
  1651. return defaults
  1652. def guess_host_provider(self):
  1653. """ Guess the host provider
  1654. Returns:
  1655. dict: The generated default facts for the detected provider
  1656. """
  1657. # TODO: cloud provider facts should probably be submitted upstream
  1658. product_name = self.system_facts['ansible_product_name']
  1659. product_version = self.system_facts['ansible_product_version']
  1660. virt_type = self.system_facts['ansible_virtualization_type']
  1661. virt_role = self.system_facts['ansible_virtualization_role']
  1662. provider = None
  1663. metadata = None
  1664. # TODO: this is not exposed through module_utils/facts.py in ansible,
  1665. # need to create PR for ansible to expose it
  1666. bios_vendor = get_file_content(
  1667. '/sys/devices/virtual/dmi/id/bios_vendor'
  1668. )
  1669. if bios_vendor == 'Google':
  1670. provider = 'gce'
  1671. metadata_url = ('http://metadata.google.internal/'
  1672. 'computeMetadata/v1/?recursive=true')
  1673. headers = {'Metadata-Flavor': 'Google'}
  1674. metadata = get_provider_metadata(metadata_url, True, headers,
  1675. True)
  1676. # Filter sshKeys and serviceAccounts from gce metadata
  1677. if metadata:
  1678. metadata['project']['attributes'].pop('sshKeys', None)
  1679. metadata['instance'].pop('serviceAccounts', None)
  1680. elif (virt_type == 'xen' and virt_role == 'guest'
  1681. and re.match(r'.*\.amazon$', product_version)):
  1682. provider = 'aws'
  1683. metadata_url = 'http://169.254.169.254/latest/meta-data/'
  1684. metadata = get_provider_metadata(metadata_url)
  1685. elif re.search(r'OpenStack', product_name):
  1686. provider = 'openstack'
  1687. metadata_url = ('http://169.254.169.254/openstack/latest/'
  1688. 'meta_data.json')
  1689. metadata = get_provider_metadata(metadata_url, True, None,
  1690. True)
  1691. if metadata:
  1692. ec2_compat_url = 'http://169.254.169.254/latest/meta-data/'
  1693. metadata['ec2_compat'] = get_provider_metadata(
  1694. ec2_compat_url
  1695. )
  1696. # disable pylint maybe-no-member because overloaded use of
  1697. # the module name causes pylint to not detect that results
  1698. # is an array or hash
  1699. # pylint: disable=maybe-no-member
  1700. # Filter public_keys and random_seed from openstack metadata
  1701. metadata.pop('public_keys', None)
  1702. metadata.pop('random_seed', None)
  1703. if not metadata['ec2_compat']:
  1704. metadata = None
  1705. return dict(name=provider, metadata=metadata)
  1706. def init_provider_facts(self):
  1707. """ Initialize the provider facts
  1708. Returns:
  1709. dict: The normalized provider facts
  1710. """
  1711. provider_info = self.guess_host_provider()
  1712. provider_facts = normalize_provider_facts(
  1713. provider_info.get('name'),
  1714. provider_info.get('metadata')
  1715. )
  1716. return provider_facts
  1717. @staticmethod
  1718. def split_openshift_env_fact_keys(openshift_env_fact, openshift_env_structures):
  1719. """ Split openshift_env facts based on openshift_env structures.
  1720. Args:
  1721. openshift_env_fact (string): the openshift_env fact to split
  1722. ex: 'openshift_cloudprovider_openstack_auth_url'
  1723. openshift_env_structures (list): a list of structures to determine fact keys
  1724. ex: ['openshift.cloudprovider.openstack.*']
  1725. Returns:
  1726. list: a list of keys that represent the fact
  1727. ex: ['openshift', 'cloudprovider', 'openstack', 'auth_url']
  1728. """
  1729. # By default, we'll split an openshift_env fact by underscores.
  1730. fact_keys = openshift_env_fact.split('_')
  1731. # Determine if any of the provided variable structures match the fact.
  1732. matching_structure = None
  1733. if openshift_env_structures != None:
  1734. for structure in openshift_env_structures:
  1735. if re.match(structure, openshift_env_fact):
  1736. matching_structure = structure
  1737. # Fact didn't match any variable structures so return the default fact keys.
  1738. if matching_structure is None:
  1739. return fact_keys
  1740. final_keys = []
  1741. structure_keys = matching_structure.split('.')
  1742. for structure_key in structure_keys:
  1743. # Matched current key. Add to final keys.
  1744. if structure_key == fact_keys[structure_keys.index(structure_key)]:
  1745. final_keys.append(structure_key)
  1746. # Wildcard means we will be taking everything from here to the end of the fact.
  1747. elif structure_key == '*':
  1748. final_keys.append('_'.join(fact_keys[structure_keys.index(structure_key):]))
  1749. # Shouldn't have gotten here, return the fact keys.
  1750. else:
  1751. return fact_keys
  1752. return final_keys
  1753. # Disabling too-many-branches and too-many-locals.
  1754. # This should be cleaned up as a TODO item.
  1755. #pylint: disable=too-many-branches, too-many-locals
  1756. def init_local_facts(self, facts=None,
  1757. additive_facts_to_overwrite=None,
  1758. openshift_env=None,
  1759. openshift_env_structures=None,
  1760. protected_facts_to_overwrite=None):
  1761. """ Initialize the local facts
  1762. Args:
  1763. facts (dict): local facts to set
  1764. additive_facts_to_overwrite (list): additive facts to overwrite in jinja
  1765. '.' notation ex: ['master.named_certificates']
  1766. openshift_env (dict): openshift env facts to set
  1767. protected_facts_to_overwrite (list): protected facts to overwrite in jinja
  1768. '.' notation ex: ['master.master_count']
  1769. Returns:
  1770. dict: The result of merging the provided facts with existing
  1771. local facts
  1772. """
  1773. changed = False
  1774. facts_to_set = dict()
  1775. if facts is not None:
  1776. facts_to_set[self.role] = facts
  1777. if openshift_env != {} and openshift_env != None:
  1778. for fact, value in openshift_env.iteritems():
  1779. oo_env_facts = dict()
  1780. current_level = oo_env_facts
  1781. keys = self.split_openshift_env_fact_keys(fact, openshift_env_structures)[1:]
  1782. if len(keys) > 0 and keys[0] != self.role:
  1783. continue
  1784. for key in keys:
  1785. if key == keys[-1]:
  1786. current_level[key] = value
  1787. elif key not in current_level:
  1788. current_level[key] = dict()
  1789. current_level = current_level[key]
  1790. facts_to_set = merge_facts(orig=facts_to_set,
  1791. new=oo_env_facts,
  1792. additive_facts_to_overwrite=[],
  1793. protected_facts_to_overwrite=[])
  1794. local_facts = get_local_facts_from_file(self.filename)
  1795. migrated_facts = migrate_local_facts(local_facts)
  1796. new_local_facts = merge_facts(migrated_facts,
  1797. facts_to_set,
  1798. additive_facts_to_overwrite,
  1799. protected_facts_to_overwrite)
  1800. if 'docker' in new_local_facts:
  1801. # remove duplicate and empty strings from registry lists
  1802. for cat in ['additional', 'blocked', 'insecure']:
  1803. key = '{0}_registries'.format(cat)
  1804. if key in new_local_facts['docker']:
  1805. val = new_local_facts['docker'][key]
  1806. if isinstance(val, basestring):
  1807. val = [x.strip() for x in val.split(',')]
  1808. new_local_facts['docker'][key] = list(set(val) - set(['']))
  1809. # Convert legacy log_options comma sep string to a list if present:
  1810. if 'log_options' in new_local_facts['docker'] and \
  1811. isinstance(new_local_facts['docker']['log_options'], basestring):
  1812. new_local_facts['docker']['log_options'] = new_local_facts['docker']['log_options'].split(',')
  1813. new_local_facts = self.remove_empty_facts(new_local_facts)
  1814. if new_local_facts != local_facts:
  1815. self.validate_local_facts(new_local_facts)
  1816. changed = True
  1817. if not module.check_mode:
  1818. save_local_facts(self.filename, new_local_facts)
  1819. self.changed = changed
  1820. return new_local_facts
  1821. def remove_empty_facts(self, facts=None):
  1822. """ Remove empty facts
  1823. Args:
  1824. facts (dict): facts to clean
  1825. """
  1826. facts_to_remove = []
  1827. for fact, value in facts.iteritems():
  1828. if isinstance(facts[fact], dict):
  1829. facts[fact] = self.remove_empty_facts(facts[fact])
  1830. else:
  1831. if value == "" or value == [""] or value is None:
  1832. facts_to_remove.append(fact)
  1833. for fact in facts_to_remove:
  1834. del facts[fact]
  1835. return facts
  1836. def validate_local_facts(self, facts=None):
  1837. """ Validate local facts
  1838. Args:
  1839. facts (dict): local facts to validate
  1840. """
  1841. invalid_facts = dict()
  1842. invalid_facts = self.validate_master_facts(facts, invalid_facts)
  1843. if invalid_facts:
  1844. msg = 'Invalid facts detected:\n'
  1845. for key in invalid_facts.keys():
  1846. msg += '{0}: {1}\n'.format(key, invalid_facts[key])
  1847. module.fail_json(msg=msg,
  1848. changed=self.changed)
  1849. # disabling pylint errors for line-too-long since we're dealing
  1850. # with best effort reduction of error messages here.
  1851. # disabling errors for too-many-branches since we require checking
  1852. # many conditions.
  1853. # pylint: disable=line-too-long, too-many-branches
  1854. @staticmethod
  1855. def validate_master_facts(facts, invalid_facts):
  1856. """ Validate master facts
  1857. Args:
  1858. facts (dict): local facts to validate
  1859. invalid_facts (dict): collected invalid_facts
  1860. Returns:
  1861. dict: Invalid facts
  1862. """
  1863. if 'master' in facts:
  1864. # openshift.master.session_auth_secrets
  1865. if 'session_auth_secrets' in facts['master']:
  1866. session_auth_secrets = facts['master']['session_auth_secrets']
  1867. if not issubclass(type(session_auth_secrets), list):
  1868. invalid_facts['session_auth_secrets'] = 'Expects session_auth_secrets is a list.'
  1869. elif 'session_encryption_secrets' not in facts['master']:
  1870. invalid_facts['session_auth_secrets'] = ('openshift_master_session_encryption secrets must be set '
  1871. 'if openshift_master_session_auth_secrets is provided.')
  1872. elif len(session_auth_secrets) != len(facts['master']['session_encryption_secrets']):
  1873. invalid_facts['session_auth_secrets'] = ('openshift_master_session_auth_secrets and '
  1874. 'openshift_master_session_encryption_secrets must be '
  1875. 'equal length.')
  1876. else:
  1877. for secret in session_auth_secrets:
  1878. if len(secret) < 32:
  1879. invalid_facts['session_auth_secrets'] = ('Invalid secret in session_auth_secrets. '
  1880. 'Secrets must be at least 32 characters in length.')
  1881. # openshift.master.session_encryption_secrets
  1882. if 'session_encryption_secrets' in facts['master']:
  1883. session_encryption_secrets = facts['master']['session_encryption_secrets']
  1884. if not issubclass(type(session_encryption_secrets), list):
  1885. invalid_facts['session_encryption_secrets'] = 'Expects session_encryption_secrets is a list.'
  1886. elif 'session_auth_secrets' not in facts['master']:
  1887. invalid_facts['session_encryption_secrets'] = ('openshift_master_session_auth_secrets must be '
  1888. 'set if openshift_master_session_encryption_secrets '
  1889. 'is provided.')
  1890. else:
  1891. for secret in session_encryption_secrets:
  1892. if len(secret) not in [16, 24, 32]:
  1893. invalid_facts['session_encryption_secrets'] = ('Invalid secret in session_encryption_secrets. '
  1894. 'Secrets must be 16, 24, or 32 characters in length.')
  1895. return invalid_facts
  1896. def main():
  1897. """ main """
  1898. # disabling pylint errors for global-variable-undefined and invalid-name
  1899. # for 'global module' usage, since it is required to use ansible_facts
  1900. # pylint: disable=global-variable-undefined, invalid-name
  1901. global module
  1902. module = AnsibleModule(
  1903. argument_spec=dict(
  1904. role=dict(default='common', required=False,
  1905. choices=OpenShiftFacts.known_roles),
  1906. local_facts=dict(default=None, type='dict', required=False),
  1907. additive_facts_to_overwrite=dict(default=[], type='list', required=False),
  1908. openshift_env=dict(default={}, type='dict', required=False),
  1909. openshift_env_structures=dict(default=[], type='list', required=False),
  1910. protected_facts_to_overwrite=dict(default=[], type='list', required=False)
  1911. ),
  1912. supports_check_mode=True,
  1913. add_file_common_args=True,
  1914. )
  1915. module.params['gather_subset'] = ['hardware', 'network', 'virtual', 'facter']
  1916. module.params['gather_timeout'] = 10
  1917. module.params['filter'] = '*'
  1918. role = module.params['role']
  1919. local_facts = module.params['local_facts']
  1920. additive_facts_to_overwrite = module.params['additive_facts_to_overwrite']
  1921. openshift_env = module.params['openshift_env']
  1922. openshift_env_structures = module.params['openshift_env_structures']
  1923. protected_facts_to_overwrite = module.params['protected_facts_to_overwrite']
  1924. fact_file = '/etc/ansible/facts.d/openshift.fact'
  1925. openshift_facts = OpenShiftFacts(role,
  1926. fact_file,
  1927. local_facts,
  1928. additive_facts_to_overwrite,
  1929. openshift_env,
  1930. openshift_env_structures,
  1931. protected_facts_to_overwrite)
  1932. file_params = module.params.copy()
  1933. file_params['path'] = fact_file
  1934. file_args = module.load_file_common_arguments(file_params)
  1935. changed = module.set_fs_attributes_if_different(file_args,
  1936. openshift_facts.changed)
  1937. return module.exit_json(changed=changed,
  1938. ansible_facts=openshift_facts.facts)
  1939. # ignore pylint errors related to the module_utils import
  1940. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
  1941. # import module snippets
  1942. from ansible.module_utils.basic import *
  1943. from ansible.module_utils.facts import *
  1944. from ansible.module_utils.urls import *
  1945. if __name__ == '__main__':
  1946. main()