2018-05-31 21:49:23 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
# NodeInstance.py - Class implementing a PVC node in pvcd
|
2018-06-06 01:47:53 -04:00
|
|
|
# Part of the Parallel Virtual Cluster (PVC) system
|
|
|
|
#
|
2019-10-13 12:09:51 -04:00
|
|
|
# Copyright (C) 2018-2019 Joshua M. Boniface <joshua@boniface.me>
|
2018-06-06 01:47:53 -04:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
###############################################################################
|
|
|
|
|
2018-09-20 03:42:40 -04:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import psutil
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
import libvirt
|
|
|
|
import threading
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
import pvcd.log as log
|
|
|
|
import pvcd.zkhandler as zkhandler
|
|
|
|
import pvcd.common as common
|
2018-06-06 22:56:03 -04:00
|
|
|
|
2018-10-14 22:14:29 -04:00
|
|
|
class NodeInstance(object):
|
2018-06-06 22:59:31 -04:00
|
|
|
# Initialization function
|
2019-12-14 15:55:30 -05:00
|
|
|
def __init__(self, name, this_node, zk_conn, config, logger, d_node, d_network, d_domain, dns_aggregator, metadata_api):
|
2018-05-31 21:49:23 -04:00
|
|
|
# Passed-in variables on creation
|
2018-10-14 02:01:35 -04:00
|
|
|
self.name = name
|
|
|
|
self.this_node = this_node
|
2018-06-17 21:55:39 -04:00
|
|
|
self.zk_conn = zk_conn
|
2018-06-08 12:19:48 -04:00
|
|
|
self.config = config
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logger = logger
|
|
|
|
# Which node is primary
|
|
|
|
self.primary_node = None
|
|
|
|
# States
|
|
|
|
self.daemon_mode = zkhandler.readdata(self.zk_conn, '/nodes/{}/daemonmode'.format(self.name))
|
2018-06-11 02:46:24 -04:00
|
|
|
self.daemon_state = 'stop'
|
2018-10-14 02:01:35 -04:00
|
|
|
self.router_state = 'client'
|
2018-06-11 02:46:24 -04:00
|
|
|
self.domain_state = 'ready'
|
2018-10-14 02:01:35 -04:00
|
|
|
# Object lists
|
|
|
|
self.d_node = d_node
|
|
|
|
self.d_network = d_network
|
|
|
|
self.d_domain = d_domain
|
2018-10-15 21:09:40 -04:00
|
|
|
self.dns_aggregator = dns_aggregator
|
2019-12-14 15:55:30 -05:00
|
|
|
self.metadata_api = metadata_api
|
2018-10-14 02:01:35 -04:00
|
|
|
# Printable lists
|
2018-06-04 16:34:41 -04:00
|
|
|
self.active_node_list = []
|
|
|
|
self.flushed_node_list = []
|
|
|
|
self.inactive_node_list = []
|
2018-10-14 02:01:35 -04:00
|
|
|
self.network_list = []
|
2018-06-01 12:21:58 -04:00
|
|
|
self.domain_list = []
|
2018-10-14 02:01:35 -04:00
|
|
|
# Node resources
|
2018-06-11 01:50:06 -04:00
|
|
|
self.domains_count = 0
|
|
|
|
self.memused = 0
|
|
|
|
self.memfree = 0
|
2018-07-17 21:34:11 -04:00
|
|
|
self.memalloc = 0
|
2018-07-18 12:09:07 -04:00
|
|
|
self.vcpualloc = 0
|
2018-10-17 00:23:43 -04:00
|
|
|
# Floating upstreams
|
2019-03-11 01:44:26 -04:00
|
|
|
if self.config['enable_networking']:
|
|
|
|
self.vni_dev = self.config['vni_dev']
|
|
|
|
self.vni_ipaddr, self.vni_cidrnetmask = self.config['vni_floating_ip'].split('/')
|
|
|
|
self.upstream_dev = self.config['upstream_dev']
|
|
|
|
self.upstream_ipaddr, self.upstream_cidrnetmask = self.config['upstream_floating_ip'].split('/')
|
|
|
|
else:
|
|
|
|
self.vni_dev = None
|
|
|
|
self.vni_ipaddr = None
|
|
|
|
self.vni_cidrnetmask = None
|
|
|
|
self.upstream_dev = None
|
|
|
|
self.upstream_ipaddr = None
|
|
|
|
self.upstream_cidrnetmask = None
|
2019-07-10 01:07:56 -04:00
|
|
|
# Threads
|
|
|
|
self.flush_thread = None
|
2018-10-14 02:01:35 -04:00
|
|
|
# Flags
|
2019-07-10 01:07:56 -04:00
|
|
|
self.flush_stopper = False
|
2018-06-01 12:21:58 -04:00
|
|
|
|
|
|
|
# Zookeeper handlers for changed states
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/daemonstate'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_daemonstate(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-06-06 01:24:28 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 'stop'
|
|
|
|
|
|
|
|
if data != self.daemon_state:
|
|
|
|
self.daemon_state = data
|
|
|
|
|
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/routerstate'.format(self.name))
|
|
|
|
def watch_node_routerstate(data, stat, event=''):
|
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
data = 'client'
|
|
|
|
|
|
|
|
if self.name == self.this_node and self.daemon_mode == 'coordinator':
|
|
|
|
# We're a coordinator so we care about networking
|
|
|
|
if data != self.router_state:
|
|
|
|
self.router_state = data
|
2019-08-04 16:42:06 -04:00
|
|
|
if self.config['enable_networking']:
|
|
|
|
if self.router_state == 'primary':
|
|
|
|
self.become_primary()
|
|
|
|
else:
|
|
|
|
self.become_secondary()
|
2018-06-11 02:46:24 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/domainstate'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_domainstate(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-06-11 02:46:24 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-06-11 02:46:24 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 'unknown'
|
|
|
|
|
|
|
|
if data != self.domain_state:
|
|
|
|
self.domain_state = data
|
|
|
|
|
|
|
|
# toggle state management of this node
|
|
|
|
if self.name == self.this_node:
|
2019-07-10 01:07:56 -04:00
|
|
|
# Stop any existing flush jobs
|
|
|
|
if self.flush_thread:
|
|
|
|
self.flush_stopper = True
|
|
|
|
self.logger.out('Waiting for previous migration to complete'.format(self.name), state='i')
|
|
|
|
while self.flush_stopper:
|
|
|
|
time.sleep(1)
|
|
|
|
self.flush_stopper = False
|
|
|
|
# Do flushing in a thread so it doesn't block the migrates out
|
|
|
|
if self.domain_state == 'flush':
|
|
|
|
self.flush_thread = threading.Thread(target=self.flush, args=(), kwargs={})
|
|
|
|
self.flush_thread.start()
|
|
|
|
# Do unflushing in a thread so it doesn't block the migrates in
|
|
|
|
if self.domain_state == 'unflush':
|
|
|
|
self.flush_thread = threading.Thread(target=self.unflush, args=(), kwargs={})
|
|
|
|
self.flush_thread.start()
|
2018-10-14 02:01:35 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/memfree'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_memfree(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-06-06 01:24:28 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 0
|
|
|
|
|
|
|
|
if data != self.memfree:
|
|
|
|
self.memfree = data
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/memused'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_memused(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-06-11 02:46:24 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-06-11 02:46:24 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 0
|
|
|
|
|
|
|
|
if data != self.memused:
|
|
|
|
self.memused = data
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/memalloc'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_memalloc(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-07-17 21:34:11 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-07-17 21:34:11 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 0
|
|
|
|
|
|
|
|
if data != self.memalloc:
|
|
|
|
self.memalloc = data
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/vcpualloc'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_vcpualloc(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-07-18 12:09:07 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii')
|
2018-07-18 12:09:07 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = 0
|
|
|
|
|
|
|
|
if data != self.vcpualloc:
|
|
|
|
self.vcpualloc = data
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-10-01 22:51:34 -04:00
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/runningdomains'.format(self.name))
|
2018-10-14 02:01:35 -04:00
|
|
|
def watch_node_runningdomains(data, stat, event=''):
|
2018-10-01 22:51:34 -04:00
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
|
|
|
|
2018-06-06 01:24:28 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = data.decode('ascii').split()
|
2018-06-06 01:24:28 -04:00
|
|
|
except AttributeError:
|
2018-10-14 02:01:35 -04:00
|
|
|
data = []
|
2018-05-31 21:49:23 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
if data != self.domain_list:
|
|
|
|
self.domain_list = data
|
|
|
|
|
|
|
|
@self.zk_conn.DataWatch('/nodes/{}/domainscount'.format(self.name))
|
|
|
|
def watch_node_domainscount(data, stat, event=''):
|
|
|
|
if event and event.type == 'DELETED':
|
|
|
|
# The key has been deleted after existing before; terminate this watcher
|
|
|
|
# because this class instance is about to be reaped in Daemon.py
|
|
|
|
return False
|
2018-05-31 23:40:21 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
try:
|
|
|
|
data = data.decode('ascii')
|
|
|
|
except AttributeError:
|
|
|
|
data = 0
|
2018-06-01 12:21:58 -04:00
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
if data != self.domains_count:
|
|
|
|
self.domains_count = data
|
2019-06-25 22:31:04 -04:00
|
|
|
|
2018-05-31 23:01:22 -04:00
|
|
|
# Update value functions
|
2018-10-14 02:01:35 -04:00
|
|
|
def update_node_list(self, d_node):
|
|
|
|
self.d_node = d_node
|
|
|
|
|
|
|
|
def update_network_list(self, d_network):
|
|
|
|
self.d_network = d_network
|
|
|
|
network_list = []
|
|
|
|
for network in self.d_network:
|
|
|
|
network_list.append(d_network[network].vni)
|
|
|
|
self.network_list = network_list
|
|
|
|
|
|
|
|
def update_domain_list(self, d_domain):
|
|
|
|
self.d_domain = d_domain
|
|
|
|
|
|
|
|
# Routing primary/secondary states
|
|
|
|
def become_secondary(self):
|
2019-08-04 16:42:06 -04:00
|
|
|
self.logger.out('Setting router {} to secondary state'.format(self.name), state='i')
|
|
|
|
self.logger.out('Network list: {}'.format(', '.join(self.network_list)), state='i')
|
|
|
|
time.sleep(2)
|
|
|
|
if self.config['enable_api']:
|
|
|
|
self.logger.out('Stopping PVC API client service', state='i')
|
|
|
|
common.run_os_command("systemctl stop pvc-api.service")
|
|
|
|
for network in self.d_network:
|
|
|
|
self.d_network[network].stopDHCPServer()
|
|
|
|
self.d_network[network].removeGateways()
|
|
|
|
self.dns_aggregator.stop_aggregator()
|
2019-12-14 15:55:30 -05:00
|
|
|
self.metadata_api.stop()
|
|
|
|
self.removeFloatingAddresses()
|
2018-10-14 02:01:35 -04:00
|
|
|
|
|
|
|
def become_primary(self):
|
2019-08-04 16:42:06 -04:00
|
|
|
# Establish a lock
|
|
|
|
with zkhandler.writelock(self.zk_conn, '/primary_node'):
|
2019-03-11 01:44:26 -04:00
|
|
|
self.logger.out('Setting router {} to primary state'.format(self.name), state='i')
|
2019-08-04 16:42:06 -04:00
|
|
|
|
|
|
|
# Create floating addresses
|
2019-07-10 22:24:03 -04:00
|
|
|
self.logger.out('Network list: {}'.format(', '.join(self.network_list)), state='i')
|
2019-03-11 01:44:26 -04:00
|
|
|
self.createFloatingAddresses()
|
|
|
|
# Start up the gateways and DHCP servers
|
|
|
|
for network in self.d_network:
|
|
|
|
self.d_network[network].createGateways()
|
|
|
|
self.d_network[network].startDHCPServer()
|
2019-05-20 22:40:07 -04:00
|
|
|
time.sleep(1)
|
2019-08-04 16:42:06 -04:00
|
|
|
|
|
|
|
# Switch Patroni leader to the local instance
|
2019-05-20 22:40:07 -04:00
|
|
|
self.logger.out('Setting Patroni leader to this node', state='i')
|
2019-08-07 11:46:42 -04:00
|
|
|
tick = 1
|
2019-08-04 16:42:06 -04:00
|
|
|
while True:
|
|
|
|
retcode, stdout, stderr = common.run_os_command(
|
|
|
|
"""
|
|
|
|
patronictl
|
|
|
|
-c /etc/patroni/config.yml
|
|
|
|
-d zookeeper://localhost:2181
|
|
|
|
switchover
|
|
|
|
--candidate {}
|
|
|
|
--force
|
|
|
|
pvcdns
|
|
|
|
""".format(self.name)
|
|
|
|
)
|
|
|
|
if stdout:
|
|
|
|
self.logger.out('Successfully switched Patroni leader\n{}'.format(stdout), state='o')
|
|
|
|
break
|
2019-08-04 17:00:33 -04:00
|
|
|
elif stderr == "Error: Switchover target and source are the same.\n":
|
2019-08-04 16:50:21 -04:00
|
|
|
self.logger.out('Failed to switch Patroni leader to ourselves; this is fine\n{}'.format(stderr), state='w')
|
2019-08-04 17:00:33 -04:00
|
|
|
break
|
2019-08-07 11:46:42 -04:00
|
|
|
elif tick >= 5:
|
|
|
|
self.logger.out('Failed to switch Patroni leader after 5 tries; aborting\n{}'.format(stderr), state='e')
|
|
|
|
break
|
2019-08-04 16:42:06 -04:00
|
|
|
else:
|
2019-08-07 11:46:42 -04:00
|
|
|
self.logger.out('Failed to switch Patroni leader; retrying [{}/5]\n{}'.format(tick, stderr), state='e')
|
|
|
|
tick += 1
|
2019-08-04 16:42:06 -04:00
|
|
|
time.sleep(2)
|
|
|
|
|
2019-05-20 22:40:07 -04:00
|
|
|
# Start the DNS aggregator instance
|
2019-08-04 16:42:06 -04:00
|
|
|
time.sleep(1)
|
2019-03-11 01:44:26 -04:00
|
|
|
self.dns_aggregator.start_aggregator()
|
2019-12-14 15:55:30 -05:00
|
|
|
self.metadata_api.start()
|
2018-06-02 15:03:44 -04:00
|
|
|
|
2019-12-12 23:03:34 -05:00
|
|
|
# Start the clients
|
|
|
|
if self.config['enable_api']:
|
|
|
|
self.logger.out('Starting PVC API client service', state='i')
|
|
|
|
common.run_os_command("systemctl start pvc-api.service")
|
2019-12-14 14:13:26 -05:00
|
|
|
self.logger.out('Starting PVC Provisioner Worker service', state='i')
|
2019-12-12 23:03:34 -05:00
|
|
|
common.run_os_command("systemctl start pvc-provisioner-worker.service")
|
|
|
|
|
2018-10-17 00:23:43 -04:00
|
|
|
def createFloatingAddresses(self):
|
2019-12-14 15:55:30 -05:00
|
|
|
# Metadata link-local IP
|
|
|
|
self.logger.out(
|
|
|
|
'Creating Metadata link-local IP {}/{} on interface {}'.format(
|
|
|
|
'169.254.169.254',
|
|
|
|
'32',
|
|
|
|
'lo'
|
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
|
|
|
common.createIPAddress('169.254.169.254', '32', 'lo')
|
|
|
|
|
2018-10-17 00:23:43 -04:00
|
|
|
# VNI floating IP
|
|
|
|
self.logger.out(
|
|
|
|
'Creating floating management IP {}/{} on interface {}'.format(
|
|
|
|
self.vni_ipaddr,
|
|
|
|
self.vni_cidrnetmask,
|
2019-03-18 20:17:26 -04:00
|
|
|
'brcluster'
|
2018-10-17 00:23:43 -04:00
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
2019-03-18 20:17:26 -04:00
|
|
|
common.createIPAddress(self.vni_ipaddr, self.vni_cidrnetmask, 'brcluster')
|
2019-12-14 15:55:30 -05:00
|
|
|
|
2018-10-17 00:23:43 -04:00
|
|
|
# Upstream floating IP
|
|
|
|
self.logger.out(
|
|
|
|
'Creating floating upstream IP {}/{} on interface {}'.format(
|
|
|
|
self.upstream_ipaddr,
|
|
|
|
self.upstream_cidrnetmask,
|
|
|
|
self.upstream_dev
|
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
2018-10-27 16:31:31 -04:00
|
|
|
common.createIPAddress(self.upstream_ipaddr, self.upstream_cidrnetmask, self.upstream_dev)
|
2018-10-17 00:23:43 -04:00
|
|
|
|
|
|
|
def removeFloatingAddresses(self):
|
2019-12-14 15:55:30 -05:00
|
|
|
# Metadata link-local IP
|
|
|
|
self.logger.out(
|
|
|
|
'Removing Metadata link-local IP {}/{} from interface {}'.format(
|
|
|
|
'169.254.169.254',
|
|
|
|
'32',
|
|
|
|
'lo'
|
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
|
|
|
common.removeIPAddress('169.254.169.254', '32', 'lo')
|
|
|
|
|
2018-10-17 00:23:43 -04:00
|
|
|
# VNI floating IP
|
|
|
|
self.logger.out(
|
|
|
|
'Removing floating management IP {}/{} from interface {}'.format(
|
|
|
|
self.vni_ipaddr,
|
|
|
|
self.vni_cidrnetmask,
|
2019-03-18 20:17:26 -04:00
|
|
|
'brcluster'
|
2018-10-17 00:23:43 -04:00
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
2019-03-18 20:17:26 -04:00
|
|
|
common.removeIPAddress(self.vni_ipaddr, self.vni_cidrnetmask, 'brcluster')
|
2019-12-14 15:55:30 -05:00
|
|
|
|
2018-10-17 00:23:43 -04:00
|
|
|
# Upstream floating IP
|
|
|
|
self.logger.out(
|
|
|
|
'Removing floating upstream IP {}/{} from interface {}'.format(
|
|
|
|
self.upstream_ipaddr,
|
|
|
|
self.upstream_cidrnetmask,
|
|
|
|
self.upstream_dev
|
|
|
|
),
|
|
|
|
state='o'
|
|
|
|
)
|
2018-10-27 16:31:31 -04:00
|
|
|
common.removeIPAddress(self.upstream_ipaddr, self.upstream_cidrnetmask, self.upstream_dev)
|
2018-10-17 00:23:43 -04:00
|
|
|
|
2018-05-31 22:31:20 -04:00
|
|
|
# Flush all VMs on the host
|
2018-06-04 16:34:41 -04:00
|
|
|
def flush(self):
|
2019-05-10 23:52:24 -04:00
|
|
|
# Begin flush
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logger.out('Flushing node "{}" of running VMs'.format(self.name), state='i')
|
2019-07-10 22:24:03 -04:00
|
|
|
self.logger.out('VM list: {}'.format(', '.join(self.domain_list)), state='i')
|
2018-07-17 14:29:18 -04:00
|
|
|
fixed_domain_list = self.domain_list.copy()
|
|
|
|
for dom_uuid in fixed_domain_list:
|
2019-07-10 01:07:56 -04:00
|
|
|
# Allow us to cancel the operation
|
|
|
|
if self.flush_stopper:
|
|
|
|
self.logger.out('Aborting node flush'.format(self.name), state='i')
|
|
|
|
self.flush_thread = None
|
|
|
|
self.flush_stopper = False
|
|
|
|
return
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logger.out('Selecting target to migrate VM "{}"'.format(dom_uuid), state='i')
|
2018-07-17 14:29:18 -04:00
|
|
|
|
2019-10-12 01:59:08 -04:00
|
|
|
target_node = common.findTargetNode(self.zk_conn, self.config, dom_uuid)
|
2019-07-07 15:27:37 -04:00
|
|
|
|
|
|
|
# Don't replace the previous node if the VM is already migrated
|
2019-07-10 00:43:55 -04:00
|
|
|
if zkhandler.readdata(self.zk_conn, '/domains/{}/lastnode'.format(dom_uuid)):
|
2019-07-07 15:29:34 -04:00
|
|
|
current_node = zkhandler.readdata(self.zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
2019-07-07 15:27:37 -04:00
|
|
|
else:
|
|
|
|
current_node = zkhandler.readdata(self.zk_conn, '/domains/{}/node'.format(dom_uuid))
|
|
|
|
|
2019-10-12 01:17:39 -04:00
|
|
|
if target_node is None:
|
|
|
|
self.logger.out('Failed to find migration target for VM "{}"; shutting down and setting autostart flag'.format(dom_uuid), state='e')
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/state'.format(dom_uuid): 'shutdown' })
|
2019-10-12 01:17:39 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/domains/{}/node_autostart'.format(dom_uuid): 'True' })
|
2019-07-07 15:27:37 -04:00
|
|
|
|
|
|
|
# Wait for the VM to shut down
|
|
|
|
while zkhandler.readdata(self.zk_conn, '/domains/{}/state'.format(dom_uuid)) != 'stop':
|
2018-07-17 00:25:36 -04:00
|
|
|
time.sleep(1)
|
|
|
|
|
2019-07-07 15:27:37 -04:00
|
|
|
continue
|
|
|
|
|
|
|
|
self.logger.out('Migrating VM "{}" to node "{}"'.format(dom_uuid, target_node), state='i')
|
|
|
|
zkhandler.writedata(self.zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): 'migrate',
|
|
|
|
'/domains/{}/node'.format(dom_uuid): target_node,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): current_node
|
|
|
|
})
|
|
|
|
|
|
|
|
# Wait for the VM to migrate so the next VM's free RAM count is accurate (they migrate in serial anyways)
|
|
|
|
while zkhandler.readdata(self.zk_conn, '/domains/{}/state'.format(dom_uuid)) != 'start':
|
|
|
|
time.sleep(1)
|
|
|
|
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/nodes/{}/runningdomains'.format(self.name): '' })
|
|
|
|
zkhandler.writedata(self.zk_conn, { '/nodes/{}/domainstate'.format(self.name): 'flushed' })
|
2019-07-10 01:07:56 -04:00
|
|
|
self.flush_thread = None
|
|
|
|
self.flush_stopper = False
|
2018-06-11 18:05:35 -04:00
|
|
|
|
2018-06-04 03:09:51 -04:00
|
|
|
def unflush(self):
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logger.out('Restoring node {} to active service.'.format(self.name), state='i')
|
|
|
|
fixed_domain_list = self.d_domain.copy()
|
2018-07-17 14:29:18 -04:00
|
|
|
for dom_uuid in fixed_domain_list:
|
2019-07-10 01:07:56 -04:00
|
|
|
# Allow us to cancel the operation
|
|
|
|
if self.flush_stopper:
|
|
|
|
self.logger.out('Aborting node unflush'.format(self.name), state='i')
|
|
|
|
self.flush_thread = None
|
|
|
|
self.flush_stopper = False
|
|
|
|
return
|
|
|
|
|
2019-10-12 01:17:39 -04:00
|
|
|
# Handle autostarts
|
|
|
|
autostart = zkhandler.readdata(self.zk_conn, '/domains/{}/node_autostart'.format(dom_uuid))
|
|
|
|
node = zkhandler.readdata(self.zk_conn, '/domains/{}/node'.format(dom_uuid))
|
|
|
|
if autostart == 'True' and node == self.name:
|
|
|
|
self.logger.out('Starting autostart VM "{}"'.format(dom_uuid), state='i')
|
|
|
|
zkhandler.writedata(self.zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): 'start',
|
|
|
|
'/domains/{}/node'.format(dom_uuid): self.name,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): '',
|
|
|
|
'/domains/{}/node_autostart'.format(dom_uuid): 'False'
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
|
2018-07-17 00:14:46 -04:00
|
|
|
try:
|
2018-10-14 02:01:35 -04:00
|
|
|
last_node = zkhandler.readdata(self.zk_conn, '/domains/{}/lastnode'.format(dom_uuid))
|
2018-07-17 00:14:46 -04:00
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
if last_node != self.name:
|
2018-06-06 20:36:03 -04:00
|
|
|
continue
|
|
|
|
|
2018-10-14 02:01:35 -04:00
|
|
|
self.logger.out('Setting unmigration for VM "{}"'.format(dom_uuid), state='i')
|
2018-06-26 23:24:33 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, {
|
|
|
|
'/domains/{}/state'.format(dom_uuid): 'migrate',
|
2018-10-14 02:01:35 -04:00
|
|
|
'/domains/{}/node'.format(dom_uuid): self.name,
|
|
|
|
'/domains/{}/lastnode'.format(dom_uuid): ''
|
2018-06-26 23:24:33 -04:00
|
|
|
})
|
2018-06-04 03:09:51 -04:00
|
|
|
|
2019-05-11 00:30:47 -04:00
|
|
|
# Wait for the VM to migrate back
|
|
|
|
while zkhandler.readdata(self.zk_conn, '/domains/{}/state'.format(dom_uuid)) != 'start':
|
|
|
|
time.sleep(1)
|
|
|
|
|
2019-05-11 00:55:15 -04:00
|
|
|
zkhandler.writedata(self.zk_conn, { '/nodes/{}/domainstate'.format(self.name): 'ready' })
|
2019-07-10 01:07:56 -04:00
|
|
|
self.flush_thread = None
|
|
|
|
self.flush_stopper = False
|