installer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. if self.benchmarker.install == 'all' or self.benchmarker.install == 'server':
  17. self.__install_server_software()
  18. if self.benchmarker.install == 'all' or self.benchmarker.install == 'database':
  19. self.__install_database_software()
  20. if self.benchmarker.install == 'all' or self.benchmarker.install == 'client':
  21. self.__install_client_software()
  22. ############################################################
  23. # End install_software
  24. ############################################################
  25. ############################################################
  26. # __install_server_software
  27. ############################################################
  28. def __install_server_software(self):
  29. print("\nINSTALL: Installing server software (strategy=%s)\n"%self.strategy)
  30. # Install global prerequisites
  31. bash_functions_path='$FWROOT/toolset/setup/linux/bash_functions.sh'
  32. prereq_path='$FWROOT/toolset/setup/linux/prerequisites.sh'
  33. self.__run_command(". %s && . %s" % (bash_functions_path, prereq_path))
  34. tests = gather_tests(include=self.benchmarker.test,
  35. exclude=self.benchmarker.exclude,
  36. benchmarker=self.benchmarker)
  37. dirs = [t.directory for t in tests]
  38. # Locate all installation files
  39. install_files = glob.glob("%s/*/install.sh" % self.fwroot)
  40. install_files.extend(glob.glob("%s/frameworks/*/*/install.sh" % self.fwroot))
  41. # Run install for selected tests
  42. for test_install_file in install_files:
  43. test_dir = os.path.dirname(test_install_file)
  44. test_rel_dir = os.path.relpath(test_dir, self.fwroot)
  45. logging.debug("Considering install of %s (%s, %s)", test_install_file, test_rel_dir, test_dir)
  46. if test_dir not in dirs:
  47. continue
  48. logging.info("Running installation for directory %s (cwd=%s)", test_dir, test_dir)
  49. # Collect the tests in this directory
  50. # local_tests = [t for t in tests if t.directory == test_dir]
  51. # Find installation directory
  52. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  53. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  54. if self.strategy is 'pertest':
  55. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  56. if not os.path.exists(test_install_dir):
  57. os.makedirs(test_install_dir)
  58. # Move into the proper working directory
  59. previousDir = os.getcwd()
  60. os.chdir(test_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("Directory %s does not have a bash_profile"%test_dir)
  65. profile="$FWROOT/config/benchmark_profile"
  66. else:
  67. logging.info("Loading environment from %s (cwd=%s)", profile, test_dir)
  68. setup_util.replace_environ(config=profile,
  69. command='export TROOT=%s && export IROOT=%s' %
  70. (test_dir, test_install_dir))
  71. # Run test installation script
  72. # FWROOT - Path of the FwBm root
  73. # IROOT - Path of this test's install directory
  74. # TROOT - Path to this test's directory
  75. self.__run_command('''
  76. export TROOT=%s &&
  77. export IROOT=%s &&
  78. source %s &&
  79. source %s''' %
  80. (test_dir, test_install_dir,
  81. bash_functions_path, test_install_file),
  82. cwd=test_install_dir)
  83. # Move back to previous directory
  84. os.chdir(previousDir)
  85. self.__run_command("sudo apt-get -y autoremove");
  86. print("\nINSTALL: Finished installing server software\n")
  87. ############################################################
  88. # End __install_server_software
  89. ############################################################
  90. ############################################################
  91. # __install_error
  92. ############################################################
  93. def __install_error(self, message):
  94. print("\nINSTALL ERROR: %s\n" % message)
  95. if self.benchmarker.install_error_action == 'abort':
  96. sys.exit("Installation aborted.")
  97. ############################################################
  98. # End __install_error
  99. ############################################################
  100. ############################################################
  101. # __install_database_software
  102. ############################################################
  103. def __install_database_software(self):
  104. print("\nINSTALL: Installing database software\n")
  105. self.__run_command("cd .. && " + self.benchmarker.database_sftp_string(batch_file="../config/database_sftp_batch"), True)
  106. remote_script = """
  107. ##############################
  108. # Prerequisites
  109. ##############################
  110. sudo apt-get -y update
  111. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev postgresql redis-server
  112. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  113. # Create a user-owned directory for our databases
  114. sudo mkdir -p /ssd
  115. sudo mkdir -p /ssd/log
  116. sudo chown -R $USER:$USER /ssd
  117. # Additional user account (only use if required)
  118. sudo useradd benchmarkdbuser -p benchmarkdbpass
  119. ##############################
  120. # MySQL
  121. ##############################
  122. sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
  123. sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"
  124. sudo apt-get -y install mysql-server
  125. sudo stop mysql
  126. # disable checking of disk size
  127. sudo mv mysql /etc/init.d/mysql
  128. sudo chmod +x /etc/init.d/mysql
  129. sudo mv mysql.conf /etc/init/mysql.conf
  130. # use the my.cnf file to overwrite /etc/mysql/my.cnf
  131. sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
  132. sudo mv my.cnf /etc/mysql/my.cnf
  133. sudo cp -R -p /var/lib/mysql /ssd/
  134. sudo cp -R -p /var/log/mysql /ssd/log
  135. sudo cp usr.sbin.mysqld /etc/apparmor.d/
  136. sudo /etc/init.d/apparmor reload
  137. sudo start mysql
  138. # Insert data
  139. mysql -uroot -psecret < create.sql
  140. rm create.sql
  141. ##############################
  142. # Postgres
  143. ##############################
  144. sudo -u postgres psql template1 < create-postgres-database.sql
  145. sudo -u benchmarkdbuser psql hello_world < create-postgres.sql
  146. rm create-postgres-database.sql create-postgres.sql
  147. sudo -u postgres -H /etc/init.d/postgresql stop
  148. # NOTE: This will cause errors on Ubuntu 12.04, as apt installs
  149. # an older version (9.1 instead of 9.3)
  150. sudo mv postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
  151. sudo mv pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
  152. sudo cp -R -p /var/lib/postgresql/9.3/main /ssd/postgresql
  153. sudo -u postgres -H /etc/init.d/postgresql start
  154. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  155. ##############################
  156. # MongoDB
  157. ##############################
  158. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  159. echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  160. sudo apt-get -y update
  161. sudo apt-get -y remove mongodb-clients
  162. sudo apt-get -y install mongodb-org
  163. sudo service mongod stop
  164. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  165. sudo mv mongodb.conf /etc/mongodb.conf
  166. sudo mv mongodb.conf /etc/mongod.conf
  167. sudo cp -R -p /var/lib/mongodb /ssd/
  168. sudo cp -R -p /var/log/mongodb /ssd/log/
  169. sudo service mongod start
  170. until nc -z localhost 27017 ; do echo Waiting for MongoDB; sleep 1; done
  171. mongo < create.js
  172. rm create.js
  173. ##############################
  174. # Apache Cassandra
  175. ##############################
  176. sudo apt-get install -qqy openjdk-7-jdk
  177. export CASS_V=2.0.7
  178. wget -nv http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  179. tar xzf apache-cassandra-$CASS_V-bin.tar.gz
  180. rm -rf /ssd/cassandra /ssd/log/cassandra
  181. mkdir -p /ssd/cassandra /ssd/log/cassandra
  182. sed -i "s/^.*seeds:.*/ - seeds: \"{database_host}\"/" cassandra/cassandra.yaml
  183. sed -i "s/^listen_address:.*/listen_address: {database_host}/" cassandra/cassandra.yaml
  184. sed -i "s/^rpc_address:.*/rpc_address: {database_host}/" cassandra/cassandra.yaml
  185. mv cassandra/cassandra.yaml apache-cassandra-$CASS_V/conf
  186. mv cassandra/log4j-server.properties apache-cassandra-$CASS_V/conf
  187. nohup apache-cassandra-$CASS_V/bin/cassandra -p c.pid > cassandra.log
  188. until nc -z {database_host} 9160 ; do echo Waiting for Cassandra; sleep 1; done
  189. cat cassandra/cleanup-keyspace.cql | apache-cassandra-$CASS_V/bin/cqlsh {database_host}
  190. python cassandra/db-data-gen.py > cassandra/tfb-data.cql
  191. apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/create-keyspace.cql {database_host}
  192. apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/tfb-data.cql {database_host}
  193. rm -rf apache-cassandra-*-bin.tar.gz cassandra
  194. # next lines are for debugging
  195. uname -a
  196. ps -ef
  197. ifconfig
  198. ##############################
  199. # Redis
  200. ##############################
  201. sudo service redis-server stop
  202. # NOTE: This will cause errors on Ubuntu 12.04, as apt installs
  203. # an older version of redis
  204. sudo mv redis.conf /etc/redis/redis.conf
  205. sudo service redis-server start
  206. bash create-redis.sh
  207. rm create-redis.sh
  208. """.format(database_host=self.benchmarker.database_host)
  209. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  210. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  211. p.communicate(remote_script)
  212. returncode = p.returncode
  213. if returncode != 0:
  214. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  215. print("\nINSTALL: Finished installing database software\n")
  216. ############################################################
  217. # End __install_database_software
  218. ############################################################
  219. ############################################################
  220. # __install_client_software
  221. ############################################################
  222. def __install_client_software(self):
  223. print("\nINSTALL: Installing client software\n")
  224. remote_script = """
  225. ##############################
  226. # Prerequisites
  227. ##############################
  228. sudo apt-get -y update
  229. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev
  230. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  231. ##############################
  232. # wrk
  233. ##############################
  234. git clone https://github.com/wg/wrk.git
  235. cd wrk
  236. git checkout 205a1960c8b8de5f500bb143863ae293456b7add
  237. make
  238. sudo cp wrk /usr/local/bin
  239. cd ~
  240. #############################
  241. # pipeline.lua
  242. #############################
  243. cat << EOF | tee pipeline.lua
  244. init = function(args)
  245. wrk.init(args)
  246. local r = {}
  247. local depth = tonumber(args[1]) or 1
  248. for i=1,depth do
  249. r[i] = wrk.format()
  250. end
  251. req = table.concat(r)
  252. end
  253. request = function()
  254. return req
  255. end
  256. EOF
  257. """
  258. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  259. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE)
  260. p.communicate(remote_script)
  261. returncode = p.returncode
  262. if returncode != 0:
  263. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  264. print("\nINSTALL: Finished installing client software\n")
  265. ############################################################
  266. # End __install_client_software
  267. ############################################################
  268. ############################################################
  269. # __path_exists
  270. ############################################################
  271. def __path_exists(self, path, cwd=None):
  272. full_path = os.path.join(cwd or self.install_dir, path)
  273. if os.path.exists(full_path):
  274. print("\nEXISTS: %s " % full_path)
  275. return True
  276. print("\nNOT_EXISTS: %s" % full_path)
  277. return False
  278. ############################################################
  279. # End __path_exists
  280. ############################################################
  281. ############################################################
  282. # __run_command
  283. ############################################################
  284. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  285. if cwd is None:
  286. cwd = self.install_dir
  287. if retry:
  288. max_attempts = 5
  289. else:
  290. max_attempts = 1
  291. attempt = 1
  292. delay = 0
  293. if send_yes:
  294. command = "yes yes | " + command
  295. rel_cwd = setup_util.path_relative_to_root(cwd)
  296. print("INSTALL: %s (cwd=$FWROOT%s)" % (command, rel_cwd))
  297. while attempt <= max_attempts:
  298. error_message = ""
  299. try:
  300. # Execute command.
  301. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  302. break # Exit loop if successful.
  303. except:
  304. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  305. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  306. # Exit if there are no more attempts left.
  307. attempt += 1
  308. if attempt > max_attempts:
  309. break
  310. # Delay before next attempt.
  311. if delay == 0:
  312. delay = 5
  313. else:
  314. delay = delay * 2
  315. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  316. time.sleep(delay)
  317. if error_message:
  318. self.__install_error(error_message)
  319. ############################################################
  320. # End __run_command
  321. ############################################################
  322. ############################################################
  323. # __bash_from_string
  324. # Runs bash -c "command" in install_dir.
  325. ############################################################
  326. def __bash_from_string(self, command):
  327. self.__run_command('bash -c "%s"' % command)
  328. ############################################################
  329. # End __bash_from_string
  330. ############################################################
  331. ############################################################
  332. # __download
  333. # Downloads a file from a URI.
  334. ############################################################
  335. def __download(self, uri, filename=""):
  336. if filename:
  337. if os.path.exists(filename):
  338. return
  339. filename_option = "-O %s " % filename
  340. else:
  341. filename_option = ""
  342. command = "wget -nv --no-check-certificate --trust-server-names %s%s" % (filename_option, uri)
  343. self.__run_command(command, retry=True)
  344. ############################################################
  345. # End __download
  346. ############################################################
  347. ############################################################
  348. # __init__(benchmarker)
  349. ############################################################
  350. def __init__(self, benchmarker, install_strategy):
  351. self.benchmarker = benchmarker
  352. self.install_dir = "installs"
  353. self.fwroot = benchmarker.fwroot
  354. self.strategy = install_strategy
  355. # setup logging
  356. logging.basicConfig(stream=sys.stderr, level=logging.INFO)
  357. try:
  358. os.mkdir(self.install_dir)
  359. except OSError:
  360. pass
  361. ############################################################
  362. # End __init__
  363. ############################################################
  364. # vim: sw=2