From dfbb41df82efd57f529564a7842fff67636bf49e Mon Sep 17 00:00:00 2001
From: Rowan Powell <rp17@it-innovation.soton.ac.uk>
Date: Fri, 2 Feb 2018 14:17:16 +0000
Subject: [PATCH] Reformatted vagrantfile, added /infra/streaming/rspec.yml

---
 Vagrantfile | 216 +++++++++++++++-------------------------------------
 1 file changed, 63 insertions(+), 153 deletions(-)

diff --git a/Vagrantfile b/Vagrantfile
index c850860..a75baee 100644
--- a/Vagrantfile
+++ b/Vagrantfile
@@ -22,168 +22,78 @@
 #//      Created for Project :   FLAME
 #//
 #/////////////////////////////////////////////////////////////////////////
-# Define ipendpoint configuration parameters
+# Requirements
+require 'getoptlong'
+require 'yaml'
 
-ipendpoints = {
-  "apache1" => {
-    :ip_address => "192.168.50.11",
-    :location => "DC1",
-    :sfc_id => "MS_Template_1",
-    :sfc_id_instance => "MS_I1",
-    :sf_id => "adaptive_streaming",
-    :sf_id_instance => "adaptive_streaming_I1",
-    :ipendpoint_id => "adaptive_streaming_I1_apache1",
-    :influxdb_url => "http://192.168.50.10:8086",
-    :database_name => "CLMCMetrics"
-  },
-  "apache2" => { 
-    :ip_address => "192.168.50.12",
-    :location => "DC2",
-    :sfc_id => "MS_Template_1",
-    :sfc_id_instance => "MS_I1",
-    :sf_id => "adaptive_streaming",
-    :sf_id_instance => "adaptive_streaming_I1",
-    :ipendpoint_id => "adaptive_streaming_I1_apache2",
-    :influxdb_url => "http://192.168.50.10:8086",
-    :database_name => "CLMCMetrics"      
-  },
-  "nginx1" => {
-    :ip_address => "192.168.50.13",
-    :location => "DC1",
-    :sfc_id => "MS_Template_1",
-    :sfc_id_instance => "MS_I1",
-    :sf_id => "adaptive_streaming",
-    :sf_id_instance => "adaptive_streaming_nginx_I1",
-    :ipendpoint_id => "adaptive_streaming_nginx_I1_apache1",
-    :influxdb_url => "http://192.168.50.10:8086",
-    :database_name => "CLMCMetrics"
-  },
-  "mongo1" => { 
-    :ip_address => "192.168.50.14",
-    :location => "DC1",
-    :sfc_id => "MS_Template_1",
-    :sfc_id_instance => "MS_I1",
-    :sf_id => "metadata_database",
-    :sf_id_instance => "metadata_database_I1",
-    :ipendpoint_id => "metadata_database_I1_apache1",
-    :influxdb_url => "http://192.168.50.10:8086",
-    :database_name => "CLMCMetrics"      
-  }
 
-}
+# Custom options:
+#   --infra <infradir>
 
-Vagrant.configure("2") do |config|
-  config.vm.box = "ubuntu/xenial64"
- 
-  config.vm.define "clmc-service" do |my|
-
-      config.vm.network :private_network, ip: "192.168.50.10", virtualbox__intnet: "clmc-net"
-
-      my.vm.provider "virtualbox" do |v|
-        v.customize ["modifyvm", :id, "--memory", 2048]
-        v.customize ["modifyvm", :id, "--cpus", 1]
-      end
-
-      # open InfluxDB port
-      config.vm.network "forwarded_port", guest: 8086, host: 8086 
-
-      # open Chronograf port
-      config.vm.network "forwarded_port", guest: 8888, host: 8888
-
-      # open Kapacitor port
-      config.vm.network "forwarded_port", guest: 9092, host: 9092
-
-      # install the CLMC service
-      config.vm.provision :shell, :path => 'scripts/clmc-service/install-clmc-service.sh'
-
-      # start the CLMC service
-      config.vm.provision :shell, :path => 'scripts/clmc-service/start-clmc-service.sh'
-  end  
-  # Apache Server 1
-  config.vm.define "apache1" do |my|
-      config.vm.network :private_network, ip: "#{ipendpoints['apache1'][:ip_address]}", virtualbox__intnet: "clmc-net"
-
-      my.vm.provider "virtualbox" do |v|
-        v.customize ["modifyvm", :id, "--memory", 512]
-        v.customize ["modifyvm", :id, "--cpus", 1]
-      end
-
-      # open apache port
-      config.vm.network "forwarded_port", guest: 80, host: 8081 
-	  
-
-      # install the apache  service
-      config.vm.provision :shell, :path => 'test/services/apache/install-apache.sh'
-
-      # Install CLMC agent 1
-      config.vm.provision :shell, :path => 'scripts/clmc-agent/install-clmc-agent.sh', :args => "/vagrant/test/services/apache/telegraf_apache_template.conf #{ipendpoints['apache1'][:location]} #{ipendpoints['apache1'][:sfc_id]} #{ipendpoints['apache1'][:sfc_id_instance]} #{ipendpoints['apache1'][:sf_id]} #{ipendpoints['apache1'][:sf_id_instance]} #{ipendpoints['apache1'][:ipendpoint_id]} #{ipendpoints['apache1'][:influxdb_url]} #{ipendpoints['apache1'][:database_name]}"
-  end    
-  # Apache Server 2
-  config.vm.define "apache2" do |my|
-
-      config.vm.network :private_network, ip: "#{ipendpoints['apache2'][:ip_address]}", virtualbox__intnet: "clmc-net"
-
-      my.vm.provider "virtualbox" do |v|
-        v.customize ["modifyvm", :id, "--memory", 512]
-        v.customize ["modifyvm", :id, "--cpus", 1]
-      end
+# Set defaults
+DEFAULT_INFRA = "streaming"
 
-      # open apache port
-      config.vm.network "forwarded_port", guest: 80, host: 8082 
+# Define custom options
+opts = GetoptLong.new(
+  [ '--infra', GetoptLong::OPTIONAL_ARGUMENT]
+)
 
-      # install the apache  service
-      config.vm.provision :shell, :path => 'test/services/apache/install-apache.sh'
+# Retrieve custom option values
+infra = DEFAULT_INFRA
+opts.each do |opt, arg|
+ case opt
+   when '--infra'
+    infra = arg    
+ end
+end
 
-      # Install CLMC agent
-      config.vm.provision :shell, :path => 'scripts/clmc-agent/install-clmc-agent.sh', :args => "/vagrant/test/services/apache/telegraf_apache_template.conf #{ipendpoints['apache2'][:location]} #{ipendpoints['apache2'][:sfc_id]} #{ipendpoints['apache2'][:sfc_id_instance]} #{ipendpoints['apache2'][:sf_id]} #{ipendpoints['apache2'][:sf_id_instance]} #{ipendpoints['apache2'][:ipendpoint_id]} #{ipendpoints['apache2'][:influxdb_url]} #{ipendpoints['apache2'][:database_name]}"      
-  end
-  
-  # NGINX VM
-  
-  config.vm.define "NGINX_Service" do |my|
+# load custom config file
+puts "loading custom infrastructure configuration: #{infra}"
+puts "custom config file: /infra/#{infra}/rspec.yml"
+host_rspec_file = "infra/#{infra}/rspec.yml"
+hosts = YAML.load_file(host_rspec_file)
 
-      config.vm.network :private_network, ip: "#{ipendpoints['nginx1'][:ip_address]}", virtualbox__intnet: "clmc-net"
 
-      my.vm.provider "virtualbox" do |v|
-        v.customize ["modifyvm", :id, "--memory", 512]
-        v.customize ["modifyvm", :id, "--cpus", 1]
+# Start creating VMS using xenial64 as the base box
+Vagrant.configure("2") do |config|
+  config.vm.box = "ubuntu/xenial64"
+ 
+ 
+  # Dynamic VMs
+  hosts['hosts'].each do |host|
+    #p host["name"]
+    instance_name = host["name"]
+    config.vm.define instance_name do |instance_config|
+	  # Specify VM properties
+      instance_config.vm.hostname = instance_name
+      instance_config.disksize.size = host["disk"]
+      instance_config.vm.provider "virtualbox" do |v|
+        v.customize ["modifyvm", :id, "--memory", host["memory"]]
+        v.customize ["modifyvm", :id, "--cpus", host["cpus"]]
       end
