installer.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. if test_dir not in dirs:
  46. continue
  47. logging.info("Running installation for directory %s", test_dir)
  48. # Find installation directory
  49. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  50. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  51. if self.strategy is 'pertest':
  52. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  53. if not os.path.exists(test_install_dir):
  54. os.makedirs(test_install_dir)
  55. # Move into the proper working directory
  56. previousDir = os.getcwd()
  57. os.chdir(test_dir)
  58. # Load profile for this installation
  59. profile="%s/bash_profile.sh" % test_dir
  60. if not os.path.exists(profile):
  61. logging.warning("Directory %s does not have a bash_profile"%test_dir)
  62. profile="$FWROOT/config/benchmark_profile"
  63. else:
  64. logging.info("Loading environment from %s", profile)
  65. setup_util.replace_environ(config=profile,
  66. command='export TROOT=%s && export IROOT=%s' %
  67. (test_dir, test_install_dir))
  68. # Run test installation script
  69. # FWROOT - Path of the FwBm root
  70. # IROOT - Path of this test's install directory
  71. # TROOT - Path to this test's directory
  72. self.__run_command('''
  73. export TROOT=%s &&
  74. export IROOT=%s &&
  75. source %s &&
  76. source %s''' %
  77. (test_dir, test_install_dir,
  78. bash_functions_path, test_install_file),
  79. cwd=test_install_dir)
  80. # Move back to previous directory
  81. os.chdir(previousDir)
  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 redis-server
  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-org
  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. # Apache Cassandra
  161. ##############################
  162. sudo apt-get install -qqy openjdk-7-jdk
  163. export CASS_V=2.0.7
  164. wget http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  165. tar xzf apache-cassandra-$CASS_V-bin.tar.gz
  166. rm apache-cassandra-*-bin.tar.gz
  167. fuser -k -TERM /ssd/log/cassandra/system.log
  168. sleep 5
  169. sudo rm -rf /ssd/cassandra /ssd/log/cassandra
  170. sudo mkdir -p /ssd/cassandra /ssd/log/cassandra
  171. sudo chown tfb:tfb /ssd/cassandra /ssd/log/cassandra
  172. sed -i "s/^.*seeds:.*/ - seeds: \"%s\"/" cassandra/cassandra.yaml
  173. sed -i "s/^listen_address:.*/listen_address: %s/" cassandra/cassandra.yaml
  174. sed -i "s/^rpc_address:.*/rpc_address: %s/" cassandra/cassandra.yaml
  175. cp cassandra/cassandra.yaml apache-cassandra-$CASS_V/conf
  176. cp cassandra/log4j-server.properties apache-cassandra-$CASS_V/conf
  177. pushd apache-cassandra-$CASS_V
  178. nohup ./bin/cassandra
  179. sleep 10
  180. cat ../cassandra/create-keyspace.cql | ./bin/cqlsh $TFB_DATABASE_HOST
  181. python ../cassandra/db-data-gen.py | ./bin/cqlsh $TFB_DATABASE_HOST
  182. popd
  183. ##############################
  184. # Redis
  185. ##############################
  186. sudo service redis-server stop
  187. sudo cp redis.conf /etc/redis/redis.conf
  188. sudo service redis-server start
  189. bash create-redis.sh
  190. """ % (self.benchmarker.database_host, self.benchmarker.database_host, self.benchmarker.database_host)
  191. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  192. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  193. p.communicate(remote_script)
  194. returncode = p.returncode
  195. if returncode != 0:
  196. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  197. print("\nINSTALL: Finished installing database software\n")
  198. ############################################################
  199. # End __install_database_software
  200. ############################################################
  201. ############################################################
  202. # __install_client_software
  203. ############################################################
  204. def __install_client_software(self):
  205. print("\nINSTALL: Installing client software\n")
  206. remote_script = """
  207. ##############################
  208. # Prerequisites
  209. ##############################
  210. sudo apt-get -y update
  211. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev
  212. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  213. ##############################
  214. # wrk
  215. ##############################
  216. git clone https://github.com/wg/wrk.git
  217. cd wrk
  218. git checkout 205a1960c8b8de5f500bb143863ae293456b7add
  219. make
  220. sudo cp wrk /usr/local/bin
  221. cd ~
  222. #############################
  223. # pipeline.lua
  224. #############################
  225. cat << EOF | tee pipeline.lua
  226. init = function(args)
  227. wrk.init(args)
  228. local r = {}
  229. local depth = tonumber(args[1]) or 1
  230. for i=1,depth do
  231. r[i] = wrk.format()
  232. end
  233. req = table.concat(r)
  234. end
  235. request = function()
  236. return req
  237. end
  238. EOF
  239. """
  240. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  241. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE)
  242. p.communicate(remote_script)
  243. returncode = p.returncode
  244. if returncode != 0:
  245. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  246. print("\nINSTALL: Finished installing client software\n")
  247. ############################################################
  248. # End __install_client_software
  249. ############################################################
  250. ############################################################
  251. # __path_exists
  252. ############################################################
  253. def __path_exists(self, path, cwd=None):
  254. full_path = os.path.join(cwd or self.install_dir, path)
  255. if os.path.exists(full_path):
  256. print("\nEXISTS: %s " % full_path)
  257. return True
  258. print("\nNOT_EXISTS: %s" % full_path)
  259. return False
  260. ############################################################
  261. # End __path_exists
  262. ############################################################
  263. ############################################################
  264. # __run_command
  265. ############################################################
  266. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  267. if cwd is None:
  268. cwd = self.install_dir
  269. if retry:
  270. max_attempts = 5
  271. else:
  272. max_attempts = 1
  273. attempt = 1
  274. delay = 0
  275. if send_yes:
  276. command = "yes yes | " + command
  277. rel_cwd = setup_util.path_relative_to_root(cwd)
  278. print("INSTALL: %s (cwd=$FWROOT%s)" % (command, rel_cwd))
  279. while attempt <= max_attempts:
  280. error_message = ""
  281. try:
  282. # Execute command.
  283. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  284. break # Exit loop if successful.
  285. except:
  286. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  287. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  288. # Exit if there are no more attempts left.
  289. attempt += 1
  290. if attempt > max_attempts:
  291. break
  292. # Delay before next attempt.
  293. if delay == 0:
  294. delay = 5
  295. else:
  296. delay = delay * 2
  297. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  298. time.sleep(delay)
  299. if error_message:
  300. self.__install_error(error_message)
  301. ############################################################
  302. # End __run_command
  303. ############################################################
  304. ############################################################
  305. # __bash_from_string
  306. # Runs bash -c "command" in install_dir.
  307. ############################################################
  308. def __bash_from_string(self, command):
  309. self.__run_command('bash -c "%s"' % command)
  310. ############################################################
  311. # End __bash_from_string
  312. ############################################################
  313. ############################################################
  314. # __download
  315. # Downloads a file from a URI.
  316. ############################################################
  317. def __download(self, uri, filename=""):
  318. if filename:
  319. if os.path.exists(filename):
  320. return
  321. filename_option = "-O %s " % filename
  322. else:
  323. filename_option = ""
  324. command = "wget -nv --no-check-certificate --trust-server-names %s%s" % (filename_option, uri)
  325. self.__run_command(command, retry=True)
  326. ############################################################
  327. # End __download
  328. ############################################################
  329. ############################################################
  330. # __init__(benchmarker)
  331. ############################################################
  332. def __init__(self, benchmarker, install_strategy):
  333. self.benchmarker = benchmarker
  334. self.install_dir = "installs"
  335. self.fwroot = benchmarker.fwroot
  336. self.strategy = install_strategy
  337. # setup logging
  338. logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
  339. try:
  340. os.mkdir(self.install_dir)
  341. except OSError:
  342. pass
  343. ############################################################
  344. # End __init__
  345. ############################################################
  346. # vim: sw=2