def test_update_hosts_file_entry(self, mock_log): altmap = {'1.1.1.1': 'alt-host'} _map = { '1.1.1.1': 'hostA', '2.2.2.2': 'hostB', '3.3.3.3': 'hostC', '4.4.4.4': 'hostD' } with tempfile.NamedTemporaryFile(delete=False) as tmpfile: rabbit_utils.HOSTS_FILE = tmpfile.name with open(tmpfile.name, 'w') as fd: fd.write("#somedata\n") fd.write("%s %s\n" % (list(altmap.items())[0])) rabbit_utils.update_hosts_file(_map) with open(rabbit_utils.HOSTS_FILE, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 5) self.assertEqual(lines[0], "#somedata\n") self.assertEqual(lines[1], "%s %s\n" % (list(_map.items())[0])) self.assertEqual(lines[4], "%s %s\n" % (list(_map.items())[3]))
def cluster_changed(relation_id=None, remote_unit=None): # Future travelers beware ordering is significant rdata = relation_get(rid=relation_id, unit=remote_unit) # sync passwords blacklist = ['hostname', 'private-address', 'public-address'] whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) cookie = peer_retrieve('cookie') if not cookie: log('cluster_changed: cookie not yet set.', level=INFO) return if rdata: hostname = rdata.get('hostname', None) private_address = rdata.get('private-address', None) if hostname and private_address: rabbit.update_hosts_file({private_address: hostname}) # sync the cookie with peers if necessary update_cookie() if is_relation_made('ha') and \ config('ha-vip-only') is False: log('hacluster relation is present, skipping native ' 'rabbitmq cluster config.', level=INFO) return # NOTE(freyes): all the nodes need to marked as 'clustered' (LP: #1691510) rabbit.cluster_with() if not is_leader(): update_nrpe_checks()
def test_update_empty_hosts_file(self, mock_log): _map = {'1.2.3.4': 'my-host'} with tempfile.NamedTemporaryFile(delete=False) as tmpfile: rabbit_utils.HOSTS_FILE = tmpfile.name rabbit_utils.HOSTS_FILE = tmpfile.name rabbit_utils.update_hosts_file(_map) with open(tmpfile.name, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 1) self.assertEqual(lines[0], "%s %s\n" % (list(_map.items())[0]))
def cluster_changed(relation_id=None, remote_unit=None): # Future travelers beware ordering is significant rdata = relation_get(rid=relation_id, unit=remote_unit) # sync passwords blacklist = ['hostname', 'private-address', 'public-address'] whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) cookie = peer_retrieve('cookie') if not cookie: log('cluster_changed: cookie not yet set.', level=INFO) return if rdata: hostname = rdata.get('hostname', None) private_address = rdata.get('private-address', None) if hostname and private_address: rabbit.update_hosts_file({private_address: hostname}) # sync the cookie with peers if necessary update_cookie() if is_relation_made('ha') and \ config('ha-vip-only') is False: log( 'hacluster relation is present, skipping native ' 'rabbitmq cluster config.', level=INFO) return if rabbit.is_sufficient_peers(): # NOTE(freyes): all the nodes need to marked as 'clustered' # (LP: #1691510) rabbit.cluster_with() # Local rabbit maybe clustered now so check and inform clients if # needed. update_clients() if is_leader(): if (leader_get(rabbit.CLUSTER_MODE_KEY) != config( rabbit.CLUSTER_MODE_KEY)): log("Informing peers via leaderdb to change {} to {}".format( rabbit.CLUSTER_MODE_KEY, config(rabbit.CLUSTER_MODE_KEY))) leader_set( {rabbit.CLUSTER_MODE_KEY: config(rabbit.CLUSTER_MODE_KEY)}) rabbit.ConfigRenderer(rabbit.CONFIG_FILES).write_all() if not is_leader() and is_relation_made('nrpe-external-master'): update_nrpe_checks()
def cluster_changed(): # Future travelers beware ordering is significant rdata = relation_get() # sync passwords blacklist = ['hostname', 'private-address', 'public-address'] whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) cookie = peer_retrieve('cookie') if not cookie: log('cluster_joined: cookie not yet set.', level=INFO) return rdata = relation_get() if rdata: hostname = rdata.get('hostname', None) private_address = rdata.get('private-address', None) if hostname and private_address: rabbit.update_hosts_file({private_address: hostname}) if not is_sufficient_peers(): log('Not enough peers, waiting until leader is configured', level=INFO) return # sync the cookie with peers if necessary update_cookie() if is_relation_made('ha') and \ config('ha-vip-only') is False: log('hacluster relation is present, skipping native ' 'rabbitmq cluster config.', level=INFO) return # cluster with node? try: if not is_leader(): rabbit.cluster_with() update_nrpe_checks() except NotImplementedError: if is_newer(): rabbit.cluster_with() update_nrpe_checks() # If cluster has changed peer db may have changed so run amqp_changed # to sync any changes for rid in relation_ids('amqp'): for unit in related_units(rid): amqp_changed(relation_id=rid, remote_unit=unit)
def cluster_changed(): cookie = peer_retrieve('cookie') if not cookie: log('cluster_joined: cookie not yet set.', level=INFO) return rdata = relation_get() if config('prefer-ipv6') and rdata.get('hostname'): private_address = rdata['private-address'] hostname = rdata['hostname'] if hostname: rabbit.update_hosts_file({private_address: hostname}) # sync passwords blacklist = ['hostname', 'private-address', 'public-address'] whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) if not is_sufficient_peers(): # Stop rabbit until leader has finished configuring service_stop('rabbitmq-server') return # sync the cookie with peers if necessary update_cookie() if is_relation_made('ha') and \ config('ha-vip-only') is False: log('hacluster relation is present, skipping native ' 'rabbitmq cluster config.', level=INFO) return # cluster with node? try: if not is_leader(): rabbit.cluster_with() update_nrpe_checks() except NotImplementedError: if is_newer(): rabbit.cluster_with() update_nrpe_checks() # If cluster has changed peer db may have changed so run amqp_changed # to sync any changes for rid in relation_ids('amqp'): for unit in related_units(rid): amqp_changed(relation_id=rid, remote_unit=unit)
def test_update_hosts_file_entry(self, mock_log): altmap = {'1.1.1.1': 'alt-host'} _map = {'1.1.1.1': 'hostA', '2.2.2.2': 'hostB', '3.3.3.3': 'hostC', '4.4.4.4': 'hostD'} with tempfile.NamedTemporaryFile(delete=False) as tmpfile: rabbit_utils.HOSTS_FILE = tmpfile.name with open(tmpfile.name, 'w') as fd: fd.write("#somedata\n") fd.write("%s %s\n" % (list(altmap.items())[0])) rabbit_utils.update_hosts_file(_map) with open(rabbit_utils.HOSTS_FILE, 'r') as fd: lines = fd.readlines() os.remove(tmpfile.name) self.assertEqual(len(lines), 5) self.assertEqual(lines[0], "#somedata\n") self.assertEqual(lines[1], "%s %s\n" % (list(_map.items())[0])) self.assertEqual(lines[4], "%s %s\n" % (list(_map.items())[3]))
def cluster_changed(relation_id=None, remote_unit=None): # Future travelers beware ordering is significant rdata = relation_get(rid=relation_id, unit=remote_unit) # sync passwords blacklist = ['hostname', 'private-address', 'public-address'] whitelist = [a for a in rdata.keys() if a not in blacklist] peer_echo(includes=whitelist) cookie = peer_retrieve('cookie') if not cookie: log('cluster_changed: cookie not yet set.', level=INFO) return if rdata: hostname = rdata.get('hostname', None) private_address = rdata.get('private-address', None) if hostname and private_address: rabbit.update_hosts_file({private_address: hostname}) # sync the cookie with peers if necessary update_cookie() if is_relation_made('ha') and \ config('ha-vip-only') is False: log('hacluster relation is present, skipping native ' 'rabbitmq cluster config.', level=INFO) return if rabbit.is_sufficient_peers(): # NOTE(freyes): all the nodes need to marked as 'clustered' (LP: #1691510) rabbit.cluster_with() if not is_leader() and is_relation_made('nrpe-external-master'): update_nrpe_checks()
def config_changed(check_deferred_restarts=True): """Run config-chaged hook. :param check_deferred_events: Whether to check if restarts are permitted before running hook. :type check_deferred_events: bool """ configure_deferred_restarts(rabbit.services()) allowed, reason = is_hook_allowed( 'config-changed', check_deferred_restarts=check_deferred_restarts) if not allowed: log(reason, "WARN") return # Update hosts with this unit's information cluster_ip = ch_ip.get_relation_ip( rabbit_net_utils.CLUSTER_INTERFACE, cidr_network=config(rabbit_net_utils.CLUSTER_OVERRIDE_CONFIG)) rabbit.update_hosts_file({cluster_ip: rabbit.get_unit_hostname()}) # Add archive source if provided and not in the upgrade process if not leader_get("cluster_series_upgrading"): add_source(config('source'), config('key')) # Copy in defaults file for updated ulimits shutil.copyfile('templates/rabbitmq-server', '/etc/default/rabbitmq-server') # Install packages to ensure any changes to source # result in an upgrade if applicable only if we change the 'source' # config option if rabbit.archive_upgrade_available(): # Avoid packge upgrade collissions # Stopping and attempting to start rabbitmqs at the same time leads to # failed restarts rabbit.cluster_wait() rabbit.install_or_upgrade_packages() if config('ssl') == 'off': open_port(5672) close_port(int(config('ssl_port'))) elif config('ssl') == 'on': open_port(5672) open_port(int(config('ssl_port'))) elif config('ssl') == 'only': close_port(5672) open_port(int(config('ssl_port'))) else: log("Unknown ssl config value: '%s'" % config('ssl'), level=ERROR) chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER) chmod(RABBIT_DIR, 0o775) if rabbit.management_plugin_enabled(): rabbit.enable_plugin(MAN_PLUGIN) open_port(rabbit.get_managment_port()) else: rabbit.disable_plugin(MAN_PLUGIN) close_port(rabbit.get_managment_port()) # LY: Close the old managment port since it may have been opened in a # previous version of the charm. close_port is a noop if the port # is not open close_port(55672) # NOTE(jamespage): If a newer RMQ version is # installed and the old style configuration file # is still on disk, remove before re-rendering # any new configuration. if (os.path.exists(rabbit.RABBITMQ_CONFIG) and cmp_pkgrevno('rabbitmq-server', '3.7') >= 0): os.remove(rabbit.RABBITMQ_CONFIG) rabbit.ConfigRenderer(rabbit.CONFIG_FILES()).write_all() if is_relation_made("ha"): ha_is_active_active = config("ha-vip-only") if ha_is_active_active: update_nrpe_checks() else: if is_elected_leader('res_rabbitmq_vip'): update_nrpe_checks() else: log("hacluster relation is present but this node is not active" " skipping update nrpe checks") elif is_relation_made('nrpe-external-master'): update_nrpe_checks() # Only set values if this is the leader if not is_leader(): return rabbit.set_all_mirroring_queues(config('mirroring-queues')) # Update cluster in case min-cluster-size has changed for rid in relation_ids('cluster'): for unit in related_units(rid): cluster_changed(relation_id=rid, remote_unit=unit) # NOTE(jamespage): Workaround until we have a good way # of generally disabling notifications # based on which services are deployed. if 'openstack' in rabbit.list_vhosts(): rabbit.configure_notification_ttl('openstack', config('notification-ttl'))
def config_changed(): # Update hosts with this unit's information rabbit.update_hosts_file({ rabbit.get_unit_ip(config_override=rabbit.CLUSTER_OVERRIDE_CONFIG, interface=rabbit.CLUSTER_INTERFACE): rabbit.get_unit_hostname() }) # Add archive source if provided add_source(config('source'), config('key')) apt_update(fatal=True) # Copy in defaults file for updated ulimits shutil.copyfile('templates/rabbitmq-server', '/etc/default/rabbitmq-server') # Install packages to ensure any changes to source # result in an upgrade if applicable. status_set('maintenance', 'Installing/upgrading RabbitMQ packages') apt_install(rabbit.PACKAGES, fatal=True) open_port(5672) chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER) chmod(RABBIT_DIR, 0o775) if config('management_plugin') is True: rabbit.enable_plugin(MAN_PLUGIN) open_port(rabbit.get_managment_port()) else: rabbit.disable_plugin(MAN_PLUGIN) close_port(rabbit.get_managment_port()) # LY: Close the old managment port since it may have been opened in a # previous version of the charm. close_port is a noop if the port # is not open close_port(55672) rabbit.ConfigRenderer(rabbit.CONFIG_FILES).write_all() # Only set values if this is the leader if not is_leader(): return rabbit.set_all_mirroring_queues(config('mirroring-queues')) if is_relation_made("ha"): ha_is_active_active = config("ha-vip-only") if ha_is_active_active: update_nrpe_checks() else: if is_elected_leader('res_rabbitmq_vip'): update_nrpe_checks() else: log("hacluster relation is present but this node is not active" " skipping update nrpe checks") else: update_nrpe_checks() # Update cluster in case min-cluster-size has changed for rid in relation_ids('cluster'): for unit in related_units(rid): cluster_changed(relation_id=rid, remote_unit=unit)
def config_changed(): # Update hosts with this unit's information rabbit.update_hosts_file( {rabbit.get_unit_ip(config_override=rabbit.CLUSTER_OVERRIDE_CONFIG, interface=rabbit.CLUSTER_INTERFACE): rabbit.get_unit_hostname()}) # Add archive source if provided add_source(config('source'), config('key')) # Copy in defaults file for updated ulimits shutil.copyfile( 'templates/rabbitmq-server', '/etc/default/rabbitmq-server') # Install packages to ensure any changes to source # result in an upgrade if applicable only if we change the 'source' # config option if rabbit.archive_upgrade_available(): # Avoid packge upgrade collissions # Stopping and attempting to start rabbitmqs at the same time leads to # failed restarts rabbit.cluster_wait() rabbit.install_or_upgrade_packages() if config('ssl') == 'off': open_port(5672) close_port(int(config('ssl_port'))) elif config('ssl') == 'on': open_port(5672) open_port(int(config('ssl_port'))) elif config('ssl') == 'only': close_port(5672) open_port(int(config('ssl_port'))) else: log("Unknown ssl config value: '%s'" % config('ssl'), level=ERROR) chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER) chmod(RABBIT_DIR, 0o775) if config('management_plugin') is True: rabbit.enable_plugin(MAN_PLUGIN) open_port(rabbit.get_managment_port()) else: rabbit.disable_plugin(MAN_PLUGIN) close_port(rabbit.get_managment_port()) # LY: Close the old managment port since it may have been opened in a # previous version of the charm. close_port is a noop if the port # is not open close_port(55672) rabbit.ConfigRenderer( rabbit.CONFIG_FILES).write_all() # Only set values if this is the leader if not is_leader(): return rabbit.set_all_mirroring_queues(config('mirroring-queues')) if is_relation_made("ha"): ha_is_active_active = config("ha-vip-only") if ha_is_active_active: update_nrpe_checks() else: if is_elected_leader('res_rabbitmq_vip'): update_nrpe_checks() else: log("hacluster relation is present but this node is not active" " skipping update nrpe checks") else: update_nrpe_checks() # Update cluster in case min-cluster-size has changed for rid in relation_ids('cluster'): for unit in related_units(rid): cluster_changed(relation_id=rid, remote_unit=unit)
def config_changed(): if is_unit_paused_set(): log("Do not run config_changed while unit is paused", "WARNING") return # Update hosts with this unit's information cluster_ip = ch_ip.get_relation_ip( rabbit_net_utils.CLUSTER_INTERFACE, cidr_network=config(rabbit_net_utils.CLUSTER_OVERRIDE_CONFIG)) rabbit.update_hosts_file({cluster_ip: rabbit.get_unit_hostname()}) # Add archive source if provided and not in the upgrade process if not leader_get("cluster_series_upgrading"): add_source(config('source'), config('key')) # Copy in defaults file for updated ulimits shutil.copyfile( 'templates/rabbitmq-server', '/etc/default/rabbitmq-server') # Install packages to ensure any changes to source # result in an upgrade if applicable only if we change the 'source' # config option if rabbit.archive_upgrade_available(): # Avoid packge upgrade collissions # Stopping and attempting to start rabbitmqs at the same time leads to # failed restarts rabbit.cluster_wait() rabbit.install_or_upgrade_packages() if config('ssl') == 'off': open_port(5672) close_port(int(config('ssl_port'))) elif config('ssl') == 'on': open_port(5672) open_port(int(config('ssl_port'))) elif config('ssl') == 'only': close_port(5672) open_port(int(config('ssl_port'))) else: log("Unknown ssl config value: '%s'" % config('ssl'), level=ERROR) chown(RABBIT_DIR, rabbit.RABBIT_USER, rabbit.RABBIT_USER) chmod(RABBIT_DIR, 0o775) if config('management_plugin') is True: rabbit.enable_plugin(MAN_PLUGIN) open_port(rabbit.get_managment_port()) else: rabbit.disable_plugin(MAN_PLUGIN) close_port(rabbit.get_managment_port()) # LY: Close the old managment port since it may have been opened in a # previous version of the charm. close_port is a noop if the port # is not open close_port(55672) rabbit.ConfigRenderer( rabbit.CONFIG_FILES).write_all() if is_relation_made("ha"): ha_is_active_active = config("ha-vip-only") if ha_is_active_active: update_nrpe_checks() else: if is_elected_leader('res_rabbitmq_vip'): update_nrpe_checks() else: log("hacluster relation is present but this node is not active" " skipping update nrpe checks") elif is_relation_made('nrpe-external-master'): update_nrpe_checks() # Only set values if this is the leader if not is_leader(): return rabbit.set_all_mirroring_queues(config('mirroring-queues')) # Update cluster in case min-cluster-size has changed for rid in relation_ids('cluster'): for unit in related_units(rid): cluster_changed(relation_id=rid, remote_unit=unit) # NOTE(jamespage): Workaround until we have a good way # of generally disabling notifications # based on which services are deployed. if 'openstack' in rabbit.list_vhosts(): rabbit.configure_notification_ttl('openstack', config('notification-ttl'))