Added exception in case that "arp" command is not available on local system. Small spaces reflow.

Change-Id: I360b5162415197f9b0f0cc87d154b52d16f30533
This commit is contained in:
Franciszek Przewozny 2021-09-21 16:15:42 +02:00
parent 2585b12b87
commit d096bb551e
1 changed files with 19 additions and 14 deletions

View File

@ -56,7 +56,6 @@ def virsh_log(call_args, err_code=None):
def virsh(*args, **kwargs):
show_err = kwargs.pop('show_err', True)
virsh_exe = "virsh"
@ -185,6 +184,7 @@ def stop_running_cluster_vms():
logger.info("VM will not shut down, powering it off.")
vm_power_off(vm_id)
# -----------------------------------------------------------------------------
# Network functions
# -----------------------------------------------------------------------------
@ -201,7 +201,6 @@ def log_xml_dump(vm_name, desc, xml=None):
# Get the MAC address from a node name (default network)
def node_to_mac(node):
logger.info("Waiting for MAC address.")
while True:
dump = virsh("dumpxml", node)
@ -218,10 +217,14 @@ def node_to_mac(node):
# Get the IP address from a MAC address (default network)
def mac_to_ip(mac):
logger.info("Waiting for IP address.")
while True:
lines = subprocess.check_output(["sudo", "arp", "-n"])
try:
lines = subprocess.check_output(["sudo", "arp", "-n"])
except subprocess.CalledProcessError:
logger.error("Error invoking \"arp\" command, check if proper"
" packages are installed.")
sys.exit(1)
ma = re.search(r"(\S+).*{}".format(mac), lines)
if ma:
ip = ma.group(1)
@ -233,7 +236,6 @@ def mac_to_ip(mac):
def node_to_ip(vm_name):
# TODO refactor node_to_ip()
# Store vm_name, IP address, and MAC address in text file for later use
@ -261,7 +263,7 @@ def node_to_ip(vm_name):
ip = ma.group(1)
logger.debug("IP address for %s: %s (cached)", vm_name,
ip)
#conf.vm[vm_name].ssh_ip = ip
# conf.vm[vm_name].ssh_ip = ip
return ip
ip = mac_to_ip(mac)
@ -310,7 +312,6 @@ def virsh_destroy_network(net_name):
def virsh_stop_network(net_name):
# Undo our changes to iptables before letting libvirt deal with it
iptables_forward_new_connections(False)
@ -392,7 +393,6 @@ def virsh_network_active(net_name):
def virsh_define_network(net_name, ip_address):
network = "labs-{}".format(net_name)
if not virsh_network_defined(net_name):
logger.debug("Defining network %s (%s)", network, ip_address)
@ -416,6 +416,7 @@ def create_network(net_name, ip_address):
virsh_define_network(net_name, ip_address)
virsh_start_network(net_name)
# -----------------------------------------------------------------------------
# VM create and configure
# -----------------------------------------------------------------------------
@ -484,6 +485,7 @@ def vm_boot_order_pxe(vm_name):
xf.write(changed)
virsh("define", xml_file)
# -----------------------------------------------------------------------------
# VM unregister, remove, delete
# -----------------------------------------------------------------------------
@ -507,6 +509,7 @@ def vm_delete(vm_name):
else:
logger.info("\tnot found")
# -----------------------------------------------------------------------------
# Disk functions
# -----------------------------------------------------------------------------
@ -576,7 +579,7 @@ def disk_compress(disk_name):
# The default (/tmp) is often too small (especially if it is a RAM
# disk). We use the pool_dir instead.
subprocess.call(["sudo", spexe, "--tmp", pool_dir, "--compress",
disk_path, tmp_file])
disk_path, tmp_file])
logger.info("Restoring owner.")
# No root wrapper, so use sudo with shell commands
@ -588,11 +591,11 @@ def disk_compress(disk_name):
logger.info("Moving temporary file into final location.")
subprocess.call(["sudo", "mv", "-v", "-f", tmp_file, disk_path])
# os.chown(tmp_file, uid, gid)
# os.chmod(tmp_file, mode)
# os.chown(tmp_file, uid, gid)
# os.chmod(tmp_file, mode)
# import shutil
# shutil.move(tmp_file, disk_path)
# import shutil
# shutil.move(tmp_file, disk_path)
stat = os.stat(disk_path)
mode = stat.st_mode
@ -604,7 +607,7 @@ def disk_compress(disk_name):
new_size = stat.st_size
logger.debug("size\t%s", new_size)
compression = "%0.0f" % round((1-new_size/size)*100) + "%"
compression = "%0.0f" % round((1 - new_size / size) * 100) + "%"
# logger.info("size\t%s (compressed by %s%)", new_size, compression)
logger.info("size\t%s (compressed by %s)", new_size, compression)
@ -613,6 +616,7 @@ def get_disk_path(disk_name):
# Result comes with trailing newlines
return virsh("vol-path", "--pool", kvm_vol_pool, disk_name).rstrip()
# -----------------------------------------------------------------------------
# Snapshots
# -----------------------------------------------------------------------------
@ -641,6 +645,7 @@ def vm_snapshot(vm_name, shot_name):
virsh("snapshot-create-as", vm_name, shot_name,
"{}: {}".format(vm_name, shot_name))
# -----------------------------------------------------------------------------
# Booting a VM
# -----------------------------------------------------------------------------