Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
caosdb-mysqlbackend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
caosdb-mysqlbackend
Commits
084a0240
Commit
084a0240
authored
5 years ago
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
utils/backup.py
+5
-9
5 additions, 9 deletions
utils/backup.py
utils/log.py
+18
-31
18 additions, 31 deletions
utils/log.py
utils/log.sh
+18
-4
18 additions, 4 deletions
utils/log.sh
with
41 additions
and
44 deletions
utils/backup.py
+
5
−
9
View file @
084a0240
...
...
@@ -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
():
...
...
This diff is collapsed.
Click to expand it.
utils/log.py
+
18
−
31
View file @
084a0240
...
...
@@ -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
)
This diff is collapsed.
Click to expand it.
utils/log.sh
+
18
−
4
View file @
084a0240
...
...
@@ -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
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment