import getopt
import sys
import os
import json
import subprocess
sys.path.append(sys.path[0] + "/../")
from gspylib.common.GaussLog import GaussLog
from gspylib.common.ParameterParsecheck import Parameter
from base_utils.os.env_util import EnvUtil
libpath = os.path.join(EnvUtil.getEnv("GAUSSHOME"), "lib")
sys.path.append(libpath)
from gspylib.common.ErrorCode import ErrorCode
from base_utils.os.file_util import FileUtil
from base_utils.os.cmd_util import CmdUtil
from domain_utils.sql_handler.sql_executor import SqlExecutor
def usage():
"""
Usage:
python3 CheckCNStatus.py -h|--help
python3 CheckCNStatus.py -p port -S sql -f outputfile -s snapid -d database
General options:
-p cn port
-S SQL senned to be executed
-f, result output file
-s, snapid for special use
-d, database to execute the sql
-h, --help Show help information for this utility,
and exit the command line mode.
"""
print(usage.__doc__)
def main():
"""
main function
"""
try:
(opts, args) = getopt.getopt(sys.argv[1:], "p:S:f:s:d:h", ["help"])
except Exception as e:
usage()
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % str(e))
if (len(args) > 0):
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"]
% str(args[0]))
port = ""
sqlfile = ""
outputfile = ""
database = ""
for (key, value) in opts:
if (key == "-h" or key == "--help"):
usage()
sys.exit(0)
elif (key == "-p"):
port = value
elif (key == "-S"):
sqlfile = value
elif (key == "-f"):
outputfile = value
elif (key == "-s"):
snapid = value
elif (key == "-d"):
database = value
else:
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % key)
Parameter.checkParaVaild(key, value)
if (port == ""):
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"]
% 'p' + ".")
if (sqlfile == ""):
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"]
% 'S' + ".")
if (outputfile == ""):
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"]
% 'f' + ".")
if (database == ""):
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"]
% 'd' + ".")
try:
output = {}
exesql = ""
if os.path.exists(sqlfile):
with open(sqlfile, "r") as fp:
lines = fp.readlines()
for line in lines:
exesql += line + "\n"
(status, result, err_output) = \
SqlExecutor.excuteSqlOnLocalhost(port, exesql, database)
if (err_output != ""):
output["status"] = status
output["error_output"] = err_output
GaussLog.exitWithError(ErrorCode.GAUSS_513["GAUSS_51300"] % exesql
+ "Errors:%s" % err_output)
output["status"] = status
output["result"] = result
output["error_output"] = err_output
FileUtil.createFileInSafeMode(outputfile)
with open(outputfile, "w") as fp_json:
json.dump(output, fp_json)
cmd_list = ['rm', '-rf', sqlfile]
(output, error, status) = CmdUtil.execCmdList(cmd_list)
if status != 0:
raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % ' '.join(cmd_list)
+ "Error:\n%s" % output)
except Exception as e:
GaussLog.exitWithError("Errors:%s" % str(e))
if __name__ == '__main__':
main()