import socket
import os
import sys
import subprocess
import signal
import re
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
#Exit Code
EXIT_SUCCESS = 0
EXIT_FAILURE = -1
EXIT_STATUS_IDEL = 1
EXIT_STATUS_RUNNING = 2
class gds_entry:
port = -1
name = None
log_file = None
data_dir = None
err_dir = None
data_seg = None
err_seg = None
ctl_file = None
recursive = False
daemon = False
host = None
ip = None
parallel = -1
help = False
def is_valid(self):
if self.help == True:
return True
if self.port < 1024 or self.port > 65535:
return False
if self.data_dir == None:
return False
if self.name == None:
return False
if re.search('^\w+([-+.]\w+)*$', self.name) == None:
return False
return True
def to_str(self):
str = ('name:{0}'.format(self.name))
if self.ip != None:
str += (' ip:{0}'.format(self.ip))
str += (' port:{0:d}'.format(self.port))
str += (' data_dir:{0}'.format(self.dta_dir))
if self.err_dir != None:
str += (' err_dir:{0}'.format(self.drr_dir))
if self.data_seg != None:
str += (' data_seg:{0}'.format(self.data_seg))
if self.err_seg != None:
str += (' err_seg:{0}'.format(self.err_seg))
if self.log_file != None:
str += (' log_file:{0}'.format(self.log_file))
if self.host != None:
str += (' host:{0}'.format(self.host))
if self.recursive == True:
str += ' recursive:{true}'
else:
str += ' recursive:{false}'
if self.daemon == True:
str += ' daemon:{true}'
else:
str += ' daemon:{false}'
if self.enablessl == True:
str += ' enablessl:{true}'
else:
str += ' enablessl:{false}'
if self.parallel != None:
str += (' parallel:{0}'.format(self.parallel))
if self.help == True:
str += ' help:{true}'
else:
str += ' help:{false}'
return str
def usage():
print "gds_ctl.py [start|stop|status]"
#Read the GDS configration
def read_conf(filename):
try:
tree = ET.ElementTree(file=filename)
root = tree.getroot()
gds_array = []
gds_dict = {}
except:
print 'Invalid gds configuration file \"' + filename + '\"'
sys.exit(EXIT_FAILURE)
for cell in root:
entry = gds_entry()
entry.port = int(cell.get('port', '-1'))
entry.data_dir = cell.get('data_dir', None)
entry.err_dir = cell.get('err_dir', None)
entry.data_seg = cell.get('data_seg', None)
entry.err_seg = cell.get('err_seg', None)
entry.recursive = (cell.get('recursive', 'False').lower() == 'true')
entry.daemon = (cell.get('daemon', 'False').lower() == 'true')
entry.help = (cell.get('help', 'False').lower() == 'true')
#make the data_dir as a ABS path
if entry.data_dir != None:
entry.data_dir = entry.data_dir.strip()
if os.path.isabs(entry.data_dir) == False:
entry.data_dir = sys.path[0] + '/' +entry.data_dir
#make the err_dir as a ABS path
if entry.err_dir != None:
entry.err_dir = entry.err_dir.strip()
if os.path.isabs(entry.err_dir) == False:
entry.err_dir = sys.path[0] + '/' +entry.err_dir
entry.name = cell.get('name', None)
entry.ctl_file = '.gds.' + entry.name
entry.log_file = cell.get('log_file', None)
entry.host = cell.get('host', None)
entry.ip = cell.get('ip', None)
entry.parallel = cell.get('parallel', None)
if entry.is_valid() != True:
print 'Invalid item in configuration file \"' + filename + '\"'
sys.exit(EXIT_FAILURE);
gds_array.append(entry)
if gds_dict.has_key(entry.name):
print ('Conflict GDS name \"{0}\"'.format(entry.name))
sys.exit(EXIT_FAILURE)
gds_dict[entry.name] = entry
gds_dict.clear()
return gds_array
#Start GDS
def gds_start(entries):
for cell in entries:
if cell.help == True:
cmd = g_bin_path + '/gds -h'
if subprocess.call(cmd, shell = True) !=0:
return EXIT_FAILURE
else:
return EXIT_SUCCESS
if __is_GDS_exist__(cell):
print (('GDS {0} already exists.').format(cell.name))
return EXIT_FAILURE
#Create the data directory if dosen't exist,
if os.path.exists(cell.data_dir) == False:
os.makedirs(cell.data_dir)
#Create the error log directory if dosen't exist,
if os.path.exists(cell.err_dir) == False:
os.makedirs(cell.err_dir)
if os.path.exists(cell.ctl_file):
subprocess.call(('rm {0}'.format(cell.ctl_file)), shell = True)
cmd = g_bin_path + ('/gds -d {0} -s {1} -D'.format(cell.data_dir, cell.ctl_file))
if cell.ip != None:
cmd += (' -p {0}:{1:d}'.format(cell.ip, cell.port))
else:
cmd += (' -p {0:d}'.format(cell.port))
if cell.log_file != None:
cmd += (' -l {0}'.format(cell.log_file))
if cell.recursive == True:
cmd += ' -r'
if cell.daemon == True:
cmd += ' -D'
if cell.host != None:
cmd += (' -H {0}'.format(cell.host))
if cell.parallel != -1:
cmd += (' -t {0}'.format(cell.parallel))
if cell.err_dir != None:
cmd += (' -e {0}'.format(cell.err_dir))
if cell.data_seg != None:
cmd += (' -S {0}'.format(cell.data_seg))
if cell.err_seg != None:
cmd += (' -E {0}'.format(cell.err_seg))
if subprocess.call(cmd, shell = True) != 0:
print ('Start GDS {0:20s}[ERROR]'.format(cell.name))
return EXIT_FAILURE
print ('Start GDS {0:30s}[OK]'.format(cell.name))
return EXIT_SUCCESS
#Stop the GDS
def gds_stop_entries(entries):
gds_proc = []
out,err = subprocess.Popen(['ps', 'ax'], stdout = subprocess.PIPE).communicate()
for line in out.splitlines():
if 'gds' in line:
gds_proc.append(line)
for cell in entries:
for proc in gds_proc:
if cell.ctl_file in proc.split(' '):
pid = int(proc.split(None, 1)[0])
os.kill(pid, signal.SIGRTMIN)
if (os.path.exists(cell.ctl_file)):
os.system(('rm {0}'.format(cell.ctl_file)))
if __is_GDS_exist__(cell) == False:
print ('Stop GDS {0:32s}[OK]'.format(cell.name))
else:
print ('Stop GDS {0:32s}[ERROR]'.format(cell.name))
return EXIT_SUCCESS
def gds_stop_all():
out,err = subprocess.Popen(['ps', 'ax'], stdout = subprocess.PIPE).communicate()
for line in out.splitlines():
if 'gds' in line and '-p' in line and '-d' in line:
tokens = line.split(' ')
i = 0
for token in tokens:
if token == '-p':
break
i = i + 1
single = tokens[i+1]
pid = int(line.split(None, 1)[0])
os.kill(pid, signal.SIGRTMIN)
if __is_GDS_exist__(pid) == False:
print ('Stop GDS {0:32s}[OK]'.format(single))
else:
print ('Stop GDS {0:32s}[ERROR]'.format(single))
return EXIT_SUCCESS
def gds_stop_single(single):
out,err = subprocess.Popen(['ps', 'ax'], stdout = subprocess.PIPE).communicate()
for line in out.splitlines():
if 'gds' in line and '-p' in line and '-d' in line:
tokens = line.split(' ')
i = 0