installer.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if imode == 'all' or imode == 'server':
  19. self.__install_server_software()
  20. if imode == 'all' or imode == 'database':
  21. print("\nINSTALL: Installing database software\n")
  22. self.__run_command("cd .. && " + self.benchmarker.database_sftp_string(batch_file="../config/database_sftp_batch"), True)
  23. with open (linux_install_root + "/database.sh", "r") as myfile:
  24. remote_script=myfile.read().format(database_host=self.benchmarker.database_host)
  25. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  26. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  27. p.communicate(remote_script)
  28. returncode = p.returncode
  29. if returncode != 0:
  30. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  31. print("\nINSTALL: Finished installing database software\n")
  32. if imode == 'all' or imode == 'client':
  33. print("\nINSTALL: Installing client software\n")
  34. with open (linux_install_root + "/client.sh", "r") as myfile:
  35. remote_script=myfile.read()
  36. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  37. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  38. p.communicate(remote_script)
  39. returncode = p.returncode
  40. if returncode != 0:
  41. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  42. print("\nINSTALL: Finished installing client software\n")
  43. ############################################################
  44. # End install_software
  45. ############################################################
  46. ############################################################
  47. # __install_server_software
  48. ############################################################
  49. def __install_server_software(self):
  50. print("\nINSTALL: Installing server software (strategy=%s)\n"%self.strategy)
  51. # Install global prerequisites
  52. bash_functions_path='$FWROOT/toolset/setup/linux/bash_functions.sh'
  53. prereq_path='$FWROOT/toolset/setup/linux/prerequisites.sh'
  54. self.__run_command(". %s && . %s" % (bash_functions_path, prereq_path))
  55. tests = gather_tests(include=self.benchmarker.test,
  56. exclude=self.benchmarker.exclude,
  57. benchmarker=self.benchmarker)
  58. dirs = [t.directory for t in tests]
  59. # Locate all installation files
  60. install_files = glob.glob("%s/*/install.sh" % self.fwroot)
  61. install_files.extend(glob.glob("%s/frameworks/*/*/install.sh" % self.fwroot))
  62. # Run install for selected tests
  63. for test_install_file in install_files:
  64. test_dir = os.path.dirname(test_install_file)
  65. test_rel_dir = os.path.relpath(test_dir, self.fwroot)
  66. logging.debug("Considering install of %s (%s, %s)", test_install_file, test_rel_dir, test_dir)
  67. if test_dir not in dirs:
  68. continue
  69. logging.info("Running installation for directory %s (cwd=%s)", test_dir, test_dir)
  70. # Collect the tests in this directory
  71. # local_tests = [t for t in tests if t.directory == test_dir]
  72. # Find installation directory
  73. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  74. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  75. if self.strategy is 'pertest':
  76. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  77. if not os.path.exists(test_install_dir):
  78. os.makedirs(test_install_dir)
  79. # Move into the proper working directory
  80. previousDir = os.getcwd()
  81. os.chdir(test_dir)
  82. # Load profile for this installation
  83. profile="%s/bash_profile.sh" % test_dir
  84. if not os.path.exists(profile):
  85. logging.warning("Directory %s does not have a bash_profile"%test_dir)
  86. profile="$FWROOT/config/benchmark_profile"
  87. else:
  88. logging.info("Loading environment from %s (cwd=%s)", profile, test_dir)
  89. setup_util.replace_environ(config=profile,
  90. command='export TROOT=%s && export IROOT=%s' %
  91. (test_dir, test_install_dir))
  92. # Run test installation script
  93. # FWROOT - Path of the FwBm root
  94. # IROOT - Path of this test's install directory
  95. # TROOT - Path to this test's directory
  96. # Note: Cannot use ''' for newlines here or the script
  97. # passed to `bash -c` will fail.
  98. self.__run_command('sudo -u %s -E bash -c "export TROOT=%s && export IROOT=%s && source %s && source %s"' %
  99. (self.benchmarker.runner_user, test_dir, test_install_dir,
  100. bash_functions_path, test_install_file),
  101. cwd=test_install_dir)
  102. # Move back to previous directory
  103. os.chdir(previousDir)
  104. self.__run_command("sudo apt-get -y autoremove");
  105. print("\nINSTALL: Finished installing server software\n")
  106. ############################################################
  107. # End __install_server_software
  108. ############################################################
  109. ############################################################
  110. # __install_error
  111. ############################################################
  112. def __install_error(self, message):
  113. print("\nINSTALL ERROR: %s\n" % message)
  114. if self.benchmarker.install_error_action == 'abort':
  115. sys.exit("Installation aborted.")
  116. ############################################################
  117. # End __install_error
  118. ############################################################
  119. ############################################################
  120. # __run_command
  121. ############################################################
  122. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  123. if cwd is None:
  124. cwd = self.install_dir
  125. if retry:
  126. max_attempts = 5
  127. else:
  128. max_attempts = 1
  129. attempt = 1
  130. delay = 0
  131. if send_yes:
  132. command = "yes yes | " + command
  133. rel_cwd = setup_util.path_relative_to_root(cwd)
  134. print("INSTALL: %s (cwd=$FWROOT/%s)" % (command, rel_cwd))
  135. while attempt <= max_attempts:
  136. error_message = ""
  137. try:
  138. # Execute command.
  139. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  140. break # Exit loop if successful.
  141. except:
  142. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  143. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  144. # Exit if there are no more attempts left.
  145. attempt += 1
  146. if attempt > max_attempts:
  147. break
  148. # Delay before next attempt.
  149. if delay == 0:
  150. delay = 5
  151. else:
  152. delay = delay * 2
  153. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  154. time.sleep(delay)
  155. if error_message:
  156. self.__install_error(error_message)
  157. ############################################################
  158. # End __run_command
  159. ############################################################
  160. ############################################################
  161. # __init__(benchmarker)
  162. ############################################################
  163. def __init__(self, benchmarker, install_strategy):
  164. self.benchmarker = benchmarker
  165. self.install_dir = "installs"
  166. self.fwroot = benchmarker.fwroot
  167. self.strategy = install_strategy
  168. # setup logging
  169. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  170. try:
  171. os.mkdir(self.install_dir)
  172. except OSError:
  173. pass
  174. ############################################################
  175. # End __init__
  176. ############################################################
  177. # vim: sw=2