installer.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. self.__run_command('''
  97. export TROOT=%s &&
  98. export IROOT=%s &&
  99. source %s &&
  100. source %s''' %
  101. (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 -y 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. ############################################################
  123. # __path_exists
  124. ############################################################
  125. def __path_exists(self, path, cwd=None):
  126. full_path = os.path.join(cwd or self.install_dir, path)
  127. if os.path.exists(full_path):
  128. print("\nEXISTS: %s " % full_path)
  129. return True
  130. print("\nNOT_EXISTS: %s" % full_path)
  131. return False
  132. ############################################################
  133. # End __path_exists
  134. ############################################################
  135. ############################################################
  136. # __run_command
  137. ############################################################
  138. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  139. if cwd is None:
  140. cwd = self.install_dir
  141. if retry:
  142. max_attempts = 5
  143. else:
  144. max_attempts = 1
  145. attempt = 1
  146. delay = 0
  147. if send_yes:
  148. command = "yes yes | " + command
  149. rel_cwd = setup_util.path_relative_to_root(cwd)
  150. print("INSTALL: %s (cwd=$FWROOT%s)" % (command, rel_cwd))
  151. while attempt <= max_attempts:
  152. error_message = ""
  153. try:
  154. # Execute command.
  155. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  156. break # Exit loop if successful.
  157. except:
  158. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  159. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  160. # Exit if there are no more attempts left.
  161. attempt += 1
  162. if attempt > max_attempts:
  163. break
  164. # Delay before next attempt.
  165. if delay == 0:
  166. delay = 5
  167. else:
  168. delay = delay * 2
  169. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  170. time.sleep(delay)
  171. if error_message:
  172. self.__install_error(error_message)
  173. ############################################################
  174. # End __run_command
  175. ############################################################
  176. ############################################################
  177. # __bash_from_string
  178. # Runs bash -c "command" in install_dir.
  179. ############################################################
  180. def __bash_from_string(self, command):
  181. self.__run_command('bash -c "%s"' % command)
  182. ############################################################
  183. # End __bash_from_string
  184. ############################################################
  185. ############################################################
  186. # __download
  187. # Downloads a file from a URI.
  188. ############################################################
  189. def __download(self, uri, filename=""):
  190. if filename:
  191. if os.path.exists(filename):
  192. return
  193. filename_option = "-O %s " % filename
  194. else:
  195. filename_option = ""
  196. command = "wget -nv --no-check-certificate --trust-server-names %s%s" % (filename_option, uri)
  197. self.__run_command(command, retry=True)
  198. ############################################################
  199. # End __download
  200. ############################################################
  201. ############################################################
  202. # __init__(benchmarker)
  203. ############################################################
  204. def __init__(self, benchmarker, install_strategy):
  205. self.benchmarker = benchmarker
  206. self.install_dir = "installs"
  207. self.fwroot = benchmarker.fwroot
  208. self.strategy = install_strategy
  209. # setup logging
  210. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  211. try:
  212. os.mkdir(self.install_dir)
  213. except OSError:
  214. pass
  215. ############################################################
  216. # End __init__
  217. ############################################################
  218. # vim: sw=2