From 93474dbaad8dd95201509e981dbb834e3ffba1cd Mon Sep 17 00:00:00 2001 From: Hui Xiang Date: Thu, 11 Dec 2014 18:30:21 +0800 Subject: [PATCH 1/3] Fix envrc file and MonitorNeutron --- files/MonitorNeutron | 5 +++-- files/monitor.py | 39 ++++++++++++++++++++------------------- hooks/quantum_hooks.py | 4 +++- hooks/quantum_utils.py | 40 +++++++++++++++++++++++++++++++--------- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/files/MonitorNeutron b/files/MonitorNeutron index 0464bde3..48f31541 100644 --- a/files/MonitorNeutron +++ b/files/MonitorNeutron @@ -98,8 +98,9 @@ END MonitorNeutron_start() { ocf_log info "MonitorNeutron_start" - su ${OCF_RESKEY_user} -s /bin/sh -c "python ${OCF_RESKEY_binary} $OCF_RESKEY_additional_parameters" \ - ' >> /dev/null 2>&1 & echo $!' > $OCF_RESKEY_pid + su ${OCF_RESKEY_user} -s /bin/sh -c "python ${OCF_RESKEY_binary} \ + $OCF_RESKEY_additional_parameters"' \ + >> /dev/null 2>&1 & echo $!' > $OCF_RESKEY_pid if [ $? = $OCF_SUCCESS ]; then return $OCF_SUCCESS diff --git a/files/monitor.py b/files/monitor.py index 381eb2eb..f386b6c5 100644 --- a/files/monitor.py +++ b/files/monitor.py @@ -93,19 +93,26 @@ class MonitorNeutronAgentsDaemon(Daemon): super(MonitorNeutronAgentsDaemon, self).__init__() self.check_interval = check_interval LOG.info('Monitor Neutron Agent Loop Init') + self.env = {} def get_env(self): - env = {} - env_data = '/etc/legacy_ha_env_data' - if os.path.isfile(env_data): - with open(env_data, 'r') as f: - line = f.readline() - data = line.split('=').strip() - if data and data[0] and data[1]: - env[data[0]] = env[data[1]] - else: - raise Exception("OpenStack env data uncomplete.") - return env + envrc_f = '/etc/legacy_ha_envrc' + envrc_f_m = False + if os.path.isfile(envrc_f): + ctime = time.ctime(os.stat(envrc_f).st_ctime) + mtime = time.ctime(os.stat(envrc_f).st_mtime) + if ctime != mtime: + envrc_f_m = True + + if not self.env or envrc_f_m: + with open(envrc_f, 'r') as f: + line = f.readline() + data = line.split('=').strip() + if data and data[0] and data[1]: + self.env[data[0]] = self.env[data[1]] + else: + raise Exception("OpenStack env data uncomplete.") + return self.env def reassign_agent_resources(self): ''' Use agent scheduler API to detect down agents and re-schedule ''' @@ -130,10 +137,6 @@ class MonitorNeutronAgentsDaemon(Daemon): region_name=env['region']) partner_gateways = [] - #partner_gateways = [unit_private_ip().split('.')[0]] - #for partner_gateway in relations_of_type(reltype='cluster'): - # gateway_hostname = get_hostname(partner_gateway['private-address']) - # partner_gateways.append(gateway_hostname.partition('.')[0]) agents = quantum.list_agents(agent_type=DHCP_AGENT) dhcp_agents = [] @@ -147,8 +150,7 @@ class MonitorNeutronAgentsDaemon(Daemon): agent['id'])['networks']: networks[network['id']] = agent['id'] else: - if agent['host'].partition('.')[0] in partner_gateways: - dhcp_agents.append(agent['id']) + dhcp_agents.append(agent['id']) agents = quantum.list_agents(agent_type=L3_AGENT) routers = {} @@ -160,8 +162,7 @@ class MonitorNeutronAgentsDaemon(Daemon): agent['id'])['routers']: routers[router['id']] = agent['id'] else: - if agent['host'].split('.')[0] in partner_gateways: - l3_agents.append(agent['id']) + l3_agents.append(agent['id']) if len(dhcp_agents) == 0 or len(l3_agents) == 0: LOG.info('Unable to relocate resources, there are %s dhcp_agents ' diff --git a/hooks/quantum_hooks.py b/hooks/quantum_hooks.py index 041d8e1d..2e7ed64f 100755 --- a/hooks/quantum_hooks.py +++ b/hooks/quantum_hooks.py @@ -192,6 +192,9 @@ def nm_changed(): ca_crt = b64decode(relation_get('ca_cert')) install_ca_cert(ca_crt) + if config('ha-legacy-mode'): + cache_env_data() + @hooks.hook("cluster-relation-departed") @restart_on_change(restart_map()) @@ -245,7 +248,6 @@ def ha_relation_joined(): 'res_MonitorHA': 'op monitor interval="5s"', 'nees_connectivity': 'location res_MonitorHA ' 'rule pingd: defined pingd' - #'rule -inf: not_defined pingd or pingd lte 0' } clones = { diff --git a/hooks/quantum_utils.py b/hooks/quantum_utils.py index 4dc521b7..668c1a4d 100644 --- a/hooks/quantum_utils.py +++ b/hooks/quantum_utils.py @@ -473,11 +473,7 @@ def reassign_agent_resources(): auth_url=auth_url, region_name=env['region']) - partner_gateways = [unit_private_ip().split('.')[0]] - for partner_gateway in relations_of_type(reltype='cluster'): - gateway_hostname = get_hostname(partner_gateway['private-address']) - partner_gateways.append(gateway_hostname.partition('.')[0]) - + partner_gateways = get_quantum_gateway_cluster_nodes() agents = quantum.list_agents(agent_type=DHCP_AGENT) dhcp_agents = [] l3_agents = [] @@ -608,6 +604,14 @@ def get_dns_host(): return ' '.join(dns_hosts) +def get_quantum_gateway_cluster_nodes(): + partner_gateways = get_hostname(unit_private_ip()) + for partner_gateway in relations_of_type(reltype='cluster'): + gateway_hostname = get_hostname(partner_gateway['private-address']) + partner_gateways.append(gateway_hostname.partition('.')[0]) + return partner_gateways + + def copy_file(source_dir, des_dir, f, f_mod=None, update=False): if not os.path.isdir(des_dir): mkdir(des_dir) @@ -663,7 +667,7 @@ def install_legacy_ha_files(update=False): if config('ha-legacy-mode'): init_ocf_MonitorNeutron_f(update=update) init_external_agent_f(update=update) - #init_reassign_agent_services_binary() + # init_reassign_agent_services_binary() init_monitor_daemon(update=update) @@ -679,6 +683,24 @@ def cache_env_data(): log('Unable to get NetworkServiceContext at this time', level=ERROR) return - with open('/etc/legacy_ha_env_data', 'w') as f: - for k, v in env.items(): - f.write(''.join([k, '=', v, '\n'])) + no_envrc = False + envrc_f = '/etc/legacy_ha_envrc' + if os.path.isfile(envrc): + with open(envrc_f, 'r') as f: + data = f.read() + data = data.strip().split('\n') + + diff = False + for line in data: + k = line.split('=')[0] + v = line.split('=')[1] + if k not in env or v != env[k]: + diff = True + break + else: + no_envrc = True + + if no_envrc or diff: + with open(envrc, 'w') as f: + for k, v in env.items(): + f.write(''.join([k, '=', v, '\n'])) From 9e36aa33c2e505f9e2da2aa774b8de1d973fac71 Mon Sep 17 00:00:00 2001 From: Hui Xiang Date: Thu, 11 Dec 2014 18:50:20 +0800 Subject: [PATCH 2/3] Fix neutron-gateway ha err. --- hooks/quantum_utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hooks/quantum_utils.py b/hooks/quantum_utils.py index 668c1a4d..526567b9 100644 --- a/hooks/quantum_utils.py +++ b/hooks/quantum_utils.py @@ -685,7 +685,7 @@ def cache_env_data(): no_envrc = False envrc_f = '/etc/legacy_ha_envrc' - if os.path.isfile(envrc): + if os.path.isfile(envrc_f): with open(envrc_f, 'r') as f: data = f.read() data = data.strip().split('\n') @@ -699,8 +699,8 @@ def cache_env_data(): break else: no_envrc = True - + if no_envrc or diff: - with open(envrc, 'w') as f: + with open(envrc_f, 'w') as f: for k, v in env.items(): f.write(''.join([k, '=', v, '\n'])) From 44b91d5bdfde4d7f958fc427a1e888bca1015a80 Mon Sep 17 00:00:00 2001 From: Hui Xiang Date: Fri, 12 Dec 2014 10:52:06 +0800 Subject: [PATCH 3/3] Fix monitor err --- files/monitor.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/files/monitor.py b/files/monitor.py index f386b6c5..10ea6159 100644 --- a/files/monitor.py +++ b/files/monitor.py @@ -106,12 +106,12 @@ class MonitorNeutronAgentsDaemon(Daemon): if not self.env or envrc_f_m: with open(envrc_f, 'r') as f: - line = f.readline() - data = line.split('=').strip() - if data and data[0] and data[1]: - self.env[data[0]] = self.env[data[1]] - else: - raise Exception("OpenStack env data uncomplete.") + for line in f: + data = line.strip().split('=') + if data and data[0] and data[1]: + self.env[data[0]] = self.env[data[1]] + else: + raise Exception("OpenStack env data uncomplete.") return self.env def reassign_agent_resources(self):