database.sh 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. # Version: 9.*
  86. ##############################
  87. echo "Setting up Postgres database"
  88. # This will support all 9.* versions depending on the machine
  89. PG_VERSION=`pg_config --version | grep -oP '\d\.\d'`
  90. sudo service postgresql stop
  91. # Sometimes this doesn't work with postgresql
  92. sudo killall -s 9 -u postgres
  93. sudo mv postgresql.conf /etc/postgresql/${PG_VERSION}/main/postgresql.conf
  94. sudo mv pg_hba.conf /etc/postgresql/${PG_VERSION}/main/pg_hba.conf
  95. # Make sure all the configuration files in main belong to postgres
  96. sudo chown -Rf postgres:postgres /etc/postgresql/${PG_VERSION}/main
  97. sudo rm -rf /ssd/postgresql
  98. sudo cp -R -p /var/lib/postgresql/${PG_VERSION}/main /ssd/postgresql
  99. sudo mv 60-postgresql-shm.conf /etc/sysctl.d/60-postgresql-shm.conf
  100. sudo chown postgres:postgres /etc/sysctl.d/60-postgresql-shm.conf
  101. sudo chown postgres:postgres create-postgres*
  102. sudo service postgresql start
  103. sudo -u postgres psql template1 < create-postgres-database.sql
  104. sudo -u postgres psql hello_world < create-postgres.sql
  105. sudo rm create-postgres-database.sql create-postgres.sql
  106. ##############################
  107. # MongoDB
  108. #
  109. # Note for 12.04: Using mongodb.org ensures 2.6 is installed
  110. ##############################
  111. echo "Setting up MongoDB database"
  112. sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
  113. echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
  114. sudo apt-get -y update
  115. sudo apt-get -y remove mongodb-clients
  116. sudo apt-get -y install mongodb-org
  117. sudo service mongod stop
  118. sudo mv /etc/mongodb.conf /etc/mongodb.conf.orig
  119. sudo cp mongodb.conf /etc/mongodb.conf
  120. sudo mv mongodb.conf /etc/mongod.conf
  121. sudo rm -rf /ssd/mongodb
  122. sudo rm -rf /ssd/log/mongodb
  123. sudo cp -R -p /var/lib/mongodb /ssd/
  124. sudo cp -R -p /var/log/mongodb /ssd/log/
  125. sudo service mongod start
  126. for i in {1..15}; do
  127. nc -z localhost 27017 && break || sleep 1;
  128. echo "Waiting for MongoDB ($i/15}"
  129. done
  130. nc -z localhost 27017
  131. if [ $? -eq 0 ]; then
  132. mongo < create.js
  133. rm create.js
  134. mongod --version
  135. else
  136. >&2 echo "MongoDB did not start, skipping"
  137. fi
  138. ##############################
  139. # Apache Cassandra
  140. ##############################
  141. echo "Setting up Apache Cassandra database"
  142. sudo apt-get install -qqy openjdk-7-jdk
  143. sudo addgroup --system cassandra
  144. sudo adduser --system --home /ssd/cassandra --no-create-home --ingroup cassandra cassandra
  145. export CASS_V=2.0.12
  146. #wget -nv http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  147. curl -Os http://archive.apache.org/dist/cassandra/$CASS_V/apache-cassandra-$CASS_V-bin.tar.gz
  148. sudo tar xzf apache-cassandra-$CASS_V-bin.tar.gz -C /opt
  149. sudo ln -s /opt/apache-cassandra-$CASS_V /opt/cassandra
  150. rm -rf /ssd/cassandra /ssd/log/cassandra
  151. mkdir -p /ssd/cassandra /ssd/log/cassandra
  152. sudo chown -R cassandra:cassandra /ssd/cassandra /ssd/log/cassandra
  153. cp cassandra/cassandra.yaml cassandra/cassandra.yaml.mod
  154. cat <<EOF > cassandra/cass_conf_replace.sed
  155. s/- seeds: "\([^"]*\)"/- seeds: "$TFB_DBHOST"/
  156. s/listen_address: \(.*\)/listen_address: $TFB_DBHOST/
  157. s/rpc_address: \(.*\)/rpc_address: $TFB_DBHOST/
  158. EOF
  159. sed -i -f cassandra/cass_conf_replace.sed cassandra/cassandra.yaml.mod
  160. sudo cp -f cassandra/cassandra.init /etc/init.d/cassandra
  161. sudo cp -f cassandra/cassandra.init.env /etc/default/cassandra
  162. sudo cp -f cassandra/cassandra.yaml.mod /opt/apache-cassandra-$CASS_V/conf/cassandra.yaml
  163. sudo cp -f cassandra/log4j-server.properties /opt/apache-cassandra-$CASS_V/conf
  164. sudo update-rc.d cassandra defaults
  165. sudo service cassandra restart
  166. for i in {1..15}; do
  167. nc -z $TFB_DBHOST 9160 && break || sleep 1;
  168. echo "Waiting for Cassandra ($i/15}"
  169. done
  170. nc -z $TFB_DBHOST 9160
  171. if [ $? -eq 0 ]; then
  172. cat cassandra/cleanup-keyspace.cql | /opt/apache-cassandra-$CASS_V/bin/cqlsh $TFB_DBHOST
  173. python cassandra/db-data-gen.py > cassandra/tfb-data.cql
  174. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/create-keyspace.cql $TFB_DBHOST
  175. /opt/apache-cassandra-$CASS_V/bin/cqlsh -f cassandra/tfb-data.cql $TFB_DBHOST
  176. else
  177. >&2 echo "Cassandra did not start, skipping"
  178. fi
  179. ##############################
  180. # Elasticsearch
  181. ##############################
  182. echo "Setting up Elasticsearch"
  183. export ES_V=1.5.0
  184. #wget -nv https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-$ES_V.tar.gz
  185. curl -Os https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-$ES_V.tar.gz
  186. sudo tar zxf elasticsearch-$ES_V.tar.gz -C /opt
  187. sudo ln -s /opt/elasticsearch-$ES_V /opt/elasticsearch
  188. rm -rf /ssd/elasticsearch /ssd/log/elasticsearch
  189. mkdir -p /ssd/elasticsearch /ssd/log/elasticsearch
  190. sudo cp elasticsearch/elasticsearch.yml /opt/elasticsearch/config
  191. sudo cp elasticsearch/elasticsearch /opt/elasticsearch
  192. /opt/elasticsearch/elasticsearch restart
  193. for i in {1..15}; do
  194. nc -z $TFB_DBHOST 9200 && break || sleep 1;
  195. echo "Waiting for Elasticsearch ($i/15}"
  196. done
  197. nc -z $TFB_DBHOST 9200
  198. if [ $? -eq 0 ]; then
  199. sh elasticsearch/es-create-index.sh
  200. python elasticsearch/es-db-data-gen.py > elasticsearch/tfb-data.json
  201. curl -sS -D - -o /dev/null -XPOST localhost:9200/tfb/world/_bulk --data-binary @elasticsearch/tfb-data.json
  202. echo "Elasticsearch DB populated"
  203. else
  204. >&2 echo "Elasticsearch did not start, skipping"
  205. fi
  206. ##############################
  207. # Redis
  208. ##############################
  209. echo "Setting up Redis database"
  210. if [ "$TFB_DISTRIB_CODENAME" == "precise" ]; then
  211. echo "WARNING: Downgrading Redis configuration for Ubuntu 12.04"
  212. # On 12.04, Redis 2.4 is installed. It doesn't support
  213. # some of the 2.6 options, so we have to remove or comment
  214. # those
  215. sed -i 's/tcp-keepalive/# tcp-keepalive/' redis.conf
  216. sed -i 's/stop-writes-on-bgsave-error/# stop-writes-on-bgsave-error/' redis.conf
  217. sed -i 's/rdbchecksum/# rdbchecksum/' redis.conf
  218. sed -i 's/slave-read-only/# slave-read-only/' redis.conf
  219. sed -i 's/repl-disable-tcp-nodelay/# repl-disable-tcp-nodelay/' redis.conf
  220. sed -i 's/slave-priority/# slave-priority/' redis.conf
  221. sed -i 's/auto-aof-rewrite-percentage/# auto-aof-rewrite-percentage/' redis.conf
  222. sed -i 's/auto-aof-rewrite-min-size/# auto-aof-rewrite-min-size/' redis.conf
  223. sed -i 's/lua-time-limit/# lua-time-limit/' redis.conf
  224. sed -i 's/notify-keyspace-events/# notify-keyspace-events/' redis.conf
  225. sed -i 's/hash-max-ziplist-entries/# hash-max-ziplist-entries/' redis.conf
  226. sed -i 's/hash-max-ziplist-value/# hash-max-ziplist-value/' redis.conf
  227. sed -i 's/zset-max-ziplist-entries/# zset-max-ziplist-entries/' redis.conf
  228. sed -i 's/zset-max-ziplist-value/# zset-max-ziplist-value/' redis.conf
  229. sed -i 's/client-output-buffer-limit/# client-output-buffer-limit/' redis.conf
  230. sed -i 's/hz 10/# hz 10/' redis.conf
  231. sed -i 's/aof-rewrite-incremental-fsync/# aof-rewrite-incremental-fsync/' redis.conf
  232. fi
  233. sudo service redis-server stop
  234. sudo mv redis.conf /etc/redis/redis.conf
  235. sudo service redis-server start
  236. bash create-redis.sh
  237. rm create-redis.sh