Browse Source

Refactor Vagrantfiles to use external Ruby scripts

Hamilton Turner 11 years ago
parent
commit
9ed303700c

+ 15 - 118
toolset/deployment/vagrant-multi/Vagrantfile

@@ -1,6 +1,12 @@
 # -*- mode: ruby -*-
 # -*- mode: ruby -*-
 # vi: set ft=ruby :
 # vi: set ft=ruby :
 
 
+require_relative '../vagrant/common'
+provider = get_provider
+check_provider_needs(provider)
+
+require_relative 'production-env'
+
 Vagrant.configure("2") do |config|
 Vagrant.configure("2") do |config|
   
   
   # Put the keys inside each box
   # Put the keys inside each box
@@ -12,131 +18,22 @@ Vagrant.configure("2") do |config|
   end
   end
 
 
   config.vm.define "client" do |client|
   config.vm.define "client" do |client|
-
-    client.vm.provision "shell" do |sh|
-      sh.path = "bootstrap.sh"
-      sh.privileged = false
-      sh.args = "client"
-    end
-
-    client.vm.provider :aws do |aws, override|
-      aws.access_key_id = ENV['TFB_AWS_ACCESS_KEY'] 
-      aws.secret_access_key = ENV['TFB_AWS_SECRET_KEY']
-      aws.keypair_name = ENV['TFB_AWS_KEY_NAME']
-      override.ssh.private_key_path = ENV['TFB_AWS_KEY_PATH']
-
-      override.vm.box = "dummy"
-      override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
-      aws.ami = "ami-62c8160a"
-      override.ssh.username = "ubuntu"
-      
-      aws.private_ip_address = "172.16.0.17"
-      aws.associate_public_ip = true
-      aws.subnet_id = "subnet-2737230f"
-      aws.security_groups = ["sg-871240e2"]
-
-      aws.tags = {
-        'Project' => 'FrameworkBenchmarks',
-        'TFB_role' => 'loadgen'
-       }
-
-      aws.instance_type = "m1.small"
-    end
-
-    client.vm.provider :virtualbox do |vb, override|
-      override.vm.hostname = "TFB-client"
-      override.vm.box = "ubuntu/trusty64"
-      override.vm.network "private_network", ip: "172.16.0.17"
-      
-      override.vm.synced_folder "../../..", "/FrameworkBenchmarks"
-    end
+    provision_bootstrap(client, "client")
+    provider_aws(client, "loadgen", "172.16.0.17")
+    provider_virtualbox(client, "client", "172.16.0.17")
   end
   end
 
 
-  # Build the DB and client servers before the 
-  # app server
   config.vm.define "db" do |db|
   config.vm.define "db" do |db|
-
-    db.vm.provision "shell" do |sh|
-      sh.path = "bootstrap.sh"
-      sh.privileged = false
-      sh.args = "database"
-    end
-
-    db.vm.provider :aws do |aws, override|
-      aws.access_key_id = ENV['TFB_AWS_ACCESS_KEY'] 
-      aws.secret_access_key = ENV['TFB_AWS_SECRET_KEY']
-      aws.keypair_name = ENV['TFB_AWS_KEY_NAME']
-      override.ssh.private_key_path = ENV['TFB_AWS_KEY_PATH']
-
-      override.vm.box = "dummy"
-      override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
-      aws.ami = "ami-62c8160a"
-      override.ssh.username = "ubuntu"
-      
-      aws.private_ip_address = "172.16.0.18"
-      aws.associate_public_ip = true
-      aws.subnet_id = "subnet-2737230f"
-      aws.security_groups = ["sg-871240e2"]
-
-      aws.tags = {
-        'Project' => 'FrameworkBenchmarks',
-        'TFB_role' => 'database'
-       }
-
-      aws.instance_type = "m1.small"
-    end
-
-    db.vm.provider :virtualbox do |vb, override|
-      override.vm.hostname = "TFB-database"
-      override.vm.box = "ubuntu/trusty64"
-      override.vm.network "private_network", ip: "172.16.0.18"
-      
-      override.vm.synced_folder "../../..", "/FrameworkBenchmarks"
-    end
+    provision_bootstrap(db, "database")
+    provider_aws(db, "database", "172.16.0.18")
+    provider_virtualbox(db, "database", "172.16.0.18")
   end
   end
 
 
   # Define the app server as the primary VM
   # Define the app server as the primary VM
   config.vm.define "app", primary: true do |app|
   config.vm.define "app", primary: true do |app|
-    
-    app.vm.provision "shell" do |sh|
-      sh.path = "bootstrap.sh"
-      sh.privileged = false
-      sh.args = "server"
-    end
-
-    app.vm.provider :aws do |aws, override|
-      aws.access_key_id = ENV['TFB_AWS_ACCESS_KEY'] 
-      aws.secret_access_key = ENV['TFB_AWS_SECRET_KEY']
-      aws.keypair_name = ENV['TFB_AWS_KEY_NAME']
-      override.ssh.private_key_path = ENV['TFB_AWS_KEY_PATH']
-
-      override.vm.box = "dummy"
-      override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
-      aws.ami = "ami-62c8160a"
-      override.ssh.username = "ubuntu"
-      
-      aws.private_ip_address = "172.16.0.16"
-      aws.associate_public_ip = true
-      aws.subnet_id = "subnet-2737230f"
-      aws.security_groups = ["sg-871240e2"]
-
-      aws.tags = {
-        'Project' => 'FrameworkBenchmarks',
-        'TFB_role' => 'appserver'
-       }
-
-      aws.instance_type = "m1.small"
-    end
-
-    app.vm.provider :virtualbox do |vb, override|
-      override.vm.hostname = "TFB-server"
-      override.vm.box = "ubuntu/trusty64"
-      override.vm.network "private_network", ip: "172.16.0.16"
-      
-      override.vm.synced_folder "../../..", "/FrameworkBenchmarks"
-      override.vm.network :forwarded_port, guest: 8080, host: 28080
-    end
+    provision_bootstrap(app, "server")
+    provider_aws(app, "appserver", "172.16.0.16")
+    provider_virtualbox(app, "server", "172.16.0.16")
   end
   end
 
 
-
 end
 end

+ 52 - 0
toolset/deployment/vagrant-multi/production-env.rb

@@ -0,0 +1,52 @@
+
+
+def provision_bootstrap(config, role)
+  config.vm.provision "shell" do |sh|
+    sh.path = "bootstrap.sh"
+    sh.privileged = false
+    sh.args = role
+  end
+end
+
+def provider_aws(config, role, ip_address)
+  config.vm.provider :aws do |aws, override|
+    aws.access_key_id = ENV['TFB_AWS_ACCESS_KEY'] 
+    aws.secret_access_key = ENV['TFB_AWS_SECRET_KEY']
+    aws.keypair_name = ENV['TFB_AWS_KEY_NAME']
+    override.ssh.private_key_path = ENV['TFB_AWS_KEY_PATH']
+
+    override.vm.box = "dummy"
+    override.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"
+    aws.ami = "ami-62c8160a"
+    override.ssh.username = "ubuntu"
+    
+    aws.private_ip_address = ip_address
+    aws.associate_public_ip = true
+    aws.subnet_id = "subnet-2737230f"
+    aws.security_groups = ["sg-871240e2"]
+
+    aws.tags = {
+      'Project' => 'FrameworkBenchmarks',
+      'TFB_role' => role
+     }
+    
+    # Double the default volume size, as we download and 
+    # install a *lot* of stuff
+
+    # TODO use http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-blockdev-template.html
+    # and read the type from the environment
+    aws.block_device_mapping = [{ 'DeviceName' => '/dev/sda1', 'Ebs.VolumeSize' => 15 }]
+
+    aws.instance_type = "m1.small"
+  end
+end
+
+def provider_virtualbox(config, role, ip_address)
+  config.vm.provider :virtualbox do |vb, override|
+    override.vm.hostname = "TFB-#{role}"
+    override.vm.box = "ubuntu/trusty64"
+    override.vm.network "private_network", ip: ip_address
+    
+    override.vm.synced_folder "../../..", "/FrameworkBenchmarks"
+  end
+end

+ 25 - 101
toolset/deployment/vagrant/Vagrantfile

@@ -1,98 +1,10 @@
 # -*- mode: ruby -*-
 # -*- mode: ruby -*-
 # vi: set ft=ruby :
 # vi: set ft=ruby :
 
 
