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

cmd: add pop-db as a flux account command #487

Merged
merged 2 commits into from
Aug 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ creation_time mod_time deleted username bank shares max_jobs max_wall_
```

Multiple rows of data can be loaded to the database at once using `.csv` files
and the `flux account-pop-db` command. Run `flux account-pop-db --help` for
and the `flux account pop-db` command. Run `flux account pop-db --help` for
`.csv` formatting instructions.

User and bank information can also be exported from the database using the
Expand Down
1 change: 0 additions & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ dist_fluxcmd_SCRIPTS = \
cmd/flux-account.py \
cmd/flux-account-update-fshare \
cmd/flux-account-priority-update.py \
cmd/flux-account-pop-db.py \
cmd/flux-account-update-db.py \
cmd/flux-account-service.py \
cmd/flux-account-fetch-job-records.py
51 changes: 51 additions & 0 deletions src/bindings/python/fluxacct/accounting/db_info_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
###############################################################
import csv

from fluxacct.accounting import bank_subcommands as b
from fluxacct.accounting import user_subcommands as u


def export_db_info(conn, users=None, banks=None):
try:
Expand Down Expand Up @@ -46,3 +49,51 @@ def export_db_info(conn, users=None, banks=None):
writer.writerow(row)
except IOError as err:
print(err)


def populate_db(conn, users=None, banks=None):
if banks is not None:
try:
with open(banks) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")

for row in csv_reader:
b.add_bank(
conn,
bank=row[0],
parent_bank=row[1],
shares=row[2],
)
except IOError as err:
print(err)

if users is not None:
try:
with open(users) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")

# assign default values to fields if
# their slot is empty in the csv file
for row in csv_reader:
username = row[0]
uid = row[1]
bank = row[2]
shares = row[3] if row[3] != "" else 1
max_running_jobs = row[4] if row[4] != "" else 5
max_active_jobs = row[5] if row[5] != "" else 7
max_nodes = row[6] if row[6] != "" else 2147483647
queues = row[7]

u.add_user(
conn,
username,
bank,
uid,
shares,
max_running_jobs,
max_active_jobs,
max_nodes,
queues,
)
except IOError as err:
print(err)
153 changes: 0 additions & 153 deletions src/cmd/flux-account-pop-db.py

This file was deleted.

19 changes: 19 additions & 0 deletions src/cmd/flux-account-service.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ def __init__(self, flux_handle, conn):
"delete_project",
"scrub_old_jobs",
"export_db",
"pop_db",
"shutdown_service",
]

Expand Down Expand Up @@ -543,6 +544,24 @@ def export_db(self, handle, watcher, msg, arg):
msg, 0, f"a non-OSError exception was caught: {str(exc)}"
)

def pop_db(self, handle, watcher, msg, arg):
try:
val = d.populate_db(
self.conn,
msg.payload["users"],
msg.payload["banks"],
)

payload = {"pop_db": val}

handle.respond(msg, payload)
except KeyError as exc:
handle.respond_error(msg, 0, f"missing key in payload: {exc}")
except Exception as exc:
handle.respond_error(
msg, 0, f"a non-OSError exception was caught: {str(exc)}"
)


LOGGER = logging.getLogger("flux-uri")

Expand Down
42 changes: 41 additions & 1 deletion src/cmd/flux-account.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def add_export_db_arg(subparsers):

Use these two files to populate a new flux-accounting DB with:

flux account-pop-db -p path/to/db -b banks.csv -u users.csv
flux account pop-db -b banks.csv -u users.csv
""",
formatter_class=flux.util.help_formatter(),
)
Expand All @@ -569,6 +569,38 @@ def add_export_db_arg(subparsers):
)


def add_pop_db_arg(subparsers):
subparser = subparsers.add_parser(
"pop-db",
help="""
Description: Populate a flux-accounting database with a .csv file.

Order of elements required for populating association_table:

Username,UserID,Bank,Shares,MaxRunningJobs,MaxActiveJobs,MaxNodes,Queues

[Shares], [MaxRunningJobs], [MaxActiveJobs], and [MaxNodes] can be left
blank ('') in the .csv file for a given row.

----------------

Order of elements required for populating bank_table:

Bank,ParentBank,Shares

[ParentBank] can be left blank ('') in .csv file for a given row.
""",
formatter_class=flux.util.help_formatter(),
)
subparser.set_defaults(func="pop_db")
subparser.add_argument(
"-u", "--users", help="path to a .csv file containing user information"
)
subparser.add_argument(
"-b", "--banks", help="path to a .csv file containing bank information"
)


def add_arguments_to_parser(parser, subparsers):
add_path_arg(parser)
add_output_file_arg(parser)
Expand All @@ -593,6 +625,7 @@ def add_arguments_to_parser(parser, subparsers):
add_delete_project_arg(subparsers)
add_scrub_job_records_arg(subparsers)
add_export_db_arg(subparsers)
add_pop_db_arg(subparsers)


def set_db_location(args):
Expand Down Expand Up @@ -779,6 +812,13 @@ def select_accounting_function(args, output_file, parser):
"banks": args.banks,
}
return_val = flux.Flux().rpc("accounting.export_db", data).get()
elif args.func == "pop_db":
data = {
"path": args.path,
"users": args.users,
"banks": args.banks,
}
return_val = flux.Flux().rpc("accounting.pop_db", data).get()
else:
print(parser.print_usage())
return
Expand Down
6 changes: 3 additions & 3 deletions t/t1009-pop-db.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test_expect_success 'create a banks.csv file containing bank information' '
'

test_expect_success 'populate flux-accounting DB with banks.csv' '
flux account-pop-db -p ${DB_PATH} -b banks.csv
flux account pop-db -b banks.csv
'

test_expect_success 'create a users.csv file containing user information' '
Expand All @@ -45,7 +45,7 @@ test_expect_success 'create a users.csv file containing user information' '
'

test_expect_success 'populate flux-accounting DB with users.csv' '
flux account-pop-db -p ${DB_PATH} -u users.csv
flux account pop-db -u users.csv
'

test_expect_success 'check database hierarchy to make sure all banks & users were added' '
Expand All @@ -64,7 +64,7 @@ test_expect_success 'create a users.csv file with some missing optional user inf
'

test_expect_success 'populate flux-accounting DB with users_optional_vals.csv' '
flux account-pop-db -p ${DB_PATH} -u users_optional_vals.csv
flux account pop-db -u users_optional_vals.csv
'

test_expect_success 'check database hierarchy to make sure new users were added' '
Expand Down
4 changes: 2 additions & 2 deletions t/t1016-export-db.t
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ test_expect_success 'restart service against new DB' '
'

test_expect_success 'import information into new DB' '
flux account-pop-db -p ${DB_PATHv2} -b banks.csv &&
flux account-pop-db -p ${DB_PATHv2} -u users.csv
flux account pop-db -b banks.csv &&
flux account pop-db -u users.csv
'

test_expect_success 'create hierarchy output of the new DB and store it in a file' '
Expand Down
Loading
Loading