installer.py 13 KB

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