Updated the knob/metric output samples for each of the Oracle versions we support since my fix changed all of them.

This commit is contained in:
dvanaken
2019-11-05 19:39:34 -05:00
committed by Dana Van Aken
parent e1b29eaafa
commit cf19380107
19 changed files with 44991 additions and 46524 deletions

View File

@@ -23,58 +23,23 @@ import requests
from fabric.api import env, lcd, local, settings, show, task
from fabric.state import output as fabric_output
from utils import file_exists, get, load_driver_conf, parse_bool, put, run, sudo
from utils import (file_exists, get, load_driver_conf, parse_bool,
put, run, run_sql_script, sudo, FabricException)
# Loads the driver config file (defaults to driver_config.py)
dconf = load_driver_conf()
# Fabric settings
fabric_output.update({
'running': True,
'stdout': True,
})
env.abort_exception = FabricException
env.hosts = [dconf.LOGIN]
# Loads the driver config file (default: driver_config.py)
dconf = load_driver_conf()
def _setup():
global LOGIN
# Determine correct login for the type of host connection
if dconf.HOST_CONN == 'local':
LOGIN = 'localhost'
elif dconf.HOST_CONN == 'remote':
if not dconf.LOGIN_HOST:
raise ValueError("LOGIN_HOST must be set if HOST_CONN=remote")
LOGIN = dconf.LOGIN_HOST
if dconf.LOGIN_NAME:
LOGIN = '{}@{}'.format(dconf.LOGIN_NAME, LOGIN)
if dconf.LOGIN_PORT:
LOGIN += ':{}'.format(dconf.LOGIN_PORT)
elif dconf.HOST_CONN == 'docker':
if not dconf.CONTAINER_NAME:
raise ValueError("CONTAINER_NAME must be set if HOST_CONN=docker")
LOGIN = 'localhost'
else:
raise ValueError(("Invalid HOST_CONN: {}. Valid values are "
"'local', 'remote', or 'docker'.").format(dconf.HOST_CONN))
# Update Fabric's host list
env.hosts = [LOGIN]
# Create local directories
for d in (dconf.RESULT_DIR, dconf.LOG_DIR, dconf.TEMP_DIR):
os.makedirs(d, exist_ok=True)
# Copy Oracle scripts
if dconf.DB_TYPE == 'oracle':
put('./oracleScripts', '/home/oracle')
sudo('chown -R oracle:oinstall /home/oracle/oracleScripts')
_setup()
# Create local directories
for _d in (dconf.RESULT_DIR, dconf.LOG_DIR, dconf.TEMP_DIR):
os.makedirs(_d, exist_ok=True)
# Configure logging
@@ -145,7 +110,7 @@ def restart_database():
else:
sudo('pg_ctl -D {} -w -t 600 restart -m fast'.format(dconf.PG_DATADIR), user=dconf.DB_USER)
elif dconf.DB_TYPE == 'oracle':
run('sh oracleScripts/shutdownOracle.sh && sh oracleScripts/startupOracle.sh')
run_sql_script('restartOracle.sh')
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
@@ -175,7 +140,7 @@ def create_user():
run("PGPASSWORD={} psql -c \\\"{}\\\" -U postgres -h {}".format(
dconf.DB_PASSWORD, sql, dconf.DB_USER, dconf.DB_HOST))
elif dconf.DB_TYPE == 'oracle':
run('sh oracleScripts/createUser.sh {} {}'.format(dconf.DB_USER, dconf.DB_PASSWORD))
run_sql_script('createUser.sh', dconf.DB_USER, dconf.DB_PASSWORD)
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
@@ -187,7 +152,7 @@ def drop_user():
run("PGPASSWORD={} psql -c \\\"{}\\\" -U postgres -h {}".format(
dconf.DB_PASSWORD, sql, dconf.DB_HOST))
elif dconf.DB_TYPE == 'oracle':
run('sh oracleScripts/dropUser.sh {}'.format(dconf.DB_USER))
run_sql_script('dropUser.sh', dconf.DB_USER)
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
@@ -456,16 +421,15 @@ def dump_database():
LOG.info('Dump database %s to %s', dconf.DB_NAME, dumpfile)
if dconf.DB_TYPE == 'oracle':
cmd = 'sh oracleScripts/dumpOracle.sh {} {} {} {}'.format(
dconf.DB_USER, dconf.DB_PASSWORD, dconf.DB_NAME, dconf.DB_DUMP_DIR)
run_sql_script('dumpOracle.sh', dconf.DB_USER, dconf.DB_PASSWORD,
dconf.DB_NAME, dconf.DB_DUMP_DIR)
elif dconf.DB_TYPE == 'postgres':
cmd = 'PGPASSWORD={} pg_dump -U {} -h {} -F c -d {} > {}'.format(
run('PGPASSWORD={} pg_dump -U {} -h {} -F c -d {} > {}'.format(
dconf.DB_PASSWORD, dconf.DB_USER, dconf.DB_HOST, dconf.DB_NAME,
dumpfile)
dumpfile))
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
run(cmd)
return True
@@ -475,23 +439,20 @@ def restore_database():
if not file_exists(dumpfile):
raise FileNotFoundError("Database dumpfile '{}' does not exist!".format(dumpfile))
LOG.info('Start restoring database')
if dconf.DB_TYPE == 'oracle':
# You must create a directory named dpdata through sqlplus in your Oracle database
# The following script assumes such directory exists.
drop_user()
create_user()
cmd = 'sh oracleScripts/restoreOracle.sh {} {}'.format(dconf.DB_USER, dconf.DB_NAME)
run_sql_script('restoreOracle.sh', dconf.DB_USER, dconf.DB_NAME)
elif dconf.DB_TYPE == 'postgres':
drop_database()
create_database()
cmd = 'PGPASSWORD={} pg_restore -U {} -h {} -n public -j 8 -F c -d {} {}'.format(
dconf.DB_PASSWORD, dconf.DB_USER, dconf.DB_HOST, dconf.DB_NAME, dumpfile)
run('PGPASSWORD={} pg_restore -U {} -h {} -n public -j 8 -F c -d {} {}'.format(
dconf.DB_PASSWORD, dconf.DB_USER, dconf.DB_HOST, dconf.DB_NAME, dumpfile))
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
LOG.info('Start restoring database')
run(cmd)
LOG.info('Finish restoring database')

