def getUOSModule(): if Const.isWindows(): import uWindows uOS = uWindows else: import uLinux uOS = uLinux return uOS
def tuneJBoss(config): uLogging.debug("tuneJBoss started, scale_down: %s" % config.scale_down) if not Const.isWindows() and config.scale_down: from u import bootstrap jbossdir = bootstrap.getJBossDir(config.rootpath) uLogging.info("Tuning JBoss connection pool") bootstrap.execCLI( jbossdir, 'embed-server --server-config=%s,' % bootstrap.serverConfig + '/subsystem=datasources/data-source=pauds:write-attribute(name="max-pool-size", value="80")') # jboss restart required, performed after PUI deployment else: uLogging.debug("nothing done")
def update_install_win_sn(sn_installer_path, main_ppm_mirror_path): # copy PAgent.exe to PPM mirror for later use by SharedNodeRegistrator registrar_path = os.path.join(main_ppm_mirror_path, 'Win32Bin') if not os.path.exists(registrar_path): os.makedirs(registrar_path) os.chmod(registrar_path, 0775) if not Const.isWindows(): import grp grpinfo = grp.getgrnam('pemgroup') os.chown(registrar_path, 0, grpinfo.gr_gid) if sn_installer_path: shutil.copy(sn_installer_path, registrar_path)
def stopMN(minimal=False): if Const.isWindows(): # ignore 2 error code, because in 5.4 pem cannot be stopped properly uUtil.readCmd(['net', 'stop', 'pem'], valid_codes=[0, 2]) uUtil.readCmd(['net', 'stop', 'PAU'], valid_codes=[0, 1, 2]) else: if not minimal: # pem script actually returns 1 and 123 on valid stops uUtil.execCommand('service pa-agent stop', [0, 1, 123]) uUtil.execCommand('service pau stop', [0, 1, 5]) # In some mysterious cases "service pa-agent stop" doesn't work uUtil.readCmdExt(['killall', '-9', 'pa-agent']) uUtil.readCmdExt(['killall', '-9', 'SoLoader']) resetPIDs()
def getPemDirectory(): if not Const.isWindows(): return "/usr/local/pem" def getPOAHostRegistryKey(hostname): baseRegPath = None for regPath in ["SOFTWARE\\SWsoft\\PEM", "SOFTWARE\\Wow6432Node\\SWsoft\\PEM"]: try: regkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regPath, 0, _winreg.KEY_READ) baseRegPath = regPath break except WindowsError: pass if baseRegPath is None: raise Exception('System does not have valid Operation Automation installation.') foundHosts = [] i = 0 try: while True: foundHosts += [_winreg.EnumKey(regkey, i)] i += 1 except WindowsError: pass _winreg.CloseKey(regkey) if not len(foundHosts): raise Exception('System does not have valid Operation Automation installation.') if hostname not in foundHosts: raise Exception("Detected: system hostname was changed.\nActual hostname is %s.\nExpected hostnames: %s." % ( hostname, ', '.join(foundHosts))) return _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, '%s\\%s' % (baseRegPath, hostname), 0, _winreg.KEY_READ) regkey = getPOAHostRegistryKey(socket.getfqdn()) try: pempath = _winreg.QueryValueEx(regkey, "conf_files_path")[0] # It returns something like this: # "C:\Program Files\SWsoft\PEM\etc" pempath = os.path.split(pempath)[0] _winreg.CloseKey(regkey) return pempath except: _winreg.CloseKey(regkey) raise Exception("Failed to find PEM installation directory.")
def execCommand(command, valid_codes=None, retries=0, command_to_log=None): if not command_to_log: command_to_log = command uLogging.debug("Executing command: '%s'", command_to_log) if valid_codes is None: valid_exit_codes = [0] else: valid_exit_codes = valid_codes stdout = None stderr = sp.STDOUT if uLogging.logfile: stdout = uLogging.logfile stderr = stdout use_shell = type(command) not in (tuple, list) while True: if Const.isWindows(): cmd = sp.Popen(command, stdout=stdout, stderr=stderr, shell=use_shell, startupinfo=startup_info) else: env = os.environ.copy() env["LANG"] = "C" env["LC_CTYPE"] = "C" env["LD_LIBRARY_PATH"] = env.get("LD_LIBRARY_PATH", "") + ":/usr/pgsql-9.5/lib/" cmd = sp.Popen(command, stdout=stdout, stderr=stderr, close_fds=True, shell=use_shell, env=env) status = cmd.wait() if status and status not in valid_exit_codes: uLogging.debug( "Executing command '%s' failed with status '%s', '%s' retries left" % (command_to_log, status, retries)) if retries > 0: retries -= 1 continue raise ExecFailed(command_to_log, status) return status
def tuneDatabase(config): uLogging.debug("tuneDatabase started, scale_down: %s" % config.scale_down) if not Const.isWindows() and config.scale_down: uLogging.debug("tuning PgSQL") p = uPgSQL.PostgreSQLConfig() pg_conf = p.get_postgresql_conf() env = os.environ.copy() # 2147483648 bytes = 2 GB # 2 GB limit is set because 'shared_buffers' should be equal to 512 mb uUtil.readCmdExt(["odin-pg-tune", "-f", "--input-config=" + pg_conf, "--min-connections=128", "--output-config=" + pg_conf, "--memory=2147483648"], env = env) uLogging.debug("restarting PgSQL...") p.restart() else: uLogging.debug("nothing done")
def update_binary(pkg, rootpath): def do_unpack(pkg, rootpath): uLogging.info("Updating %s (%s)", pkg.package, pkg.tarball_location) arc = tarfile.open(pkg.tarball_location, 'r') for tarinfo in arc: arc.extract(tarinfo, path=rootpath) arc.close() if pkg.tarball_location is not None: try: do_unpack(pkg, rootpath) except IOError, e: if Const.isWindows() and e.errno == errno.EACCES: uLogging.err( "Cannot unpack, file is probably locked. retrying") time.sleep(1) do_unpack(pkg, rootpath) else: raise
def doMassImport(rootpath, pkglist, pkg_counter): command = [ os.path.join(rootpath, "bin", "ppm_ctl"), '-q', '-b', '-E', '-f', os.path.join(rootpath, "etc", "pleskd.props"), 'add', 'pkgmass' ] pkglist = __filterCustomPackages(pkglist) if not pkglist: return command += [os.path.join(x.topdir, x.manifest_file) for x in pkglist] uLogging.debug("doMassImport: %s", command) if Const.isWindows(): p = sp.Popen(command, bufsize=1, stdout=sp.PIPE, stderr=sp.PIPE, startupinfo=uUtil.startup_info) else: p = sp.Popen(command, bufsize=1, stdout=sp.PIPE, stderr=sp.PIPE) out_thread = threading.Thread(target=__assignPkgIds, args=(p.stdout, pkglist)) out_thread.setDaemon(True) errlines = [] err_thread = threading.Thread(target=__showProgress, args=(p.stderr, pkg_counter, errlines)) err_thread.setDaemon(True) out_thread.start() err_thread.start() out_thread.join() err_thread.join() status = p.wait() if status > 0: raise Exception('ppm ctl exited with code %d: %s' % (status, "\n".join(errlines))) elif status < 0: raise Exception('ppm ctl terminated with signal %d' % -status)
def perform_aps2_db_upgrade(binfo): from poaupdater import uAction scripts_dirs = binfo.upgrade_instructions.aps if not scripts_dirs: uLogging.info( "There is no APS Database upgrade scripts. Skip upgrading.") return uAction.progress.do("updating APS database") con = uSysDB.connect() for scripts_dir in scripts_dirs: upgrade_aps_db(scripts_dir, con) if Const.isWindows(): uAction.progress.do("dropping APS database page locking") performMSSQLUpgrade(con) uAction.progress.done() uAction.progress.done() uSysDB.close(con)
def readCmdExt(command, input_data=None, env=None): uLogging.debug("Executing command: '%s'", command) use_shell = type(command) not in (tuple, list) if Const.isWindows(): cmd = sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=use_shell, startupinfo=startup_info) else: cmd = sp.Popen(command, close_fds=True, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=use_shell, env=env) out, err = cmd.communicate(input=input_data) status = cmd.returncode return str(out), str(err), status
def startMN(minimal=False): resetPIDs() platform, root = getMNInfo() if Const.isWindows(): stopMN() # To ensure waitForJBossStarted will do correct from u import bootstrap os.remove(bootstrap.getJBossDir(root) + '\\standalone\\log\\standalone.log') uUtil.readCmd(['net', 'start', 'PAU']) # Service is stated as "started" before JBoss is actually started, need to wait waitForJBossStarted(root) uUtil.readCmd(['net', 'start', 'pem'], valid_codes=[0]) else: if minimal: env = dict() env.update(os.environ) pleskd_env = dict( LD_LIBRARY_PATH=str(os.path.join(root, "lib") + ":" + "/usr/pgsql-9.5/lib/"), SVC_CONF=str(os.path.join(root, "etc", "svc.conf")), PLESKD_PROPS=str(os.path.join(root, "etc", "pleskd.props")) ) env.update(pleskd_env) progname = os.path.join(root, "sbin", "pa-agent") cmd = [] cmd.append(progname) cmd.append("--props-file=" + os.path.join(root, "etc", "pleskd.props")) # LD_LIBRARY_PATH="/usr/local/pem/lib:/usr/pgsql-9.4/lib/" # PATH="${PATH}:/usr/local/pem/bin" /usr/local/pem/sbin/pleskd # --props-file /usr/local/pem/etc/pleskd.props --send-signal sp.Popen(cmd, env=env) else: uUtil.execCommand('service pau start', valid_codes=[0, 1]) uUtil.execCommand('service pa-agent start')
def init(config=None): global _config global _inited if config is not None: _config = config if _config is None: raise Exception, "config arg is mandatory when you're initializing uSysDB first time" if not hasattr(config, "database_type"): if Const.isWindows(): _config.database_type = 'MSSQL' else: _config.database_type = 'PGSQL' else: _config.database_type = config.database_type if not _inited: setup_connection() global DBType uLogging.debug('Using database_name: %s(%s)' % (_config.database_name, DBType)) import uDBSchema uDBSchema.init(DBType)
def _get_openssl_binary(): if Const.isWindows(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), "openssl.exe") else: return "openssl"
def findDBConfig(rootpath): default_rv = { 'database_host': socket.gethostname(), 'database_name': 'oss', 'database_port': '5432', 'dsn': 'oss', 'dsn_login': '******', 'dsn_passwd': '', 'database_type': '', 'kernel_priv_key': '' } etc_dir = os.path.join(rootpath, "etc") kernel_conf_path = os.path.join(etc_dir, "Kernel.conf") if Const.isWindows(): default_rv['database_type'] = 'MSSQL' if os.path.isfile(kernel_conf_path): kernel_conf = file(kernel_conf_path) pp = uUtil.readPropertiesFile(kernel_conf) kernel_conf.close() uLogging.debug('read kernel config from %s' % kernel_conf_path) default_rv['kernel_priv_key'] = pp['kernel_priv_key'] default_rv['dsn_passwd'] = pp['dsn_passwd'] default_rv['dsn_login'] = pp['dsn_login'] else: uLogging.debug( 'no db config, taking defaults. valid for fresh install only!') return default_rv else: odbc_ini_path = os.path.join(etc_dir, "odbc.ini") if not os.path.isfile(kernel_conf_path) or not os.path.isfile( odbc_ini_path): uLogging.debug( 'no db config, taking defaults. valid for fresh install only!') default_rv['database_type'] = 'PGSQL' return default_rv kernel_conf = file(kernel_conf_path) pp = uUtil.readPropertiesFile(kernel_conf) kernel_conf.close() uLogging.debug('read kernel config from %s' % kernel_conf_path) odbc = ConfigParser.ConfigParser() odbc.read(odbc_ini_path) uLogging.debug('read odbc config from %s' % odbc_ini_path) dsnname = pp["dsn"] rv = { 'database_host': odbc.get(dsnname, "Servername"), 'database_port': odbc.get(dsnname, "Port"), 'database_name': odbc.get(dsnname, "Database"), 'dsn': dsnname, 'dsn_login': pp["dsn_login"], 'dsn_passwd': pp['dsn_passwd'], 'database_type': 'PGSQL', 'kernel_priv_key': pp['kernel_priv_key'] } # we are here because ODBC DSN configured and actually the type must be rv['type'] = 'ODBC' # but uSysDB doesn't know how to deal with 'ConcatOperator' and 'nowfun' in this case return rv
import uLogging import uSysDB import uUtil import uHCL import uAction import uBuild import uPrecheck import PEMVersion import uTextRender import uURLChecker import uCrypt import uThreadPool from uConst import Const import uTasks if Const.isWindows(): import _winreg # needed on Windows import subprocess as sp def getPemDirectory(): if not Const.isWindows(): return "/usr/local/pem" def getPOAHostRegistryKey(hostname): baseRegPath = None for regPath in ["SOFTWARE\\SWsoft\\PEM", "SOFTWARE\\Wow6432Node\\SWsoft\\PEM"]: try: regkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, regPath, 0, _winreg.KEY_READ) baseRegPath = regPath
__author__ = 'imartynov' import sys import os from uConst import Const from poaupdater import uUtil, uLogging if not Const.isWindows(): from poaupdater import uPgSQL def tuneDatabase(config): uLogging.debug("tuneDatabase started, scale_down: %s" % config.scale_down) if not Const.isWindows() and config.scale_down: uLogging.debug("tuning PgSQL") p = uPgSQL.PostgreSQLConfig() pg_conf = p.get_postgresql_conf() env = os.environ.copy() # 2147483648 bytes = 2 GB # 2 GB limit is set because 'shared_buffers' should be equal to 512 mb uUtil.readCmdExt(["odin-pg-tune", "-f", "--input-config=" + pg_conf, "--min-connections=128", "--output-config=" + pg_conf, "--memory=2147483648"], env = env) uLogging.debug("restarting PgSQL...") p.restart() else: uLogging.debug("nothing done") def tuneJBoss(config): uLogging.debug("tuneJBoss started, scale_down: %s" % config.scale_down)
def findHome(): if Const.isWindows(): return os.environ.get('HOMEDRIVE', 'C:') + os.environ.get( 'HOMEPATH', '\\') else: return os.environ.get('HOME', '/root')