installer.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. class Installer:
  11. ############################################################
  12. # install_software
  13. ############################################################
  14. def install_software(self):
  15. if self.benchmarker.install == 'all' or self.benchmarker.install == 'server':
  16. self.__install_server_software()
  17. if self.benchmarker.install == 'all' or self.benchmarker.install == 'database':
  18. self.__install_database_software()
  19. if self.benchmarker.install == 'all' or self.benchmarker.install == 'client':
  20. self.__install_client_software()
  21. ############################################################
  22. # End install_software
  23. ############################################################
  24. ############################################################
  25. # __install_server_software
  26. ############################################################
  27. def __install_server_software(self):
  28. print("\nINSTALL: Installing server software (strategy=%s)\n"%self.strategy)
  29. # Install global prerequisites
  30. bash_functions_path='$FWROOT/toolset/setup/linux/bash_functions.sh'
  31. prereq_path='$FWROOT/toolset/setup/linux/prerequisites.sh'
  32. self.__run_command(". %s && . %s" % (bash_functions_path, prereq_path))
  33. # Pull in benchmarker include and exclude list
  34. exclude = self.benchmarker.exclude
  35. include = self.benchmarker.test
  36. if exclude == None:
  37. exclude = []
  38. # Locate all known tests
  39. install_files = glob.glob("%s/*/install.sh" % self.fwroot)
  40. # Run install for selected tests
  41. for test_install_file in install_files:
  42. test_dir = os.path.dirname(test_install_file)
  43. test_name = os.path.basename(test_dir)
  44. test_rel_dir = setup_util.path_relative_to_root(test_dir)
  45. if test_name in exclude:
  46. logging.debug("%s has been excluded", test_name)
  47. continue
  48. elif include is not None and test_name not in include:
  49. logging.debug("%s not in include list", test_name)
  50. continue
  51. else:
  52. logging.info("Running installation for %s"%test_name)
  53. # Find installation directory
  54. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  55. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  56. if self.strategy is 'pertest':
  57. test_install_dir="%s/pertest/%s" % (test_install_dir, test_name)
  58. test_rel_install_dir=setup_util.path_relative_to_root(test_install_dir)
  59. if not os.path.exists(test_install_dir):
  60. os.makedirs(test_install_dir)
  61. # Load profile for this installation
  62. profile="%s/bash_profile.sh" % test_dir
  63. if not os.path.exists(profile):
  64. logging.warning("Framework %s does not have a bash_profile"%test_name)
  65. profile="$FWROOT/config/benchmark_profile"
  66. setup_util.replace_environ(config=profile)
  67. # Find relative installation file
  68. test_rel_install_file = "$FWROOT%s" % setup_util.path_relative_to_root(test_install_file)
  69. # Then run test installer file
  70. # Give all installers a number of variables
  71. # FWROOT - Path of the FwBm root
  72. # IROOT - Path of this test's install directory
  73. # TROOT - Path to this test's directory
  74. self.__run_command('''
  75. export TROOT=$FWROOT%s &&
  76. export IROOT=$FWROOT%s &&
  77. . %s &&
  78. . %s''' %
  79. (test_rel_dir, test_rel_install_dir,
  80. bash_functions_path, test_rel_install_file),
  81. cwd=test_install_dir)
  82. self.__run_command("sudo apt-get -y autoremove");
  83. print("\nINSTALL: Finished installing server software\n")
  84. ############################################################
  85. # End __install_server_software
  86. ############################################################
  87. ############################################################
  88. # __install_error
  89. ############################################################
  90. def __install_error(self, message):
  91. print("\nINSTALL ERROR: %s\n" % message)
  92. if self.benchmarker.install_error_action == 'abort':
  93. sys.exit("Installation aborted.")
  94. ############################################################
  95. # End __install_error
  96. ############################################################
  97. ############################################################
  98. # __install_database_software
  99. ############################################################
  100. def __install_database_software(self):
  101. print("\nINSTALL: Installing database software\n")
  102. self.__run_command("cd .. && " + self.benchmarker.database_sftp_string(batch_file="../config/database_sftp_batch"), True)
  103. remote_script = """
  104. ##############################
  105. # Prerequisites
  106. ##############################
  107. sudo apt-get -y update
  108. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev postgresql
  109. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  110. sudo mkdir -p /ssd
  111. sudo mkdir -p /ssd/log
  112. ##############################
  113. # MySQL
  114. ##############################
  115. sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
  116. sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"
  117. sudo apt-get -y install mysql-server
  118. sudo stop mysql
  119. # disable checking of disk size
  120. sudo cp mysql /etc/init.d/mysql
  121. sudo chmod +x /etc/init.d/mysql
  122. sudo cp mysql.conf /etc/init/mysql.conf
  123. # use the my.cnf file to overwrite /etc/mysql/my.cnf
  124. sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
  125. sudo mv my.cnf /etc/mysql/my.cnf
  126. sudo cp -R -p /var/lib/mysql /ssd/
  127. sudo cp -R -p /var/log/mysql /ssd/log
  128. sudo cp usr.sbin.mysqld /etc/apparmor.d/
  129. sudo /etc/init.d/apparmor reload
  130. sudo start mysql
  131. # Insert data
  132. mysql -uroot -psecret < create.sql
  133. ##############################
  134. # Postgres
  135. ##############################
  136. sudo useradd benchmarkdbuser -p benchmarkdbpass
  137. sudo -u postgres psql template1 < create-postgres-database.sql
  138. sudo -u benchmarkdbuser psql hello_world < create-postgres.sql
  139. sudo -u postgres -H /etc/init.d/postgresql stop
  140. sudo mv postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
  141. sudo mv pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
  142. sudo cp -R -p /var/lib/postgresql/9.3/main /ssd/postgresql
  143. sudo -u postgres -H /etc/init.d/postgresql start
  144. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  145. ##############################
  146. # MongoDB
  147. ##############################
  148. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  149. sudo cp 10gen.list /etc/apt/sources.list.d/10gen.list
  150. sudo apt-get -y update
  151. sudo apt-get -y remove mongodb-clients
  152. sudo apt-get -y install mongodb-10gen
  153. sudo stop mongodb
  154. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  155. sudo mv mongodb.conf /etc/mongodb.conf
  156. sudo cp -R -p /var/lib/mongodb /ssd/
  157. sudo cp -R -p /var/log/mongodb /ssd/log/
  158. sudo start mongodb
  159. """
  160. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  161. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" "), stdin=subprocess.PIPE)
  162. p.communicate(remote_script)
  163. returncode = p.returncode
  164. if returncode != 0:
  165. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  166. print("\nINSTALL: Finished installing database software\n")
  167. ############################################################
  168. # End __install_database_software
  169. ############################################################
  170. ############################################################
  171. # __install_client_software
  172. ############################################################
  173. def __install_client_software(self):
  174. print("\nINSTALL: Installing client software\n")
  175. remote_script = """
  176. ##############################
  177. # Prerequisites
  178. ##############################
  179. sudo apt-get -y update
  180. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev
  181. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  182. ##############################
  183. # wrk
  184. ##############################
  185. git clone https://github.com/wg/wrk.git
  186. cd wrk
  187. make
  188. sudo cp wrk /usr/local/bin
  189. cd ~
  190. #############################
  191. # pipeline.lua
  192. #############################
  193. cat << EOF | tee pipeline.lua
  194. init = function(args)
  195. wrk.init(args)
  196. local r = {}
  197. local depth = tonumber(args[1]) or 1
  198. for i=1,depth do
  199. r[i] = wrk.format()
  200. end
  201. req = table.concat(r)
  202. end
  203. request = function()
  204. return req
  205. end
  206. EOF
  207. """
  208. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  209. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE)
  210. p.communicate(remote_script)
  211. returncode = p.returncode
  212. if returncode != 0:
  213. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  214. print("\nINSTALL: Finished installing client software\n")
  215. ############################################################
  216. # End __install_client_software
  217. ############################################################
  218. ############################################################
  219. # __path_exists
  220. ############################################################
  221. def __path_exists(self, path, cwd=None):
  222. full_path = os.path.join(cwd or self.install_dir, path)
  223. if os.path.exists(full_path):
  224. print("\nEXISTS: %s " % full_path)
  225. return True
  226. print("\nNOT_EXISTS: %s" % full_path)
  227. return False
  228. ############################################################
  229. # End __path_exists
  230. ############################################################
  231. ############################################################
  232. # __run_command
  233. ############################################################
  234. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  235. if cwd is None:
  236. cwd = self.install_dir
  237. if retry:
  238. max_attempts = 5
  239. else:
  240. max_attempts = 1
  241. attempt = 1
  242. delay = 0
  243. if send_yes:
  244. command = "yes yes | " + command
  245. rel_cwd = setup_util.path_relative_to_root(cwd)
  246. print("INSTALL: %s (cwd=$FWROOT/%s)" % (command, rel_cwd))
  247. while attempt <= max_attempts:
  248. error_message = ""
  249. try:
  250. # Execute command.
  251. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  252. break # Exit loop if successful.
  253. except:
  254. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  255. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  256. # Exit if there are no more attempts left.
  257. attempt += 1
  258. if attempt > max_attempts:
  259. break
  260. # Delay before next attempt.
  261. if delay == 0:
  262. delay = 5
  263. else:
  264. delay = delay * 2
  265. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  266. time.sleep(delay)
  267. if error_message:
  268. self.__install_error(error_message)
  269. ############################################################
  270. # End __run_command
  271. ############################################################
  272. ############################################################
  273. # __bash_from_string
  274. # Runs bash -c "command" in install_dir.
  275. ############################################################
  276. def __bash_from_string(self, command):
  277. self.__run_command('bash -c "%s"' % command)
  278. ############################################################
  279. # End __bash_from_string
  280. ############################################################
  281. ############################################################
  282. # __download
  283. # Downloads a file from a URI.
  284. ############################################################
  285. def __download(self, uri, filename=""):
  286. if filename:
  287. if os.path.exists(filename):
  288. return
  289. filename_option = "-O %s " % filename
  290. else:
  291. filename_option = ""
  292. command = "wget -nv --no-check-certificate --trust-server-names %s%s" % (filename_option, uri)
  293. self.__run_command(command, retry=True)
  294. ############################################################
  295. # End __download
  296. ############################################################
  297. ############################################################
  298. # __init__(benchmarker)
  299. ############################################################
  300. def __init__(self, benchmarker, install_strategy):
  301. self.benchmarker = benchmarker
  302. self.install_dir = "installs"
  303. self.fwroot = benchmarker.fwroot
  304. self.strategy = install_strategy
  305. # setup logging
  306. logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
  307. try:
  308. os.mkdir(self.install_dir)
  309. except OSError:
  310. pass
  311. ############################################################
  312. # End __init__
  313. ############################################################
  314. # vim: sw=2