installer.py 8.1 KB

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