diff --git a/ceo/cli/database.py b/ceo/cli/database.py index 3522996..b36e3a6 100644 --- a/ceo/cli/database.py +++ b/ceo/cli/database.py @@ -5,20 +5,21 @@ from zope import component from ceo_common.interfaces import IConfig from ..utils import http_post, http_get -from .utils import handle_sync_response - - -# possible to make default [username] argument the user calling +from .utils import handle_sync_response, Abort def check_file_path(file): - if os.path.exists(file): - if os.path.isfile(file): - click.echo(f"{file} will be overwritten") - click.confirm('Do you want to continue?', abort=True) - if os.path.isdir(file): - click.echo(f"Error there exists a directory at {file}") - raise click.Abort() + if os.path.isfile(file): + click.echo(f"{file} will be overwritten") + click.confirm('Do you want to continue?', abort=True) + elif os.path.isdir(file): + click.echo(f"Error: there exists a directory at {file}") + raise Abort() + + +@click.group(short_help='Perform operations on MySQL') +def mysql(): + pass def mysql_create_info_file(file, username, password): @@ -37,9 +38,10 @@ def mysql_create_info_file(file, username, password): mysql {username} -u {username} -p - From other CSC servers you can connect using + From other CSC machines you can connect using mysql {username} -h {mysql_host} -u {username} -p + """ with click.open_file(file, "w") as f: f.write(info) @@ -47,16 +49,7 @@ def mysql_create_info_file(file, username, password): os.chmod(file, 0o640) -def psql_create_info_file(file, username, password): - pass - - -@click.group(short_help='Perform operations on MySQL') -def mysql(): - pass - - -@mysql.command(short_help='Create a MySQL database for the user') +@mysql.command(short_help='Create a MySQL database for a user') @click.argument('username') def create(username): resp = http_get(f'/api/members/{username}') @@ -71,14 +64,29 @@ def create(username): mysql_create_info_file(info_file_path, username, password) - click.echo(f"""MySQL database {username} with password {password} has been created - The password and more details have been written to {info_file_path}""") + click.echo(f""" + MySQL database {username} with password {password} has been created + This password and more details have been written to {info_file_path}""") -@mysql.command(short_help='Reset the password for MySQL user') +@mysql.command(short_help='Reset the password of a MySQL user') @click.argument('username') -def reset(username): - pass +def pwreset(username): + resp = http_get(f'/api/members/{username}') + result = handle_sync_response(resp) + info_file_path = os.path.join(result['home_directory'], "ceo-mysql-info") + + check_file_path(info_file_path) + + resp = http_post(f'/api/db/mysql/{username}/pwreset') + result = handle_sync_response(resp) + password = result['password'] + + mysql_create_info_file(info_file_path, username, password) + + click.echo(f""" + MySQL database {username} now has the password {password} + This password and more details have been written to {info_file_path}""") @click.group(short_help='Perform operations on PostgreSQL') @@ -86,13 +94,68 @@ def postgresql(): pass -@postgresql.command(short_help='Create a PostgreSQL database for the user') +def psql_create_info_file(file, username, password): + cfg_srv = component.getUtility(IConfig) + psql_host = cfg_srv.get('postgresql_host') + info = f"""PostgreSQL Database Information for {username} + + Your new PostgreSQL database was created. To connect, use the following options: + + Database: {username} + Username: {username} + Password: {password} + Host: {psql_host} + + On {psql_host} to connect using the PostgreSQL command-line client use + + psql -d {username} -U {username} -W + + From other CSC machines you can connect using + + psql -d {username} -h {psql_host} -U {username} -W + + """ + with click.open_file(file, "w") as f: + f.write(info) + os.chown(file, username, username) + os.chmod(file, 0o640) + + +@postgresql.command(short_help='Create a PostgreSQL database for a user') @click.argument('username') def create(username): - pass + resp = http_get(f'/api/members/{username}') + result = handle_sync_response(resp) + info_file_path = os.path.join(result['home_directory'], "ceo-psql-info") + + check_file_path(info_file_path) + + resp = http_post(f'/api/db/postgresql/{username}') + result = handle_sync_response(resp) + password = result['password'] + + psql_create_info_file(info_file_path, username, password) + + click.echo(f""" + PostgreSQL database {username} with password {password} has been created + This password and more details have been written to {info_file_path}""") -@postgresql.command(short_help='Reset password for PostgreSQL user') +@postgresql.command(short_help='Reset the password of a PostgreSQL user') @click.argument('username') -def reset(username): - pass +def pwreset(username): + resp = http_get(f'/api/members/{username}') + result = handle_sync_response(resp) + info_file_path = os.path.join(result['home_directory'], "ceo-psql-info") + + check_file_path(info_file_path) + + resp = http_post(f'/api/db/postgresql/{username}/pwreset') + result = handle_sync_response(resp) + password = result['password'] + + psql_create_info_file(info_file_path, username, password) + + click.echo(f""" + PostgreSQL database {username} now has the password {password} + This password and more details have been written to {info_file_path}""")