import _xmlplus.xpath as xpath
import xml.dom.minidom
import time
-from xml.dom.minidom import Node
-
-
-# Set global variables here
-global config
-global nodeType
-nodeType = "lb"
-global verbose
-global certs
-
-#-------------------------------------------------------------------------------
-# Print some help
-#-------------------------------------------------------------------------------
-
-def usage(msg='glite-lb-bkserverd start/stop/status'):
- pass
-
-#-------------------------------------------------------------------------------
-# Set all environment variables
-#-------------------------------------------------------------------------------
-
-def set_env():
- print '#-------------------------------------------------------------------'
- print 'Setting ENV variables in /root/.bashrc and /root/.profile'
- print '#-------------------------------------------------------------------'
- file = open('/root/.bashrc', 'a')
- file.write('\nexport GLITE_LOCATION=%s\n' % config['glite_location'])
- file.write('export GLOBUS_LOCATION=%s\n' % config['globus_location'])
- file.write('export LD_LIBRARY_PATH=$GLITE_LOCATION/lib:$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH\n')
- file.write('export GLITE_HOST_KEY=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostkey']))
- file.write('export GLITE_HOST_CERT=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostcert']))
- file.write('export GLITE_CERT_DIR=%s\n\n' % config['certs_path'])
- file.close()
- file = open('/root/.profile', 'a')
- file.write('\nexport GLITE_LOCATION=%s\n' % config['glite_location'])
- file.write('export GLOBUS_LOCATION=%s\n' % config['globus_location'])
- file.write('export LD_LIBRARY_PATH=$GLITE_LOCATION/lib:$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH\n')
- file.write('export GLITE_HOST_KEY=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostkey']))
- file.write('export GLITE_HOST_CERT=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostcert']))
- file.write('export GLITE_CERT_DIR=%s\n\n' % config['certs_path'])
- file.close()
- print 'OK!\n'
-
- print '#-------------------------------------------------------------------'
- print ('Setting ENV variables in /home/%s/.bashrc and /home/%s/.profile' % (config['username'], config['username']))
- print '#-------------------------------------------------------------------'
- file = open('/home/%s/.bashrc' % config['username'], 'a')
- file.write('\nexport GLITE_LOCATION=%s\n' % config['glite_location'])
- file.write('export GLOBUS_LOCATION=%s\n' % config['globus_location'])
- file.write('export LD_LIBRARY_PATH=$GLITE_LOCATION/lib:$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH\n')
- file.write('export GLITE_HOST_KEY=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostkey']))
- file.write('export GLITE_HOST_CERT=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostcert']))
- file.write('export GLITE_CERT_DIR=%s\n\n' % config['certs_path'])
- file.close()
- file = open('/home/%s/.profile' % config['username'], 'a')
- file.write('\nexport GLITE_LOCATION=%s\n' % config['glite_location'])
- file.write('export GLOBUS_LOCATION=%s\n' % config['globus_location'])
- file.write('export LD_LIBRARY_PATH=$GLITE_LOCATION/lib:$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH\n')
- file.write('export GLITE_HOST_KEY=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostkey']))
- file.write('export GLITE_HOST_CERT=/home/%s/%s/%s\n' % (config['username'], config['user_cert_path'], config['hostcert']))
- file.write('export GLITE_CERT_DIR=%s\n\n' % config['certs_path'])
- file.close()
- os.system('/bin/chown %s:%s /home/%s/.bashrc' % (config['username'], config['group_name'], config['username']))
- os.system('/bin/chown %s:%s /home/%s/.profile' % (config['username'], config['group_name'], config['username']))
- print 'OK!\n'
-
-
-#-------------------------------------------------------------------------------
-# Display the service configuration parameters
-#-------------------------------------------------------------------------------
-
-def print_config(config):
- print '#-------------------------------------------------------------------'
- print 'Configuration parameters for the gLite %s installation: ' % (nodeType)
- print '#-------------------------------------------------------------------'
-
- for x in config.keys():
- print '%s = %s ' % (x, config[x])
-
- print "\n"
-
-
-#-------------------------------------------------------------------------------
-# Check to see if host certs exist and that the permissions are correct
-#-------------------------------------------------------------------------------
-
-def check_certs():
- # Check host certicicates exist
- print '#-------------------------------------------------------------------'
- print 'Checking if the hostkey and hostcert are in the machine.'
- print '#-------------------------------------------------------------------'
-
- hostcert = '%s/%s' % (config['host_cert_path'], config['hostcert'])
- hostkey = '%s/%s' % (config['host_cert_path'], config['hostkey'])
- if not os.path.exists(hostcert):
- sys.exit("Error: Host certificate not found at %s" % hostcert)
- if not os.path.exists(hostkey) :
- sys.exit("Error: Host key not found at %s" % hostkey)
-
- print('OK!\n')
- os.chmod(hostcert, 0644)
- os.chmod(hostkey, 0600)
-
-#-------------------------------------------------------------------------------
-# All the configuration code goes here
-#-------------------------------------------------------------------------------
-
-def configure():
- # Load environment variables
- print '#-------------------------------------------------------------------'
- print 'Loading environment variables.'
- print '#-------------------------------------------------------------------'
- os.system('source /root/.bashrc')
- print('OK!\n')
-
-
- # Stop MySQL
-# print '#-------------------------------------------------------------------'
-# print 'Stopping MySQL...'
-# print '#-------------------------------------------------------------------'
-# os.system('/usr/share/mysql/mysql.server stop')
-# print('\n')
-
-
- # Create /etc/my.cnf file
-# print '#-------------------------------------------------------------------'
-# print 'Creating /etc/my.cnf file...'
-# print '#-------------------------------------------------------------------'
-# file = open('/etc/my.cnf', 'w')
-# file.write('[mysqld]\n')
-# file.write('datadir=/var/lib/mysql\n')
-# file.write('socket=/tmp/mysql.sock\n')
-# file.write('\n')
-# file.write('[mysql.server]\n')
-# file.write('user=mysql\n')
-# file.write('basedir=/var/lib\n')
-# file.write('\n')
-# file.write('[safe_mysqld]\n')
-# file.write('err-log=/var/log/mysqld.log\n')
-# file.write('pid-file=/var/run/mysqld/mysqld.pid\n')
-# file.close()
-# print('\n')
-
-
- # Start MySQL
-# print '#-------------------------------------------------------------------'
-# print 'Starting MySQL...'
-# print '#-------------------------------------------------------------------'
-# os.system('/usr/share/mysql/mysql.server start')
-# print('\n')
-
-
- # Create the MySQL database
- print '#-------------------------------------------------------------------'
- print ('Creating MySQL %s database.' % config['db_name'])
- print '#-------------------------------------------------------------------'
- os.system('/usr/bin/mysqlaccess %s %s' % (config['db_user'], config['db_name']))
- file = open('/tmp/mysql_ct', 'w')
- file.write('CREATE DATABASE %s;\n' % config['db_name'])
- file.write('GRANT ALL PRIVILEGES ON %s.* TO %s@localhost IDENTIFIED BY "";\n' % (config['db_name'], config['db_user']))
- file.write('USE %s;\n' % config['db_name'])
- file.write('\. %s/etc/glite-lb-dbsetup.sql\n' % config['glite_location'])
- file.close()
- os.system('/usr/bin/mysql < /tmp/mysql_ct')
- os.system('/bin/rm /tmp/mysql_ct')
-
-
- # Restarting MySQL
- print '#-------------------------------------------------------------------'
- print 'Restarting MySQL.'
- print '#-------------------------------------------------------------------'
- os.system('/etc/init.d/mysql stop')
- time.sleep(5) # Sleep 5 seconds before starting MySQL
- os.system('/etc/init.d/mysql start')
- print '\n'
-
-
- # Create var directory and give privileges for all
- print '#-------------------------------------------------------------------'
- print ('Creating %s/var directory.' % config['glite_location'])
- print '#-------------------------------------------------------------------'
- os.system('/bin/mkdir %s/var' % config['glite_location'])
- os.chmod('%s/var' % config['glite_location'], 0777)
- if os.path.exists('%s/var' % config['glite_location']):
- print('OK!\n')
- else:
- sys.exit('Could not create directory %s/var\n' % config['glite_location'])
-
-
-
- # Copy the host certificate to the user account
- print '#-------------------------------------------------------------------'
- print 'Copying the host certificate to the user account.'
- print '#-------------------------------------------------------------------'
- os.system('/bin/mkdir /home/%s/.cert' % config['username'])
- os.system('/bin/cp %s/%s /home/%s/.cert/hostcert.pem' % (config['host_cert_path'], config['hostcert'], config['username']))
- os.system('/bin/cp %s/%s /home/%s/.cert/hostkey.pem' % (config['host_cert_path'], config['hostkey'], config['username']))
- os.system('/bin/chown -R %s:%s /home/%s/.cert/' % (config['username'], config['group_name'], config['username']))
- os.system('/bin/chmod 0644 /home/%s/.cert/hostcert.pem' % config['username'])
- os.system('/bin/chmod 0400 /home/%s/.cert/hostkey.pem' % config['username'])
- if (os.path.exists('/home/%s/.cert/hostkey.pem' % config['username'])) and \
- (os.path.exists('/home/%s/.cert/hostcert.pem' % config['username'])) :
- print('OK!\n')
- else:
- sys.exit('Could not copy the host certificate to /home/%s/.cert directory!!\n\n' % config['username'])
-
-
- return 1
-
-
-#-------------------------------------------------------------------------------
-# Add user and group
-#-------------------------------------------------------------------------------
-
-def create_account():
- # Create the group and user account
- print '#-------------------------------------------------------------------'
- print ('Adding group name "%s"' % config['group_name'])
- print '#-------------------------------------------------------------------'
- os.system('/usr/sbin/groupadd %s' % config['group_name'])
- file = open('/etc/group', 'r')
- group = config['group_name']
- found_group = 0;
- for line in file.readlines():
- rec = string.splitfields(line, ':')
- if rec[0] == group:
- print ('OK!\n')
- found_group = 1
- break
- if (found_group == 0):
- sys.exit('Could not add group: %s' % group)
-
- print '#-------------------------------------------------------------------'
- print ('Adding user "%s" to group "%s"' % (config['username'], config['group_name']))
- print '#-------------------------------------------------------------------'
- os.system('/usr/sbin/useradd -s /bin/sh -g %s %s' % (config['group_name'], config['username']))
- file = open('/etc/passwd', 'r')
- user = config['username']
- found_user = 0;
- for line in file.readlines():
- rec = string.splitfields(line, ':')
- if rec[0] == user:
- print ('OK!\n')
- found_user = 1
- break
- if (found_user == 0):
- sys.exit('Could not add user: %s' % user)
-
-
-
-#-------------------------------------------------------------------------------
-# Load the configuration values for the service
-#-------------------------------------------------------------------------------
-
-def LoadParameterFile(filename='config.xml'):
- doc = xml.dom.minidom.parse(filename)
-
- nodes = xpath.Evaluate("/config/parameter", doc.documentElement)
- params = {}
- for node in nodes:
- if params.has_key(node.attributes['name'].nodeValue):
- print 'Warning name: %s already defined with value: %s' \
- % (node.attributes['name'].nodeValue, \
- node.attributes['value'].nodeValue)
- else:
- params[node.attributes['name'].nodeValue] = \
- node.attributes['value'].nodeValue
- return params
-
-
-#-------------------------------------------------------------------------------
-# Main program begins here
-#-------------------------------------------------------------------------------
-
-if __name__ == '__main__':
-
- print '#-------------------------------------------------------------------'
- print 'Installing a gLite %s' % (nodeType)
- print '#-------------------------------------------------------------------'
-
- # Command line opts if any
- try:
- opts, args = getopt.getopt(sys.argv[1:], 'h:t', ['help', 'test='])
- except getopt.GetoptError:
- usage()
- sys.exit(2)
-
- for o, a in opts:
- if o in ("-h", "--help"):
- usage('Test message if needed')
- sys.exit()
- lfn = a
- if o == "--test":
- var=a
-
- # The script must be run as root
- if not os.geteuid()==0:
- sys.exit("\nThis script must be run as root\n")
-
- # Read service configuration information from a file
- configFile = "../glite-%s.cfg.xml" % nodeType
- config = LoadParameterFile(configFile)
-
- # Create user account
- create_account()
-
- # Host certificates
- if config['certs'] == 'true':
- if not check_certs():
- os.path.isfile("No host certificats found - plase install them and rerun")
-
- # Verbose
- if config['verbose'] == 'true':
- verbose = 1
- else:
- verbose = 0
-
- # Set up the environment
- set_env()
-
- # Print configuration parameters
- if verbose:
- print_config(config)
-
- # Configure the service
- if configure():
- print '#-------------------------------------------------------------------'
- print ('%s configuration successfully completed' % nodeType)
- print '#-------------------------------------------------------------------'
- print '\n'
-
- # Start the daemon
- print '#-------------------------------------------------------------------'
- print ('Starting the gLite LB daemon using: %s/etc/init.d/glite-lb-bkserverd start' % config['glite_location'])
- print '#-------------------------------------------------------------------'
- os.system('%s/etc/init.d/glite-lb-bkserverd start' % config['glite_location'])
- print '\n'
+import mysql
+import gLiteInstallerLib as lib
+
+class glite_lb:
+ def start(self):
+ mysql.start()
+ os.system('%s/etc/init.d/glite-lb-bkserverd start' % os.environ['GLITE_LOCATION'])
+
+ def stop(self):
+ os.system('%s/etc/init.d/glite-lb-bkserverd stop' % os.environ['GLITE_LOCATION'])
+ mysql.stop()
+
+ def configure(self):
+ mysql.start()
+
+ # Create the MySQL database
+ print '#-------------------------------------------------------------------'
+ print ('Creating MySQL %s database.' % config['db_name'])
+ print '#-------------------------------------------------------------------'
+
+ os.system('/usr/bin/mysqlaccess %s %s' % (config['lb.database.username'], config['lb.database.name']))
+ file = open('/tmp/mysql_ct', 'w')
+ text = ['CREATE DATABASE %s;\n' % config['lb.database.name'],
+ 'GRANT ALL PRIVILEGES ON %s.* TO %s@localhost IDENTIFIED BY "";\n' % (config['lb.database.name'], config['lb.database.username']),
+ 'USE %s;\n' % config['lb.database.name'],
+ '\. %s/etc/glite-lb-dbsetup.sql\n' % os.environ['GLITE_LOCATION']]
+
+ file.writelines(text)
+# file.write('CREATE DATABASE %s;\n' % config['lb.database.name'])
+# file.write('GRANT ALL PRIVILEGES ON %s.* TO %s@localhost IDENTIFIED BY "";\n' % (config['lb.database.name'], config['lb.database.username']))
+# file.write('USE %s;\n' % config['lb.database.name'])
+# file.write('\. %s/etc/glite-lb-dbsetup.sql\n' % os.environ['GLITE_LOCATION'])
+ file.close()
+ os.system('/usr/bin/mysql < /tmp/mysql_ct')
+ os.system('/bin/rm /tmp/mysql_ct')
+
+ mysql.restart()
+
+ def init(self):
+ return 0
+
+ lib.loadConfiguration("glite-lb.cfg.xml")
+ lib.getEnvironment()
+ configure()
+ start()