View File

@@ -0,0 +1,8 @@
#!/bin/sh
sqlplus / as sysdba <<EOF
shutdown immediate
startup
quit
EOF

View File

@@ -8,14 +8,43 @@ dconf = None
def load_driver_conf():
driver_conf = os.environ.get('DRIVER_CONFIG', 'driver_config')
if driver_conf.endswith('.py'):
driver_conf = driver_conf[:-len('.py')]
dmod = importlib.import_module(driver_conf)
# The default config file is 'driver_config.py' but you can use
# set the env 'DRIVER_CONFIG' to the path of a different config
# file to override it.
global dconf
if not dconf:
dconf = dmod
return dmod
driver_conf = os.environ.get('DRIVER_CONFIG', 'driver_config')
if driver_conf.endswith('.py'):
driver_conf = driver_conf[:-len('.py')]
mod = importlib.import_module(driver_conf)
dconf = mod
# Generate the login string of the host connection
if dconf.HOST_CONN == 'local':
login_str = 'localhost'
elif dconf.HOST_CONN == 'remote':
if not dconf.LOGIN_HOST:
raise ValueError("LOGIN_HOST must be set if HOST_CONN=remote")
login_str = dconf.LOGIN_HOST
if dconf.LOGIN_NAME:
login_str = '{}@{}'.format(dconf.LOGIN_NAME, login_str)
if dconf.LOGIN_PORT:
login_str += ':{}'.format(dconf.LOGIN_PORT)
elif dconf.HOST_CONN == 'docker':
if not dconf.CONTAINER_NAME:
raise ValueError("CONTAINER_NAME must be set if HOST_CONN=docker")
login_str = 'localhost'
else:
raise ValueError(("Invalid HOST_CONN: {}. Valid values are "
"'local', 'remote', or 'docker'.").format(dconf.HOST_CONN))
dconf.LOGIN = login_str
return dconf
def parse_bool(value):
@@ -103,8 +132,35 @@ def put(local_path, remote_path, use_sudo=False):
return res
@task
def run_sql_script(scriptfile, *args):
if dconf.DB_TYPE == 'oracle':
if dconf.HOST_CONN != 'local':
scriptdir = '/home/oracle/oracleScripts'
remote_path = os.path.join(scriptdir, scriptfile)
if not file_exists(remote_path):
run('mkdir -p {}'.format(scriptdir))
put(os.path.join('./oracleScripts', scriptfile), remote_path)
sudo('chown -R oracle:oinstall /home/oracle/oracleScripts')
res = run('sh {} {}'.format(remote_path, ' '.join(args)))
else:
raise Exception("Database Type {} Not Implemented !".format(dconf.DB_TYPE))
return res
@task
def file_exists(filename):
with settings(warn_only=True), hide('warnings'):
res = run('[ -f {} ]'.format(filename))
return res.return_code == 0
@task
def dir_exists(dirname):
with settings(warn_only=True), hide('warnings'):
res = run('[ -d {} ]'.format(dirname))
return res.return_code == 0
class FabricException(Exception):
pass