installer.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import subprocess
  2. import os
  3. import os.path
  4. import time
  5. import traceback
  6. import sys
  7. import glob
  8. import logging
  9. import setup_util
  10. from benchmark.utils import gather_tests
  11. class Installer:
  12. ############################################################
  13. # install_software
  14. ############################################################
  15. def install_software(self):
  16. linux_install_root = self.fwroot + "/toolset/setup/linux"
  17. imode = self.benchmarker.install
  18. script_vars = {
  19. 'TFB_DBHOST': self.benchmarker.database_host
  20. }
  21. l=[]
  22. for k,v in script_vars.iteritems():
  23. l.append("export %s=%s" % (k,v))
  24. script_vars_str = "\n".join(l) + "\n\n"
  25. if imode == 'all' or imode == 'database':
  26. print("\nINSTALL: Installing database software\n")
  27. self.__run_command("cd .. && " + self.benchmarker.database_sftp_string(batch_file="../config/database_sftp_batch"), True)
  28. with open (linux_install_root + "/database.sh", "r") as myfile:
  29. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  30. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" ") +
  31. ["bash"], stdin=subprocess.PIPE)
  32. remote_script = myfile.read()
  33. p.communicate(script_vars_str + remote_script)
  34. returncode = p.returncode
  35. if returncode != 0:
  36. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  37. print("\nINSTALL: Finished installing database software\n")
  38. if imode == 'all' or imode == 'client':
  39. print("\nINSTALL: Installing client software\n")
  40. with open (linux_install_root + "/client.sh", "r") as myfile:
  41. remote_script=myfile.read()
  42. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  43. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  44. p.communicate(remote_script)
  45. returncode = p.returncode
  46. if returncode != 0:
  47. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  48. print("\nINSTALL: Finished installing client software\n")
  49. ############################################################
  50. # End install_software
  51. ############################################################
  52. ############################################################
  53. # __install_server_software
  54. ############################################################
  55. def __install_server_software(self):
  56. print("\nINSTALL: Installing server software (strategy=%s)\n"%self.strategy)
  57. # Install global prerequisites (requires sudo)
  58. bash_functions_path='$FWROOT/toolset/setup/linux/bash_functions.sh'
  59. prereq_path='$FWROOT/toolset/setup/linux/prerequisites.sh'
  60. self.__run_command(". %s && . %s" % (bash_functions_path, prereq_path))
  61. self.__run_command("sudo chown -R %s:%s %s" % (self.benchmarker.runner_user,
  62. self.benchmarker.runner_user, os.path.join(self.fwroot, self.install_dir)))
  63. tests = gather_tests(include=self.benchmarker.test,
  64. exclude=self.benchmarker.exclude,
  65. benchmarker=self.benchmarker)
  66. dirs = [t.directory for t in tests]
  67. # Locate all installation files
  68. install_files = glob.glob("%s/*/install.sh" % self.fwroot)
  69. install_files.extend(glob.glob("%s/frameworks/*/*/install.sh" % self.fwroot))
  70. # Run install for selected tests
  71. for test_install_file in install_files:
  72. test_dir = os.path.dirname(test_install_file)
  73. test_rel_dir = os.path.relpath(test_dir, self.fwroot)
  74. logging.debug("Considering install of %s (%s, %s)", test_install_file, test_rel_dir, test_dir)
  75. if test_dir not in dirs:
  76. continue
  77. logging.info("Running installation for directory %s (cwd=%s)", test_dir, test_dir)
  78. # Collect the tests in this directory
  79. # local_tests = [t for t in tests if t.directory == test_dir]
  80. # Find installation directory
  81. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  82. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  83. if self.strategy is 'pertest':
  84. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  85. if not os.path.exists(test_install_dir):
  86. os.makedirs(test_install_dir)
  87. # Move into the proper working directory
  88. previousDir = os.getcwd()
  89. os.chdir(test_dir)
  90. # Load environment
  91. setup_util.replace_environ(config='$FWROOT/config/benchmark_profile',
  92. command='export TROOT=%s && export IROOT=%s' %
  93. (test_dir, test_install_dir))
  94. # Run the install.sh script for the test as the "testrunner" user
  95. #
  96. # `sudo` - Switching user requires superuser privs
  97. # -u [username] The username
  98. # -E Preserves the current environment variables
  99. # -H Forces the home var (~) to be reset to the user specified
  100. # TROOT - Path to this test's directory
  101. # IROOT - Path of this test's install directory
  102. # TODO export bash functions and call install.sh directly
  103. command = 'sudo -u %s -E -H bash -c "source %s && source %s"' % (
  104. self.benchmarker.runner_user,
  105. bash_functions_path,
  106. test_install_file)
  107. debug_command = '''\
  108. export FWROOT=%s && \\
  109. export TROOT=%s && \\
  110. export IROOT=%s && \\
  111. cd $IROOT && \\
  112. %s''' % (self.fwroot,
  113. test_dir,
  114. test_install_dir,
  115. command)
  116. logging.info("To run installation manually, copy/paste this:\n%s", debug_command)
  117. # Run test installation script
  118. self.__run_command(command, cwd=test_install_dir)
  119. # Move back to previous directory
  120. os.chdir(previousDir)
  121. self.__run_command("sudo apt-get -yq autoremove");
  122. print("\nINSTALL: Finished installing server software\n")
  123. ############################################################
  124. # End __install_server_software
  125. ############################################################
  126. ############################################################
  127. # __install_error
  128. ############################################################
  129. def __install_error(self, message):
  130. print("\nINSTALL ERROR: %s\n" % message)
  131. if self.benchmarker.install_error_action == 'abort':
  132. sys.exit("Installation aborted.")
  133. ############################################################
  134. # End __install_error
  135. ############################################################
  136. ############################################################
  137. # __run_command
  138. ############################################################
  139. def __run_command(self, command, send_yes=False, cwd=None):
  140. if cwd is None:
  141. cwd = self.install_dir
  142. if send_yes:
  143. command = "yes yes | " + command
  144. rel_cwd = setup_util.path_relative_to_root(cwd)
  145. print("INSTALL: %s (cwd=$FWROOT/%s)" % (command, rel_cwd))
  146. try:
  147. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  148. except:
  149. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  150. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  151. self.__install_error(error_message)
  152. ############################################################
  153. # End __run_command
  154. ############################################################
  155. ############################################################
  156. # __init__(benchmarker)
  157. ############################################################
  158. def __init__(self, benchmarker, install_strategy):
  159. self.benchmarker = benchmarker
  160. self.install_dir = "installs"
  161. self.fwroot = benchmarker.fwroot
  162. self.strategy = install_strategy
  163. # setup logging
  164. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  165. try:
  166. os.mkdir(self.install_dir)
  167. except OSError:
  168. pass
  169. ############################################################
  170. # End __init__
  171. ############################################################
  172. # vim: sw=2