Charm Interface - Nova Compute
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.

requires.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # Licensed under the Apache License, Version 2.0 (the "License");
  2. # you may not use this file except in compliance with the License.
  3. # You may obtain a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS,
  9. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. # See the License for the specific language governing permissions and
  11. # limitations under the License.
  12. from urllib.parse import urlparse
  13. import uuid
  14. from charms.reactive import set_flag, clear_flag
  15. from charms.reactive import Endpoint
  16. from charms.reactive import when_not, when
  17. class NovaComputeRequires(Endpoint):
  18. @when('endpoint.{endpoint_name}.changed')
  19. def data_changed(self):
  20. """Set flag to indicate to charm relation data has changed."""
  21. set_flag(self.expand_name('{endpoint_name}.available'))
  22. @when_not('endpoint.{endpoint_name}.joined')
  23. def broken(self):
  24. """Remove flag to indicate to charm relation has gone.."""
  25. clear_flag(self.expand_name('{endpoint_name}.available'))
  26. @when('endpoint.{endpoint_name}.joined')
  27. def joined(self):
  28. """Set flag to indicate to charm relation has been joined."""
  29. set_flag(self.expand_name('{endpoint_name}.connected'))
  30. def set_network_data(self, neutron_url, neutron_plugin=None,
  31. network_manager=None, enable_security_groups=True):
  32. """Send compute nodes data relating to network setup.
  33. :param neutron_url: URL for network API service
  34. :type neutron_url: str
  35. :param neutron_plugin: Neutron plugin to use
  36. :type neutron_plugin: str
  37. :param network_manager: Network Manager
  38. :type network_manager: str
  39. :param enable_security_groups: Whether to enable security groups
  40. :type enable_security_group: bool
  41. """
  42. o = urlparse(neutron_url)
  43. if enable_security_groups:
  44. security_groups = 'yes'
  45. else:
  46. security_groups = 'no'
  47. for r in self.relations:
  48. r.to_publish_raw['quantum_host'] = o.hostname
  49. r.to_publish_raw['quantum_plugin'] = neutron_plugin or 'ovs'
  50. r.to_publish_raw['quantum_port'] = o.port
  51. r.to_publish_raw['quantum_security_groups'] = security_groups
  52. r.to_publish_raw['quantum_url'] = neutron_url
  53. r.to_publish_raw['network_manager'] = network_manager or 'neutron'
  54. def set_console_data(self, serial_console_base_url, enable_serial_console):
  55. """Send compute nodes data relating to serial console access.
  56. :param serial_console_base_url: URL for accessing the serial console.
  57. :type serial_console_base_url: str
  58. :param enable_serial_console: Whether to enable the serial console
  59. :type enable_serial_console: bool
  60. """
  61. for r in self.relations:
  62. r.to_publish_raw[
  63. 'serial_console_base_url'] = serial_console_base_url
  64. r.to_publish_raw['enable_serial_console'] = enable_serial_console
  65. def trigger_remote_restart(self, restart_key=None):
  66. """Trigger a restart of services on the remote application.
  67. :param restart_key: Key to send to remote service, restarts are
  68. triggered when the key changes.
  69. :type restart_key: str
  70. """
  71. if not restart_key:
  72. restart_key = uuid.uuid1()
  73. for relation in self.relations:
  74. relation.to_publish_raw['restart_trigger'] = restart_key
  75. def set_region(self, region):
  76. """Send compute nodes region information.
  77. :param region: Region compute nodes will belong to.
  78. :type region: str
  79. """
  80. for relation in self.relations:
  81. relation.to_publish_raw['region'] = region
  82. def set_volume_data(self, volume_service):
  83. """Send compute nodes volume information.
  84. :param volume_service: Name of volume service to use, eg cinder
  85. :type volume_service: str
  86. """
  87. for relation in self.relations:
  88. relation.to_publish_raw['volume_service'] = volume_service
  89. def set_ec2_data(self, ec2_host):
  90. """Send compute nodes ec2 information.
  91. :param ec2_host: Name of ec2_host.
  92. :type ec2_host: str
  93. """
  94. for relation in self.relations:
  95. relation.to_publish_raw['ec2_host'] = ec2_host
  96. def collect_ssh_keys(self, application_name=None):
  97. """Query related units and collect ssh artifacts.
  98. :param application_name: Only return artifacts from units of this
  99. applicationa.
  100. :type application_name: str
  101. :returns: {APP_NAME: {UNIT_NAME: {pupkey1:.., hostkey1:...}}}
  102. :rtype: dict
  103. """
  104. ssh_keys = {}
  105. for rel in self.relations:
  106. if application_name and application_name != rel.application_name:
  107. continue
  108. ssh_keys[rel.application_name] = {}
  109. for unit in rel.units:
  110. nova_ssh_pub_key = unit.received.get('nova_ssh_public_key')
  111. ssh_pub_key = unit.received.get('ssh_public_key')
  112. if nova_ssh_pub_key and ssh_pub_key:
  113. ssh_keys[rel.application_name][unit.unit_name] = {
  114. 'nova_ssh_pub_key': nova_ssh_pub_key,
  115. 'hostname': unit.received.get('hostname'),
  116. 'private-address': unit.received.get(
  117. 'private-address'),
  118. 'ssh_pub_key': ssh_pub_key}
  119. return ssh_keys
  120. def send_ssh_keys(self, relation, settings):
  121. """Publish the provided ssh settings on the given relation
  122. :param relation: Relation to publish settings on.
  123. :type relation: charms.reactive.endpoints.Relation
  124. :param settings: SSH settings to publish.
  125. :type settings: dict
  126. """
  127. for key, value in settings.items():
  128. relation.to_publish_raw[key] = value