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