database cli
This commit is contained in:
parent
eb5d632606
commit
ba50a39700
|
@ -0,0 +1,98 @@
|
||||||
|
import click
|
||||||
|
import os
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
|
def mysql_create_info_file(file, username, password):
|
||||||
|
cfg_srv = component.getUtility(IConfig)
|
||||||
|
mysql_host = cfg_srv.get('mysql_host')
|
||||||
|
info = f"""MySQL Database Information for {username}
|
||||||
|
|
||||||
|
Your new MySQL database was created. To connect, use the following options:
|
||||||
|
|
||||||
|
Database: {username}
|
||||||
|
Username: {username}
|
||||||
|
Password: {password}
|
||||||
|
Host: {mysql_host}
|
||||||
|
|
||||||
|
On {mysql_host} to connect using the MySQL command-line client use
|
||||||
|
|
||||||
|
mysql {username} -u {username} -p
|
||||||
|
|
||||||
|
From other CSC servers you can connect using
|
||||||
|
|
||||||
|
mysql {username} -h {mysql_host} -u {username} -p
|
||||||
|
"""
|
||||||
|
with click.open_file(file, "w") as f:
|
||||||
|
f.write(info)
|
||||||
|
os.chown(file, username, username)
|
||||||
|
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')
|
||||||
|
@click.argument('username')
|
||||||
|
def create(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}')
|
||||||
|
result = handle_sync_response(resp)
|
||||||
|
password = result['password']
|
||||||
|
|
||||||
|
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}""")
|
||||||
|
|
||||||
|
|
||||||
|
@mysql.command(short_help='Reset the password for MySQL user')
|
||||||
|
@click.argument('username')
|
||||||
|
def reset(username):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@click.group(short_help='Perform operations on PostgreSQL')
|
||||||
|
def postgresql():
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@postgresql.command(short_help='Create a PostgreSQL database for the user')
|
||||||
|
@click.argument('username')
|
||||||
|
def create(username):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@postgresql.command(short_help='Reset password for PostgreSQL user')
|
||||||
|
@click.argument('username')
|
||||||
|
def reset(username):
|
||||||
|
pass
|
|
@ -9,6 +9,7 @@ from ..krb_check import krb_check
|
||||||
from .members import members
|
from .members import members
|
||||||
from .groups import groups
|
from .groups import groups
|
||||||
from .updateprograms import updateprograms
|
from .updateprograms import updateprograms
|
||||||
|
from .database import mysql, postgresql
|
||||||
from ceo_common.interfaces import IConfig, IHTTPClient
|
from ceo_common.interfaces import IConfig, IHTTPClient
|
||||||
from ceo_common.model import Config, HTTPClient
|
from ceo_common.model import Config, HTTPClient
|
||||||
|
|
||||||
|
@ -30,6 +31,8 @@ def cli(ctx):
|
||||||
cli.add_command(members)
|
cli.add_command(members)
|
||||||
cli.add_command(groups)
|
cli.add_command(groups)
|
||||||
cli.add_command(updateprograms)
|
cli.add_command(updateprograms)
|
||||||
|
cli.add_command(mysql)
|
||||||
|
cli.add_command(postgresql)
|
||||||
|
|
||||||
|
|
||||||
def register_services():
|
def register_services():
|
||||||
|
|
Loading…
Reference in New Issue