def print_data(msg, user): #if user["zfs_quota"]: _zfs_quota = bytes2human(user["zfs_quota"]) #else: # _zfs_quota = "None" print "%s - user=%s, uid=%s, email=%s, used=%s, current-quota=%s, ldap-quota=%s" \ % (msg, user["username"], user["uid"], user["mail"], bytes2human(user["zfs_used"]), _zfs_quota, bytes2human(user["ldap_quota"]))
def main(): parser = argparse.ArgumentParser() parser.add_argument('--config-env', help="config environment", dest="config_env", default="production") parser.add_argument('--force', help="force bootstrap of account even if files exist", dest="force", action="store_true", default=False) parser.add_argument('--debug', help="set debug level (0-4)", dest="debug", nargs="?", const=0, type=int) parser.add_argument('--noop', help="only print actions, make no changes", dest="noop", action="store_true", default=False) parser.add_argument('--report', help="generate report of what will be done", dest="report", action="store_true", default=False) parser.add_argument('--report-space', help="report on space that can be removed", dest="report_space", action="store_true", default=False) parser.add_argument('--account', help="account to create", dest="account", default=None) parser.add_argument('--exclude-accounts', nargs="+", help="accounts to exclude", dest="exclude_accounts", default=[]) args = parser.parse_args() options = vars(args) # Set values based on loaded config config = load_config() _auth_token = config[args.config_env].get("api_auth_token") _account_home_config = config[args.config_env].get("account_home") _cleanup_exclude = _account_home_config.get("cleanup_exclude", []) + args.exclude_accounts _host = config[args.config_env].get("host") _port = config[args.config_env].get("port") _https = config[args.config_env].get("https") _protocol = 'https' if _https else 'http' _url = "%s://%s:%s/" % (_protocol, _host, _port) if _port else "%s://%s/" % (_protocol, _host) _json_headers = { "Accept": "application/json", "Content-Type": "application/json", "Authorization": "Token token=%s" % _auth_token, } # Setup logging setup_logging(debug=args.debug, noop=args.noop) logger.debug4("OPTIONS: %s" % options) logger.debug4("CONFIG: %s" % config) # Get status ID status = actmgr_api.get_status(url=_url, headers=_json_headers, name='CLOSED') logger.debug1("STATUS: %s", status) status_id = status.get("id") # Get accounts and perform account cleanup steps if args.account: accounts = actmgr_api.get_accounts(url=_url, headers=_json_headers, params={"username": args.account, "status_id": status_id}) else: accounts = actmgr_api.get_accounts(url=_url, headers=_json_headers, params={"status_id": status_id}) logger.debug4("Number of accounts returned: %s", len(accounts)) _report = [] for account in accounts: logger.debug4("Account data: %s", json.dumps(account)) _username = account["username"] if _username in _cleanup_exclude: logger.info("EXCLUDED: %s", _username) continue try: _shell = getpwnam(_username).pw_shell except KeyError: logger.warn("Unable to get shell for %s", _username) _shell = None if _shell != '/sbin/nologin': logger.warn("User %s shell %s != /sbin/nologin", _username, _shell) continue _account_home = AccountHome(username=_username, config=_account_home_config, options=options) _slurm_account = SlurmAccount(username=_username, options=options) if args.report: _account_home.check_path_owner(_account_home.home) _account_home.check_path_owner(_account_home.scratch) for _dir in _account_home.extra_directories: _account_home.check_path_owner(_dir) _data = {} _data["username"] = _username _data["HOME"] = _account_home.home_exists() _data["SCRATCH"] = _account_home.scratch_exists() _data["EXTRA"] = _account_home.extra_directories _data["SLURM"] = _slurm_account.exists() if args.report_space: _data["HOME_USED"] = get_space_used(host=_account_home_config["server"], path=_account_home.home) _data["SCRATCH_USED"] = get_space_used(path=_account_home.scratch) _data["EXTRA_USED"] = 0 for _dir in _account_home.extra_directories: _data["EXTRA_USED"] += get_space_used(path=_dir) _report.append(_data) else: _account_home.cleanup() _slurm_account.delete() if args.report: if args.report_space: table = prettytable.PrettyTable(["Username", "HOME", "HOME-USED", "SCRATCH", "SCRATCH-USED", "EXTRA", "EXTRA-USED", "SLURM"]) else: table = prettytable.PrettyTable(["Username", "HOME", "SCRATCH", "EXTRA", "SLURM"]) table.hrules = prettytable.FRAME _home_total = 0 _home_used_total = 0 _scratch_total = 0 _scratch_used_total = 0 _extra_total = 0 _extra_used_total = 0 _slurm_total = 0 for r in sorted(_report, key=lambda k: k["username"]): _home = r["HOME"] _scratch = r["SCRATCH"] _extra = r["EXTRA"] _slurm = r["SLURM"] if _home: _home_total += 1 if _scratch: _scratch_total += 1 if _extra: _extra_total += len(_extra) if _slurm: _slurm_total += 1 if args.report_space: _home_used = bytes2human(r["HOME_USED"]) _home_used_total += r["HOME_USED"] _scratch_used = bytes2human(r["SCRATCH_USED"]) _scratch_used_total += r["SCRATCH_USED"] _extra_used = bytes2human(r["EXTRA_USED"]) _extra_used_total += r["EXTRA_USED"] table.add_row([r["username"], _home, _home_used, _scratch, _scratch_used, "\n".join(_extra), _extra_used, _slurm]) else: table.add_row([r["username"], _home, _scratch, "\n".join(_extra), _slurm]) if args.report_space: table.add_row(["", "", "", "", "", "", "", ""]) table.add_row(["Total", _home_total, bytes2human(_home_used_total), _scratch_total, bytes2human(_scratch_used_total), _extra_total, bytes2human(_extra_used_total), _slurm_total]) else: table.add_row(["", "", "", "", ""]) table.add_row(["Total", _home_total, _scratch_total, _extra_total, _slurm_total]) print table
def main(): parser = argparse.ArgumentParser() parser.add_argument('--config-env', help="config environment", dest="config_env", default="production") parser.add_argument('--force', help="force bootstrap of account even if files exist", dest="force", action="store_true", default=False) parser.add_argument('--debug', help="set debug level (0-4)", dest="debug", nargs="?", const=0, type=int) parser.add_argument('--noop', help="only print actions, make no changes", dest="noop", action="store_true", default=False) parser.add_argument('--report', help="generate report of what will be done", dest="report", action="store_true", default=False) parser.add_argument('--report-space', help="report on space that can be removed", dest="report_space", action="store_true", default=False) parser.add_argument('--account', help="account to create", dest="account", default=None) parser.add_argument('--exclude-accounts', nargs="+", help="accounts to exclude", dest="exclude_accounts", default=[]) args = parser.parse_args() options = vars(args) # Set values based on loaded config config = load_config() _auth_token = config[args.config_env].get("api_auth_token") _account_home_config = config[args.config_env].get("account_home") _cleanup_exclude = _account_home_config.get("cleanup_exclude", []) + args.exclude_accounts _host = config[args.config_env].get("host") _port = config[args.config_env].get("port") _https = config[args.config_env].get("https") _protocol = 'https' if _https else 'http' _url = "%s://%s:%s/" % ( _protocol, _host, _port) if _port else "%s://%s/" % (_protocol, _host) _json_headers = { "Accept": "application/json", "Content-Type": "application/json", "Authorization": "Token token=%s" % _auth_token, } # Setup logging setup_logging(debug=args.debug, noop=args.noop) logger.debug4("OPTIONS: %s" % options) logger.debug4("CONFIG: %s" % config) # Get status ID status = actmgr_api.get_status(url=_url, headers=_json_headers, name='CLOSED') logger.debug1("STATUS: %s", status) status_id = status.get("id") # Get accounts and perform account cleanup steps if args.account: accounts = actmgr_api.get_accounts(url=_url, headers=_json_headers, params={ "username": args.account, "status_id": status_id }) else: accounts = actmgr_api.get_accounts(url=_url, headers=_json_headers, params={"status_id": status_id}) logger.debug4("Number of accounts returned: %s", len(accounts)) _report = [] for account in accounts: logger.debug4("Account data: %s", json.dumps(account)) _username = account["username"] if _username in _cleanup_exclude: logger.info("EXCLUDED: %s", _username) continue try: _shell = getpwnam(_username).pw_shell except KeyError: logger.warn("Unable to get shell for %s", _username) _shell = None if _shell != '/sbin/nologin': logger.warn("User %s shell %s != /sbin/nologin", _username, _shell) continue _account_home = AccountHome(username=_username, config=_account_home_config, options=options) _slurm_account = SlurmAccount(username=_username, options=options) if args.report: _account_home.check_path_owner(_account_home.home) _account_home.check_path_owner(_account_home.scratch) for _dir in _account_home.extra_directories: _account_home.check_path_owner(_dir) _data = {} _data["username"] = _username _data["HOME"] = _account_home.home_exists() _data["SCRATCH"] = _account_home.scratch_exists() _data["EXTRA"] = _account_home.extra_directories _data["SLURM"] = _slurm_account.exists() if args.report_space: _data["HOME_USED"] = get_space_used( host=_account_home_config["server"], path=_account_home.home) _data["SCRATCH_USED"] = get_space_used( path=_account_home.scratch) _data["EXTRA_USED"] = 0 for _dir in _account_home.extra_directories: _data["EXTRA_USED"] += get_space_used(path=_dir) _report.append(_data) else: _account_home.cleanup() _slurm_account.delete() if args.report: if args.report_space: table = prettytable.PrettyTable([ "Username", "HOME", "HOME-USED", "SCRATCH", "SCRATCH-USED", "EXTRA", "EXTRA-USED", "SLURM" ]) else: table = prettytable.PrettyTable( ["Username", "HOME", "SCRATCH", "EXTRA", "SLURM"]) table.hrules = prettytable.FRAME _home_total = 0 _home_used_total = 0 _scratch_total = 0 _scratch_used_total = 0 _extra_total = 0 _extra_used_total = 0 _slurm_total = 0 for r in sorted(_report, key=lambda k: k["username"]): _home = r["HOME"] _scratch = r["SCRATCH"] _extra = r["EXTRA"] _slurm = r["SLURM"] if _home: _home_total += 1 if _scratch: _scratch_total += 1 if _extra: _extra_total += len(_extra) if _slurm: _slurm_total += 1 if args.report_space: _home_used = bytes2human(r["HOME_USED"]) _home_used_total += r["HOME_USED"] _scratch_used = bytes2human(r["SCRATCH_USED"]) _scratch_used_total += r["SCRATCH_USED"] _extra_used = bytes2human(r["EXTRA_USED"]) _extra_used_total += r["EXTRA_USED"] table.add_row([ r["username"], _home, _home_used, _scratch, _scratch_used, "\n".join(_extra), _extra_used, _slurm ]) else: table.add_row([ r["username"], _home, _scratch, "\n".join(_extra), _slurm ]) if args.report_space: table.add_row(["", "", "", "", "", "", "", ""]) table.add_row([ "Total", _home_total, bytes2human(_home_used_total), _scratch_total, bytes2human(_scratch_used_total), _extra_total, bytes2human(_extra_used_total), _slurm_total ]) else: table.add_row(["", "", "", "", ""]) table.add_row([ "Total", _home_total, _scratch_total, _extra_total, _slurm_total ]) print table