jolausa

check deployment

Jul 14th, 2017
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. import logging
  2. import logging.config
  3. import os
  4. import pkg_resources
  5. import socket
  6. import time
  7. from urlparse import urlparse
  8.  
  9. from snaps.openstack.utils import glance_utils
  10. from snaps.openstack.utils import keystone_utils
  11. from snaps.openstack.utils import neutron_utils
  12. from snaps.openstack.utils import nova_utils
  13. from snaps.openstack.tests import openstack_tests
  14.  
  15. __author__ = "Jose Lausuch <[email protected]>"
  16.  
  17. LOGGER = logging.getLogger(__name__)
  18. LOGGER.setLevel(logging.DEBUG)
  19.  
  20.  
  21. def verify_connectivity(adress, port, timeout=10):
  22. """ Returns true if an ip/port is reachable"""
  23. connection = socket.socket()
  24. count = 0
  25. while count < timeout:
  26. try:
  27. connection.connect((adress, port))
  28. LOGGER.debug('%s:%s is reachable!', adress, port)
  29. return True
  30. except socket.error:
  31. count += 1
  32. time.sleep(1)
  33. continue
  34. LOGGER.error('%s:%s is not reachable.', adress, port)
  35. return False
  36.  
  37.  
  38. class CheckDeployment(object):
  39. """ Check deployment class."""
  40.  
  41. def __init__(self, rc_file='/home/opnfv/functest/conf/openstack.creds'):
  42. self.rc_file = rc_file
  43. self.services = ('compute', 'network', 'image')
  44. self.os_creds = None
  45.  
  46. def check_rc(self):
  47. """ Check if RC file exists and contains OS_AUTH_URL """
  48. if not os.path.isfile(self.rc_file):
  49. raise IOError('RC file {} does not exist!'.format(self.rc_file))
  50. if 'OS_AUTH_URL' not in open(self.rc_file).read():
  51. raise SyntaxError('OS_AUTH_URL not defined in {}.'.
  52. format(self.rc_file))
  53.  
  54. def check_auth_endpoint(self):
  55. """ Verifies connectivity to the OS_AUTH_URL given in the RC file """
  56. rc_endpoint = self.os_creds.auth_url
  57. if not (verify_connectivity(urlparse(rc_endpoint).hostname,
  58. urlparse(rc_endpoint).port)):
  59. raise Exception("OS_AUTH_URL {} is not reachable.".
  60. format(rc_endpoint))
  61. LOGGER.info("Connectivity to OS_AUTH_URL %s ...OK", rc_endpoint)
  62.  
  63. def check_public_endpoint(self):
  64. """ Gets the public endpoint and verifies connectivity to it """
  65. LOGGER.info("Getting public endpoint...")
  66. public_endpoint = keystone_utils.get_endpoint(self.os_creds,
  67. 'identity',
  68. interface='public')
  69. LOGGER.info("Public endpoint is %s", public_endpoint)
  70. if not (verify_connectivity(urlparse(public_endpoint).hostname,
  71. urlparse(public_endpoint).port)):
  72. raise Exception("Public endpoint {} is not reachable.".
  73. format(public_endpoint))
  74. LOGGER.info("Connectivity to the public endpoint %s ...OK",
  75. public_endpoint)
  76.  
  77. def check_service_endpoint(self, service):
  78. """ Verifies connectivity to a given openstack service """
  79. endpoint = keystone_utils.get_endpoint(self.os_creds,
  80. service,
  81. interface='public')
  82. if not (verify_connectivity(urlparse(endpoint).hostname,
  83. urlparse(endpoint).port)):
  84. raise Exception("{} endpoint {} is not reachable.".
  85. format(service, endpoint))
  86. LOGGER.info("Connectivity to endpoint '%s' %s ...OK",
  87. service, endpoint)
  88.  
  89. def check_nova(self):
  90. """ checks that a simple nova operation works """
  91. try:
  92. client = nova_utils.nova_client(self.os_creds)
  93. client.servers.list()
  94. LOGGER.info("Nova service ...OK")
  95. except Exception as error:
  96. LOGGER.error("Nova service ...FAILED")
  97. raise error
  98.  
  99. def check_neutron(self):
  100. """ checks that a simple neutron operation works """
  101. try:
  102. client = neutron_utils.neutron_client(self.os_creds)
  103. client.list_networks()
  104. LOGGER.info("Neutron service ...OK")
  105. except Exception as error:
  106. LOGGER.error("Neutron service ...FAILED")
  107. raise error
  108.  
  109. def check_glance(self):
  110. """ checks that a simple glance operation works """
  111. try:
  112. client = glance_utils.glance_client(self.os_creds)
  113. client.images.list()
  114. LOGGER.info("Glance service ...OK")
  115. except Exception as error:
  116. LOGGER.error("Glance service ...FAILED")
  117. raise error
  118.  
  119. def check_all(self):
  120. """
  121. Calls all the class functions and returns 0 if all of them succeed.
  122. This is the method called by prepare_env or CLI
  123. """
  124. self.check_rc()
  125. try:
  126. self.os_creds = openstack_tests.get_credentials(
  127. os_env_file=self.rc_file,
  128. proxy_settings_str=None,
  129. ssh_proxy_cmd=None)
  130. except:
  131. raise Exception("Problem while getting credentials object.")
  132. if self.os_creds is None:
  133. raise Exception("Credentials is None.")
  134. self.check_auth_endpoint()
  135. self.check_public_endpoint()
  136. for service in self.services:
  137. self.check_service_endpoint(service)
  138. self.check_nova()
  139. self.check_neutron()
  140. self.check_glance()
  141. return 0
  142.  
  143.  
  144. def main():
  145. """Entry point"""
  146. logging.basicConfig()
  147. deployment = CheckDeployment()
  148. return deployment.check_all()
  149.  
  150.  
  151. main()
Advertisement
Add Comment
Please, Sign In to add comment