common.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # -*- coding: utf-8 -*-
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  11. # implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import netaddr
  15. from ..installer import utils
  16. def filtered_hosts(config, exclude=True, dbhost=True):
  17. """
  18. Returns list of hosts which need installation taking into account
  19. CONFIG_MARIADB_INSTALL if parameter dbhost is True and EXCLUDE_SERVERS
  20. if parameter exclude is True.
  21. """
  22. exclset = set([i.strip()
  23. for i in config.get('EXCLUDE_SERVERS', '').split(',')
  24. if i.strip()])
  25. result = set()
  26. dbinst = config.get('CONFIG_MARIADB_INSTALL') == 'y'
  27. vcenter = config.get('CONFIG_VMWARE_BACKEND') == 'y'
  28. for hosttype, hostname in utils.host_iter(config):
  29. # if dbhost is being taken into account and we are not installing
  30. # MariaDB then we should omit the MariaDB host
  31. if dbhost and not dbinst and hosttype == 'CONFIG_MARIADB_HOST':
  32. continue
  33. if vcenter and hosttype == 'CONFIG_VCENTER_HOST':
  34. continue
  35. result.add(hostname)
  36. if exclude:
  37. result = result - exclset
  38. return result
  39. def is_all_in_one(config):
  40. """
  41. Returns True if packstack is running allinone setup, otherwise
  42. returns False.
  43. """
  44. # Even if some host have been excluded from installation, we must count
  45. # with them when checking all-in-one. MariaDB host should however be
  46. # omitted if we are not installing MariaDB.
  47. return len(filtered_hosts(config, exclude=False, dbhost=True)) == 1
  48. def cidr_to_ifname(cidr, host, config):
  49. """
  50. Returns appropriate host's interface name from given CIDR subnet. Passed
  51. config dict has to contain discovered hosts details.
  52. """
  53. if not config or not config['HOST_DETAILS'] or '/' not in cidr:
  54. raise ValueError(
  55. 'Cannot translate CIDR to interface, invalid parameters '
  56. 'were given.'
  57. )
  58. info = config['HOST_DETAILS'][host]
  59. result = []
  60. for item in cidr.split(','):
  61. translated = []
  62. for fragment in item.split(':'):
  63. try:
  64. subnet_a = netaddr.IPNetwork(fragment)
  65. except netaddr.AddrFormatError:
  66. translated.append(fragment)
  67. continue
  68. for interface in info['interfaces'].split(','):
  69. interface = interface.strip()
  70. ipaddr = info.get('ipaddress_{}'.format(interface))
  71. netmask = info.get('netmask_{}'.format(interface))
  72. if ipaddr and netmask:
  73. subnet_b = netaddr.IPNetwork('{ipaddr}/{netmask}'.format(**locals()))
  74. if subnet_a == subnet_b:
  75. translated.append(interface)
  76. break
  77. result.append(':'.join(translated))
  78. return ','.join(result)
  79. # Function find_pair_with search in a list of "key:value" pairs, one
  80. # containing the desired element as key (if index is 0), or value (if index
  81. # is 1). It returns the pair if it's found or KeyError.
  82. def find_pair_with(pairs_list, element, index):
  83. for pair in pairs_list:
  84. found_element = pair.split(':')[index].strip()
  85. if found_element == element:
  86. return pair
  87. raise KeyError('Couldn\'t find element %s in %s.' % (element, pairs_list))