A Python library for code common to TripleO CLI and TripleO UI.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.0KB

  1. # Copyright 2015 Red Hat, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import socket
  15. from six.moves import urllib
  16. from tripleo_common import constants
  17. try: # py3
  18. from shlex import quote
  19. except ImportError: # py2
  20. from pipes import quote
  21. def get_service_ips(stack):
  22. service_ips = {}
  23. for output in stack.to_dict().get('outputs', {}):
  24. service_ips[output['output_key']] = output['output_value']
  25. return service_ips
  26. def get_endpoint_map(stack):
  27. endpoint_map = {}
  28. for output in stack.to_dict().get('outputs', {}):
  29. if output['output_key'] == 'EndpointMap':
  30. endpoint_map = output['output_value']
  31. break
  32. return endpoint_map
  33. def get_endpoint(key, stack):
  34. endpoint_map = get_endpoint_map(stack)
  35. if endpoint_map:
  36. return endpoint_map[key]['host']
  37. else:
  38. return get_service_ips(stack).get(key + 'Vip')
  39. def get_overcloud_endpoint(stack):
  40. for output in stack.to_dict().get('outputs', {}):
  41. if output['output_key'] == 'KeystoneURL':
  42. return output['output_value']
  43. def bracket_ipv6(address):
  44. """Put a bracket around address if it is valid IPv6
  45. Return it unchanged if it is a hostname or IPv4 address.
  46. """
  47. try:
  48. socket.inet_pton(socket.AF_INET6, address)
  49. return "[%s]" % address
  50. except socket.error:
  51. return address
  52. CLEAR_ENV = """# Clear any old environment that may conflict.
  53. for key in $( set | awk '{FS=\"=\"} /^OS_/ {print $1}' ); do unset $key ; done
  54. """
  55. CLOUDPROMPT = """
  56. # Add OS_CLOUDNAME to PS1
  57. if [ -z "${CLOUDPROMPT_ENABLED:-}" ]; then
  58. export PS1=${PS1:-""}
  59. export PS1=\${OS_CLOUDNAME:+"(\$OS_CLOUDNAME)"}\ $PS1
  60. export CLOUDPROMPT_ENABLED=1
  61. fi
  62. """
  63. def create_overcloudrc(stack, no_proxy, admin_password, region_name):
  64. """Given the stack and proxy settings, create the overcloudrc
  65. stack: Heat stack containing the deployed overcloud
  66. no_proxy: a comma-separated string of hosts that shouldn't be proxied
  67. """
  68. overcloud_endpoint = get_overcloud_endpoint(stack)
  69. overcloud_host = urllib.parse.urlparse(overcloud_endpoint).hostname
  70. overcloud_admin_vip = get_endpoint('KeystoneAdmin', stack)
  71. no_proxy_list = map(bracket_ipv6,
  72. [no_proxy, overcloud_host, overcloud_admin_vip])
  73. rc_params = {
  74. 'NOVA_VERSION': '1.1',
  75. 'COMPUTE_API_VERSION': '1.1',
  76. 'OS_USERNAME': 'admin',
  77. 'OS_PROJECT_NAME': 'admin',
  78. 'OS_USER_DOMAIN_NAME': 'Default',
  79. 'OS_PROJECT_DOMAIN_NAME': 'Default',
  80. 'OS_NO_CACHE': 'True',
  81. 'OS_CLOUDNAME': stack.stack_name,
  82. 'no_proxy': ','.join(no_proxy_list),
  83. 'PYTHONWARNINGS': ('ignore:Certificate has no, ignore:A true '
  84. 'SSLContext object is not available'),
  85. 'OS_AUTH_TYPE': 'password',
  86. 'OS_PASSWORD': admin_password,
  87. 'OS_AUTH_URL': overcloud_endpoint.replace('/v2.0', ''),
  88. 'OS_IDENTITY_API_VERSION': '3',
  89. 'OS_IMAGE_API_VERSION': constants.DEFAULT_IMAGE_API_VERSION,
  90. 'OS_VOLUME_API_VERSION': constants.DEFAULT_VOLUME_API_VERSION,
  91. 'OS_REGION_NAME': region_name or 'regionOne'
  92. }
  93. overcloudrc = CLEAR_ENV
  94. for key, value in rc_params.items():
  95. line = "export %(key)s=%(value)s\n" % {'key': key,
  96. 'value': quote(value)}
  97. overcloudrc = overcloudrc + line
  98. overcloudrc = overcloudrc + CLOUDPROMPT
  99. return {
  100. "overcloudrc": overcloudrc,
  101. }