installer.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 profile for this installation
  93. profile="%s/bash_profile.sh" % test_dir
  94. if not os.path.exists(profile):
  95. profile="$FWROOT/config/benchmark_profile"
  96. else:
  97. logging.info("Loading environment from %s (cwd=%s)", profile, test_dir)
  98. setup_util.replace_environ(config=profile,
  99. command='export TROOT=%s && export IROOT=%s' %
  100. (test_dir, test_install_dir))
  101. # Run test installation script
  102. # FWROOT - Path of the FwBm root
  103. # IROOT - Path of this test's install directory
  104. # TROOT - Path to this test's directory
  105. # Note: Cannot use ''' for newlines here or the script
  106. # passed to `bash -c` will fail.
  107. self.__run_command('sudo -u %s -E -H bash -c "export TROOT=%s && export IROOT=%s && source %s && source %s"' %
  108. (self.benchmarker.runner_user, test_dir, test_install_dir,
  109. bash_functions_path, test_install_file),
  110. cwd=test_install_dir)
  111. # Move back to previous directory
  112. os.chdir(previousDir)
  113. self.__run_command("sudo apt-get -yq autoremove");
  114. print("\nINSTALL: Finished installing server software\n")
  115. ############################################################
  116. # End __install_server_software
  117. ############################################################
  118. ############################################################
  119. # __install_error
  120. ############################################################
  121. def __install_error(self, message):
  122. print("\nINSTALL ERROR: %s\n" % message)
  123. if self.benchmarker.install_error_action == 'abort':
  124. sys.exit("Installation aborted.")
  125. ############################################################
  126. # End __install_error
  127. ############################################################
  128. ############################################################
  129. # __run_command
  130. ############################################################
  131. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  132. if cwd is None:
  133. cwd = self.install_dir
  134. if retry:
  135. max_attempts = 5
  136. else:
  137. max_attempts = 1
  138. attempt = 1
  139. delay = 0
  140. if send_yes:
  141. command = "yes yes | " + command
  142. rel_cwd = setup_util.path_relative_to_root(cwd)
  143. print("INSTALL: %s (cwd=$FWROOT/%s)" % (command, rel_cwd))
  144. while attempt <= max_attempts:
  145. error_message = ""
  146. try:
  147. # Execute command.
  148. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  149. break # Exit loop if successful.
  150. except:
  151. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  152. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  153. # Exit if there are no more attempts left.
  154. attempt += 1
  155. if attempt > max_attempts:
  156. break
  157. # Delay before next attempt.
  158. if delay == 0:
  159. delay = 5
  160. else:
  161. delay = delay * 2
  162. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  163. time.sleep(delay)
  164. if error_message:
  165. self.__install_error(error_message)
  166. ############################################################
  167. # End __run_command
  168. ############################################################
  169. ############################################################
  170. # __init__(benchmarker)
  171. ############################################################
  172. def __init__(self, benchmarker, install_strategy):
  173. self.benchmarker = benchmarker
  174. self.install_dir = "installs"
  175. self.fwroot = benchmarker.fwroot
  176. self.strategy = install_strategy
  177. # setup logging
  178. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  179. try:
  180. os.mkdir(self.install_dir)
  181. except OSError:
  182. pass
  183. ############################################################
  184. # End __init__
  185. ############################################################
  186. # vim: sw=2