Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Vagrant.require_version ">= 1.9.2"
- require 'erb'
- require 'yaml'
- @usage = <<-EOF
- This Vagrantfile uses environment variables for its configuration.
- Depending on deploy, some variables are required.
- VAGRANT_PROFILE [REQUIRED] define the AWS credentials profile to use for creating instances
- VAGRANT_EC2_KEY [REQUIRED] define which pre-existing EC2 Key Pair to use;
- you'll need to have the corresponding public PEM key
- VAGRANT_PEM_KEY [REQUIRED] define the fully qualified path to the PEM key that is located on your file system;
- this should correspond to the EC2 Key Pair you designated
- VAGRANT_LM_OS [REQUIRED] define what OS to use for `linux_minion`; supported values: centos7 (default)
- VAGRANT_WM_OS [REQUIRED] define what OS to use for `win_minion`; supported values: winsrv2012r2, winsrv2016docker
- VAGRANT_SERVICE [REQUIRED] define service to provision; this should match the hostname regex found in top.sls
- VAGRANT_MINION_PASS define the password to assign the Administrator user of the `win_minion`;
- optimal password is 32 characters including uppercase, lowercase AND numbers
- VAGRANT_NAME_TAG define name of instance within AWS; visible from the AWS Web Console (defaults to local username)
- VAGRANT_INSTANCE_TYPE define AWS instance type to use (t2.small by default); this currently effects all instances created with this Vagrantfile
- VAGRANT_PKG_BUCKET define the AWS S3 bucket where packages and artifacts are kept (spglobal-rbn-pkg-us-east-1 by default)
- EOF
- if [ENV['VAGRANT_PROFILE'],ENV['VAGRANT_EC2_KEY'],ENV['VAGRANT_PEM_KEY'],ENV['VAGRANT_SERVICE']].include?(nil)
- puts "#{@usage}"
- exit
- end
- @profile = ENV['VAGRANT_PROFILE']
- @ec2_key = ENV['VAGRANT_EC2_KEY']
- @pem_key = ENV['VAGRANT_PEM_KEY']
- @service = ENV['VAGRANT_SERVICE']
- if ENV['VAGRANT_NAME_TAG']
- @name_tag = ENV['VAGRANT_NAME_TAG']
- else
- @name_tag = Etc.getlogin # grab the local user name and use it to set the instance name
- end
- if ENV['VAGRANT_INSTANCE_TYPE']
- @instance_type = ENV['VAGRANT_INSTANCE_TYPE']
- else
- @instance_type = "t2.small"
- end
- if ENV['VAGRANT_PKG_BUCKET']
- @pkg_bucket = ENV['VAGRANT_PKG_BUCKET']
- else
- @pkg_bucket = "spglobal-rbn-pkg-us-east-1" ## Hardcoded values. Sad!
- end
- head = YAML.load_file("../.git/HEAD")
- split = head['ref'].split('/')
- @git_branch = split[2..-1].join('/')
- Vagrant.configure("2") do |config|
- config.vm.define "master", primary: true do |master|
- master.vm.box = "aws_blank"
- master.vm.provider "aws" do |aws, override|
- creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
- aws.access_key_id = creds.credentials.access_key_id
- aws.secret_access_key = creds.credentials.secret_access_key
- template = ERB.new File.read(".vagrant_templates/master.sh.erb")
- aws.user_data = template.result()
- override.ssh.username = "centos"
- override.ssh.private_key_path = "#{@pem_key}"
- aws.keypair_name = "#{@ec2_key}"
- aws.ami = "ami-6d1c2007"
- aws.instance_type = "#{@instance_type}"
- aws.security_groups = [ "sg-#######" ]
- aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
- aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
- aws.associate_public_ip = true
- aws.tags = {
- "Name" => "#{@name_tag}-master"
- }
- end
- end
- config.vm.define "win_minion", autostart: false do |wm|
- if File.file?(".vagrant/machines/master/aws/id")
- @master_id = File.read(".vagrant/machines/master/aws/id")
- elsif File.file?(".vagrant/machines/win_minion/aws/id")
- else
- puts "No master instance was found!"
- puts "Please create a master instance before creating minion instances."
- exit
- end
- if ENV['VAGRANT_MINION_PASS']
- @pass = ENV['VAGRANT_MINION_PASS']
- else
- puts "VAGRANT_MINION_PASS variable is not set!"
- puts "#{@usage}"
- exit
- end
- if ENV['VAGRANT_WM_OS']
- @os = ENV['VAGRANT_WM_OS']
- else
- puts "VAGRANT_WM_OS variable is not set!"
- puts "#{@usage}"
- exit
- end
- case @os
- when "winsrv2012r2"
- @ami_id = "ami-36f81820"
- when "winsrv2016docker"
- @ami_id = "ami-e7b755f1"
- end
- wm.vm.box = "aws_blank"
- wm.vm.guest = "windows"
- wm.vm.communicator = "winrm"
- wm.winrm.username = "Administrator"
- wm.winrm.password = "#{@pass}"
- wm.winrm.guest_port = "5986"
- wm.winrm.transport = "ssl"
- wm.winrm.ssl_peer_verification = false
- wm.vm.provider "aws" do |aws, override|
- creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
- aws.access_key_id = creds.credentials.access_key_id
- aws.secret_access_key = creds.credentials.secret_access_key
- template = ERB.new File.read(".vagrant_templates/windows_userdata.ps1.erb")
- aws.user_data = template.result()
- aws.keypair_name = "#{@ec2_key}"
- aws.ami = "#{@ami_id}"
- aws.instance_type = "#{@instance_type}"
- aws.security_groups = [ "sg-#######" ]
- aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
- aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
- aws.associate_public_ip = true
- aws.tags = {
- "Name" => "#{@name_tag}-win-minion"
- }
- end
- creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
- if @master_id
- client = Aws::EC2::Client.new(region: "us-east-1", credentials: creds)
- ec2 = Aws::EC2::Resource.new(client: client)
- instance = ec2.instance("#{@master_id}")
- @master_ip = "#{instance.private_ip_address}"
- end
- template = ERB.new File.read(".vagrant_templates/windows_minion.ps1.erb")
- wm.vm.provision "shell", inline: template.result()
- end
- config.vm.define "linux_minion", autostart: false do |lm|
- if File.file?(".vagrant/machines/master/aws/id")
- @master_id = File.read(".vagrant/machines/master/aws/id")
- elsif File.file?(".vagrant/machines/linux_minion/aws/id")
- else
- puts "No master instance was found!"
- puts "Please create a master instance before creating minion instances."
- exit
- end
- @os = ENV['VAGRANT_LM_OS']||"centos7"
- case @os
- when "centos7"
- @user = "centos"
- @ami_id = "ami-6d1c2007"
- end
- lm.vm.box = "aws_blank"
- lm.vm.provider "aws" do |aws, override|
- creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
- aws.access_key_id = creds.credentials.access_key_id
- aws.secret_access_key = creds.credentials.secret_access_key
- template = ERB.new File.read(".vagrant_templates/linux_userdata.sh.erb")
- aws.user_data = template.result()
- override.ssh.username = "centos"
- override.ssh.private_key_path = "#{@pem_key}"
- aws.keypair_name = "#{@ec2_key}"
- aws.ami = "#{@ami_id}"
- aws.instance_type = "#{@instance_type}"
- aws.security_groups = [ "sg-#######" ]
- aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
- aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
- aws.associate_public_ip = true
- aws.tags = {
- "Name" => "#{@name_tag}-linux-minion"
- }
- end
- creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
- if @master_id
- client = Aws::EC2::Client.new(region: "us-east-1", credentials: creds)
- ec2 = Aws::EC2::Resource.new(client: client)
- instance = ec2.instance("#{@master_id}")
- @master_ip = "#{instance.private_ip_address}"
- end
- template = ERB.new File.read(".vagrant_templates/linux_minion.sh.erb")
- lm.vm.provision "shell", inline: template.result()
- end
- # explicitly disable nfs to avoid provisioning error
- config.vm.synced_folder ".", "/vagrant", type: "nfs", disabled: "true"
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement