installer.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 (requires sudo)
  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. self.__run_command("sudo chown -R %s:%s %s" % (self.benchmarker.runner_user,
  56. self.benchmarker.runner_user, os.path.join(self.fwroot, self.install_dir)))
  57. tests = gather_tests(include=self.benchmarker.test,
  58. exclude=self.benchmarker.exclude,
  59. benchmarker=self.benchmarker)
  60. dirs = [t.directory for t in tests]
  61. # Locate all installation files
  62. install_files = glob.glob("%s/*/install.sh" % self.fwroot)
  63. install_files.extend(glob.glob("%s/frameworks/*/*/install.sh" % self.fwroot))
  64. # Run install for selected tests
  65. for test_install_file in install_files:
  66. test_dir = os.path.dirname(test_install_file)
  67. test_rel_dir = os.path.relpath(test_dir, self.fwroot)
  68. logging.debug("Considering install of %s (%s, %s)", test_install_file, test_rel_dir, test_dir)
  69. if test_dir not in dirs:
  70. continue
  71. logging.info("Running installation for directory %s (cwd=%s)", test_dir, test_dir)
  72. # Collect the tests in this directory
  73. # local_tests = [t for t in tests if t.directory == test_dir]
  74. # Find installation directory
  75. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  76. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  77. if self.strategy is 'pertest':
  78. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  79. if not os.path.exists(test_install_dir):
  80. os.makedirs(test_install_dir)
  81. # Move into the proper working directory
  82. previousDir = os.getcwd()
  83. os.chdir(test_dir)
  84. # Load profile for this installation
  85. profile="%s/bash_profile.sh" % test_dir
  86. if not os.path.exists(profile):
  87. profile="$FWROOT/config/benchmark_profile"
  88. else:
  89. logging.info("Loading environment from %s (cwd=%s)", profile, test_dir)
  90. setup_util.replace_environ(config=profile,
  91. command='export TROOT=%s && export IROOT=%s' %
  92. (test_dir, test_install_dir))
  93. # Run test installation script
  94. # FWROOT - Path of the FwBm root
  95. # IROOT - Path of this test's install directory
  96. # TROOT - Path to this test's directory
  97. # Note: Cannot use ''' for newlines here or the script
  98. # passed to `bash -c` will fail.
  99. self.__run_command('sudo -u %s -E -H bash -c "export TROOT=%s && export IROOT=%s && source %s && source %s"' %
  100. (self.benchmarker.runner_user, test_dir, test_install_dir,
  101. bash_functions_path, test_install_file),
  102. cwd=test_install_dir)
  103. # Move back to previous directory
  104. os.chdir(previousDir)
  105. self.__run_command("sudo apt-get -yq autoremove");
  106. print("\nINSTALL: Finished installing server software\n")
  107. ############################################################
  108. # End __install_server_software
  109. ############################################################
  110. ############################################################
  111. # __install_error
  112. ############################################################
  113. def __install_error(self, message):
  114. print("\nINSTALL ERROR: %s\n" % message)
  115. if self.benchmarker.install_error_action == 'abort':
  116. sys.exit("Installation aborted.")
  117. ############################################################
  118. # End __install_error
  119. ############################################################
  120. ############################################################
  121. # __run_command
  122. ############################################################
  123. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  124. if cwd is None:
  125. cwd = self.install_dir
  126. if retry:
  127. max_attempts = 5
  128. else:
  129. max_attempts = 1
  130. attempt = 1
  131. delay = 0
  132. if send_yes:
  133. command = "yes yes | " + command
  134. rel_cwd = setup_util.path_relative_to_root(cwd)
  135. print("INSTALL: %s (cwd=$FWROOT/%s)" % (command, rel_cwd))
  136. while attempt <= max_attempts:
  137. error_message = ""
  138. try:
  139. # Execute command.
  140. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  141. break # Exit loop if successful.
  142. except:
  143. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  144. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  145. # Exit if there are no more attempts left.
  146. attempt += 1
  147. if attempt > max_attempts:
  148. break
  149. # Delay before next attempt.
  150. if delay == 0:
  151. delay = 5
  152. else:
  153. delay = delay * 2
  154. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  155. time.sleep(delay)
  156. if error_message:
  157. self.__install_error(error_message)
  158. ############################################################
  159. # End __run_command
  160. ############################################################
  161. ############################################################
  162. # __init__(benchmarker)
  163. ############################################################
  164. def __init__(self, benchmarker, install_strategy):
  165. self.benchmarker = benchmarker
  166. self.install_dir = "installs"
  167. self.fwroot = benchmarker.fwroot
  168. self.strategy = install_strategy
  169. # setup logging
  170. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  171. try:
  172. os.mkdir(self.install_dir)
  173. except OSError:
  174. pass
  175. ############################################################
  176. # End __init__
  177. ############################################################
  178. # vim: sw=2