Skip to content
This repository has been archived by the owner on Aug 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #85 from Decipher/features/box-vagrantfile
Browse files Browse the repository at this point in the history
#74: Supply a default Vagrantfile with the box.
  • Loading branch information
Decipher committed Jan 31, 2016
2 parents 500361c + f3ba228 commit 1a38834
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ require 'yaml'

Vagrant.require_version '>= 1.8.0'

dir = File.dirname(File.expand_path(__FILE__))
cwd = File.dirname(File.expand_path(__FILE__))
config_dir = '.beetbox/'
vagrant_config = "#{dir}/#{config_dir}config.yml"
local_config = "#{dir}/#{config_dir}local.config.yml"
vagrant_config = "#{cwd}/#{config_dir}config.yml"
local_config = "#{cwd}/#{config_dir}local.config.yml"

# Default vagrant config.
vconfig = {
Expand All @@ -17,7 +17,7 @@ vconfig = {
'vagrant_cpus' => 2,
'beet_home' => '/beetbox',
'beet_base' => '/var/beetbox',
'beet_domain' => 'beetbox.local'
'beet_domain' => cwd.split('/').last.gsub(/[\._]/, '-') + ".local"
}

if !File.exist?(vagrant_config)
Expand Down
174 changes: 174 additions & 0 deletions packer/includes/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.require_version '>= 1.8.0'

# Find the Vagrant ID if supplied.
id = nil
ARGV.each do |arg|
id = arg[/\h{7}/] ? arg : id
end

# Determine the current working directory and vagrantfile.
cwd = nil
vagrantfile = ENV['VAGRANT_VAGRANTFILE'] || 'Vagrantfile'
if id != nil
require 'json'
vagrant_home = ENV['VAGRANT_HOME'] || "#{Dir.home}/.vagrant.d"
JSON.parse(File.read("#{vagrant_home}/data/machine-index/index"))['machines'].each do |hash, data|
if hash[0..6] == id
cwd = data['vagrantfile_path']
vagrantfile = data['vagrantfile_name'] != nil ? data['vagrantfile_name'] : vagrantfile
break
end
end
else
paths = Dir.pwd.split('/')
while cwd == nil && !paths.empty?
dir = paths.join('/')
current = paths.pop
if File.exists?("#{dir}/#{vagrantfile}")
cwd = dir
end
end
end

# Ensure project Vagrantfile hasn't been customized.
# @TODO - Add secondary check that parses the Vagrantfile if the MD5 is changed.
if cwd != nil && Digest::MD5.file("#{cwd}/#{vagrantfile}").hexdigest == "abef0f6a03cc23bb3da842cd1d12aa50"
config_dir = '.beetbox/'
vagrant_config = "#{cwd}/#{config_dir}config.yml"
local_config = "#{cwd}/#{config_dir}local.config.yml"

# Default vagrant config.
vconfig = {
'vagrant_ip' => '0.0.0.0',
'vagrant_memory' => 1024,
'vagrant_cpus' => 2,
'beet_home' => '/beetbox',
'beet_base' => '/var/beetbox',
'beet_domain' => cwd.split('/').last.gsub(/[\._]/, '-') + ".local"
}

if !File.exist?(vagrant_config)
# Create default config file.
require 'fileutils'
FileUtils::mkdir_p config_dir
File.open(vagrant_config, "w+") {|f| f.write("---\nbeet_domain: #{vconfig['beet_domain']}\n") }
end

require 'yaml'
vconfig = vconfig.merge YAML::load_file(vagrant_config)

# Merge local.config.yml
if File.exist?(local_config)
lconfig = YAML::load_file(local_config)
vconfig = vconfig.merge lconfig
end

# Replace variables in YAML config.
vconfig.each do |key, value|
while vconfig[key].is_a?(String) && vconfig[key].match(/{{ .* }}/)
vconfig[key] = vconfig[key].gsub(/{{ (.*?) }}/) { |match| match = vconfig[$1] }
end
end

hostname = vconfig['beet_domain']

Vagrant.configure("2") do |config|

# Check for plugins and attempt to install if not (Windows only).
if vconfig['vagrant_ip'] == "0.0.0.0" && Vagrant::Util::Platform.windows?
# Check for plugins and attempt to install if not.
%x(vagrant plugin install vagrant-hostsupdater) unless Vagrant.has_plugin?('vagrant-hostsupdater')
%x(vagrant plugin install vagrant-auto_network) unless Vagrant.has_plugin?('vagrant-auto_network')
raise 'Your config requires hostsupdater plugin.' unless Vagrant.has_plugin?('vagrant-hostsupdater')
raise 'Your config requires auto_network plugin.' unless Vagrant.has_plugin?('vagrant-auto_network')
end

config.vm.box = "DrupalMel/beetbox"
config.vm.box_version = ">= 0.1.18"
config.vm.hostname = hostname
config.ssh.insert_key = false
config.ssh.forward_agent = true

# Network config.
if vconfig['vagrant_ip'] == "0.0.0.0" && Vagrant::Util::Platform.windows?
config.vm.network :private_network, :ip => "0.0.0.0", :auto_network => true
elsif vconfig['vagrant_ip'] == "0.0.0.0"
config.vm.network :private_network, :type => "dhcp"
else
config.vm.network :private_network, ip: vconfig['vagrant_ip']
end

# Synced folders.
config.vm.synced_folder ".", vconfig['beet_base'],
type: "nfs",
id: "drupal"

if vconfig['beet_debug']
config.vm.synced_folder "./ansible", "#{vconfig['beet_home']}/ansible",
type: "nfs",
id: "ansible"
debug_mode = "BEETBOX_DEBUG=true"
end

# Upload vagrant.config.yml
config.vm.provision "vagrant_config", type: "file" do |s|
s.source = vagrant_config
s.destination = "#{vconfig['beet_home']}/ansible/vagrant.config.yml"
end

# Upload local.config.yml
if File.exist?(local_config)
config.vm.provision "local_config", type: "file" do |s|
s.source = local_config
s.destination = "#{vconfig['beet_home']}/ansible/local.config.yml"
end
end

# Provision box
config.vm.provision "ansible", type: "shell" do |s|
s.privileged = true
s.inline = "chmod +x #{vconfig['beet_home']}/ansible/build.sh && #{debug_mode} #{vconfig['beet_home']}/ansible/build.sh"
end

# VirtualBox.
config.vm.provider :virtualbox do |v|
v.name = "#{config.vm.hostname}.#{Time.now.to_i}"
v.memory = vconfig['vagrant_memory']
v.cpus = vconfig['vagrant_cpus']
v.linked_clone = true
v.customize ["modifyvm", :id, "--natdnshostresolver1", "on"]
v.customize ["modifyvm", :id, "--ioapic", "on"]
end
end

# Create local drush alias.
if File.directory?("#{Dir.home}/.drush")
alias_file = "#{Dir.home}/.drush/" + hostname + ".aliases.drushrc.php"
if ARGV[0] == "destroy"
File.delete(alias_file) if File.exist?(alias_file)
else
require 'erb'
class DrushAlias
attr_accessor :hostname, :uri, :key, :root
def template_binding
binding
end
end

template_file = File.dirname(File.expand_path(__FILE__)) + '/drush.alias.erb'
template = File.read(template_file)

alias_file = File.open(alias_file, "w+")
da = DrushAlias.new
da.hostname = hostname
da.uri = hostname
da.key = "#{Dir.home}/.vagrant.d/insecure_private_key"
da.root = vconfig['beet_web'] ||= vconfig['beet_root'] ||= vconfig['beet_base']
alias_file << ERB.new(template).result(da.template_binding)
alias_file.close
end
end
end
9 changes: 9 additions & 0 deletions packer/includes/drush.alias.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

$aliases['<%= @hostname %>'] = array(
'uri' => '<%= @uri %>',
'remote-host' => '<%= @uri %>',
'remote-user' => 'vagrant',
'ssh-options' => '-i <%= @key %> -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no',
'root' => '<%= @root %>',
);
4 changes: 4 additions & 0 deletions packer/template-12.04.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
{
"type": "vagrant",
"compression_level": 9,
"vagrantfile_template": "includes/Vagrantfile",
"include": [
"includes/drush.alias.erb"
],
"output": "{{user `atlas_username`}}-{{user `atlas_name`}}-{{.Provider}}.box"
},
{
Expand Down
4 changes: 4 additions & 0 deletions packer/template-14.04.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
{
"type": "vagrant",
"compression_level": 9,
"vagrantfile_template": "includes/Vagrantfile",
"include": [
"includes/drush.alias.erb"
],
"output": "{{user `atlas_username`}}-{{user `atlas_name`}}-{{.Provider}}.box"
},
{
Expand Down
4 changes: 4 additions & 0 deletions packer/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
{
"type": "vagrant",
"compression_level": 9,
"vagrantfile_template": "includes/Vagrantfile",
"include": [
"includes/drush.alias.erb"
],
"output": "{{user `atlas_username`}}-{{user `atlas_name`}}-{{.Provider}}.box"
},
{
Expand Down

0 comments on commit 1a38834

Please sign in to comment.