def run_relation_hook(self, relation_name, action): # TODO: It would be nice to use our own relation here, rather than reusing the 'website' relation if relation_name == 'website': return self._run_loadbalancer_hook(action) # TODO: Only on certain actions? logger.info("Running relation hook %s %s", relation_name, action) relation = Relation.default() properties = {} if not action == "broken": properties = relation.get_properties() logger.info("Got relation properties %s", properties) xaas = self._privateclient() config = Juju.config() service_name = Juju.service_name() unit_id = Juju.unit_name() remote_name = os.environ["JUJU_REMOTE_UNIT"] relation_id = relation.relation_id new_properties = xaas.update_relation_properties(service_name=service_name, relation=relation_name, relation_id=relation_id, unit_id=unit_id, remote_name=remote_name, action=action, properties=properties) if new_properties: logger.info("Setting relation properties to: %s", new_properties) relation.set_properties(new_properties)
def run_relation_hook(self, relation_name, action, interface_id=None): logger.info("Running relation hook %s %s", relation_name, action) # TODO: Only on certain actions? if action == "broken": return xaas = self._client() bundle_type = self.config["charm"] instance_id = Juju.service_name() if interface_id is None: interface_id = instance_id logger.info("Fetching service properties for %s/%s/%s", bundle_type, instance_id, interface_id) response = xaas.get_relation_properties(bundle_type=bundle_type, instance_id=instance_id, relation=interface_id) relation_properties = response.get("Properties", {}) protocol = relation_properties.get("protocol") if protocol == "tls": relation_properties = self._ensure_tls(bundle_type, relation_properties) relation = Relation.default() logger.info("Setting relation properties to: %s", relation_properties) relation.set_properties(relation_properties)
def _run_loadbalancer_hook(self, action): logger.info("Running load-balancer hook %s", action) host = Juju.private_address() config = Juju.config() private_port = config['private-port'] if private_port == 0: logger.info("Private port is 0; won't configure load balancer") public_port = config['public-port'] if public_port == 0: logger.info("Public port is 0; won't configure load balancer") protocol = config.get('protocol', '').strip().lower() service_name = Juju.unit_name() service_name = service_name.split('/')[0] relation = Relation.default() relation_id = relation.relation_id servers = [] servers.append(['s_1', host, private_port, '']) service_options = [ 'mode tcp', 'balance leastconn' ] if protocol == 'tls': service_options.append('ssl') service = {} service['service_name'] = service_name service['service_options'] = service_options service['servers'] = servers # Must set both service_host and service_port, or else haproxy ignores the other service['service_host'] = '0.0.0.0' service['service_port'] = public_port services = [] services.append(service) new_properties = {} new_properties['services'] = yaml.dump(services) # relation-set "services= # - { service_name: my_web_app, # service_options: [mode http, balance leastconn], # servers: [[my_web_app_1, $host, $port, option httpchk GET / HTTP/1.0], # [... optionally more servers here ...]]} # - { ... optionally more services here ... } # " if new_properties: logger.info("Setting relation properties to: %s", new_properties) relation.set_properties(new_properties)