-
-      # open apache port
-      config.vm.network "forwarded_port", guest: 80, host: 8083
-
-      # install the apache  service
-      config.vm.provision :shell, :path => 'test/services/nginx/install-nginx.sh'
-
-      # Install CLMC agent
-      config.vm.provision :shell, :path => 'scripts/clmc-agent/install-clmc-agent.sh', :args => "/vagrant/test/services/nginx/telegraf_nginx_template.conf #{ipendpoints['nginx1'][:location]} #{ipendpoints['nginx1'][:sfc_id]} #{ipendpoints['nginx1'][:sfc_id_instance]} #{ipendpoints['nginx1'][:sf_id]} #{ipendpoints['nginx1'][:sf_id_instance]} #{ipendpoints['nginx1'][:ipendpoint_id]} #{ipendpoints['nginx1'][:influxdb_url]} #{ipendpoints['nginx1'][:database_name]}"      
-  end
-  
-  # MONGODB VM
-  config.vm.define "MONGO_Service" do |my|
-
-      config.vm.network :private_network, ip: "#{ipendpoints['mongo1'][:ip_address]}", virtualbox__intnet: "clmc-net"
-
-      my.vm.provider "virtualbox" do |v|
-        v.customize ["modifyvm", :id, "--memory", 512]
-        v.customize ["modifyvm", :id, "--cpus", 1]
+	  # Configure network
+      config.vm.network :private_network, ip: "#{host["ip_address"]}", virtualbox__intnet: "clmc-net"
+	  # Port forwarding
+	  puts "Forwarding the following specified ports for #{host["name"]}:"
+	  host['forward_ports'].each do |port|
+		puts "Forwarding guest:#{port["guest"]} => host:#{port["host"]}"
+		config.vm.network "forwarded_port", guest: port["guest"], host: port["host"]
+	  end
+	  # Switch case added here to make clmc-service provisioning simple without having to have a complex rspec.yml file
+	  # We only run a service installation script and the agent installation script when creating a specific service VM, not the clmc-service VM
+	  case host
+	    when 'clmc-service'
+	      config.vm.provision :shell, :path => "scripts/clmc-service/#{host["install_script"]}"
+	      config.vm.provision :shell, :path => "scripts/clmc-service/#{host["start_script"]}"
+	    when (not 'clmc-service')
+		  # specific service install
+	      service_install_path = "test/services/#{host["service_name"]}/install-#{host["service_name"]}.sh"
+	      puts "installing service script: #{service_install_path}"
+	      config.vm.provision :shell, :path => service_install_path
+	      # agent install
+	      config.vm.provision :shell, :path => "scripts/clmc-agent/install-clmc-agent.sh", :args => "/vagrant/test/services/#{host["service_name"]}/telegraf_#{host["service_name"]}_template.conf #{host["location"]} #{host["sfc_id"]} #{host["sfc_id_instance"]} #{host["sf_id"]} #{host["sf_id_instance"]} #{host["ipendpoint_id"]} #{host["influxdb_url"]} #{host["database_name"]}"
       end
-
-      # open apache port
-      config.vm.network "forwarded_port", guest: 80, host: 8084
-
-      # install the apache  service
-      config.vm.provision :shell, :path => 'test/services/mongo/install-mongo.sh'
-
-      # Install CLMC agent
-      config.vm.provision :shell, :path => 'scripts/clmc-agent/install-clmc-agent.sh', :args => "/vagrant/test/services/mongo/telegraf_mongo_template.conf #{ipendpoints['mongo1'][:location]} #{ipendpoints['mongo1'][:sfc_id]} #{ipendpoints['mongo1'][:sfc_id_instance]} #{ipendpoints['mongo1'][:sf_id]} #{ipendpoints['mongo1'][:sf_id_instance]} #{ipendpoints['mongo1'][:ipendpoint_id]} #{ipendpoints['mongo1'][:influxdb_url]} #{ipendpoints['mongo1'][:database_name]}"      
-  end
-  
-  
-  config.vm.define "telegraf" do |my|
+	end
   end
-  
-  config.vm.define "ipendpoint1" do |my|
-  end
-  
+ 
 end
-- 
GitLab