database.sh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/bin/bash
  2. #
  3. # Configures the database server for TFB
  4. #
  5. # Note: This is not used for Travis-CI. See run-ci.py to see
  6. # how databases are configured for Travis.
  7. #
  8. # Note on Compatibility: TFB *only* supports Ubuntu 14.04 64bit
  9. # (e.g. trusty64). However, it's nice to retain 12.04 support
  10. # where possible, as it's still heavily used.
  11. #
  12. # Database setup is one core area where we can help ensure TFB
  13. # works on 12.04 with minimal frustration. In some cases we
  14. # manually install the DB version that's expected, instead of the
  15. # 12.04 default. In other cases we can use a 12.04 specific
  16. # configuration file. These patches are not intended to enable
  17. # benchmarking (e.g. there are no guarantees that
  18. # the databases will be tuned for performance correctly), but
  19. # they do allow users on 12.04 to install and run most TFB tests.
  20. # Some tests internally have 12.04 incompatibilities, we make no
  21. # concentrated effort to address these cases, but PR's for specific
  22. # problems are welcome
  23. set -x
  24. export DEBIAN_FRONTEND=noninteractive
  25. source /etc/lsb-release
  26. export TFB_DISTRIB_ID=$DISTRIB_ID
  27. export TFB_DISTRIB_RELEASE=$DISTRIB_RELEASE
  28. export TFB_DISTRIB_CODENAME=$DISTRIB_CODENAME
  29. export TFB_DISTRIB_DESCRIPTION=$DISTRIB_DESCRIPTION
  30. ##############################
  31. # check environment
  32. ##############################
  33. # verify that $TFB_DBHOST is set
  34. echo "TFB_DBHOST: $TFB_DBHOST"
  35. [ -z "$TFB_DBHOST" ] && echo "ERROR: TFB_DBHOST is not set!"
  36. ##############################
  37. # Prerequisites
  38. ##############################
  39. sudo apt-get -y update
  40. # WARNING: DONT PUT A SPACE AFTER ANY BACKSLASH OR APT WILL BREAK
  41. # Dpkg::Options avoid hangs on Travis-CI, don't affect clean systems
  42. sudo apt-get -y install -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" \
  43. build-essential \
  44. git \
  45. libev-dev \
  46. libpq-dev \
  47. libreadline6-dev \
  48. postgresql `# Installs 9.1 or 9.3, based on Ubuntu version` \
  49. redis-server `# Installs 2.4 or 2.6, based on Ubuntu version` \
  50. lsb-core `# Ensure that lsb_release can be used`
  51. sudo sh -c "echo '* - nofile 65535' >> /etc/security/limits.conf"
  52. # Create a user-owned directory for our databases
  53. sudo mkdir -p /ssd
  54. sudo mkdir -p /ssd/log
  55. sudo chown -R $USER:$USER /ssd
  56. # Additional user account (only use if required)
  57. sudo useradd benchmarkdbuser -p benchmarkdbpass
  58. ##############################
  59. # MySQL
  60. ##############################
  61. echo "Setting up MySQL database"
  62. sudo DEBIAN_FRONTEND=noninteractive apt-get -y install mysql-server
  63. sudo stop mysql
  64. # disable checking of disk size
  65. sudo mv mysql /etc/init.d/mysql
  66. sudo chmod +x /etc/init.d/mysql
  67. sudo mv mysql.conf /etc/init/mysql.conf
  68. # use the my.cnf file to overwrite /etc/mysql/my.cnf
  69. sudo mv /etc/mysql/my.cnf /etc/mysql/my.cnf.orig
  70. sudo mv my.cnf /etc/mysql/my.cnf
  71. sudo rm -rf /ssd/mysql
  72. sudo rm -rf /ssd/log/mysql
  73. sudo cp -R -p /var/lib/mysql /ssd/
  74. sudo cp -R -p /var/log/mysql /ssd/log
  75. sudo cp usr.sbin.mysqld /etc/apparmor.d/
  76. sudo /etc/init.d/apparmor reload
  77. sudo start mysql
  78. # Set root password
  79. sudo mysqladmin -u root password secret
  80. # Insert data
  81. mysql -uroot -psecret < create.sql
  82. rm create.sql
  83. ##############################
  84. # Postgres
  85. ##############################
  86. echo "Setting up Postgres database"
  87. if [ "$TFB_DISTRIB_CODENAME" == "precise" ]; then
  88. echo "WARNING: Force upgrading Postgres for Ubuntu 12.04"
  89. sudo apt-get remove -y postgresql postgresql-9.1 postgresql-client-9.1
  90. echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" | sudo tee /etc/apt/sources.list.d/pgdg.list
  91. #wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  92. curl -s https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
  93. sudo apt-get update
  94. sudo apt-get install -y postgresql-9.3 postgresql-client-9.3
  95. sudo service postgresql start
  96. fi
  97. sudo service postgresql stop
  98. # Sometimes this doesn't work with postgresql
  99. sudo killall -s 9 -u postgres
  100. sudo mv postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
  101. sudo mv pg_hba.conf /etc/postgresql/9.3/main/pg_hba.conf
  102. sudo rm -rf /ssd/postgresql
  103. sudo cp -R -p /var/lib/postgresql/9.3/main /ssd/postgresql
  104. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  105. sudo service postgresql start
  106. sudo -u postgres psql template1 < create-postgres-database.sql
  107. sudo -u benchmarkdbuser psql hello_world < create-postgres.sql
  108. rm create-postgres-database.sql create-postgres.sql
  109. # Last chance to make sure postgresql starts up correctly
  110. sudo killall -s 9 -u postgres
  111. sudo service postgresql restart
  112. ##############################
  113. # MongoDB
  114. #
  115. # Note for 12.04: Using mongodb.org ensures 2.6 is installed
  116. ##############################
  117. echo "Setting up MongoDB database"
  118. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  119. echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  120. sudo apt-get -y update
  121. sudo apt-get -y remove mongodb-clients
  122. sudo apt-get -y install mongodb-org
  123. sudo service mongod stop
  124. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  125. sudo cp mongodb.conf /etc/mongodb.conf
  126. sudo mv mongodb.conf /etc/mongod.conf
  127. sudo rm -rf /ssd/mongodb
  128. sudo rm -rf /ssd/log/mongodb
  129. sudo cp -R -p /var/lib/mongodb /ssd/
  130. sudo cp -R -p /var/log/mongodb /ssd/log/
  131. sudo service mongod start
  132. for i in {1..15}; do
  133. nc -z localhost 27017 && break || sleep 1;
  134. echo "Waiting for MongoDB ($i/15}"
  135. done
  136. nc -z localhost 27017
  137. if [ $? -eq 0 ]; then
  138. mongo < create.js
  139. rm create.js
  140. mongod --version
  141. else
  142. >&2 echo "MongoDB did not start, skipping"
  143. fi
  144. ##############################
  145. # Apache Cassandra
  146. ##############################
  147. echo "Setting up Apache Cassandra database"
  148. sudo apt-get install -qqy openjdk-7-jdk
  149. sudo addgroup --system cassandra
  150. sudo adduser --system --home /ssd/cassandra --no-create-home --ingroup cassandra cassandra
  151. export CASS_V=2.0.12
  152. #wget -nv http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  153. curl -Os http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  154. sudo tar xzf apache-cassandra-$CASS_V-bin.tar.gz -C /opt
  155. sudo ln -s /opt/apache-cassandra-$CASS_V /opt/cassandra
  156. rm -rf /ssd/cassandra /ssd/log/cassandra
  157. mkdir -p /ssd/cassandra /ssd/log/cassandra
  158. sudo chown -R cassandra:cassandra /ssd/cassandra /ssd/log/cassandra
  159. cp cassandra/cassandra.yaml cassandra/cassandra.yaml.mod
  160. cat <<EOF > cassandra/cass_conf_replace.sed
  161. s/- seeds: "\([^"]*\)"/- seeds: "$TFB_DBHOST"/
  162. s/listen_address: \(.*\)/listen_address: $TFB_DBHOST/
  163. s/rpc_address: \(.*\)/rpc_address: $TFB_DBHOST/
  164. EOF
  165. sed -i -f cassandra/cass_conf_replace.sed cassandra/cassandra.yaml.mod
  166. sudo cp -f cassandra/cassandra.init /etc/init.d/cassandra
  167. sudo cp -f cassandra/cassandra.init.env /etc/default/cassandra
  168. sudo cp -f cassandra/cassandra.yaml.mod /opt/apache-cassandra-$CASS_V/conf/cassandra.yaml
  169. sudo cp -f cassandra/log4j-server.properties /opt/apache-cassandra-$CASS_V/conf
  170. sudo update-rc.d cassandra defaults
  171. sudo service cassandra restart
  172. for i in {1..15}; do
  173. nc -z $TFB_DBHOST 9160 && break || sleep 1;
  174. echo "Waiting for Cassandra ($i/15}"
  175. done
  176. nc -z $TFB_DBHOST 9160
  177. if [ $? -eq 0 ]; then
  178. cat cassandra/cleanup-keyspace.cql | /opt/apache-cassandra-$CASS_V/bin/cqlsh $TFB_DBHOST
  179. python cassandra/db-data-gen.py > cassandra/tfb-data.cql
  180. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/create-keyspace.cql $TFB_DBHOST
  181. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/tfb-data.cql $TFB_DBHOST
  182. else
  183. >&2 echo "Cassandra did not start, skipping"
  184. fi
  185. ##############################
  186. # Elasticsearch
  187. ##############################
  188. echo "Setting up Elasticsearch"
  189. export ES_V=1.5.0
  190. #wget -nv https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-$ES_V.tar.gz
  191. curl -Os https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-$ES_V.tar.gz
  192. sudo tar zxf elasticsearch-$ES_V.tar.gz -C /opt
  193. sudo ln -s /opt/elasticsearch-$ES_V /opt/elasticsearch
  194. rm -rf /ssd/elasticsearch /ssd/log/elasticsearch
  195. mkdir -p /ssd/elasticsearch /ssd/log/elasticsearch
  196. sudo cp elasticsearch/elasticsearch.yml /opt/elasticsearch/config
  197. sudo cp elasticsearch/elasticsearch /opt/elasticsearch
  198. /opt/elasticsearch/elasticsearch restart
  199. for i in {1..15}; do
  200. nc -z $TFB_DBHOST 9200 && break || sleep 1;
  201. echo "Waiting for Elasticsearch ($i/15}"
  202. done
  203. nc -z $TFB_DBHOST 9200
  204. if [ $? -eq 0 ]; then
  205. sh elasticsearch/es-create-index.sh
  206. python elasticsearch/es-db-data-gen.py > elasticsearch/tfb-data.json
  207. curl -sS -D - -o /dev/null -XPOST localhost:9200/tfb/world/_bulk --data-binary @elasticsearch/tfb-data.json
  208. echo "Elasticsearch DB populated"
  209. else
  210. >&2 echo "Elasticsearch did not start, skipping"
  211. fi
  212. ##############################
  213. # Redis
  214. ##############################
  215. echo "Setting up Redis database"
  216. if [ "$TFB_DISTRIB_CODENAME" == "precise" ]; then
  217. echo "WARNING: Downgrading Redis configuration for Ubuntu 12.04"
  218. # On 12.04, Redis 2.4 is installed. It doesn't support
  219. # some of the 2.6 options, so we have to remove or comment
  220. # those
  221. sed -i 's/tcp-keepalive/# tcp-keepalive/' redis.conf
  222. sed -i 's/stop-writes-on-bgsave-error/# stop-writes-on-bgsave-error/' redis.conf
  223. sed -i 's/rdbchecksum/# rdbchecksum/' redis.conf
  224. sed -i 's/slave-read-only/# slave-read-only/' redis.conf
  225. sed -i 's/repl-disable-tcp-nodelay/# repl-disable-tcp-nodelay/' redis.conf
  226. sed -i 's/slave-priority/# slave-priority/' redis.conf
  227. sed -i 's/auto-aof-rewrite-percentage/# auto-aof-rewrite-percentage/' redis.conf
  228. sed -i 's/auto-aof-rewrite-min-size/# auto-aof-rewrite-min-size/' redis.conf
  229. sed -i 's/lua-time-limit/# lua-time-limit/' redis.conf
  230. sed -i 's/notify-keyspace-events/# notify-keyspace-events/' redis.conf
  231. sed -i 's/hash-max-ziplist-entries/# hash-max-ziplist-entries/' redis.conf
  232. sed -i 's/hash-max-ziplist-value/# hash-max-ziplist-value/' redis.conf
  233. sed -i 's/zset-max-ziplist-entries/# zset-max-ziplist-entries/' redis.conf
  234. sed -i 's/zset-max-ziplist-value/# zset-max-ziplist-value/' redis.conf
  235. sed -i 's/client-output-buffer-limit/# client-output-buffer-limit/' redis.conf
  236. sed -i 's/hz 10/# hz 10/' redis.conf
  237. sed -i 's/aof-rewrite-incremental-fsync/# aof-rewrite-incremental-fsync/' redis.conf
  238. fi
  239. sudo service redis-server stop
  240. sudo mv redis.conf /etc/redis/redis.conf
  241. sudo service redis-server start
  242. bash create-redis.sh
  243. rm create-redis.sh