Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/tier2c
File: safe_pkgacct.py
#!/opt/imh-python/bin/python3
[0] Fix | Delete
"""Wrapper around pkgacct for tier2c"""
[1] Fix | Delete
import argparse
[2] Fix | Delete
import sys
[3] Fix | Delete
import psutil
[4] Fix | Delete
import rads
[5] Fix | Delete
[6] Fix | Delete
sys.path.insert(0, '/opt/support/lib')
[7] Fix | Delete
from arg_types import cpuser_safe_arg
[8] Fix | Delete
from run_cmd import cpuwatch_execv
[9] Fix | Delete
[10] Fix | Delete
# This tool will ensure at least this much space is free after pkgacct
[11] Fix | Delete
DISK_PERCENT_LIMIT = 1.5
[12] Fix | Delete
[13] Fix | Delete
[14] Fix | Delete
def check_acct_size(user: str):
[15] Fix | Delete
"""Ensure the account is not too large to pkgacct in full"""
[16] Fix | Delete
disk_usage = psutil.disk_usage('/home')
[17] Fix | Delete
gb_free = int(disk_usage.free / 2 ** 30)
[18] Fix | Delete
gb_total = int(disk_usage.total / 2 ** 30)
[19] Fix | Delete
try:
[20] Fix | Delete
acct_gb = int(rads.QuotaCtl().getquota(user=user) / 2 ** 30)
[21] Fix | Delete
except rads.QuotasDisabled as exc:
[22] Fix | Delete
sys.exit(f"{exc} - try /scripts/fixquotas")
[23] Fix | Delete
disk_needed = gb_total * DISK_PERCENT_LIMIT / 100
[24] Fix | Delete
if gb_free - acct_gb > disk_needed:
[25] Fix | Delete
return
[26] Fix | Delete
print(f"Cannot package {user} without --skiphomedir")
[27] Fix | Delete
print(f"The account would place the disk at < {DISK_PERCENT_LIMIT}% free")
[28] Fix | Delete
print(f"Account size: {acct_gb} GB")
[29] Fix | Delete
print(f"Disk free space: {gb_free} GB")
[30] Fix | Delete
sys.exit(1)
[31] Fix | Delete
[32] Fix | Delete
[33] Fix | Delete
def parse_args():
[34] Fix | Delete
"""Parse commandline arguments"""
[35] Fix | Delete
parser = argparse.ArgumentParser(
[36] Fix | Delete
description='Wrapper around CPanel pkgacct. It is recommended to run '
[37] Fix | Delete
'this in screen due to the amount of time it may take.'
[38] Fix | Delete
)
[39] Fix | Delete
parser.add_argument(
[40] Fix | Delete
'--skiphomedir',
[41] Fix | Delete
action='store_true',
[42] Fix | Delete
help='Do not include the home directory in the CPanel package',
[43] Fix | Delete
)
[44] Fix | Delete
parser.add_argument(
[45] Fix | Delete
'user',
[46] Fix | Delete
metavar='USER',
[47] Fix | Delete
type=cpuser_safe_arg,
[48] Fix | Delete
help='user to create a CPanel package for',
[49] Fix | Delete
)
[50] Fix | Delete
args = parser.parse_args()
[51] Fix | Delete
return args
[52] Fix | Delete
[53] Fix | Delete
[54] Fix | Delete
def main():
[55] Fix | Delete
"""main: parse args, determine size, run pkgacct"""
[56] Fix | Delete
args = parse_args()
[57] Fix | Delete
if not args.skiphomedir:
[58] Fix | Delete
check_acct_size(user=args.user)
[59] Fix | Delete
cmd = ['/usr/local/cpanel/scripts/pkgacct']
[60] Fix | Delete
if args.skiphomedir:
[61] Fix | Delete
cmd.append('--skiphomedir')
[62] Fix | Delete
cmd.append(args.user)
[63] Fix | Delete
cpuwatch_execv(cmd)
[64] Fix | Delete
[65] Fix | Delete
[66] Fix | Delete
if __name__ == '__main__':
[67] Fix | Delete
main()
[68] Fix | Delete
[69] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function