Advertisement
Guest User

Untitled

a guest
Apr 10th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. Vagrant.require_version ">= 1.9.2"
  2. require 'erb'
  3. require 'yaml'
  4.  
  5. @usage = <<-EOF
  6. This Vagrantfile uses environment variables for its configuration.
  7. Depending on deploy, some variables are required.
  8.  
  9. VAGRANT_PROFILE [REQUIRED] define the AWS credentials profile to use for creating instances
  10.  
  11. VAGRANT_EC2_KEY [REQUIRED] define which pre-existing EC2 Key Pair to use;
  12. you'll need to have the corresponding public PEM key
  13.  
  14. VAGRANT_PEM_KEY [REQUIRED] define the fully qualified path to the PEM key that is located on your file system;
  15. this should correspond to the EC2 Key Pair you designated
  16.  
  17. VAGRANT_LM_OS [REQUIRED] define what OS to use for `linux_minion`; supported values: centos7 (default)
  18.  
  19. VAGRANT_WM_OS [REQUIRED] define what OS to use for `win_minion`; supported values: winsrv2012r2, winsrv2016docker
  20.  
  21. VAGRANT_SERVICE [REQUIRED] define service to provision; this should match the hostname regex found in top.sls
  22.  
  23. VAGRANT_MINION_PASS define the password to assign the Administrator user of the `win_minion`;
  24. optimal password is 32 characters including uppercase, lowercase AND numbers
  25.  
  26. VAGRANT_NAME_TAG define name of instance within AWS; visible from the AWS Web Console (defaults to local username)
  27.  
  28. VAGRANT_INSTANCE_TYPE define AWS instance type to use (t2.small by default); this currently effects all instances created with this Vagrantfile
  29.  
  30. VAGRANT_PKG_BUCKET define the AWS S3 bucket where packages and artifacts are kept (spglobal-rbn-pkg-us-east-1 by default)
  31.  
  32. EOF
  33.  
  34. if [ENV['VAGRANT_PROFILE'],ENV['VAGRANT_EC2_KEY'],ENV['VAGRANT_PEM_KEY'],ENV['VAGRANT_SERVICE']].include?(nil)
  35. puts "#{@usage}"
  36. exit
  37. end
  38.  
  39. @profile = ENV['VAGRANT_PROFILE']
  40. @ec2_key = ENV['VAGRANT_EC2_KEY']
  41. @pem_key = ENV['VAGRANT_PEM_KEY']
  42. @service = ENV['VAGRANT_SERVICE']
  43.  
  44. if ENV['VAGRANT_NAME_TAG']
  45. @name_tag = ENV['VAGRANT_NAME_TAG']
  46. else
  47. @name_tag = Etc.getlogin # grab the local user name and use it to set the instance name
  48. end
  49.  
  50. if ENV['VAGRANT_INSTANCE_TYPE']
  51. @instance_type = ENV['VAGRANT_INSTANCE_TYPE']
  52. else
  53. @instance_type = "t2.small"
  54. end
  55.  
  56. if ENV['VAGRANT_PKG_BUCKET']
  57. @pkg_bucket = ENV['VAGRANT_PKG_BUCKET']
  58. else
  59. @pkg_bucket = "spglobal-rbn-pkg-us-east-1" ## Hardcoded values. Sad!
  60. end
  61.  
  62. head = YAML.load_file("../.git/HEAD")
  63. split = head['ref'].split('/')
  64. @git_branch = split[2..-1].join('/')
  65.  
  66. Vagrant.configure("2") do |config|
  67.  
  68. config.vm.define "master", primary: true do |master|
  69. master.vm.box = "aws_blank"
  70.  
  71. master.vm.provider "aws" do |aws, override|
  72. creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
  73. aws.access_key_id = creds.credentials.access_key_id
  74. aws.secret_access_key = creds.credentials.secret_access_key
  75.  
  76. template = ERB.new File.read(".vagrant_templates/master.sh.erb")
  77. aws.user_data = template.result()
  78.  
  79. override.ssh.username = "centos"
  80. override.ssh.private_key_path = "#{@pem_key}"
  81.  
  82. aws.keypair_name = "#{@ec2_key}"
  83. aws.ami = "ami-6d1c2007"
  84. aws.instance_type = "#{@instance_type}"
  85. aws.security_groups = [ "sg-#######" ]
  86. aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
  87. aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
  88. aws.associate_public_ip = true
  89.  
  90. aws.tags = {
  91. "Name" => "#{@name_tag}-master"
  92. }
  93. end
  94.  
  95. end
  96.  
  97. config.vm.define "win_minion", autostart: false do |wm|
  98. if File.file?(".vagrant/machines/master/aws/id")
  99. @master_id = File.read(".vagrant/machines/master/aws/id")
  100. elsif File.file?(".vagrant/machines/win_minion/aws/id")
  101. else
  102. puts "No master instance was found!"
  103. puts "Please create a master instance before creating minion instances."
  104. exit
  105. end
  106.  
  107. if ENV['VAGRANT_MINION_PASS']
  108. @pass = ENV['VAGRANT_MINION_PASS']
  109. else
  110. puts "VAGRANT_MINION_PASS variable is not set!"
  111. puts "#{@usage}"
  112. exit
  113. end
  114.  
  115. if ENV['VAGRANT_WM_OS']
  116. @os = ENV['VAGRANT_WM_OS']
  117. else
  118. puts "VAGRANT_WM_OS variable is not set!"
  119. puts "#{@usage}"
  120. exit
  121. end
  122.  
  123. case @os
  124. when "winsrv2012r2"
  125. @ami_id = "ami-36f81820"
  126. when "winsrv2016docker"
  127. @ami_id = "ami-e7b755f1"
  128. end
  129.  
  130. wm.vm.box = "aws_blank"
  131. wm.vm.guest = "windows"
  132. wm.vm.communicator = "winrm"
  133.  
  134. wm.winrm.username = "Administrator"
  135. wm.winrm.password = "#{@pass}"
  136. wm.winrm.guest_port = "5986"
  137. wm.winrm.transport = "ssl"
  138. wm.winrm.ssl_peer_verification = false
  139.  
  140. wm.vm.provider "aws" do |aws, override|
  141. creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
  142. aws.access_key_id = creds.credentials.access_key_id
  143. aws.secret_access_key = creds.credentials.secret_access_key
  144.  
  145. template = ERB.new File.read(".vagrant_templates/windows_userdata.ps1.erb")
  146. aws.user_data = template.result()
  147.  
  148. aws.keypair_name = "#{@ec2_key}"
  149. aws.ami = "#{@ami_id}"
  150. aws.instance_type = "#{@instance_type}"
  151. aws.security_groups = [ "sg-#######" ]
  152. aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
  153. aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
  154. aws.associate_public_ip = true
  155.  
  156. aws.tags = {
  157. "Name" => "#{@name_tag}-win-minion"
  158. }
  159.  
  160. end
  161.  
  162. creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
  163. if @master_id
  164. client = Aws::EC2::Client.new(region: "us-east-1", credentials: creds)
  165. ec2 = Aws::EC2::Resource.new(client: client)
  166. instance = ec2.instance("#{@master_id}")
  167. @master_ip = "#{instance.private_ip_address}"
  168. end
  169.  
  170. template = ERB.new File.read(".vagrant_templates/windows_minion.ps1.erb")
  171. wm.vm.provision "shell", inline: template.result()
  172.  
  173. end
  174.  
  175. config.vm.define "linux_minion", autostart: false do |lm|
  176. if File.file?(".vagrant/machines/master/aws/id")
  177. @master_id = File.read(".vagrant/machines/master/aws/id")
  178. elsif File.file?(".vagrant/machines/linux_minion/aws/id")
  179. else
  180. puts "No master instance was found!"
  181. puts "Please create a master instance before creating minion instances."
  182. exit
  183. end
  184.  
  185. @os = ENV['VAGRANT_LM_OS']||"centos7"
  186. case @os
  187. when "centos7"
  188. @user = "centos"
  189. @ami_id = "ami-6d1c2007"
  190. end
  191.  
  192. lm.vm.box = "aws_blank"
  193.  
  194. lm.vm.provider "aws" do |aws, override|
  195. creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
  196. aws.access_key_id = creds.credentials.access_key_id
  197. aws.secret_access_key = creds.credentials.secret_access_key
  198.  
  199. template = ERB.new File.read(".vagrant_templates/linux_userdata.sh.erb")
  200. aws.user_data = template.result()
  201.  
  202. override.ssh.username = "centos"
  203. override.ssh.private_key_path = "#{@pem_key}"
  204.  
  205. aws.keypair_name = "#{@ec2_key}"
  206. aws.ami = "#{@ami_id}"
  207. aws.instance_type = "#{@instance_type}"
  208. aws.security_groups = [ "sg-#######" ]
  209. aws.subnet_id = [ "subnet-#######", "subnet-#######" ].sample
  210. aws.iam_instance_profile_arn = "arn:aws:iam::############:instance-profile/Vagrant"
  211. aws.associate_public_ip = true
  212.  
  213. aws.tags = {
  214. "Name" => "#{@name_tag}-linux-minion"
  215. }
  216. end
  217.  
  218. creds = Aws::SharedCredentials.new(:profile_name => "#{@profile}")
  219. if @master_id
  220. client = Aws::EC2::Client.new(region: "us-east-1", credentials: creds)
  221. ec2 = Aws::EC2::Resource.new(client: client)
  222. instance = ec2.instance("#{@master_id}")
  223. @master_ip = "#{instance.private_ip_address}"
  224. end
  225.  
  226. template = ERB.new File.read(".vagrant_templates/linux_minion.sh.erb")
  227. lm.vm.provision "shell", inline: template.result()
  228.  
  229. end
  230.  
  231. # explicitly disable nfs to avoid provisioning error
  232. config.vm.synced_folder ".", "/vagrant", type: "nfs", disabled: "true"
  233.  
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement