bootstrap.sh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env bash
  2. #
  3. # Prepares a virtual machine for running TFB
  4. #
  5. # Intentionally uses ~, $HOME, and $USER so that the
  6. # same script can work for VirtualBox (username vagrant)
  7. # and Amazon (username ubuntu)
  8. # Add everything passed in the first argument to our
  9. # local environment. This is a hack to let us use
  10. # environment variables defined on the host inside the
  11. # guest machine
  12. while read -r line; do
  13. export $line;
  14. done <<< "$1"
  15. # Store any custom variables used at launch, in case someone forgets
  16. # what this instance is (e.g. SSD or HDD, etc)
  17. echo "$1" > ~/.tfb_launch_options
  18. # Are we installing the server machine, the client machine,
  19. # the database machine, or all machines?
  20. # Valid values:
  21. # - all (we are setting up a development environment)
  22. # - database (we are setting up the database machine)
  23. # - client (we are setting up the client machine for load generation)
  24. # - server (we are setting up the machine that will host frameworks)
  25. ROLE=${2:-all}
  26. # Set a number of variables by either pulling them from
  27. # the existing environment or using the default values
  28. # I'm renaming them to indicate that (in this script only)
  29. # the values are provisioner agnostic
  30. SERVER_IP=${TFB_AWS_APP_IP:-172.16.0.16}
  31. CLIENT_IP=${TFB_AWS_LOAD_IP:-172.16.0.17}
  32. DATABA_IP=${TFB_AWS_DB_IP:-172.16.0.18}
  33. if [ "$ROLE" == "all" ]; then
  34. SERVER_IP=127.0.0.1
  35. CLIENT_IP=127.0.0.1
  36. DATABA_IP=127.0.0.1
  37. fi
  38. GH_REPO=${TFB_AWS_REPO_SLUG:-TechEmpower/FrameworkBenchmarks}
  39. GH_BRANCH=${TFB_AWS_REPO_BRANCH:-master}
  40. # A shell provisioner is called multiple times
  41. if [ ! -e "~/.firstboot" ]; then
  42. # Setup some nice TFB defaults
  43. if [ "$ROLE" == "all" ]; then
  44. echo "export TFB_CLIENT_IDENTITY_FILE=$HOME/.ssh/id_rsa" >> ~/.bash_profile
  45. echo "export TFB_DATABASE_IDENTITY_FILE=$HOME/.ssh/id_rsa" >> ~/.bash_profile
  46. else
  47. echo "export TFB_CLIENT_IDENTITY_FILE=$HOME/.ssh/client" >> ~/.bash_profile
  48. echo "export TFB_DATABASE_IDENTITY_FILE=$HOME/.ssh/database" >> ~/.bash_profile
  49. fi
  50. echo "export TFB_SERVER_HOST=$SERVER_IP" >> ~/.bash_profile
  51. echo "export TFB_CLIENT_HOST=$CLIENT_IP" >> ~/.bash_profile
  52. echo "export TFB_DATABASE_HOST=$DATABA_IP" >> ~/.bash_profile
  53. echo "export TFB_CLIENT_USER=$USER" >> ~/.bash_profile
  54. echo "export TFB_DATABASE_USER=$USER" >> ~/.bash_profile
  55. echo "export TFB_RUNNER_USER=testrunner" >> ~/.bash_profile
  56. echo "export FWROOT=$HOME/FrameworkBenchmarks" >> ~/.bash_profile
  57. source ~/.bash_profile
  58. # Ensure their host-local benchmark.cfg is not picked up on the remote host
  59. if [ -e "~/FrameworkBenchmarks/benchmark.cfg" ]; then
  60. echo "You have a benchmark.cfg file that will interfere with Vagrant, moving to benchmark.cfg.bak"
  61. mv ~/FrameworkBenchmarks/benchmark.cfg ~/FrameworkBenchmarks/benchmark.cfg.bak
  62. fi
  63. # Setup hosts
  64. echo "Setting up convenience hosts entries"
  65. echo $DATABA_IP TFB-database | sudo tee --append /etc/hosts
  66. echo $CLIENT_IP TFB-client | sudo tee --append /etc/hosts
  67. echo $SERVER_IP TFB-server | sudo tee --append /etc/hosts
  68. # Add user to run tests
  69. sudo adduser --disabled-password --gecos "" testrunner
  70. # WARN: testrunner will NOT have sudo access by round 11
  71. # please begin migrating scripts to not rely on sudo.
  72. sudo bash -c "echo 'testrunner ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/90-tfb-testrunner"
  73. # Update hostname to reflect our current role
  74. if [ "$ROLE" != "all" ]; then
  75. echo "Updating hostname"
  76. echo 127.0.0.1 `hostname` | sudo tee --append /etc/hosts
  77. myhost=TFB-${ROLE}
  78. echo $myhost | sudo tee --append /etc/hostname
  79. sudo hostname $myhost
  80. echo Updated /etc/hosts file to be:
  81. cat /etc/hosts
  82. fi
  83. # Workaround mitchellh/vagrant#289
  84. echo "grub-pc grub-pc/install_devices multiselect /dev/sda" | sudo debconf-set-selections
  85. # Install prerequisite tools
  86. echo "Installing prerequisites"
  87. sudo apt-get update
  88. sudo apt-get install -y git
  89. sudo apt-get install -y python-pip
  90. # Make project available
  91. # If they synced it to /FwBm, just expose it at ~/FwBm
  92. # If they didn't sync, we need to clone it
  93. if [ -d "/FrameworkBenchmarks" ]; then
  94. ln -s /FrameworkBenchmarks $FWROOT
  95. echo "Removing installs/ and results/ folders so they do not interfere"
  96. rm -rf $FWROOT/installs $FWROOT/results
  97. else
  98. # If there is no synced folder, clone the project
  99. echo "Cloning project from $GH_REPO $GH_BRANCH"
  100. git clone -b ${GH_BRANCH} https://github.com/${GH_REPO}.git $FWROOT
  101. fi
  102. sudo pip install -r $FWROOT/config/python_requirements.txt
  103. # Everyone gets SSH access to localhost
  104. echo "Setting up SSH access to localhost"
  105. ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
  106. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  107. sudo -u testrunner mkdir -p /home/testrunner/.ssh
  108. sudo -u testrunner ssh-keygen -t rsa -N '' -f /home/testrunner/.ssh/id_rsa
  109. sudo -u testrunner bash -c "cat /home/testrunner/.ssh/id_rsa.pub >> /home/testrunner/.ssh/authorized_keys"
  110. sudo -u testrunner bash -c "cat /home/vagrant/.ssh/authorized_keys >> /home/testrunner/.ssh/authorized_keys"
  111. chmod 600 ~/.ssh/authorized_keys
  112. sudo -u testrunner chmod 600 /home/testrunner/.ssh/authorized_keys
  113. # Enable remote SSH access if we are running production environment
  114. # Note : this are always copied from the local working copy using a
  115. # file provisioner. While they exist in the git clone we just
  116. # created (so we could use those), we want to let the user
  117. # have the option of replacing the keys in their working copy
  118. # and ensuring that only they can ssh into the machines
  119. if [ "$ROLE" == "server" ]; then
  120. # Ensure keys have proper permissions
  121. chmod 600 ~/.ssh/client ~/.ssh/database
  122. elif [ "$ROLE" != "all" ]; then
  123. # Ensure keys can be used to ssh in
  124. echo "Setting up SSH access for the TFB-server"
  125. mykey=~/.ssh/$ROLE.pub
  126. echo "Using key: "
  127. ssh-keygen -lv -f $mykey
  128. cat $mykey >> ~/.ssh/authorized_keys
  129. # Ensure keys have proper permissions
  130. chmod 600 ~/.ssh/client ~/.ssh/database
  131. fi
  132. # Setup
  133. echo "Installing $ROLE software"
  134. cd $FWROOT
  135. toolset/run-tests.py --verbose --install $ROLE --install-only --test ''
  136. # Setup a nice welcome message for our guest
  137. echo "Setting up welcome message"
  138. sudo rm -f /etc/update-motd.d/51-cloudguest
  139. sudo rm -f /etc/update-motd.d/98-cloudguest
  140. sudo mv ~/.custom_motd.sh /etc/update-motd.d/55-tfbwelcome
  141. fi