openshift_facts.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. #!/usr/bin/python
  2. # pylint: disable=too-many-lines
  3. # -*- coding: utf-8 -*-
  4. # vim: expandtab:tabstop=4:shiftwidth=4
  5. # Reason: Disable pylint too-many-lines because we don't want to split up this file.
  6. # Status: Permanently disabled to keep this module as self-contained as possible.
  7. """Ansible module for retrieving and setting openshift related facts"""
  8. DOCUMENTATION = '''
  9. ---
  10. module: openshift_facts
  11. short_description: Cluster Facts
  12. author: Jason DeTiberus
  13. requirements: [ ]
  14. '''
  15. EXAMPLES = '''
  16. '''
  17. import ConfigParser
  18. import copy
  19. import os
  20. from distutils.util import strtobool
  21. from distutils.version import LooseVersion
  22. from netaddr import IPNetwork
  23. def hostname_valid(hostname):
  24. """ Test if specified hostname should be considered valid
  25. Args:
  26. hostname (str): hostname to test
  27. Returns:
  28. bool: True if valid, otherwise False
  29. """
  30. if (not hostname or
  31. hostname.startswith('localhost') or
  32. hostname.endswith('localdomain') or
  33. len(hostname.split('.')) < 2):
  34. return False
  35. return True
  36. def choose_hostname(hostnames=None, fallback=''):
  37. """ Choose a hostname from the provided hostnames
  38. Given a list of hostnames and a fallback value, choose a hostname to
  39. use. This function will prefer fqdns if they exist (excluding any that
  40. begin with localhost or end with localdomain) over ip addresses.
  41. Args:
  42. hostnames (list): list of hostnames
  43. fallback (str): default value to set if hostnames does not contain
  44. a valid hostname
  45. Returns:
  46. str: chosen hostname
  47. """
  48. hostname = fallback
  49. if hostnames is None:
  50. return hostname
  51. ip_regex = r'\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\Z'
  52. ips = [i for i in hostnames
  53. if (i is not None and isinstance(i, basestring)
  54. and re.match(ip_regex, i))]
  55. hosts = [i for i in hostnames
  56. if i is not None and i != '' and i not in ips]
  57. for host_list in (hosts, ips):
  58. for host in host_list:
  59. if hostname_valid(host):
  60. return host
  61. return hostname
  62. def query_metadata(metadata_url, headers=None, expect_json=False):
  63. """ Return metadata from the provided metadata_url
  64. Args:
  65. metadata_url (str): metadata url
  66. headers (dict): headers to set for metadata request
  67. expect_json (bool): does the metadata_url return json
  68. Returns:
  69. dict or list: metadata request result
  70. """
  71. result, info = fetch_url(module, metadata_url, headers=headers)
  72. if info['status'] != 200:
  73. raise OpenShiftFactsMetadataUnavailableError("Metadata unavailable")
  74. if expect_json:
  75. return module.from_json(result.read())
  76. else:
  77. return [line.strip() for line in result.readlines()]
  78. def walk_metadata(metadata_url, headers=None, expect_json=False):
  79. """ Walk the metadata tree and return a dictionary of the entire tree
  80. Args:
  81. metadata_url (str): metadata url
  82. headers (dict): headers to set for metadata request
  83. expect_json (bool): does the metadata_url return json
  84. Returns:
  85. dict: the result of walking the metadata tree
  86. """
  87. metadata = dict()
  88. for line in query_metadata(metadata_url, headers, expect_json):
  89. if line.endswith('/') and not line == 'public-keys/':
  90. key = line[:-1]
  91. metadata[key] = walk_metadata(metadata_url + line,
  92. headers, expect_json)
  93. else:
  94. results = query_metadata(metadata_url + line, headers,
  95. expect_json)
  96. if len(results) == 1:
  97. # disable pylint maybe-no-member because overloaded use of
  98. # the module name causes pylint to not detect that results
  99. # is an array or hash
  100. # pylint: disable=maybe-no-member
  101. metadata[line] = results.pop()
  102. else:
  103. metadata[line] = results
  104. return metadata
  105. def get_provider_metadata(metadata_url, supports_recursive=False,
  106. headers=None, expect_json=False):
  107. """ Retrieve the provider metadata
  108. Args:
  109. metadata_url (str): metadata url
  110. supports_recursive (bool): does the provider metadata api support
  111. recursion
  112. headers (dict): headers to set for metadata request
  113. expect_json (bool): does the metadata_url return json
  114. Returns:
  115. dict: the provider metadata
  116. """
  117. try:
  118. if supports_recursive:
  119. metadata = query_metadata(metadata_url, headers,
  120. expect_json)
  121. else:
  122. metadata = walk_metadata(metadata_url, headers,
  123. expect_json)
  124. except OpenShiftFactsMetadataUnavailableError:
  125. metadata = None
  126. return metadata
  127. def normalize_gce_facts(metadata, facts):
  128. """ Normalize gce facts
  129. Args:
  130. metadata (dict): provider metadata
  131. facts (dict): facts to update
  132. Returns:
  133. dict: the result of adding the normalized metadata to the provided
  134. facts dict
  135. """
  136. for interface in metadata['instance']['networkInterfaces']:
  137. int_info = dict(ips=[interface['ip']], network_type='gce')
  138. int_info['public_ips'] = [ac['externalIp'] for ac
  139. in interface['accessConfigs']]
  140. int_info['public_ips'].extend(interface['forwardedIps'])
  141. _, _, network_id = interface['network'].rpartition('/')
  142. int_info['network_id'] = network_id
  143. facts['network']['interfaces'].append(int_info)
  144. _, _, zone = metadata['instance']['zone'].rpartition('/')
  145. facts['zone'] = zone
  146. # Default to no sdn for GCE deployments
  147. facts['use_openshift_sdn'] = False
  148. # GCE currently only supports a single interface
  149. facts['network']['ip'] = facts['network']['interfaces'][0]['ips'][0]
  150. pub_ip = facts['network']['interfaces'][0]['public_ips'][0]
  151. facts['network']['public_ip'] = pub_ip
  152. facts['network']['hostname'] = metadata['instance']['hostname']
  153. # TODO: attempt to resolve public_hostname
  154. facts['network']['public_hostname'] = facts['network']['public_ip']
  155. return facts
  156. def normalize_aws_facts(metadata, facts):
  157. """ Normalize aws facts
  158. Args:
  159. metadata (dict): provider metadata
  160. facts (dict): facts to update
  161. Returns:
  162. dict: the result of adding the normalized metadata to the provided
  163. facts dict
  164. """
  165. for interface in sorted(
  166. metadata['network']['interfaces']['macs'].values(),
  167. key=lambda x: x['device-number']
  168. ):
  169. int_info = dict()
  170. var_map = {'ips': 'local-ipv4s', 'public_ips': 'public-ipv4s'}
  171. for ips_var, int_var in var_map.iteritems():
  172. ips = interface.get(int_var)
  173. if isinstance(ips, basestring):
  174. int_info[ips_var] = [ips]
  175. else:
  176. int_info[ips_var] = ips
  177. if 'vpc-id' in interface:
  178. int_info['network_type'] = 'vpc'
  179. else:
  180. int_info['network_type'] = 'classic'
  181. if int_info['network_type'] == 'vpc':
  182. int_info['network_id'] = interface['subnet-id']
  183. else:
  184. int_info['network_id'] = None
  185. facts['network']['interfaces'].append(int_info)
  186. facts['zone'] = metadata['placement']['availability-zone']
  187. # TODO: actually attempt to determine default local and public ips
  188. # by using the ansible default ip fact and the ipv4-associations
  189. # from the ec2 metadata
  190. facts['network']['ip'] = metadata.get('local-ipv4')
  191. facts['network']['public_ip'] = metadata.get('public-ipv4')
  192. # TODO: verify that local hostname makes sense and is resolvable
  193. facts['network']['hostname'] = metadata.get('local-hostname')
  194. # TODO: verify that public hostname makes sense and is resolvable
  195. facts['network']['public_hostname'] = metadata.get('public-hostname')
  196. return facts
  197. def normalize_openstack_facts(metadata, facts):
  198. """ Normalize openstack facts
  199. Args:
  200. metadata (dict): provider metadata
  201. facts (dict): facts to update
  202. Returns:
  203. dict: the result of adding the normalized metadata to the provided
  204. facts dict
  205. """
  206. # openstack ec2 compat api does not support network interfaces and
  207. # the version tested on did not include the info in the openstack
  208. # metadata api, should be updated if neutron exposes this.
  209. facts['zone'] = metadata['availability_zone']
  210. local_ipv4 = metadata['ec2_compat']['local-ipv4'].split(',')[0]
  211. facts['network']['ip'] = local_ipv4
  212. facts['network']['public_ip'] = metadata['ec2_compat']['public-ipv4']
  213. # TODO: verify local hostname makes sense and is resolvable
  214. facts['network']['hostname'] = metadata['hostname']
  215. # TODO: verify that public hostname makes sense and is resolvable
  216. pub_h = metadata['ec2_compat']['public-hostname']
  217. facts['network']['public_hostname'] = pub_h
  218. return facts
  219. def normalize_provider_facts(provider, metadata):
  220. """ Normalize provider facts
  221. Args:
  222. provider (str): host provider
  223. metadata (dict): provider metadata
  224. Returns:
  225. dict: the normalized provider facts
  226. """
  227. if provider is None or metadata is None:
  228. return {}
  229. # TODO: test for ipv6_enabled where possible (gce, aws do not support)
  230. # and configure ipv6 facts if available
  231. # TODO: add support for setting user_data if available
  232. facts = dict(name=provider, metadata=metadata,
  233. network=dict(interfaces=[], ipv6_enabled=False))
  234. if provider == 'gce':
  235. facts = normalize_gce_facts(metadata, facts)
  236. elif provider == 'ec2':
  237. facts = normalize_aws_facts(metadata, facts)
  238. elif provider == 'openstack':
  239. facts = normalize_openstack_facts(metadata, facts)
  240. return facts
  241. def set_fluentd_facts_if_unset(facts):
  242. """ Set fluentd facts if not already present in facts dict
  243. dict: the facts dict updated with the generated fluentd facts if
  244. missing
  245. Args:
  246. facts (dict): existing facts
  247. Returns:
  248. dict: the facts dict updated with the generated fluentd
  249. facts if they were not already present
  250. """
  251. if 'common' in facts:
  252. if 'use_fluentd' not in facts['common']:
  253. use_fluentd = False
  254. facts['common']['use_fluentd'] = use_fluentd
  255. return facts
  256. def set_node_schedulability(facts):
  257. """ Set schedulable facts if not already present in facts dict
  258. Args:
  259. facts (dict): existing facts
  260. Returns:
  261. dict: the facts dict updated with the generated schedulable
  262. facts if they were not already present
  263. """
  264. if 'node' in facts:
  265. if 'schedulable' not in facts['node']:
  266. if 'master' in facts:
  267. facts['node']['schedulable'] = False
  268. else:
  269. facts['node']['schedulable'] = True
  270. return facts
  271. def set_master_selectors(facts):
  272. """ Set selectors facts if not already present in facts dict
  273. Args:
  274. facts (dict): existing facts
  275. Returns:
  276. dict: the facts dict updated with the generated selectors
  277. facts if they were not already present
  278. """
  279. if 'master' in facts:
  280. if 'infra_nodes' in facts['master']:
  281. deployment_type = facts['common']['deployment_type']
  282. if deployment_type == 'online':
  283. selector = "type=infra"
  284. else:
  285. selector = "region=infra"
  286. if 'router_selector' not in facts['master']:
  287. facts['master']['router_selector'] = selector
  288. if 'registry_selector' not in facts['master']:
  289. facts['master']['registry_selector'] = selector
  290. return facts
  291. def set_metrics_facts_if_unset(facts):
  292. """ Set cluster metrics facts if not already present in facts dict
  293. dict: the facts dict updated with the generated cluster metrics facts if
  294. missing
  295. Args:
  296. facts (dict): existing facts
  297. Returns:
  298. dict: the facts dict updated with the generated cluster metrics
  299. facts if they were not already present
  300. """
  301. if 'common' in facts:
  302. if 'use_cluster_metrics' not in facts['common']:
  303. use_cluster_metrics = False
  304. facts['common']['use_cluster_metrics'] = use_cluster_metrics
  305. return facts
  306. def set_project_cfg_facts_if_unset(facts):
  307. """ Set Project Configuration facts if not already present in facts dict
  308. dict:
  309. Args:
  310. facts (dict): existing facts
  311. Returns:
  312. dict: the facts dict updated with the generated Project Configuration
  313. facts if they were not already present
  314. """
  315. config = {
  316. 'default_node_selector': '',
  317. 'project_request_message': '',
  318. 'project_request_template': '',
  319. 'mcs_allocator_range': 's0:/2',
  320. 'mcs_labels_per_project': 5,
  321. 'uid_allocator_range': '1000000000-1999999999/10000'
  322. }
  323. if 'master' in facts:
  324. for key, value in config.items():
  325. if key not in facts['master']:
  326. facts['master'][key] = value
  327. return facts
  328. def set_identity_providers_if_unset(facts):
  329. """ Set identity_providers fact if not already present in facts dict
  330. Args:
  331. facts (dict): existing facts
  332. Returns:
  333. dict: the facts dict updated with the generated identity providers
  334. facts if they were not already present
  335. """
  336. if 'master' in facts:
  337. deployment_type = facts['common']['deployment_type']
  338. if 'identity_providers' not in facts['master']:
  339. identity_provider = dict(
  340. name='allow_all', challenge=True, login=True,
  341. kind='AllowAllPasswordIdentityProvider'
  342. )
  343. if deployment_type == 'enterprise':
  344. identity_provider = dict(
  345. name='deny_all', challenge=True, login=True,
  346. kind='DenyAllPasswordIdentityProvider'
  347. )
  348. facts['master']['identity_providers'] = [identity_provider]
  349. return facts
  350. def set_url_facts_if_unset(facts):
  351. """ Set url facts if not already present in facts dict
  352. Args:
  353. facts (dict): existing facts
  354. Returns:
  355. dict: the facts dict updated with the generated url facts if they
  356. were not already present
  357. """
  358. if 'master' in facts:
  359. api_use_ssl = facts['master']['api_use_ssl']
  360. api_port = facts['master']['api_port']
  361. console_use_ssl = facts['master']['console_use_ssl']
  362. console_port = facts['master']['console_port']
  363. console_path = facts['master']['console_path']
  364. etcd_use_ssl = facts['master']['etcd_use_ssl']
  365. etcd_hosts = facts['master']['etcd_hosts']
  366. etcd_port = facts['master']['etcd_port']
  367. hostname = facts['common']['hostname']
  368. public_hostname = facts['common']['public_hostname']
  369. cluster_hostname = facts['master'].get('cluster_hostname')
  370. cluster_public_hostname = facts['master'].get('cluster_public_hostname')
  371. if 'etcd_urls' not in facts['master']:
  372. etcd_urls = []
  373. if etcd_hosts != '':
  374. facts['master']['etcd_port'] = etcd_port
  375. facts['master']['embedded_etcd'] = False
  376. for host in etcd_hosts:
  377. etcd_urls.append(format_url(etcd_use_ssl, host,
  378. etcd_port))
  379. else:
  380. etcd_urls = [format_url(etcd_use_ssl, hostname,
  381. etcd_port)]
  382. facts['master']['etcd_urls'] = etcd_urls
  383. if 'api_url' not in facts['master']:
  384. api_hostname = cluster_hostname if cluster_hostname else hostname
  385. facts['master']['api_url'] = format_url(api_use_ssl, api_hostname,
  386. api_port)
  387. if 'public_api_url' not in facts['master']:
  388. api_public_hostname = cluster_public_hostname if cluster_public_hostname else public_hostname
  389. facts['master']['public_api_url'] = format_url(api_use_ssl,
  390. api_public_hostname,
  391. api_port)
  392. if 'console_url' not in facts['master']:
  393. console_hostname = cluster_hostname if cluster_hostname else hostname
  394. facts['master']['console_url'] = format_url(console_use_ssl,
  395. console_hostname,
  396. console_port,
  397. console_path)
  398. if 'public_console_url' not in facts['master']:
  399. console_public_hostname = cluster_public_hostname if cluster_public_hostname else public_hostname
  400. facts['master']['public_console_url'] = format_url(console_use_ssl,
  401. console_public_hostname,
  402. console_port,
  403. console_path)
  404. return facts
  405. def set_aggregate_facts(facts):
  406. """ Set aggregate facts
  407. Args:
  408. facts (dict): existing facts
  409. Returns:
  410. dict: the facts dict updated with aggregated facts
  411. """
  412. all_hostnames = set()
  413. internal_hostnames = set()
  414. if 'common' in facts:
  415. all_hostnames.add(facts['common']['hostname'])
  416. all_hostnames.add(facts['common']['public_hostname'])
  417. all_hostnames.add(facts['common']['ip'])
  418. all_hostnames.add(facts['common']['public_ip'])
  419. internal_hostnames.add(facts['common']['hostname'])
  420. internal_hostnames.add(facts['common']['ip'])
  421. if 'master' in facts:
  422. # FIXME: not sure why but facts['dns']['domain'] fails
  423. cluster_domain = 'cluster.local'
  424. if 'cluster_hostname' in facts['master']:
  425. all_hostnames.add(facts['master']['cluster_hostname'])
  426. if 'cluster_public_hostname' in facts['master']:
  427. all_hostnames.add(facts['master']['cluster_public_hostname'])
  428. svc_names = ['openshift', 'openshift.default', 'openshift.default.svc',
  429. 'openshift.default.svc.' + cluster_domain, 'kubernetes', 'kubernetes.default',
  430. 'kubernetes.default.svc', 'kubernetes.default.svc.' + cluster_domain]
  431. all_hostnames.update(svc_names)
  432. internal_hostnames.update(svc_names)
  433. first_svc_ip = str(IPNetwork(facts['master']['portal_net'])[1])
  434. all_hostnames.add(first_svc_ip)
  435. internal_hostnames.add(first_svc_ip)
  436. facts['common']['all_hostnames'] = list(all_hostnames)
  437. facts['common']['internal_hostnames'] = list(all_hostnames)
  438. return facts
  439. def set_deployment_facts_if_unset(facts):
  440. """ Set Facts that vary based on deployment_type. This currently
  441. includes common.service_type, common.config_base, master.registry_url,
  442. node.registry_url, node.storage_plugin_deps
  443. Args:
  444. facts (dict): existing facts
  445. Returns:
  446. dict: the facts dict updated with the generated deployment_type
  447. facts
  448. """
  449. # disabled to avoid breaking up facts related to deployment type into
  450. # multiple methods for now.
  451. # pylint: disable=too-many-statements, too-many-branches
  452. if 'common' in facts:
  453. deployment_type = facts['common']['deployment_type']
  454. if 'service_type' not in facts['common']:
  455. service_type = 'atomic-openshift'
  456. if deployment_type == 'origin':
  457. service_type = 'origin'
  458. elif deployment_type in ['enterprise', 'online']:
  459. service_type = 'openshift'
  460. facts['common']['service_type'] = service_type
  461. if 'config_base' not in facts['common']:
  462. config_base = '/etc/origin'
  463. if deployment_type in ['enterprise', 'online']:
  464. config_base = '/etc/openshift'
  465. facts['common']['config_base'] = config_base
  466. if 'data_dir' not in facts['common']:
  467. data_dir = '/var/lib/origin'
  468. if deployment_type in ['enterprise', 'online']:
  469. data_dir = '/var/lib/openshift'
  470. facts['common']['data_dir'] = data_dir
  471. facts['common']['version'] = version = get_openshift_version()
  472. if version is not None:
  473. if deployment_type == 'origin':
  474. version_gt_3_1_or_1_1 = LooseVersion(version) > LooseVersion('1.0.6')
  475. else:
  476. version_gt_3_1_or_1_1 = LooseVersion(version) > LooseVersion('3.0.2.900')
  477. else:
  478. version_gt_3_1_or_1_1 = True
  479. facts['common']['version_greater_than_3_1_or_1_1'] = version_gt_3_1_or_1_1
  480. for role in ('master', 'node'):
  481. if role in facts:
  482. deployment_type = facts['common']['deployment_type']
  483. if 'registry_url' not in facts[role]:
  484. registry_url = 'openshift/origin-${component}:${version}'
  485. if deployment_type in ['enterprise', 'online', 'openshift-enterprise']:
  486. registry_url = 'openshift3/ose-${component}:${version}'
  487. elif deployment_type == 'atomic-enterprise':
  488. registry_url = 'aep3/aep-${component}:${version}'
  489. facts[role]['registry_url'] = registry_url
  490. if 'master' in facts:
  491. deployment_type = facts['common']['deployment_type']
  492. openshift_features = ['Builder', 'S2IBuilder', 'WebConsole']
  493. if 'disabled_features' in facts['master']:
  494. if deployment_type == 'atomic-enterprise':
  495. curr_disabled_features = set(facts['master']['disabled_features'])
  496. facts['master']['disabled_features'] = list(curr_disabled_features.union(openshift_features))
  497. else:
  498. if deployment_type == 'atomic-enterprise':
  499. facts['master']['disabled_features'] = openshift_features
  500. if 'node' in facts:
  501. deployment_type = facts['common']['deployment_type']
  502. if 'storage_plugin_deps' not in facts['node']:
  503. if deployment_type in ['openshift-enterprise', 'atomic-enterprise']:
  504. facts['node']['storage_plugin_deps'] = ['ceph', 'glusterfs']
  505. else:
  506. facts['node']['storage_plugin_deps'] = []
  507. return facts
  508. def set_sdn_facts_if_unset(facts):
  509. """ Set sdn facts if not already present in facts dict
  510. Args:
  511. facts (dict): existing facts
  512. Returns:
  513. dict: the facts dict updated with the generated sdn facts if they
  514. were not already present
  515. """
  516. if 'common' in facts:
  517. use_sdn = facts['common']['use_openshift_sdn']
  518. if not (use_sdn == '' or isinstance(use_sdn, bool)):
  519. facts['common']['use_openshift_sdn'] = bool(strtobool(str(use_sdn)))
  520. if 'sdn_network_plugin_name' not in facts['common']:
  521. plugin = 'redhat/openshift-ovs-subnet' if use_sdn else ''
  522. facts['common']['sdn_network_plugin_name'] = plugin
  523. if 'master' in facts:
  524. if 'sdn_cluster_network_cidr' not in facts['master']:
  525. facts['master']['sdn_cluster_network_cidr'] = '10.1.0.0/16'
  526. if 'sdn_host_subnet_length' not in facts['master']:
  527. facts['master']['sdn_host_subnet_length'] = '8'
  528. if 'node' in facts:
  529. if 'sdn_mtu' not in facts['node']:
  530. facts['node']['sdn_mtu'] = '1450'
  531. return facts
  532. def format_url(use_ssl, hostname, port, path=''):
  533. """ Format url based on ssl flag, hostname, port and path
  534. Args:
  535. use_ssl (bool): is ssl enabled
  536. hostname (str): hostname
  537. port (str): port
  538. path (str): url path
  539. Returns:
  540. str: The generated url string
  541. """
  542. scheme = 'https' if use_ssl else 'http'
  543. netloc = hostname
  544. if (use_ssl and port != '443') or (not use_ssl and port != '80'):
  545. netloc += ":%s" % port
  546. return urlparse.urlunparse((scheme, netloc, path, '', '', ''))
  547. def get_current_config(facts):
  548. """ Get current openshift config
  549. Args:
  550. facts (dict): existing facts
  551. Returns:
  552. dict: the facts dict updated with the current openshift config
  553. """
  554. current_config = dict()
  555. roles = [role for role in facts if role not in ['common', 'provider']]
  556. for role in roles:
  557. if 'roles' in current_config:
  558. current_config['roles'].append(role)
  559. else:
  560. current_config['roles'] = [role]
  561. # TODO: parse the /etc/sysconfig/openshift-{master,node} config to
  562. # determine the location of files.
  563. # TODO: I suspect this isn't working right now, but it doesn't prevent
  564. # anything from working properly as far as I can tell, perhaps because
  565. # we override the kubeconfig path everywhere we use it?
  566. # Query kubeconfig settings
  567. kubeconfig_dir = '/var/lib/origin/openshift.local.certificates'
  568. if role == 'node':
  569. kubeconfig_dir = os.path.join(
  570. kubeconfig_dir, "node-%s" % facts['common']['hostname']
  571. )
  572. kubeconfig_path = os.path.join(kubeconfig_dir, '.kubeconfig')
  573. if (os.path.isfile('/usr/bin/openshift')
  574. and os.path.isfile(kubeconfig_path)):
  575. try:
  576. _, output, _ = module.run_command(
  577. ["/usr/bin/openshift", "ex", "config", "view", "-o",
  578. "json", "--kubeconfig=%s" % kubeconfig_path],
  579. check_rc=False
  580. )
  581. config = json.loads(output)
  582. cad = 'certificate-authority-data'
  583. try:
  584. for cluster in config['clusters']:
  585. config['clusters'][cluster][cad] = 'masked'
  586. except KeyError:
  587. pass
  588. try:
  589. for user in config['users']:
  590. config['users'][user][cad] = 'masked'
  591. config['users'][user]['client-key-data'] = 'masked'
  592. except KeyError:
  593. pass
  594. current_config['kubeconfig'] = config
  595. # override pylint broad-except warning, since we do not want
  596. # to bubble up any exceptions if oc config view
  597. # fails
  598. # pylint: disable=broad-except
  599. except Exception:
  600. pass
  601. return current_config
  602. def get_openshift_version():
  603. """ Get current version of openshift on the host
  604. Returns:
  605. version: the current openshift version
  606. """
  607. version = None
  608. if os.path.isfile('/usr/bin/openshift'):
  609. _, output, _ = module.run_command(['/usr/bin/openshift', 'version'])
  610. versions = dict(e.split(' v') for e in output.splitlines() if ' v' in e)
  611. version = versions.get('openshift', '')
  612. #TODO: acknowledge the possility of a containerized install
  613. return version
  614. def apply_provider_facts(facts, provider_facts):
  615. """ Apply provider facts to supplied facts dict
  616. Args:
  617. facts (dict): facts dict to update
  618. provider_facts (dict): provider facts to apply
  619. roles: host roles
  620. Returns:
  621. dict: the merged facts
  622. """
  623. if not provider_facts:
  624. return facts
  625. use_openshift_sdn = provider_facts.get('use_openshift_sdn')
  626. if isinstance(use_openshift_sdn, bool):
  627. facts['common']['use_openshift_sdn'] = use_openshift_sdn
  628. common_vars = [('hostname', 'ip'), ('public_hostname', 'public_ip')]
  629. for h_var, ip_var in common_vars:
  630. ip_value = provider_facts['network'].get(ip_var)
  631. if ip_value:
  632. facts['common'][ip_var] = ip_value
  633. facts['common'][h_var] = choose_hostname(
  634. [provider_facts['network'].get(h_var)],
  635. facts['common'][ip_var]
  636. )
  637. facts['provider'] = provider_facts
  638. return facts
  639. def merge_facts(orig, new):
  640. """ Recursively merge facts dicts
  641. Args:
  642. orig (dict): existing facts
  643. new (dict): facts to update
  644. Returns:
  645. dict: the merged facts
  646. """
  647. facts = dict()
  648. for key, value in orig.iteritems():
  649. if key in new:
  650. if isinstance(value, dict) and isinstance(new[key], dict):
  651. facts[key] = merge_facts(value, new[key])
  652. else:
  653. facts[key] = copy.copy(new[key])
  654. else:
  655. facts[key] = copy.deepcopy(value)
  656. new_keys = set(new.keys()) - set(orig.keys())
  657. for key in new_keys:
  658. facts[key] = copy.deepcopy(new[key])
  659. return facts
  660. def save_local_facts(filename, facts):
  661. """ Save local facts
  662. Args:
  663. filename (str): local facts file
  664. facts (dict): facts to set
  665. """
  666. try:
  667. fact_dir = os.path.dirname(filename)
  668. if not os.path.exists(fact_dir):
  669. os.makedirs(fact_dir)
  670. with open(filename, 'w') as fact_file:
  671. fact_file.write(module.jsonify(facts))
  672. except (IOError, OSError) as ex:
  673. raise OpenShiftFactsFileWriteError(
  674. "Could not create fact file: %s, error: %s" % (filename, ex)
  675. )
  676. def get_local_facts_from_file(filename):
  677. """ Retrieve local facts from fact file
  678. Args:
  679. filename (str): local facts file
  680. Returns:
  681. dict: the retrieved facts
  682. """
  683. local_facts = dict()
  684. try:
  685. # Handle conversion of INI style facts file to json style
  686. ini_facts = ConfigParser.SafeConfigParser()
  687. ini_facts.read(filename)
  688. for section in ini_facts.sections():
  689. local_facts[section] = dict()
  690. for key, value in ini_facts.items(section):
  691. local_facts[section][key] = value
  692. except (ConfigParser.MissingSectionHeaderError,
  693. ConfigParser.ParsingError):
  694. try:
  695. with open(filename, 'r') as facts_file:
  696. local_facts = json.load(facts_file)
  697. except (ValueError, IOError):
  698. pass
  699. return local_facts
  700. class OpenShiftFactsUnsupportedRoleError(Exception):
  701. """Origin Facts Unsupported Role Error"""
  702. pass
  703. class OpenShiftFactsFileWriteError(Exception):
  704. """Origin Facts File Write Error"""
  705. pass
  706. class OpenShiftFactsMetadataUnavailableError(Exception):
  707. """Origin Facts Metadata Unavailable Error"""
  708. pass
  709. class OpenShiftFacts(object):
  710. """ Origin Facts
  711. Attributes:
  712. facts (dict): facts for the host
  713. Args:
  714. role (str): role for setting local facts
  715. filename (str): local facts file to use
  716. local_facts (dict): local facts to set
  717. Raises:
  718. OpenShiftFactsUnsupportedRoleError:
  719. """
  720. known_roles = ['common', 'master', 'node', 'master_sdn', 'node_sdn', 'dns']
  721. def __init__(self, role, filename, local_facts):
  722. self.changed = False
  723. self.filename = filename
  724. if role not in self.known_roles:
  725. raise OpenShiftFactsUnsupportedRoleError(
  726. "Role %s is not supported by this module" % role
  727. )
  728. self.role = role
  729. self.system_facts = ansible_facts(module)
  730. self.facts = self.generate_facts(local_facts)
  731. def generate_facts(self, local_facts):
  732. """ Generate facts
  733. Args:
  734. local_facts (dict): local_facts for overriding generated
  735. defaults
  736. Returns:
  737. dict: The generated facts
  738. """
  739. local_facts = self.init_local_facts(local_facts)
  740. roles = local_facts.keys()
  741. defaults = self.get_defaults(roles)
  742. provider_facts = self.init_provider_facts()
  743. facts = apply_provider_facts(defaults, provider_facts)
  744. facts = merge_facts(facts, local_facts)
  745. facts['current_config'] = get_current_config(facts)
  746. facts = set_url_facts_if_unset(facts)
  747. facts = set_project_cfg_facts_if_unset(facts)
  748. facts = set_fluentd_facts_if_unset(facts)
  749. facts = set_node_schedulability(facts)
  750. facts = set_master_selectors(facts)
  751. facts = set_metrics_facts_if_unset(facts)
  752. facts = set_identity_providers_if_unset(facts)
  753. facts = set_sdn_facts_if_unset(facts)
  754. facts = set_deployment_facts_if_unset(facts)
  755. facts = set_aggregate_facts(facts)
  756. return dict(openshift=facts)
  757. def get_defaults(self, roles):
  758. """ Get default fact values
  759. Args:
  760. roles (list): list of roles for this host
  761. Returns:
  762. dict: The generated default facts
  763. """
  764. defaults = dict()
  765. ip_addr = self.system_facts['default_ipv4']['address']
  766. exit_code, output, _ = module.run_command(['hostname', '-f'])
  767. hostname_f = output.strip() if exit_code == 0 else ''
  768. hostname_values = [hostname_f, self.system_facts['nodename'],
  769. self.system_facts['fqdn']]
  770. hostname = choose_hostname(hostname_values, ip_addr)
  771. common = dict(use_openshift_sdn=True, ip=ip_addr, public_ip=ip_addr,
  772. deployment_type='origin', hostname=hostname,
  773. public_hostname=hostname)
  774. common['client_binary'] = 'oc' if os.path.isfile('/usr/bin/oc') else 'osc'
  775. common['admin_binary'] = 'oadm' if os.path.isfile('/usr/bin/oadm') else 'osadm'
  776. defaults['common'] = common
  777. if 'master' in roles:
  778. master = dict(api_use_ssl=True, api_port='8443',
  779. console_use_ssl=True, console_path='/console',
  780. console_port='8443', etcd_use_ssl=True, etcd_hosts='',
  781. etcd_port='4001', portal_net='172.30.0.0/16',
  782. embedded_etcd=True, embedded_kube=True,
  783. embedded_dns=True, dns_port='53',
  784. bind_addr='0.0.0.0', session_max_seconds=3600,
  785. session_name='ssn', session_secrets_file='',
  786. access_token_max_seconds=86400,
  787. auth_token_max_seconds=500,
  788. oauth_grant_method='auto', cluster_defer_ha=False)
  789. defaults['master'] = master
  790. if 'node' in roles:
  791. node = dict(labels={}, annotations={}, portal_net='172.30.0.0/16')
  792. defaults['node'] = node
  793. return defaults
  794. def guess_host_provider(self):
  795. """ Guess the host provider
  796. Returns:
  797. dict: The generated default facts for the detected provider
  798. """
  799. # TODO: cloud provider facts should probably be submitted upstream
  800. product_name = self.system_facts['product_name']
  801. product_version = self.system_facts['product_version']
  802. virt_type = self.system_facts['virtualization_type']
  803. virt_role = self.system_facts['virtualization_role']
  804. provider = None
  805. metadata = None
  806. # TODO: this is not exposed through module_utils/facts.py in ansible,
  807. # need to create PR for ansible to expose it
  808. bios_vendor = get_file_content(
  809. '/sys/devices/virtual/dmi/id/bios_vendor'
  810. )
  811. if bios_vendor == 'Google':
  812. provider = 'gce'
  813. metadata_url = ('http://metadata.google.internal/'
  814. 'computeMetadata/v1/?recursive=true')
  815. headers = {'Metadata-Flavor': 'Google'}
  816. metadata = get_provider_metadata(metadata_url, True, headers,
  817. True)
  818. # Filter sshKeys and serviceAccounts from gce metadata
  819. if metadata:
  820. metadata['project']['attributes'].pop('sshKeys', None)
  821. metadata['instance'].pop('serviceAccounts', None)
  822. elif (virt_type == 'xen' and virt_role == 'guest'
  823. and re.match(r'.*\.amazon$', product_version)):
  824. provider = 'ec2'
  825. metadata_url = 'http://169.254.169.254/latest/meta-data/'
  826. metadata = get_provider_metadata(metadata_url)
  827. elif re.search(r'OpenStack', product_name):
  828. provider = 'openstack'
  829. metadata_url = ('http://169.254.169.254/openstack/latest/'
  830. 'meta_data.json')
  831. metadata = get_provider_metadata(metadata_url, True, None,
  832. True)
  833. if metadata:
  834. ec2_compat_url = 'http://169.254.169.254/latest/meta-data/'
  835. metadata['ec2_compat'] = get_provider_metadata(
  836. ec2_compat_url
  837. )
  838. # disable pylint maybe-no-member because overloaded use of
  839. # the module name causes pylint to not detect that results
  840. # is an array or hash
  841. # pylint: disable=maybe-no-member
  842. # Filter public_keys and random_seed from openstack metadata
  843. metadata.pop('public_keys', None)
  844. metadata.pop('random_seed', None)
  845. if not metadata['ec2_compat']:
  846. metadata = None
  847. return dict(name=provider, metadata=metadata)
  848. def init_provider_facts(self):
  849. """ Initialize the provider facts
  850. Returns:
  851. dict: The normalized provider facts
  852. """
  853. provider_info = self.guess_host_provider()
  854. provider_facts = normalize_provider_facts(
  855. provider_info.get('name'),
  856. provider_info.get('metadata')
  857. )
  858. return provider_facts
  859. def init_local_facts(self, facts=None):
  860. """ Initialize the provider facts
  861. Args:
  862. facts (dict): local facts to set
  863. Returns:
  864. dict: The result of merging the provided facts with existing
  865. local facts
  866. """
  867. changed = False
  868. facts_to_set = {self.role: dict()}
  869. if facts is not None:
  870. facts_to_set[self.role] = facts
  871. local_facts = get_local_facts_from_file(self.filename)
  872. for arg in ['labels', 'annotations']:
  873. if arg in facts_to_set and isinstance(facts_to_set[arg],
  874. basestring):
  875. facts_to_set[arg] = module.from_json(facts_to_set[arg])
  876. new_local_facts = merge_facts(local_facts, facts_to_set)
  877. for facts in new_local_facts.values():
  878. keys_to_delete = []
  879. for fact, value in facts.iteritems():
  880. if value == "" or value is None:
  881. keys_to_delete.append(fact)
  882. for key in keys_to_delete:
  883. del facts[key]
  884. if new_local_facts != local_facts:
  885. changed = True
  886. if not module.check_mode:
  887. save_local_facts(self.filename, new_local_facts)
  888. self.changed = changed
  889. return new_local_facts
  890. def main():
  891. """ main """
  892. # disabling pylint errors for global-variable-undefined and invalid-name
  893. # for 'global module' usage, since it is required to use ansible_facts
  894. # pylint: disable=global-variable-undefined, invalid-name
  895. global module
  896. module = AnsibleModule(
  897. argument_spec=dict(
  898. role=dict(default='common', required=False,
  899. choices=OpenShiftFacts.known_roles),
  900. local_facts=dict(default=None, type='dict', required=False),
  901. ),
  902. supports_check_mode=True,
  903. add_file_common_args=True,
  904. )
  905. role = module.params['role']
  906. local_facts = module.params['local_facts']
  907. fact_file = '/etc/ansible/facts.d/openshift.fact'
  908. openshift_facts = OpenShiftFacts(role, fact_file, local_facts)
  909. file_params = module.params.copy()
  910. file_params['path'] = fact_file
  911. file_args = module.load_file_common_arguments(file_params)
  912. changed = module.set_fs_attributes_if_different(file_args,
  913. openshift_facts.changed)
  914. return module.exit_json(changed=changed,
  915. ansible_facts=openshift_facts.facts)
  916. # ignore pylint errors related to the module_utils import
  917. # pylint: disable=redefined-builtin, unused-wildcard-import, wildcard-import
  918. # import module snippets
  919. from ansible.module_utils.basic import *
  920. from ansible.module_utils.facts import *
  921. from ansible.module_utils.urls import *
  922. if __name__ == '__main__':
  923. main()