Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_ipaddr(host):
(ok, out) = run('%s "%s %s | %s \\"inet\\"| %s -v \\"127.0.0\\"| %s -v \\"inet6\\""' %
(gplib.ssh_prefix(host=host), ENV.IFCONFIG,
ENV.IFCONFIG_TXT, ENV.GREP, ENV.GREP, ENV.GREP))
if not ok:
return None
addr = []
for l in out:
x = l.strip().split()
ip = x[1].split(':')
addr.append(len(ip) == 2 and ip[1] or ip[0])
return addr
def file_exists(file, host=None):
if not host or host == 'localhost':
return os.path.isfile(file)
else:
(ok, out) = run('%s test -f %s && test -r %s' % (gplib.ssh_prefix(host=host), file, file))
return ok
def append_pg_hba(host, datadir, new_addr):
pg_hba = os.path.join(datadir, 'pg_hba.conf')
for addr in new_addr:
(ok, out) = run_warn('%s "echo host all all %s/32 trust >> %s"' %
(gplib.ssh_prefix(host=host),
addr, pg_hba))
if not ok:
fatal('update %s:%s failed' % (host, pg_hba))
return True
def postgres_active(port, host=None):
ret = 0
pg_lock_file = '/tmp/.s.PGSQL.%d.lock' % port
pg_lock_netstat = 0
pid = 0
# ping host if present
if host:
ok = ping_host(host)
if not ok:
return (ok, pid)
# netstat to check the port number
(ok, out) = run('%s %s -an 2> /dev/null | %s ".s.PGSQL.%d" | %s \'{print $NF}\'| %s -F"." \'{print $NF}\' | %s -u' %
(host and gplib.ssh_prefix(host=host) or '', ENV.NETSTAT, ENV.GREP, port, ENV.AWK, ENV.AWK, ENV.SORT))
if not ok:
return (ok, pid)
for p_chk in out:
p = int(p_chk)
if p == port:
pg_lock_netstat = port
pg_lock_tmp = False
if file_exists(file=pg_lock_file, host=host):
pg_lock_tmp = True
if pg_lock_netstat == 0 and pg_lock_tmp == False:
ret = 1
pid = 0
log_info('No socket connection or lock file in /tmp found for %s port=%d' % (host and host or '', port))
else:
def edit_file(host, file, search_txt, sub_txt, append=False):
if host != 'localhost':
gphome = os.environ.get('GPHOME')
cmd = makeCommand('''python -c \\"import sys; sys.path.extend(['%s', '%s']); import gpmlib; gpmlib.edit_file('localhost', '%s', '%s', '%s', %s)\\"''' %
(os.path.join(gphome, 'bin', 'lib'),
os.path.join(gphome, 'lib', 'python'),
file,
search_txt,
sub_txt,
str(append)))
(ok, out) = run2('''%s "%s"''' % (gplib.ssh_prefix(host=host), cmd), True)
return ok
else:
f = None
tmpf = None
tmpfile = '%s.tmp' % file
try:
try:
f = open(file, 'r')
tmpf = open(tmpfile, 'w')
for line in f:
if line.find(search_txt) != -1:
tmpf.write(sub_txt)
if append:
tmpf.write(' # ')
tmpf.write(line)
def chk_multi_home(hostlist, full=True):
hosts = full and hostlist or hostlist[0:2]
hostnames=[]
for host in hosts:
(ok, out) = run('%s %s' % (gplib.ssh_prefix(host=host), ENV.HOSTNAME))
if not ok:
error('failed to run hostname on remote host')
hostnames.append(out[0].strip())
hostnames_uniq = unique(hostnames)
return len(hostnames_uniq) != len(hostnames)
(os.path.join(ENV.GPHOME, 'bin/pg_ctl'),
dst_cfg['datadir'], dst_cfg['datadir'], dst_cfg['port']))
(ok, out) = run_warn('%s "%s"' %
(gplib.ssh_prefix(host=dst_cfg['hostname']),
cmd))
if not ok:
fatal('failed to start mirror segment %s:%s in admin mode' % (dst_cfg['hostname'], dst_cfg['datadir']))
if force_stop:
# stop mirror segment
log_info('stop segment datadir=%s port=%d on %s' % (dst_cfg['datadir'], dst_cfg['port'], dst_cfg['hostname']))
cmd = makeCommand('env PGOPTIONS=\\"-c gp_session_role=utility\\" %s -w stop -D %s -m smart -l %s.log' %
(os.path.join(ENV.GPHOME, 'bin/pg_ctl'),
dst_cfg['datadir'], dst_cfg['datadir']))
(ok, out) = run_warn('%s "%s" >> %s 2>&1' %
(gplib.ssh_prefix(host=dst_cfg['hostname']),
cmd,
logfile))
if not ok:
fatal('failed to stop mirror segment %s:%s' % (dst_cfg['hostname'], dst_cfg['datadir']))
log_info('sync segment datadir=%s port=%d on host %s succeeded' % (dst_cfg['datadir'], dst_cfg['port'], dst_cfg['hostname']))
return True
if not host or host == 'localhost':
try:
try:
f = open(file, 'w')
f.close()
except IOError, e:
fatal('write file %s error' % file)
finally:
f.close()
os.remove(file)
else:
gphome = os.environ.get('GPHOME')
cmd = makeCommand('''python -c \\"import sys, os; sys.path.extend(['%s', '%s']); import gpmlib; gpmlib.directory_writable('%s')\\"''' %
(os.path.join(gphome, 'bin', 'lib'), os.path.join(gphome, 'lib', 'python'), dir))
(ok, out) = run('''%s "%s"''' % (gplib.ssh_prefix(host=host), cmd))
if not ok:
fatal('write file %s error' % file)
return True