Skip to content

Instantly share code, notes, and snippets.

@denisemauldin
Created May 25, 2017 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denisemauldin/ce0ca4829bcb56666037c61bcb2f9582 to your computer and use it in GitHub Desktop.
Save denisemauldin/ce0ca4829bcb56666037c61bcb2f9582 to your computer and use it in GitHub Desktop.
Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
#
# Expects either a setup.yml file or a command line parameter for the github_ssh_key
require 'getoptlong'
def provisioned?(vm_name='default', provider='virtualbox')
File.exist?(".vagrant/machines/#{vm_name}/#{provider}/action_provision")
end
# use vagrant --ansible_tags <tags> up|provision|etc to run only particular ansible tags
opts = GetoptLong.new(
['--help', GetoptLong::NO_ARGUMENT],
['--github_ssh_key', GetoptLong::OPTIONAL_ARGUMENT],
['--bitbucket_ssh_key', GetoptLong::OPTIONAL_ARGUMENT],
['--setup_yaml', GetoptLong::OPTIONAL_ARGUMENT],
['--ansible_tags', GetoptLong::OPTIONAL_ARGUMENT],
['--ansible_skip_tags', GetoptLong::OPTIONAL_ARGUMENT],
['--ansible_playbook', GetoptLong::OPTIONAL_ARGUMENT],
['--git_user_name', GetoptLong::OPTIONAL_ARGUMENT],
['--git_user_email', GetoptLong::OPTIONAL_ARGUMENT],
)
defaults = Hash.new
defaults['setup_file'] = 'setup.yml'
defaults['github_ssh_key'] = '~/.ssh/id_rsa'
defaults['bitbucket_ssh_key'] = '~/.ssh/id_rsa_bitbucket'
defaults['ansible_playbook'] = 'dev.yml'
descriptions = {
'--github_ssh_key': "Provide a pathname to the github ssh key to use to check out the git repository",
'--bitbucket_ssh_key': "Provide a pathname to the bitbucket ssh key to use to check out the git repository",
'--setup_yaml': "A YAML file that contains information for any of the custom ansible parameters",
'--ansible_tags': "Passed to --tags in ansible playbook. Can be used to filter tasks run to only this set of tags",
'--ansible_skip_tags': "Passed to --skip_tags in ansible playbook. Can be used to filter tasks to remove this set of tags",
'--ansible_playbook': "Specify the ansible playbook to use. Defaults to lims-dev.yml",
'--git_user_name': "Used by the configure_git task in lims_roles to set git user.name",
'--git_user_email': "Used by the configure_git task in lims_roles to set git user.email"
}
extra_vars = {
ansible_pkg_mgr: "yum",
}
args = Hash.new
opts.each do |opt, arg|
case opt
when '--help'
puts "Custom ansible parameters:"
puts "Usage: vagrant <key>=<value> up|provision\n\n"
descriptions.each do |dkey, desc|
puts " #{dkey} #{desc}"
end
puts "\n"
when '--setup_yaml'
args['setup_file'] = arg
when '--github_ssh_key'
args['github_ssh_key'] = arg
when '--bitbucket_ssh_key'
args['bitbucket_ssh_key'] = arg
when '--ansible_tags'
args['ansible_tags'] = arg
when '--ansible_skip_tags'
args['ansible_skip_tags'] = arg
when '--ansible_playbook'
args['ansible_playbook'] = arg
when '--git_user_name'
extra_vars['git_user_name'] = arg
when '--git_user_email'
extra_vars['git_user_email'] = arg
end
end
# we want defaults to be overridden by file_opts and args
# we want file_opts to be overridden by args
# but we need to check for setup_file arg before we load file_opts
def load_file(file)
file_opts = Hash.new
if File.exist? File.expand_path(file)
file_opts=YAML.load_file(file)
else
warn "YAML file #{file} does not exist. Using defaults."
end
return file_opts
end
if args['setup_file']
file_opts = load_file(args['setup_file'])
else
file_opts = load_file(defaults['setup_file'])
end
options = defaults
if !file_opts.nil? and !file_opts.empty?
options = defaults.merge(file_opts)
end
options = options.merge(args)
if (!provisioned? or ARGV[0] == 'provision') then
abort "Must provide github_ssh_key either in setup.yml or via command line with: vagrant --github_ssh_key=<password> <up|provision>" unless options['github_ssh_key']
abort "Must provide bitbucket_ssh_key either in setup.yml or via command line with: vagrant --bitbucket_ssh_key=<password> <up|provision>" unless options['bitbucket_ssh_key']
end
# extra vars is in the setup.yml file and is a yaml list
if options["extra_vars"] then
options["extra_vars"].each do |var|
extra_vars = extra_vars.merge(var)
end
end
if (options['ansible_playbook']) then
if ! File.exist?(options['ansible_playbook']) then
abort "Ansible playbook #{options['ansible_playbook']} does not exist"
end
else
abort "No ansible_playbook option provided"
end
Vagrant.configure(2) do |config|
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://atlas.hashicorp.com/search.
config.vm.box = "virtualboxes/centos7-minimal-20170104.box"
# Upload SSH Key used to connect to GitHub and Bitbucket
config.vm.provision "file", source: options['github_ssh_key'], destination: ".ssh/id_rsa_github"
config.vm.provision "file", source: options['bitbucket_ssh_key'], destination: ".ssh/id_rsa_bitbucket"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network "forwarded_port", guest: 8000, host: 6081
config.vm.network "forwarded_port", guest: 80, host: 6082
config.vm.network "forwarded_port", guest: 443, host: 6443
# The vm will boot the first time with auto_update set to false
# but it needs it set to true every time afterwards.
config.vbguest.auto_update = true
config.vm.provider 'virtualbox' do |vb|
vb.memory = 2048
vb.customize [ "guestproperty", "set", :id, "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold", 1000 ]
end
config.vm.provision "ansible" do |ansible|
# if you need a lot of information about the background commands ansible is running, uncomment verbose
#ansible.verbose = "vvvv"
ansible.playbook = options['ansible_playbook']
ansible.extra_vars = extra_vars
ansible.tags=options['ansible_tags']
ansible.skip_tags=options['ansible_skip_tags']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment