Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable black formatter #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,20 @@ name: Check coding style
on: [push]

jobs:
black:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install black
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: black
run: |
black --unstable --check .
pycodestyle:
runs-on: ubuntu-latest
steps:
Expand Down
13 changes: 6 additions & 7 deletions SOURCES/etc/xapi.d/plugins/hyperthreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@

import XenAPIPlugin

sys.path.append('.')
sys.path.append(".")
from xcpngutils import configure_logging, run_command, error_wrapped


@error_wrapped
def get_hyperthreading(session, args):
result = run_command(['xl', 'info', 'threads_per_core'])
result = run_command(["xl", "info", "threads_per_core"])
_LOGGER.info(result)
lines = result['stdout'].splitlines()
lines = result["stdout"].splitlines()
return json.dumps(int(lines[0]) > 1)


_LOGGER = configure_logging('hyperthreading')
_LOGGER = configure_logging("hyperthreading")
if __name__ == "__main__":
XenAPIPlugin.dispatch({
'get_hyperthreading': get_hyperthreading
})
XenAPIPlugin.dispatch({"get_hyperthreading": get_hyperthreading})
8 changes: 6 additions & 2 deletions SOURCES/etc/xapi.d/plugins/lsblk.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

from xcpngutils import run_command, error_wrapped


@error_wrapped
def list_block_devices(session, args):
result = run_command(["lsblk", "-P", "-b", "-o", "NAME,KNAME,PKNAME,SIZE,TYPE,RO,MOUNTPOINT"])
result = run_command(
["lsblk", "-P", "-b", "-o", "NAME,KNAME,PKNAME,SIZE,TYPE,RO,MOUNTPOINT"]
)
output_string = result["stdout"].decode("utf-8").strip()

results = list()
Expand All @@ -30,7 +33,8 @@ def list_block_devices(session, args):

blockdevices[kname] = output_dict

return json.dumps({'blockdevices': results})
return json.dumps({"blockdevices": results})


if __name__ == "__main__":
XenAPIPlugin.dispatch({"list_block_devices": list_block_devices})
182 changes: 106 additions & 76 deletions SOURCES/etc/xapi.d/plugins/lvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,26 @@

import XenAPIPlugin

from xcpngutils import configure_logging, run_command, error_wrapped, strtobool, raise_plugin_error
from xcpngutils import (
configure_logging,
run_command,
error_wrapped,
strtobool,
raise_plugin_error,
)


def raise_lvm_error(code, stderr):
raise_plugin_error('LVM_ERROR({})'.format(code), stderr)
raise_plugin_error("LVM_ERROR({})".format(code), stderr)


def is_vg_not_found_error(name, error):
return 'Volume group "{}" not found'.format(name) in error


@error_wrapped
def list_physical_volumes(session, args):
command = ['pvs', '--noheadings', '--units', 'B', '--nosuffix']
command = ["pvs", "--noheadings", "--units", "B", "--nosuffix"]
result = run_command(command)

# Default format:
Expand All @@ -29,36 +38,39 @@ def list_physical_volumes(session, args):
# Physical free size

pvs_result = {}
for index, line in enumerate(result['stdout'].splitlines()):
for index, line in enumerate(result["stdout"].splitlines()):
fields = line.split()
if len(fields) != 6:
raise Exception('Unexpected pv result at index {}: `{}`.'.format(index, line))
raise Exception(
"Unexpected pv result at index {}: `{}`.".format(index, line)
)

pv_name = fields[0].strip()

pv_result = {}
pv_result['vg_name'] = fields[1].strip()
pv_result['format'] = fields[2].strip()
pv_result['attributes'] = fields[3].strip()
pv_result['capacity'] = int(fields[4])
pv_result['free'] = int(fields[5])
pv_result["vg_name"] = fields[1].strip()
pv_result["format"] = fields[2].strip()
pv_result["attributes"] = fields[3].strip()
pv_result["capacity"] = int(fields[4])
pv_result["free"] = int(fields[5])

pvs_result[pv_name] = pv_result

return json.dumps(pvs_result)


@error_wrapped
def list_volume_groups(session, args):
vg_name = args.get('vg_name')
command = ['vgs', '--noheadings', '--units', 'B', '--nosuffix']
vg_name = args.get("vg_name")
command = ["vgs", "--noheadings", "--units", "B", "--nosuffix"]
if vg_name:
command.append(vg_name)
result = run_command(command, check=False)
code = result['returncode']
code = result["returncode"]
if code:
if vg_name and is_vg_not_found_error(vg_name, result['stderr']):
return '{}'
raise_lvm_error(code, result['stderr'])
if vg_name and is_vg_not_found_error(vg_name, result["stderr"]):
return "{}"
raise_lvm_error(code, result["stderr"])

# Default format:
# VG name
Expand All @@ -70,34 +82,45 @@ def list_volume_groups(session, args):
# Free size in byes

vgs_result = {}
for index, line in enumerate(result['stdout'].splitlines()):
for index, line in enumerate(result["stdout"].splitlines()):
fields = line.split()
if len(fields) != 7:
raise Exception('Unexpected vg result at index {}: `{}`.'.format(index, line))
raise Exception(
"Unexpected vg result at index {}: `{}`.".format(index, line)
)

vg_name = fields[0].strip()

vg_result = {}
vg_result['pv_count'] = int(fields[1])
vg_result['lv_count'] = int(fields[2])
vg_result['sn_count'] = int(fields[3])
vg_result['attributes'] = fields[4].strip()
vg_result['capacity'] = int(fields[5])
vg_result['free'] = int(fields[6])
vg_result["pv_count"] = int(fields[1])
vg_result["lv_count"] = int(fields[2])
vg_result["sn_count"] = int(fields[3])
vg_result["attributes"] = fields[4].strip()
vg_result["capacity"] = int(fields[5])
vg_result["free"] = int(fields[6])

vgs_result[vg_name] = vg_result

return json.dumps(vgs_result)


@error_wrapped
def list_logical_volumes(session, args):
vg_name = args.get('vg_name')
vg_name = args.get("vg_name")

# Use a separator because few fields like "Pool" can be empty.
# Note: The valid characters for VG and LV names are: a-z A-Z 0-9 + _ . -
# So we use another char.
columns = ['-olv_name,vg_name,lv_attr,lv_size,pool_lv']
command = ['lvs', '--noheadings', '--units', 'B', '--nosuffix', '--separator', ':'] + columns
columns = ["-olv_name,vg_name,lv_attr,lv_size,pool_lv"]
command = [
"lvs",
"--noheadings",
"--units",
"B",
"--nosuffix",
"--separator",
":",
] + columns
if vg_name:
command.append(vg_name)
result = run_command(command)
Expand All @@ -117,103 +140,110 @@ def list_logical_volumes(session, args):
# Convert

lvs_result = {}
for index, line in enumerate(result['stdout'].splitlines()):
fields = line.split(':')
for index, line in enumerate(result["stdout"].splitlines()):
fields = line.split(":")
if len(fields) != 5:
raise Exception('Unexpected lv result at index {}: `{}`.'.format(index, line))
raise Exception(
"Unexpected lv result at index {}: `{}`.".format(index, line)
)

lv_name = fields[0].strip()

lv_result = {}
lv_result['vg_name'] = fields[1].strip()
lv_result['attributes'] = fields[2].strip()
lv_result['capacity'] = int(fields[3])
lv_result['pool'] = fields[4].strip()
lv_result["vg_name"] = fields[1].strip()
lv_result["attributes"] = fields[2].strip()
lv_result["capacity"] = int(fields[3])
lv_result["pool"] = fields[4].strip()

lvs_result[lv_name] = lv_result

return json.dumps(lvs_result)


@error_wrapped
def create_physical_volume(session, args):
devices = args['devices'].split(',')
ignore_fs = strtobool(args.get('ignore_existing_filesystems'))
force = strtobool(args.get('force'))
devices = args["devices"].split(",")
ignore_fs = strtobool(args.get("ignore_existing_filesystems"))
force = strtobool(args.get("force"))

command = ['pvcreate'] + devices
command = ["pvcreate"] + devices
if force:
command.extend(('-ff', '-y'))
command.extend(("-ff", "-y"))
elif ignore_fs:
command.extend(('-f', '-y'))
command.extend(("-f", "-y"))
else:
command.append('-qq')
command.append("-qq")

result = run_command(command, check=False)
code = result['returncode']
code = result["returncode"]
if code:
raise_lvm_error(code, result['stderr'])
raise_lvm_error(code, result["stderr"])

return "{}"

return '{}'

@error_wrapped
def create_volume_group(session, args):
vg_name = args['vg_name']
devices = args['devices'].split(',')
force = strtobool(args.get('force'))
vg_name = args["vg_name"]
devices = args["devices"].split(",")
force = strtobool(args.get("force"))

command = ['vgcreate', vg_name] + devices
command = ["vgcreate", vg_name] + devices
if force:
command.append('-f')
command.append("-f")

result = run_command(command, check=False)
code = result['returncode']
code = result["returncode"]
if code:
raise_lvm_error(code, result['stderr'])
raise_lvm_error(code, result["stderr"])

return "{}"

return '{}'

@error_wrapped
def destroy_volume_group(session, args):
vg_name = args['vg_name']
force = strtobool(args.get('force'))
vg_name = args["vg_name"]
force = strtobool(args.get("force"))

command = ['vgremove', vg_name]
command = ["vgremove", vg_name]
if force:
# Useful to remove LVMs.
command.extend(('-f', '-y'))
command.extend(("-f", "-y"))
else:
command.append('-qq')
command.append("-qq")

result = run_command(command, check=False)
code = result['returncode']
code = result["returncode"]
if code:
if force and is_vg_not_found_error(vg_name, result['stderr']):
return '{}'
raise_lvm_error(code, result['stderr'])
if force and is_vg_not_found_error(vg_name, result["stderr"]):
return "{}"
raise_lvm_error(code, result["stderr"])

return "{}"

return '{}'

@error_wrapped
def create_thin_pool(session, args):
vg_name = args['vg_name']
lv_name = args['lv_name']
vg_name = args["vg_name"]
lv_name = args["lv_name"]

command = ['lvcreate', '-l', '100%FREE', '-T', '{}/{}'.format(vg_name, lv_name)]
command = ["lvcreate", "-l", "100%FREE", "-T", "{}/{}".format(vg_name, lv_name)]
result = run_command(command, check=False)
code = result['returncode']
code = result["returncode"]
if code:
raise_lvm_error(code, result['stderr'])
raise_lvm_error(code, result["stderr"])

return "{}"

return '{}'

_LOGGER = configure_logging('lvm')
_LOGGER = configure_logging("lvm")
if __name__ == "__main__":
XenAPIPlugin.dispatch({
'list_physical_volumes': list_physical_volumes,
'list_volume_groups': list_volume_groups,
'list_logical_volumes': list_logical_volumes,
'create_physical_volume': create_physical_volume,
'create_volume_group': create_volume_group,
'destroy_volume_group': destroy_volume_group,
'create_thin_pool': create_thin_pool
"list_physical_volumes": list_physical_volumes,
"list_volume_groups": list_volume_groups,
"list_logical_volumes": list_logical_volumes,
"create_physical_volume": create_physical_volume,
"create_volume_group": create_volume_group,
"destroy_volume_group": destroy_volume_group,
"create_thin_pool": create_thin_pool,
})
Loading
Loading