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

Commit 3a95c28

Browse files
committed
bundle vagrant-hypconfigmgmt gem with hypernode-vagrant
The basic hypernode-vagrant box can now be configured and booted by cloning the repo and running "vagrant up" three times and following the dialogue. Vagrant needs to be started three consecutive times because it loads all configurations during initialization, not during runtime. When we change the configuration in the Vagrant process we need to restart Vagrant for the changes to take effect. 1. Install the vagrant-hypconfigmgmt plugin 2. Configure the local.yml and install other required plugins 3. Boot the box When the box is properly configured and all the required plugins is installed one vagrant up is sufficient.
1 parent e810e06 commit 3a95c28

13 files changed

+372
-2
lines changed

Vagrantfile

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ VAGRANTFILE_API_VERSION = "2"
88

99
# abort if vagrant-hypconfigmgmt is not installed
1010
if !Vagrant.has_plugin?("vagrant-hypconfigmgmt")
11-
abort "Please install the 'vagrant-hypconfigmgmt' module.\nRun: 'vagrant plugin install vagrant-hypconfigmgmt' first then try again."
11+
hypernode_vagrant_config_management = Dir.glob('vagrant/plugins/vagrant-hypconfigmgmt/pkg/*.gem').last
12+
system("vagrant plugin install #{hypernode_vagrant_config_management}")
13+
abort "Installed the vagrant-hypconfigmgmt.\nFor the next configuration step, please again run: \"vagrant up\""
1214
end
1315

1416
# paths to local settings file
1517
SETTINGS_FILE = "local.yml"
1618
SETTINGS_EXAMPLES_FILE = "local.example.yml"
1719

18-
# source local config
20+
# copy base settings
1921
unless File.exist?(SETTINGS_FILE)
2022
FileUtils.cp(SETTINGS_EXAMPLES_FILE, SETTINGS_FILE)
2123
end
@@ -32,6 +34,9 @@ instead of 'vagrant rsync-auto' to increase performance"
3234
end
3335

3436
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
37+
# run hypernode-vagrant configuration wizard if needed during 'vagrant up'
38+
config.hypconfigmgmt.enabled = true
39+
3540
config.ssh.forward_agent = true
3641

3742
if php_version == 7.0