-# Workaround for mitchellh/vagrant#1867
-if (ARGV[1] and ARGV[1].split('=')[0] == "--provider" and ARGV[1].split('=')[1])
-  provider = ARGV[1].split('=')[1].to_sym
-else
-  provider = (ARGV[2] || ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
-end
-
-if provider == :"aws"
-  # Check required variables
-  if !(ENV['TFB_AWS_ACCESS_KEY'] and ENV['TFB_AWS_SECRET_KEY'] \
-        and ENV['TFB_AWS_KEY_NAME'] and ENV['TFB_AWS_KEY_PATH'])
-    abort 'If you want to use the AWS provider, you must provide these four variables: 
-    TFB_AWS_ACCESS_KEY : Your Amazon Web Services Access Key
-    TFB_AWS_SECRET_KEY : Your Amazon Web Services Secret Access Key
-    TFB_AWS_KEY_NAME   : The name of the keypair you are using
-    TFB_AWS_KEY_PATH   : Path to the *.pem file for the keypair you are using'
-  end
-
-  # Print warning
-    warning = "\033[33m\
-WARNING: FrameworkBenchmarks is disabling folder sync between your
-local working copy and Amazon Web Services - the ~/FrameworkBenchmarks
-directory in your VM will be a git clone of TechEmpower/FrameworkBenchmarks. 
-You can re-enable folder sync using 
-    $ TFB_FORCE_SYNC=true vagrant up --provider=aws
-but be aware that you will need to upload ~2GB to Amazon before you can use
-the VM as normal.\033[0m"
-  puts warning
-end
-
-# Helps with finding current OS
-module OS
-  def OS.windows?
-    (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
-  end
-
-  def OS.mac?
-   (/darwin/ =~ RUBY_PLATFORM) != nil
-  end
+require_relative 'common'
 
 
-  def OS.unix?
-    !OS.windows?
-  end
-
-  def OS.linux?
-    OS.unix? and not OS.mac?
-  end
-end
-
-# Helps with finding current OS
-module ARCH
-  def ARCH.is64?
-    (/x86_64/ =~ RUBY_PLATFORM) != nil
-  end
-
-  def ARCH.is32?
-    !ARCH.is64?
-  end
-end
-
-# Check if this computer can run a 64-bit OS
-if provider == :"virtualbox"
-  warning = "\033[31m\
-WARNING: FrameworkBenchmarks only officially supports a 64-bit 
-virtual machine, which your current system may not be able to 
-support. Use `TFB_SHOW_VM=true vagrant up` to watch the VM launch -
-if you just see a black window you likely cannot run a 64-bit VM. 
-
-To workaround, consider using the Amazon (e.g. AWS) provider
-   $ vagrant up --provider=aws
-
-Or forcing FrameworkBenchmarks to attempt a 32-bit VM
-   $ TFB_VM_ARCH=32 vagrant up
-  
-  See http://askubuntu.com/questions/41550 for more info\033[0m"
-
-  # AMD-based needs svm feature, Intel-based needs vmx feature
-  if OS.linux? and %x(egrep '(vmx|svm)' /proc/cpuinfo).empty?
-    puts warning
-  end
-
-  # Ignore PowerPC, check for intel features
-  if OS.mac? and %x(sysctl -n machdep.cpu.features | grep -i vmx).empty?
-    puts warning
-  end
-
-  # Don't really know how to check CPU features, so I'll just check 
-  # the arch
-  if OS.windows? and ARCH.is32?
-    puts warning
-  end
-end
+provider = get_provider
+check_provider_needs(provider)
 
 
 Vagrant.configure("2") do |config|
 Vagrant.configure("2") do |config|
   config.vm.hostname = "TFB"
   config.vm.hostname = "TFB"
@@ -140,10 +52,22 @@ Vagrant.configure("2") do |config|
     # aws.region = "us-east-1"
     # aws.region = "us-east-1"
     # aws.security_groups = ["default"]
     # aws.security_groups = ["default"]
 
 
+    aws.private_ip_address = "10.0.0.12"
+    aws.associate_public_ip = true
+    aws.subnet_id = "subnet-53a9bc7b"
+
     aws.tags = {
     aws.tags = {
       'Project' => 'FrameworkBenchmarks',
       'Project' => 'FrameworkBenchmarks',
       'TFB_role' => 'integrated'
       'TFB_role' => 'integrated'
      }
      }
+
+    # Manually set the hostname (not supported by vagrant-aws)
+    override.vm.provision :shell do |sh|
+      sh.inline = "
+        echo '127.0.0.1 TFB-server' >> /etc/hosts
+        echo 'TFB-server' > /etc/hostname
+        hostname `cat /etc/hostname`"
+    end
     
     
     # Currently this uses 'previous generation' instance names
     # Currently this uses 'previous generation' instance names
     # See: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
     # See: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
@@ -155,14 +79,14 @@ Vagrant.configure("2") do |config|
     end
     end
   end
   end
   
   
-  config.vm.provision :shell do |sh|
-    sh.path = "bootstrap.sh"
-    
-    # We run the bootstrap as the normal vagrant user, not as root
-    sh.privileged = false
-    
-    # If you need arguments, you must pass them in, becuase Vagrant 
-    # cleans the environment before running the provision script
-    # sh.args = ""
-  end
+  #config.vm.provision :shell do |sh|
+  #  sh.path = "bootstrap.sh"
+  #  
+  #  # We run the bootstrap as the normal vagrant user, not as root
+  #  sh.privileged = false
+  ##  
+  #  # If you need arguments, you must pass them in, becuase Vagrant 
+  #  # cleans the environment before running the provision script
+  #  # sh.args = ""
+  #end
 end
 end

+ 104 - 0
toolset/deployment/vagrant/common.rb

@@ -0,0 +1,104 @@
+# Helps with finding current OS
+module OS
+  def OS.windows?
+    (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
+  end
+
+  def OS.mac?
+   (/darwin/ =~ RUBY_PLATFORM) != nil
+  end
+
+  def OS.unix?
+    !OS.windows?
+  end
+
+  def OS.linux?
+    OS.unix? and not OS.mac?
+  end
+end
+
+# Helps with finding current OS
+module ARCH
+  def ARCH.is64?
+    (/x86_64/ =~ RUBY_PLATFORM) != nil
+  end
+
+  def ARCH.is32?
+    !ARCH.is64?
+  end
+end
+
+def get_provider()
+  # Workaround for mitchellh/vagrant#1867
+  if (ARGV[1] and ARGV[1].split('=')[0] == "--provider" and ARGV[1].split('=')[1])
+    provider = ARGV[1].split('=')[1].to_sym
+  else
+    provider = (ARGV[2] || ENV['VAGRANT_DEFAULT_PROVIDER'] || :virtualbox).to_sym
+  end
+  provider
+end
+
+def check_provider_needs(provider)
+  if provider == :"aws"
+    check_aws_needs
+  elsif provider == :"virtualbox"
+    check_vb_needs
+  end
+end
+
+def check_aws_needs()
+  # Check required variables
+  if !(ENV['TFB_AWS_ACCESS_KEY'] and ENV['TFB_AWS_SECRET_KEY'] \
+        and ENV['TFB_AWS_KEY_NAME'] and ENV['TFB_AWS_KEY_PATH'])
+    abort 'If you want to use the AWS provider, you must provide these four variables: 
+    TFB_AWS_ACCESS_KEY : Your Amazon Web Services Access Key
+    TFB_AWS_SECRET_KEY : Your Amazon Web Services Secret Access Key
+    TFB_AWS_KEY_NAME   : The name of the keypair you are using
+    TFB_AWS_KEY_PATH   : Path to the *.pem file for the keypair you are using'
+  end
+
+  # Print warning
+    warning = "\033[33m\
+WARNING: FrameworkBenchmarks is disabling folder sync between your
+local working copy and Amazon Web Services - the ~/FrameworkBenchmarks
+directory in your VM will be a git clone of TechEmpower/FrameworkBenchmarks. 
+You can re-enable folder sync using 
+    $ TFB_FORCE_SYNC=true vagrant up --provider=aws
+but be aware that you will need to upload ~2GB to Amazon before you can use
+the VM as normal.\033[0m"
+  puts warning
+end
+
+def check_vb_needs()
+  # Check if this computer can run a 64-bit OS
+  warning = "\033[31m\
+WARNING: FrameworkBenchmarks only officially supports a 64-bit 
+virtual machine, which your current system may not be able to 
+support. Use `TFB_SHOW_VM=true vagrant up` to watch the VM launch -
+if you just see a black window you likely cannot run a 64-bit VM. 
+
+To workaround, consider using the Amazon (e.g. AWS) provider
+   $ vagrant up --provider=aws
+
+Or forcing FrameworkBenchmarks to attempt a 32-bit VM
+   $ TFB_VM_ARCH=32 vagrant up
+  
+  See http://askubuntu.com/questions/41550 for more info\033[0m"
+
+  # AMD-based needs svm feature, Intel-based needs vmx feature
+  if OS.linux? and %x(egrep '(vmx|svm)' /proc/cpuinfo).empty?
+    puts warning
+  end
+
+  # Ignore PowerPC, check for intel features
+  if OS.mac? and %x(sysctl -n machdep.cpu.features | grep -i vmx).empty?
+    puts warning
+  end
+
+  # Don't really know how to check CPU features, so I'll just check 
+  # the arch
+  if OS.windows? and ARCH.is32?
+    puts warning
+  end
+end
+