installer.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. # Run install for selected tests
  41. for test_install_file in install_files:
  42. test_dir = os.path.basename(os.path.dirname(test_install_file))
  43. test_rel_dir = os.path.relpath(os.path.dirname(test_install_file), self.fwroot)
  44. if test_dir not in dirs:
  45. continue
  46. logging.info("Running installation for directory %s", test_dir)
  47. # Find installation directory
  48. # e.g. FWROOT/installs or FWROOT/installs/pertest/<test-name>
  49. test_install_dir="%s/%s" % (self.fwroot, self.install_dir)
  50. if self.strategy is 'pertest':
  51. test_install_dir="%s/pertest/%s" % (test_install_dir, test_dir)
  52. test_rel_install_dir=os.path.relpath(test_install_dir, self.fwroot)
  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=$FWROOT%s && export IROOT=$FWROOT%s' %
  67. (test_rel_dir, test_rel_install_dir))
  68. # Find relative installation file
  69. test_rel_install_file = "$FWROOT%s" % setup_util.path_relative_to_root(test_install_file)
  70. # Then run test installer file
  71. # Give all installers a number of variables
  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=$FWROOT/%s &&
  77. export IROOT=$FWROOT/%s &&
  78. . %s &&
  79. . %s''' %
  80. (test_rel_dir, test_rel_install_dir,
  81. bash_functions_path, test_rel_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. sudo mkdir -p /ssd
  114. sudo mkdir -p /ssd/log
  115. ##############################
  116. # MySQL
  117. ##############################
  118. sudo sh -c "echo mysql-server mysql-server/root_password_again select secret | debconf-set-selections"
  119. sudo sh -c "echo mysql-server mysql-server/root_password select secret | debconf-set-selections"
  120. sudo apt-get -y install mysql-server
  121. sudo stop mysql
  122. # disable checking of disk size
  123. sudo cp mysql /etc/init.d/mysql
  124. sudo chmod +x /etc/init.d/mysql
  125. sudo cp mysql.conf /etc/init/mysql.conf
  126. # use the my.cnf file to overwrite /etc/mysql/my.cnf
  127. sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
  128. sudo mv my.cnf /etc/mysql/my.cnf
  129. sudo cp -R -p /var/lib/mysql /ssd/
  130. sudo cp -R -p /var/log/mysql /ssd/log
  131. sudo cp usr.sbin.mysqld /etc/apparmor.d/
  132. sudo /etc/init.d/apparmor reload
  133. sudo start mysql
  134. # Insert data
  135. mysql -uroot -psecret < create.sql
  136. ##############################
  137. # Postgres
  138. ##############################
  139. sudo useradd benchmarkdbuser -p benchmarkdbpass
  140. sudo -u postgres psql template1 < create-postgres-database.sql
  141. sudo -u benchmarkdbuser psql hello_world < create-postgres.sql
  142. sudo -u postgres -H /etc/init.d/postgresql stop
  143. sudo mv postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
  144. sudo mv pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
  145. sudo cp -R -p /var/lib/postgresql/9.3/main /ssd/postgresql
  146. sudo -u postgres -H /etc/init.d/postgresql start
  147. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  148. ##############################
  149. # MongoDB
  150. ##############################
  151. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  152. sudo cp 10gen.list /etc/apt/sources.list.d/10gen.list
  153. sudo apt-get -y update
  154. sudo apt-get -y remove mongodb-clients
  155. sudo apt-get -y install mongodb-org
  156. sudo stop mongodb
  157. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  158. sudo mv mongodb.conf /etc/mongodb.conf
  159. sudo cp -R -p /var/lib/mongodb /ssd/
  160. sudo cp -R -p /var/log/mongodb /ssd/log/
  161. sudo start mongodb
  162. ##############################
  163. # Apache Cassandra
  164. ##############################
  165. sudo apt-get install -qqy openjdk-7-jdk
  166. export CASS_V=2.0.7
  167. wget http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  168. tar xzf apache-cassandra-$CASS_V-bin.tar.gz
  169. rm apache-cassandra-*-bin.tar.gz
  170. fuser -k -TERM /ssd/log/cassandra/system.log
  171. sleep 5
  172. sudo rm -rf /ssd/cassandra /ssd/log/cassandra
  173. sudo mkdir -p /ssd/cassandra /ssd/log/cassandra
  174. sudo chown tfb:tfb /ssd/cassandra /ssd/log/cassandra
  175. sed -i "s/^.*seeds:.*/ - seeds: \"%s\"/" cassandra/cassandra.yaml
  176. sed -i "s/^listen_address:.*/listen_address: %s/" cassandra/cassandra.yaml
  177. sed -i "s/^rpc_address:.*/rpc_address: %s/" cassandra/cassandra.yaml
  178. cp cassandra/cassandra.yaml apache-cassandra-$CASS_V/conf
  179. cp cassandra/log4j-server.properties apache-cassandra-$CASS_V/conf
  180. pushd apache-cassandra-$CASS_V
  181. nohup ./bin/cassandra
  182. sleep 10
  183. cat ../cassandra/create-keyspace.cql | ./bin/cqlsh $TFB_DATABASE_HOST
  184. python ../cassandra/db-data-gen.py | ./bin/cqlsh $TFB_DATABASE_HOST
  185. popd
  186. ##############################
  187. # Redis
  188. ##############################
  189. sudo service redis-server stop
  190. sudo cp redis.conf /etc/redis/redis.conf
  191. sudo service redis-server start
  192. bash create-redis.sh
  193. """ % (self.benchmarker.database_host, self.benchmarker.database_host, self.benchmarker.database_host)
  194. print("\nINSTALL: %s" % self.benchmarker.database_ssh_string)
  195. p = subprocess.Popen(self.benchmarker.database_ssh_string.split(" ") + ["bash"], stdin=subprocess.PIPE)
  196. p.communicate(remote_script)
  197. returncode = p.returncode
  198. if returncode != 0:
  199. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.database_ssh_string))
  200. print("\nINSTALL: Finished installing database software\n")
  201. ############################################################
  202. # End __install_database_software
  203. ############################################################
  204. ############################################################
  205. # __install_client_software
  206. ############################################################
  207. def __install_client_software(self):
  208. print("\nINSTALL: Installing client software\n")
  209. remote_script = """
  210. ##############################
  211. # Prerequisites
  212. ##############################
  213. sudo apt-get -y update
  214. sudo apt-get -y install build-essential git libev-dev libpq-dev libreadline6-dev
  215. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  216. ##############################
  217. # wrk
  218. ##############################
  219. git clone https://github.com/wg/wrk.git
  220. cd wrk
  221. git checkout 205a1960c8b8de5f500bb143863ae293456b7add
  222. make
  223. sudo cp wrk /usr/local/bin
  224. cd ~
  225. #############################
  226. # pipeline.lua
  227. #############################
  228. cat << EOF | tee pipeline.lua
  229. init = function(args)
  230. wrk.init(args)
  231. local r = {}
  232. local depth = tonumber(args[1]) or 1
  233. for i=1,depth do
  234. r[i] = wrk.format()
  235. end
  236. req = table.concat(r)
  237. end
  238. request = function()
  239. return req
  240. end
  241. EOF
  242. """
  243. print("\nINSTALL: %s" % self.benchmarker.client_ssh_string)
  244. p = subprocess.Popen(self.benchmarker.client_ssh_string.split(" "), stdin=subprocess.PIPE)
  245. p.communicate(remote_script)
  246. returncode = p.returncode
  247. if returncode != 0:
  248. self.__install_error("status code %s running subprocess '%s'." % (returncode, self.benchmarker.client_ssh_string))
  249. print("\nINSTALL: Finished installing client software\n")
  250. ############################################################
  251. # End __install_client_software
  252. ############################################################
  253. ############################################################
  254. # __path_exists
  255. ############################################################
  256. def __path_exists(self, path, cwd=None):
  257. full_path = os.path.join(cwd or self.install_dir, path)
  258. if os.path.exists(full_path):
  259. print("\nEXISTS: %s " % full_path)
  260. return True
  261. print("\nNOT_EXISTS: %s" % full_path)
  262. return False
  263. ############################################################
  264. # End __path_exists
  265. ############################################################
  266. ############################################################
  267. # __run_command
  268. ############################################################
  269. def __run_command(self, command, send_yes=False, cwd=None, retry=False):
  270. if cwd is None:
  271. cwd = self.install_dir
  272. if retry:
  273. max_attempts = 5
  274. else:
  275. max_attempts = 1
  276. attempt = 1
  277. delay = 0
  278. if send_yes:
  279. command = "yes yes | " + command
  280. rel_cwd = setup_util.path_relative_to_root(cwd)
  281. print("INSTALL: %s (cwd=$FWROOT%s)" % (command, rel_cwd))
  282. while attempt <= max_attempts:
  283. error_message = ""
  284. try:
  285. # Execute command.
  286. subprocess.check_call(command, shell=True, cwd=cwd, executable='/bin/bash')
  287. break # Exit loop if successful.
  288. except:
  289. exceptionType, exceptionValue, exceptionTraceBack = sys.exc_info()
  290. error_message = "".join(traceback.format_exception_only(exceptionType, exceptionValue))
  291. # Exit if there are no more attempts left.
  292. attempt += 1
  293. if attempt > max_attempts:
  294. break
  295. # Delay before next attempt.
  296. if delay == 0:
  297. delay = 5
  298. else:
  299. delay = delay * 2
  300. print("Attempt %s/%s starting in %s seconds." % (attempt, max_attempts, delay))
  301. time.sleep(delay)
  302. if error_message:
  303. self.__install_error(error_message)
  304. ############################################################
  305. # End __run_command
  306. ############################################################
  307. ############################################################
  308. # __bash_from_string
  309. # Runs bash -c "command" in install_dir.
  310. ############################################################
  311. def __bash_from_string(self, command):
  312. self.__run_command('bash -c "%s"' % command)
  313. ############################################################
  314. # End __bash_from_string
  315. ############################################################
  316. ############################################################
  317. # __download
  318. # Downloads a file from a URI.
  319. ############################################################
  320. def __download(self, uri, filename=""):
  321. if filename:
  322. if os.path.exists(filename):
  323. return
  324. filename_option = "-O %s " % filename
  325. else:
  326. filename_option = ""
  327. command = "wget -nv --no-check-certificate --trust-server-names %s%s" % (filename_option, uri)
  328. self.__run_command(command, retry=True)
  329. ############################################################
  330. # End __download
  331. ############################################################
  332. ############################################################
  333. # __init__(benchmarker)
  334. ############################################################
  335. def __init__(self, benchmarker, install_strategy):
  336. self.benchmarker = benchmarker
  337. self.install_dir = "installs"
  338. self.fwroot = benchmarker.fwroot
  339. self.strategy = install_strategy
  340. # setup logging
  341. logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
  342. try:
  343. os.mkdir(self.install_dir)
  344. except OSError:
  345. pass
  346. ############################################################
  347. # End __init__
  348. ############################################################
  349. # vim: sw=2