vagrant/plugins/vagrant-hypconfigmgmt/.gitignore

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in vagrant-hypconfigmgmt.gemspec
4+
gemspec
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2016 Rick van de Loo
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY: all clean
2+
3+
NAME := vagrant-hypconfigmgmt
4+
VERSION := 0.1
5+
MAINTAINER := Rick van de Loo <rick@byte.nl>
6+
DESCRIPTION := Prompt to configure a hypernode-vagrant
7+
8+
all:
9+
rake build
10+
install:
11+
find pkg/ | tail -n 1 | xargs vagrant plugin install
12+
clean:
13+
git clean -xfd
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Install the build deps
2+
======================
3+
4+
```
5+
sudo apt-get install bundler
6+
sudo apt-get install rake
7+
```
8+
9+
Create the gemfile (package)
10+
============================
11+
12+
```
13+
$ make
14+
rake build
15+
vagrant-hypconfigmgmt 0.0.1 built to pkg/vagrant-hypconfigmgmt-0.0.1.gem.
16+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require "bundler/gem_tasks"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# -*- encoding: utf-8 -*-
2+
# vim: set fileencoding=utf-8
3+
4+
require "vagrant"
5+
require "vagrant-hypconfigmgmt/command"
6+
7+
module VagrantHypconfigmgmt
8+
class Plugin < Vagrant.plugin("2")
9+
name "hypconfigmgmt"
10+
description <<-DESC
11+
Configure the hypernode-vagrant during runtime
12+
DESC
13+
14+
config 'hypconfigmgmt' do
15+
require File.expand_path("../vagrant-hypconfigmgmt/config", __FILE__)
16+
Config
17+
end
18+
19+
action_hook(:VagrantHypconfigmgmt, :machine_action_up) do |hook|
20+
hook.prepend(VagrantHypconfigmgmt::Command)
21+
end
22+
end
23+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# coding: utf-8
2+
# vim: set fileencoding=utf-8
3+
4+
DEFAULT_MAGENTO_VERSION = 2
5+
AVAILABLE_MAGENTO_VERSIONS = [1, 2]
6+
7+
DEFAULT_PHP_VERSION = 7.0
8+
AVAILABLE_PHP_VERSIONS = [5.5, 7.0]
9+
10+
DEFAULT_VARNISH_STATE = false
11+
AVAILABLE_VARNISH_STATES = [true, false]
12+
13+
# paths to local settings file
14+
H_V_SETTINGS_FILE = "local.yml"
15+
16+
RECOMMENDED_PLUGINS = ["vagrant-hostmanager", "vagrant-vbguest"]
17+
18+
19+
def retrieve_settings()
20+
return YAML.load_file(H_V_SETTINGS_FILE)
21+
end
22+
23+
24+
def update_settings(settings)
25+
File.open(H_V_SETTINGS_FILE, 'w') {|f| f.write settings.to_yaml }
26+
end
27+
28+
29+
def use_default_if_input_empty(input, default)
30+
if input == ''
31+
return default.to_s
32+
else
33+
return input
34+
end
35+
end
36+
37+
38+
def get_varnish_state(env)
39+
input = env[:ui].ask("Do you want to enable Varnish? Enter true or false [default false]: ")
40+
varnish_state = use_default_if_input_empty(input, DEFAULT_VARNISH_STATE)
41+
42+
case varnish_state
43+
when "true"
44+
env[:ui].info("Varnish will be enabled.")
45+
when "false"
46+
env[:ui].info("Varnish will be disabled by loading a nocache vcl.")
47+
else
48+
env[:ui].error("The value #{varnish_state} is not a valid value. Please enter true or false")
49+
return get_varnish_state(env)
50+
end
51+
return varnish_state == "true" ? true : false
52+
end
53+
54+
55+
def get_magento_version(env)
56+
available_versions = AVAILABLE_MAGENTO_VERSIONS.join(' or ')
57+
input = env[:ui].ask("Is this a Magento #{available_versions} Hypernode? [default #{DEFAULT_MAGENTO_VERSION}]: ")
58+
magento_version = use_default_if_input_empty(input, DEFAULT_MAGENTO_VERSION)
59+
60+
case magento_version
61+
when "1"
62+
env[:ui].info("Nginx will be configured for Magento 1. The webdir will be /data/web/public")
63+
when "2"
64+
env[:ui].info("Nginx will be configured for Magento 2. /data/web/magento2/pub will be symlinked to /data/web/public")
65+
else
66+
env[:ui].error("The value #{magento_version} is not a valid Magento version. Please enter #{available_versions}")
67+
return get_magento_version(env)
68+
end
69+
return magento_version.to_i
70+
end
71+
72+
73+
# todo: refactor this and the above function into one
74+
def get_php_version(env)
75+
available_versions = AVAILABLE_PHP_VERSIONS.join(' or ')
76+
input = env[:ui].ask("Is this a PHP #{available_versions} Hypernode? [default #{DEFAULT_PHP_VERSION}]: ")
77+
php_version = use_default_if_input_empty(input, DEFAULT_PHP_VERSION)
78+
79+
case php_version
80+
when "5.5"
81+
env[:ui].info("Will boot a box with PHP 5.5 installed")
82+
when "7.0"
83+
env[:ui].info("Will boot a box with PHP 7.0 installed")
84+
else
85+
env[:ui].error("The value #{php_version} is not a valid PHP version. Please enter #{available_versions}")
86+
return get_php_version(env)
87+
end
88+
return php_version.to_f
89+
end
90+
91+
92+
def ensure_varnish_state_configured(env)
93+
settings = retrieve_settings()
94+
if settings['varnish']['enabled'].nil?
95+
settings['varnish']['enabled'] = get_varnish_state(env)
96+
elsif ![true, false].include?(settings['varnish']['enabled'])
97+
env[:ui].error("The Varnish state configured in local.yml is invalid.")
98+
settings['varnish']['enabled'] = get_varnish_state(env)
99+
end
100+
update_settings(settings)
101+
end
102+
103+
104+
def ensure_magento_version_configured(env)
105+
settings = retrieve_settings()
106+
if settings['magento']['version'].nil?
107+
settings['magento']['version'] = get_magento_version(env)
108+
elsif !AVAILABLE_MAGENTO_VERSIONS.include?(settings['magento']['version'].to_i)
109+
env[:ui].error("The Magento version configured in local.yml is invalid.")
110+
settings['magento']['version'] = get_magento_version(env)
111+
end
112+
update_settings(settings)
113+
end
114+
115+
116+
# Make sure we don't link /data/web/public on Magento 2 Vagrants
117+
# because that dir will be a symlink to /data/web/magento2/pub and
118+
# we mount that. On Magento 1 Vagrants we need to make sure we don't
119+
# mount /data/web/magento2/pub.
120+
def ensure_magento_mounts_configured(env)
121+
settings = retrieve_settings()
122+
if !settings['fs'].nil? and !settings['fs']['folders'].nil?
123+
if settings['fs']['disabled_folders'].nil?
124+
settings['fs']['disabled_folders'] = Hash.new
125+
end
126+
if settings['magento']['version'] == 1
127+
if !settings['fs']['disabled_folders']['magento1'].nil?
128+
settings['fs']['folders']['magento1'] = settings['fs']['disabled_folders']['magento1'].clone
129+
settings['fs']['disabled_folders'].delete('magento1')
130+
env[:ui].info("Re-enabling fs->disabled_folders->magento1 in the local.yml.")
131+
end
132+
if !settings['fs']['folders']['magento2'].nil?
133+
settings['fs']['disabled_folders']['magento2'] = settings['fs']['folders']['magento2'].clone
134+
settings['fs']['folders'].delete('magento2')
135+
env[:ui].info("Disabling fs->folders->magento2 in the local.yml because Magento 1 was configured.")
136+
end
137+
elsif settings['magento']['version'] == 2
138+
if !settings['fs']['disabled_folders']['magento2'].nil?
139+
settings['fs']['folders']['magento2'] = settings['fs']['disabled_folders']['magento2'].clone
140+
settings['fs']['disabled_folders'].delete('magento2')
141+
env[:ui].info("Re-enabling fs->disabled_folders->magento2 in the local.yml.")
142+
end
143+
if !settings['fs']['folders']['magento1'].nil?
144+
settings['fs']['disabled_folders']['magento1'] = settings['fs']['folders']['magento1'].clone
145+
settings['fs']['folders'].delete('magento1')
146+
env[:ui].info("Disabling fs->folders->magento1 in the local.yml because Magento 2 was configured..")
147+
end
148+
end
149+
if settings['fs']['disabled_folders'] == Hash.new
150+
settings['fs'].delete('disabled_folders')
151+
end
152+
end
153+
update_settings(settings)
154+
end
155+
156+
157+
# todo: refactor this and the above function into one
158+
def ensure_php_version_configured(env)
159+
settings = retrieve_settings()
160+
if settings['php']['version'].nil?
161+
settings['php']['version'] = get_php_version(env)
162+
elsif !AVAILABLE_PHP_VERSIONS.include?(settings['php']['version'].to_f)
163+
env[:ui].error("The PHP version configured in local.yml is invalid.")
164+
settings['php']['version'] = get_php_version(env)
165+
end
166+
update_settings(settings)
167+
end
168+
169+
170+
def ensure_setting_exists(name)
171+
settings = retrieve_settings()
172+
if settings[name].nil?
173+
settings[name] = Hash.new
174+
end
175+
update_settings(settings)
176+
end
177+
178+
179+
def validate_magento2_root(env)
180+
settings = retrieve_settings()
181+
if !settings['fs'].nil? and !settings['fs']['folders'].nil?
182+
if settings['fs']['folders'].select{ |_, f| f['guest'].start_with?('/data/web/public') }.any? && settings['magento']['version'] == 2
183+
env[:ui].info("Can not configure a synced /data/web/public directory with Magento 2, this will be symlinked to /data/web/magento2!")
184+
env[:ui].error("Please remove all fs->folders->*->guest paths that start with /data/web/public from your local.yml. Use /data/web/magento2 instead.")
185+
end
186+
end
187+
end
188+
189+
190+
def ensure_settings_configured(env)
191+
old_settings = retrieve_settings()
192+
ensure_setting_exists('magento')
193+
ensure_magento_version_configured(env)
194+
ensure_setting_exists('php')
195+
ensure_php_version_configured(env)
196+
ensure_setting_exists('varnish')
197+
ensure_varnish_state_configured(env)
198+
ensure_magento_mounts_configured(env)
199+
validate_magento2_root(env)
200+
new_settings = retrieve_settings()
201+
return new_settings.to_yaml != old_settings.to_yaml
202+
end
203+
204+
205+
def ensure_required_plugins_are_installed(env)
206+
RECOMMENDED_PLUGINS.each do |plugin|
207+
unless Vagrant.has_plugin?(plugin)
208+
env[:ui].info("Installing the #{plugin} plugin.")
209+
system("vagrant plugin install #{plugin}")
210+
end
211+
end
212+
end
213+
214+
215+
module VagrantHypconfigmgmt
216+
class Command
217+
218+
def initialize(app, env)
219+
@app = app
220+
@env = env
221+
end
222+
223+
def call(env)
224+
if env[:machine].config.hypconfigmgmt.enabled
225+
changed = ensure_settings_configured(env)
226+
ensure_required_plugins_are_installed(env)
227+
if changed
228+
env[:ui].info("Your hypernode-vagrant is now configured. Please run \"vagrant up\" again.")
229+
return
230+
end
231+
end
232+
@app.call(env)
233+
end
234+
end
235+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# -*- encoding: utf-8 -*-
2+
# vim: set fileencoding=utf-8
3+
4+
require 'vagrant'
5+
6+
module VagrantHypconfigmgmt
7+
class Config < Vagrant.plugin("2", :config)
8+
attr_accessor :enabled
9+
10+
def initialize
11+
super
12+
# UNSET_VALUE so that Vagrant can properly automatically merge multiple configurations.
13+
# https://www.vagrantup.com/docs/plugins/configuration.html
14+
@enabled = UNSET_VALUE
15+
end
16+
17+
def finalize!
18+
@enabled = (@enabled != UNSET_VALUE) && (@enabled != false)
19+
end
20+
end
21+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -*- encoding: utf-8 -*-
2+
# vim: set fileencoding=utf-8
3+
4+
module Vagrant
5+
module Hypconfigmgmt
6+
VERSION = "0.0.1"
7+
end
8+
end
Binary file not shown.

0 commit comments

Comments
 (0)