Skip to content
Snippets Groups Projects
Commit 084a0240 authored by Henrik tom Wörden's avatar Henrik tom Wörden
Browse files

MAINT: moved functionality to bash; no explicit setting of host, user etc

parent 2073ab74
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,9 @@
#
# ** end header
"""Remote module to create an SQL dump.
"""A thin wrapper around the sql backup bash script.
It provides a meaningful default directory and a nice help message
"""
import argparse
......@@ -31,16 +33,10 @@ import tempfile
def create_dump(directory=None):
"""Create the SQL dump
Parameters via `args` (a dict)
Parameters :
------------------------------
dictionary : str
If not None, the place to write the SQL dump into.
Returns
-------
out : str
A YAML text representation of the backup process.
"""
utils_path = os.path.dirname(__file__)
......@@ -52,7 +48,7 @@ out : str
directory = tempfile.mkdtemp(dir="/tmp/caosdb/tmpfiles/backup/",
prefix="backup.")
env["BACKUPDIR"] = directory
result = subprocess.run(command, cwd=os.path.dirname(__file__), env=env)
subprocess.run(command, cwd=os.path.dirname(__file__), env=env)
def parse_args():
......
......@@ -21,15 +21,17 @@
# ** end header
"""Remote module for SQL logging
Basically a wrapper with nice arguments around the bash script log.sh
"""
import argparse
import os
import subprocess
import sys
SQL_BASE = ["mysql", "--batch",
"--user=caosdb", "--password=random1234",
"--host=sqldb", "--port=3306"]
UTILS_PATH = os.path.abspath(os.path.dirname(__file__))
COMMAND = [os.path.join(UTILS_PATH, "log.sh")]
def _store_log():
......@@ -40,16 +42,14 @@ def _store_log():
out : str
The string representing the logging information, as TSV.
"""
# print("store")
sql_command = 'select event_time, argument from mysql.general_log where command_type="Query";'
result = subprocess.run(SQL_BASE, input=sql_command, encoding="utf8",
result = subprocess.run(COMMAND+["get"], encoding="utf8",
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# capture_output=True) ## Only works since 3.7
# capture_output=True) ## Only works since 3.7
if result.returncode > 0:
print("Error during SQL command, while retrieving SQL logs:", file=sys.stderr)
print(result.stderr, file=sys.stderr)
return result.stdout
print(result.stderr, file=sys.stderr)
print(result.stdout)
def _logging(turn_on):
......@@ -63,19 +63,16 @@ Parameters
turn_on : bool
Whether to turn logging on or off.
"""
on_off = "ON" if turn_on else "OFF"
sql_command = """
SET GLOBAL log_output = 'TABLE';
SET GLOBAL general_log = '{on_off}';
""".format(on_off=on_off)
result = subprocess.run(SQL_BASE, input=sql_command, encoding="utf8",
result = subprocess.run(COMMAND+["start" if turn_on else "stop"],
encoding="utf8",
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# capture_output=True) ## Only works since 3.7
# capture_output=True) ## Only works since 3.7
if result.returncode > 0:
print("Error during SQL command, while setting SQL logging to '{on_off}':".format(
on_off=on_off), file=sys.stderr)
print(result.stderr, file=sys.stderr)
print(result.stdout)
print(result.stderr, file=sys.stderr)
print(result.stdout)
# Return nothing if no error.
......@@ -89,18 +86,14 @@ def stop_logging(args):
def store(args):
print(_store_log())
_store_log()
def store_off(args):
print(_store_log())
_store_log()
_logging(False)
def example_error(args):
print(_store_log(False))
def parse_args():
"""Parse the command line arguments."""
parser = argparse.ArgumentParser(
......@@ -134,16 +127,10 @@ To view additional information about subcommands, execute:
help='Turns SQL logging on and off, stores logs.')
stop_off_parser.set_defaults(func=store_off)
example_parser = subparsers.add_parser(
'example_error',
help='Turns SQL logging on and off, stores logs.')
example_parser.set_defaults(func=example_error)
return parser.parse_args()
if __name__ == "__main__":
"""The main function."""
args = parse_args()
print(args)
args.func(args)
......@@ -24,14 +24,28 @@
#start and stop logging; get current logs
. .config
# load useful stuff - scans the parameters and so on...
. patches/utils/patch_header.sh
if [ -z "$UTILSPATH" ]; then
UTILSPATH="$(realpath $(dirname $0))"
export UTILSPATH
MAINPATH="$(dirname $UTILSPATH)"
export MAINPATH
fi
# Load settings from .config and defaults #####################################
. $UTILSPATH/load_settings.sh
# load useful functions #######################################################
. $UTILSPATH/helpers.sh
function get_logs {
echo 'select event_time, argument from mysql.general_log where command_type="Query";' | \
$MYSQL_CMD $(get_db_args_nodb) --batch
}
function set_logging {
echo "Setting logging $1..."
echo "SET GLOBAL log_output = 'TABLE'; SET GLOBAL general_log = '$1';" | \
$MYSQL_CMD $MYSQL_CONNECTION --batch
$MYSQL_CMD $(get_db_args_nodb) --batch
success
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment