import subprocess
import getopt
import sys
import os
import pwd
sys.path.append(sys.path[0] + "/../")
from gspylib.common.GaussLog import GaussLog
from gspylib.common.ParameterParsecheck import Parameter
from gspylib.common.ErrorCode import ErrorCode
from domain_utils.cluster_os.cluster_user import ClusterUser
PREINSTALL_FLAG = "1"
INSTALL_FLAG = "2"
def usage():
"""
Usage:
python3 CheckPreInstall.py -h|--help
python3 CheckPreInstall.py -U user
"""
print(usage.__doc__)
def main():
"""
function: main function:
1.parse parameter
2.check $GAUSS_ENV
input : NA
output: NA
"""
try:
(opts, args) = getopt.getopt(sys.argv[1:], "U:h:t:", ["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]))
DBUser = ""
checkInstall = "preinstall"
for (key, value) in opts:
if (key == "-h" or key == "--help"):
usage()
sys.exit(0)
elif (key == "-U"):
DBUser = value
elif (key == "-t"):
checkInstall = value
else:
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50000"] % key)
Parameter.checkParaVaild(key, value)
if DBUser == "":
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50001"] % 'U' + ".")
try:
execUser = pwd.getpwuid(os.getuid()).pw_name
if execUser != DBUser:
GaussLog.exitWithError(ErrorCode.GAUSS_500["GAUSS_50004"] % "U")
ClusterUser.checkUser(DBUser, False)
except Exception as e:
GaussLog.exitWithError(str(e))
cmd = "echo $GAUSS_ENV 2>/dev/null"
(status, output) = subprocess.getstatusoutput(cmd)
if status != 0:
GaussLog.exitWithError(
ErrorCode.GAUSS_518["GAUSS_51802"] % "GAUSS_ENV")
if checkInstall == "preinstall":
if output.strip() == PREINSTALL_FLAG or output.strip() == INSTALL_FLAG:
GaussLog.printMessage("Successfully checked GAUSS_ENV.")
sys.exit(0)
else:
GaussLog.exitWithError(
ErrorCode.GAUSS_518["GAUSS_51805"] % "GAUSS_ENV")
elif checkInstall == "install" and output.strip() == INSTALL_FLAG:
GaussLog.exitWithError(ErrorCode.GAUSS_518["GAUSS_51806"])
if __name__ == '__main__':
try:
main()
except Exception as e:
GaussLog.exitWithError(str(e))