installer.py 8.1 KB

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