Skip to content
This repository was archived by the owner on Dec 20, 2023. It is now read-only.

Handle configuration validation and plugin installation in a Vagrant plugin #80

Merged
merged 7 commits into from
Jun 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions local.example.yml → .local.base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ fs:
magento2:
host: data/web/magento2
guest: /data/web/magento2
magento1:
host: data/web/public
guest: /data/web/public
nginx:
host: data/web/nginx/
guest: /data/web/nginx/
php:
version: 5.5
hostmanager:
extra-aliases:
- my-custom-store-url1.local
- my-custom-store-url2.local
magento:
version: 2
varnish:
enabled: false
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
language: ruby
cache: bundler
rvm:
- 1.9.3
- 2.0
- 2.1
- 2.2
- ruby-head
install: "gem install bundler rake rspec simplecov coveralls"
script: ./runtests.sh
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[![Build Status](https://travis-ci.org/ByteInternet/hypernode-vagrant.svg?branch=master)](https://travis-ci.org/ByteInternet/hypernode-vagrant)
[![Coverage Status](https://coveralls.io/repos/github/ByteInternet/hypernode-vagrant/badge.svg)](https://coveralls.io/github/ByteInternet/hypernode-vagrant)

# Hypernode test environment for MacOSX and Linux

You can start developing on your own local Hypernode within 15 minutes.
Expand Down
254 changes: 113 additions & 141 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,167 +6,139 @@ require 'fileutils'
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

# paths to local settings file
SETTINGS_FILE = "local.yml"
SETTINGS_EXAMPLES_FILE = "local.example.yml"

# source local config
unless File.exist?(SETTINGS_FILE)
FileUtils.cp(SETTINGS_EXAMPLES_FILE, SETTINGS_FILE)
end
settings = YAML.load_file SETTINGS_FILE

if settings['php'].nil? or settings['php']['version'].nil?
settings_php_version = 5.5
else
settings_php_version = settings['php']['version']
end
php_version = ENV['HYPERNODE_VAGRANT_PHP_VERSION'] ? ENV['HYPERNODE_VAGRANT_PHP_VERSION'] : settings_php_version

available_php_versions = [5.5, 7.0]
unless available_php_versions.include?(php_version)
abort "Configure an available php version in local.yml: #{available_php_versions.join(', ')}. You specified: #{php_version}"
end

if settings['magento'].nil? or settings['magento']['version'].nil?
settings_magento_version = 2
else
settings_magento_version = settings['magento']['version']
# if vagrant-hypconfigmgmt is not installed, install it and abort
if !Vagrant.has_plugin?("vagrant-hypconfigmgmt")
system("vagrant plugin install vagrant-hypconfigmgmt")
abort "Installed the vagrant-hypconfigmgmt.\nFor the next configuration step, please again run: \"vagrant up\""
end

if settings['varnish'].nil? or settings['varnish']['enabled'].nil?
settings_varnish_enabled = false
else
settings_varnish_enabled = settings['varnish']['enabled']
end

if !settings['magento'].nil? and !settings['magento']['version'].nil?
if settings['fs']['folders'].select{ |_, f| f['guest'].start_with?('/data/web/public') }.any? and settings['magento']['version'] == 2
abort "Can not configure a synced /data/web/public directory with Magento 2, this will be symlinked to /data/web/magento2!"
end
end

if !Vagrant.has_plugin?("vagrant-gatling-rsync") and settings['fs']['type'] == 'rsync'
puts "Tip: run 'vagrant plugin install vagrant-gatling-rsync' to speed up \
shared folder operations.\nYou can then sync with 'vagrant gatling-rsync-auto' \
instead of 'vagrant rsync-auto' to increase performance"
end

# abort if vagrant-hostmanager is not installed
if !Vagrant.has_plugin?("vagrant-hostmanager")
abort "Please install the 'vagrant-hostmanager' module"
end
# path to local settings file
SETTINGS_FILE = "local.yml"
# load the settingsfile or if it does not exist yet a hash where every attribute two levels deep is nil
settings = YAML.load_file(SETTINGS_FILE) rescue Hash.new(Hash.new(nil))

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# run hypernode-vagrant configuration wizard if needed during 'vagrant up'
config.hypconfigmgmt.enabled = true

config.ssh.forward_agent = true

if php_version == 7.0
config.vm.box = 'hypernode_php7'
config.vm.box_url = 'http://vagrant.hypernode.com/customer/php7/catalog.json'
else
config.vm.box = 'hypernode_php5'
config.vm.box_url = 'http://vagrant.hypernode.com/customer/php5/catalog.json'
end
begin
config.vm.box = settings['vagrant']['box'] ||= 'hypernode_php7'
config.vm.box_url = settings['vagrant']['box_url'] ||= 'http://vagrant.hypernode.com/customer/php7/catalog.json'

if !settings['fs']['folders'].nil?
settings['fs']['folders'].each do |name, folder|
if settings['fs']['type'] == 'nfs'
# configure all synced folder according to the filesystem type configured.
(settings['fs']['folders'] ||= []).each do |name, folder|
case settings['fs']['type']
when 'nfs'
config.vm.synced_folder folder['host'], folder['guest'], type: settings['fs']['type'], create: true
elsif settings['fs']['type'] == 'rsync'
when 'rsync'
config.vm.synced_folder folder['host'], folder['guest'], type: 'rsync', create: true, owner: "app", group: "app"
# Configure the window for gatling to coalesce writes.
if Vagrant.has_plugin?("vagrant-gatling-rsync")
config.gatling.latency = 2.5
config.gatling.time_format = "%H:%M:%S"
# Don't automatically sync when machines with rsync folders come up.
# Start syncing by running 'vagrant gatling-rsync-auto'
config.gatling.rsync_on_startup = false
end
else
else
config.vm.synced_folder folder['host'], folder['guest'], type: settings['fs']['type'], create: true, owner: "app", group: "app"
end
end
end

config.vm.provision "shell", path: "vagrant/provisioning/hypernode.sh", args: "-m #{settings_magento_version} -v #{settings_varnish_enabled}"
if settings['fs']['type'] == 'rsync'
# Configure the window for gatling to coalesce writes.
if Vagrant.has_plugin?("vagrant-gatling-rsync")
config.gatling.latency = 2.5
config.gatling.time_format = "%H:%M:%S"
# Don't automatically sync when machines with rsync folders come up.
# Start syncing by running 'vagrant gatling-rsync-auto'
config.gatling.rsync_on_startup = false
end
end

config.vm.provider :virtualbox do |vbox, override|
override.vm.network "private_network", type: "dhcp"
override.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
override.vm.network "forwarded_port", guest: 3306, host: 3307, auto_correct: true
vbox.memory = 2048
vbox.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
end
config.vm.provision "shell", path: "vagrant/provisioning/hypernode.sh", args: "-m #{settings['magento']['version']} -v #{settings['varnish']['enabled']}"

config.vm.provider :lxc do |lxc, override|
if settings['fs']['type'] == 'nfs'
# in case of lxc and nfs make sure the app user has the same uid and gid as the host
uid = `id -u`.strip()
gid = `id -g`.strip()
config.vm.provision "shell", path: "vagrant/provisioning/fix_uid_gid_for_lxc_nfs.sh", args: "-u #{uid} -g #{gid}"
end
lxc.customize 'cgroup.memory.limit_in_bytes', '2048M'
end
config.vm.provider :virtualbox do |vbox, override|
override.vm.network "private_network", type: "dhcp"
override.vm.network "forwarded_port", guest: 80, host: 8080, auto_correct: true
override.vm.network "forwarded_port", guest: 3306, host: 3307, auto_correct: true
vbox.memory = 2048
vbox.customize ["setextradata", :id, "VBoxInternal2/SharedFoldersEnableSymlinksCreate/v-root", "1"]
end

if Vagrant.has_plugin?("vagrant-hostmanager")
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true

# Get the dynamic hostname from the running box so we know what to put in
# /etc/hosts even though we don't specify a static private ip address
# For more information about why this is necessary see:
# https://github.com/smdahlen/vagrant-hostmanager/issues/86#issuecomment-183265949
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if vm.communicate.ready?
result = ""
vm.communicate.execute("ifconfig `find /sys/class/net -name 'eth*' -printf '%f\n' | tail -n 1`") do |type, data|
result << data if type == :stdout
config.vm.provider :lxc do |lxc, override|
if settings['fs']['type'] == 'nfs'
# in case of lxc and nfs make sure the app user has the same uid and gid as the host
uid = `id -u`.strip()
gid = `id -g`.strip()
config.vm.provision "shell", path: "vagrant/provisioning/fix_uid_gid_for_lxc_nfs.sh", args: "-u #{uid} -g #{gid}"
end
end
(ip = /inet addr:(\d+\.\d+\.\d+\.\d+)/.match(result)) && ip[1]
lxc.customize 'cgroup.memory.limit_in_bytes', '2048M'
end

config.vm.define 'hypernode' do |node|
# Name the vagrant box after the directory the Vagrantfile is in. If the Vagrantfile
# is in a directory named 'hypernode-vagrant' assume the name of the parent directory.
# This is so there is a human readable difference between multiple test environments.
working_directory = File.basename(Dir.getwd)
parent_directory = File.basename(File.expand_path("..", Dir.getwd))
directory_name = working_directory == "hypernode-vagrant" ? parent_directory : working_directory
hypernode_vagrant_name = ENV['HYPERNODE_VAGRANT_NAME'] ? ENV['HYPERNODE_VAGRANT_NAME'] : directory_name

# remove special characters so we have a valid hostname
hypernode_host = hypernode_vagrant_name.gsub(/[^a-zA-Z0-9]/, "")
hypernode_host = hypernode_host.empty? ? 'hypernode' : hypernode_host
hypernode_host = hypernode_host.split('-')[0]

directory_alias = hypernode_host + ".hypernode.local"

# The directory and parent directory don't have to be unique names. You
# could have this Vagrantfile in two subdirs each named 'mytestshop' and
# the directory aliases would be double. Because there can only be one
# Vagrantfile per directory, the path is always unique. We can create a
# unique alias (well at least semi-unique, there might be some
# collisions) with the hash of that path.
require 'digest/sha1'
hostname_hash = Digest::SHA1.hexdigest(Dir.pwd).slice(0..5)
directory_hash_alias = hostname_hash + ".hypernode.local"

# set the machine hostname
node.vm.hostname = hostname_hash + "-" + hypernode_host + "-magweb-vgr.nodes.hypernode.local"

# Here you can define your own aliases for in the hosts file.
# note: if you have more than one hypernode-vagrant checkout up and
# running, the static aliases will be defined for all of those boxes.
# This means that hypernode.local will belong to box you booted as last.
node.hostmanager.aliases = ["hypernode.local", "hypernode-alias", directory_alias, directory_hash_alias]

# Add user defined (local.yml) aliases to the hosts file.
if !settings['hostmanager'].nil? and !settings['hostmanager']['extra-aliases'].nil?
node.hostmanager.aliases += settings['hostmanager']['extra-aliases']
if Vagrant.has_plugin?("vagrant-hostmanager")
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = true

# Get the dynamic hostname from the running box so we know what to put in
# /etc/hosts even though we don't specify a static private ip address
# For more information about why this is necessary see:
# https://github.com/smdahlen/vagrant-hostmanager/issues/86#issuecomment-183265949
config.hostmanager.ip_resolver = proc do |vm, resolving_vm|
if vm.communicate.ready?
result = ""
vm.communicate.execute("ifconfig `find /sys/class/net -name 'eth*' -printf '%f\n' | tail -n 1`") do |type, data|
result << data if type == :stdout
end
end
(ip = /inet addr:(\d+\.\d+\.\d+\.\d+)/.match(result)) && ip[1]
end

config.vm.define 'hypernode' do |node|
# Name the vagrant box after the directory the Vagrantfile is in. If the Vagrantfile
# is in a directory named 'hypernode-vagrant' assume the name of the parent directory.
# This is so there is a human readable difference between multiple test environments.
working_directory = File.basename(Dir.getwd)
parent_directory = File.basename(File.expand_path("..", Dir.getwd))
directory_name = working_directory == "hypernode-vagrant" ? parent_directory : working_directory
hypernode_vagrant_name = ENV['HYPERNODE_VAGRANT_NAME'] ? ENV['HYPERNODE_VAGRANT_NAME'] : directory_name

# remove special characters so we have a valid hostname
hypernode_host = hypernode_vagrant_name.gsub(/[^a-zA-Z0-9]/, "")
hypernode_host = hypernode_host.empty? ? 'hypernode' : hypernode_host
hypernode_host = hypernode_host.split('-')[0]

directory_alias = hypernode_host + ".hypernode.local"

# The directory and parent directory don't have to be unique names. You
# could have this Vagrantfile in two subdirs each named 'mytestshop' and
# the directory aliases would be double. Because there can only be one
# Vagrantfile per directory, the path is always unique. We can create a
# unique alias (well at least semi-unique, there might be some
# collisions) with the hash of that path.
require 'digest/sha1'
hostname_hash = Digest::SHA1.hexdigest(Dir.pwd).slice(0..5)
directory_hash_alias = hostname_hash + ".hypernode.local"

# set the machine hostname
node.vm.hostname = hostname_hash + "-" + hypernode_host + "-magweb-vgr.nodes.hypernode.local"

# Here you can define your own aliases for in the hosts file.
# note: if you have more than one hypernode-vagrant checkout up and
# running, the static aliases will be defined for all of those boxes.
# This means that hypernode.local will belong to box you booted as last.
node.hostmanager.aliases = ["hypernode.local", "hypernode-alias", directory_alias, directory_hash_alias]

# Add user defined (local.yml) aliases to the hosts file.
node.hostmanager.aliases.concat(settings['hostmanager']['extra-aliases'] ||= [])
end
end
rescue NoMethodError
if File.exists?(SETTINGS_FILE) and !File.exists?("invalid_" + SETTINGS_FILE)
moved_file = "invalid_%s_#{SETTINGS_FILE}" % [rand(36 ** 8).to_s(36)]
FileUtils.mv(SETTINGS_FILE, moved_file)
abort(<<-HEREDOC
Looks like your configuration file was corrupt or not compatible with this version of hypernode-vagrant.
No worries, we've moved it out of the way to "#{moved_file}".
Run "vagrant up" again to interactively generate a new settings file.
HEREDOC
)
end
end
end
15 changes: 15 additions & 0 deletions runtests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

set -e

DIRNAME="$(dirname 0)"

# validate local.example.yml
echo -n "Checking base configuration file is valid YAML.. "
ruby -e "require 'yaml';YAML.load_file('$DIRNAME/.local.base.yml')" \
&& echo "\033[0;32mOK\033[0m" \
|| (echo "\033[0;31mlocal.example.yml is not valid yaml\033[0m" && /bin/false)

echo "Running vagrant-hypconfigmgmt tests.."
# run vagrant-hypconfigmgmt tests
(cd "$DIRNAME/vagrant/plugins/vagrant-hypconfigmgmt/" && make test)
3 changes: 3 additions & 0 deletions vagrant/plugins/vagrant-hypconfigmgmt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.coverage/
Gemfile.lock
coverage/
1 change: 1 addition & 0 deletions vagrant/plugins/vagrant-hypconfigmgmt/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
4 changes: 4 additions & 0 deletions vagrant/plugins/vagrant-hypconfigmgmt/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in vagrant-hypconfigmgmt.gemspec
gemspec
22 changes: 22 additions & 0 deletions vagrant/plugins/vagrant-hypconfigmgmt/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2016 Rick van de Loo

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions vagrant/plugins/vagrant-hypconfigmgmt/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: all clean

NAME := vagrant-hypconfigmgmt
VERSION := 0.1
MAINTAINER := Rick van de Loo <rick@byte.nl>
DESCRIPTION := Prompt to configure a hypernode-vagrant

all:
rake build
test:
bundle exec rspec spec/
install:
find pkg/ -name '*.gem' | head -n 1 | xargs vagrant plugin install
clean:
git clean -xfd

Loading