Skip to content

Commit

Permalink
pre-commit disk.py
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeDog authored and dwoz committed Dec 18, 2023
1 parent da081c0 commit 2a484f5
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions salt/modules/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def _clean_flags(args, caller):
if flag in allowed:
flags += flag
else:
raise CommandExecutionError("Invalid flag passed to {}".format(caller))
raise CommandExecutionError(f"Invalid flag passed to {caller}")
return flags


Expand Down Expand Up @@ -112,7 +112,7 @@ def usage(args=None):
else:
cmd = "df"
if flags:
cmd += " -{}".format(flags)
cmd += f" -{flags}"
ret = {}
out = __salt__["cmd.run"](cmd, python_shell=False).splitlines()
oldline = None
Expand All @@ -130,7 +130,7 @@ def usage(args=None):
else:
oldline = None
while len(comps) >= 2 and not comps[1].isdigit():
comps[0] = "{} {}".format(comps[0], comps[1])
comps[0] = f"{comps[0]} {comps[1]}"
comps.pop(1)
if len(comps) < 2:
continue
Expand Down Expand Up @@ -179,7 +179,7 @@ def inodeusage(args=None):
else:
cmd = "df -iP"
if flags:
cmd += " -{}".format(flags)
cmd += f" -{flags}"
ret = {}
out = __salt__["cmd.run"](cmd, python_shell=False).splitlines()
for line in out:
Expand Down Expand Up @@ -249,7 +249,7 @@ def percent(args=None):
continue
comps = line.split()
while len(comps) >= 2 and not comps[1].isdigit():
comps[0] = "{} {}".format(comps[0], comps[1])
comps[0] = f"{comps[0]} {comps[1]}"
comps.pop(1)
if len(comps) < 2:
continue
Expand Down Expand Up @@ -354,10 +354,10 @@ def tune(device, **kwargs):
else:
args.append("getro")
if kwargs[key] == "True" or kwargs[key] is True:
opts += "--{} ".format(key)
opts += f"--{key} "
else:
opts += "--{} {} ".format(switch, kwargs[key])
cmd = "blockdev {}{}".format(opts, device)
opts += f"--{switch} {kwargs[key]} "
cmd = f"blockdev {opts}{device}"
out = __salt__["cmd.run"](cmd, python_shell=False).splitlines()
return dump(device, args)

Expand All @@ -373,7 +373,7 @@ def wipe(device):
salt '*' disk.wipe /dev/sda1
"""

cmd = "wipefs -a {}".format(device)
cmd = f"wipefs -a {device}"
try:
out = __salt__["cmd.run_all"](cmd, python_shell=False)
except subprocess.CalledProcessError as err:
Expand Down Expand Up @@ -435,7 +435,7 @@ def resize2fs(device):
salt '*' disk.resize2fs /dev/sda1
"""
cmd = "resize2fs {}".format(device)
cmd = f"resize2fs {device}"
try:
out = __salt__["cmd.run_all"](cmd, python_shell=False)
except subprocess.CalledProcessError as err:
Expand Down Expand Up @@ -502,10 +502,10 @@ def format_(
if fs_type[:3] == "ext":
cmd.extend(["-i", str(inode_size)])
elif fs_type == "xfs":
cmd.extend(["-i", "size={}".format(inode_size)])
cmd.extend(["-i", f"size={inode_size}"])
if lazy_itable_init is not None:
if fs_type[:3] == "ext":
cmd.extend(["-E", "lazy_itable_init={}".format(lazy_itable_init)])
cmd.extend(["-E", f"lazy_itable_init={lazy_itable_init}"])
if fat is not None and fat in (12, 16, 32):
if fs_type[-3:] == "fat":
cmd.extend(["-F", fat])
Expand Down Expand Up @@ -539,9 +539,7 @@ def fstype(device):
salt '*' disk.fstype /dev/sdX1
"""
if salt.utils.path.which("lsblk"):
lsblk_out = __salt__["cmd.run"](
"lsblk -o fstype {}".format(device)
).splitlines()
lsblk_out = __salt__["cmd.run"](f"lsblk -o fstype {device}").splitlines()
if len(lsblk_out) > 1:
fs_type = lsblk_out[1].strip()
if fs_type:
Expand All @@ -551,15 +549,13 @@ def fstype(device):
# the fstype was not set on the block device, so inspect the filesystem
# itself for its type
if __grains__["kernel"] == "AIX" and os.path.isfile("/usr/sysv/bin/df"):
df_out = __salt__["cmd.run"](
"/usr/sysv/bin/df -n {}".format(device)
).split()
df_out = __salt__["cmd.run"](f"/usr/sysv/bin/df -n {device}").split()
if len(df_out) > 2:
fs_type = df_out[2]
if fs_type:
return fs_type
else:
df_out = __salt__["cmd.run"]("df -T {}".format(device)).splitlines()
df_out = __salt__["cmd.run"](f"df -T {device}").splitlines()
if len(df_out) > 1:
fs_type = df_out[1]
if fs_type:
Expand All @@ -575,7 +571,7 @@ def _hdparm(args, failhard=True):
Fail hard when required
return output when possible
"""
cmd = "hdparm {}".format(args)
cmd = f"hdparm {args}"
result = __salt__["cmd.run_all"](cmd)
if result["retcode"] != 0:
msg = "{}: {}".format(cmd, result["stderr"])
Expand Down Expand Up @@ -615,9 +611,9 @@ def hdparms(disks, args="aAbBcCdgHiJkMmNnQrRuW"):
out = {}
for disk in disks:
if not disk.startswith("/dev"):
disk = "/dev/{}".format(disk)
disk = f"/dev/{disk}"
disk_data = {}
for line in _hdparm("-{} {}".format(args, disk), False).splitlines():
for line in _hdparm(f"-{args} {disk}", False).splitlines():
line = line.strip()
if not line or line == disk + ":":
continue
Expand Down Expand Up @@ -717,7 +713,7 @@ def hpa(disks, size=None):
if size <= 0:
size = data["total"]

_hdparm("--yes-i-know-what-i-am-doing -Np{} {}".format(size, disk))
_hdparm(f"--yes-i-know-what-i-am-doing -Np{size} {disk}")


def smart_attributes(dev, attributes=None, values=None):
Expand All @@ -743,7 +739,7 @@ def smart_attributes(dev, attributes=None, values=None):
if not dev.startswith("/dev/"):
dev = "/dev/" + dev

cmd = "smartctl --attributes {}".format(dev)
cmd = f"smartctl --attributes {dev}"
smart_result = __salt__["cmd.run_all"](cmd, output_loglevel="quiet")
if smart_result["retcode"] != 0:
raise CommandExecutionError(smart_result["stderr"])
Expand Down Expand Up @@ -836,9 +832,9 @@ def _iostat_fbsd(interval, count, disks):
Tested on FreeBSD, quite likely other BSD's only need small changes in cmd syntax
"""
if disks is None:
iostat_cmd = "iostat -xC -w {} -c {} ".format(interval, count)
iostat_cmd = f"iostat -xC -w {interval} -c {count} "
elif isinstance(disks, str):
iostat_cmd = "iostat -x -w {} -c {} {}".format(interval, count, disks)
iostat_cmd = f"iostat -x -w {interval} -c {count} {disks}"
else:
iostat_cmd = "iostat -x -w {} -c {} {}".format(interval, count, " ".join(disks))

Expand Down Expand Up @@ -891,9 +887,9 @@ def _iostat_fbsd(interval, count, disks):

def _iostat_linux(interval, count, disks):
if disks is None:
iostat_cmd = "iostat -x {} {} ".format(interval, count)
iostat_cmd = f"iostat -x {interval} {count} "
elif isinstance(disks, str):
iostat_cmd = "iostat -xd {} {} {}".format(interval, count, disks)
iostat_cmd = f"iostat -xd {interval} {count} {disks}"
else:
iostat_cmd = "iostat -xd {} {} {}".format(interval, count, " ".join(disks))

Expand Down Expand Up @@ -941,9 +937,9 @@ def _iostat_aix(interval, count, disks):
log.debug("AIX disk iostat entry")

if disks is None:
iostat_cmd = "iostat -dD {} {} ".format(interval, count)
iostat_cmd = f"iostat -dD {interval} {count} "
elif isinstance(disks, str):
iostat_cmd = "iostat -dD {} {} {}".format(disks, interval, count)
iostat_cmd = f"iostat -dD {disks} {interval} {count}"
else:
iostat_cmd = "iostat -dD {} {} {}".format(" ".join(disks), interval, count)

Expand Down

0 comments on commit 2a484f5

Please sign in to comment.