Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit f6b6b50

Browse files
committed
Initial vagrant box version
1 parent c168c08 commit f6b6b50

File tree

16 files changed

+435
-0
lines changed

16 files changed

+435
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/.idea/
2+
/.vagrant/
3+
/config.rb
4+

Vagrantfile

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
%w(vagrant-hostmanager vagrant-auto_network vagrant-nfs_guest).each do |plugin|
2+
unless Vagrant.has_plugin?(plugin)
3+
raise 'In order to use this box, you must install plugin: ' + plugin
4+
end
5+
end
6+
7+
require_relative 'vagrant/inline/config'
8+
require_relative 'vagrant/inline/nfs-plugin'
9+
10+
# Define Vagrantfile configuration options
11+
VagrantApp::Config.option(:varnish, false) # If varnish needs to be enabled
12+
.option(:profiler, false) # Is profiler needs to be installed
13+
.option(:developer, false) # Is developer mode should be enabled
14+
.option(:magento2, false) # Is it Magento 2.0
15+
.option(:install, false) # Install Magento? (for now only 2.0)
16+
.option(:shell, false) # Shell script?
17+
.option(:php7, false) # Is it PHP7?
18+
.option(:name, '') # Name
19+
.option(:hostname, '') # Hostname
20+
.option(:domains, []) # Domain list
21+
.option(:cpu, 1) # Number of dedicated CPU
22+
.option(:memory, 1024) # Number of dedicated memory in MB
23+
.option(:user, 'app') # User name for share
24+
.option(:group, 'app') # Group name for share
25+
.option(:uid, Process.euid) # User ID for mapping
26+
.option(:gid, Process.egid) # Group ID for mapping
27+
.option(:directory, 'server') # Directory to be used as mount on host machine
28+
.option(:network, '33.33.33.0/24') # Directory to be used as mount on host machine
29+
30+
Vagrant.configure("2") do |config|
31+
32+
# Prepare configuration and setup shell scripts for it
33+
current_file = Pathname.new(__FILE__)
34+
box_config = VagrantApp::Config.new
35+
36+
# Base hypernode provisioner
37+
box_config.shell_add('hypernode.sh')
38+
.shell_add('composer.sh') # Composer installer
39+
.shell_add('nfs.sh') # NFS server modifications to have proper permissions
40+
.shell_add('developer.sh', :developer) # Developer mode setting, depends on :developer configuration flag
41+
.shell_add('profiler.sh', :profiler) # Profiler installer, depends on :profiler configuration flag
42+
.shell_add('disable-varnish.sh', :varnish, true) # Varnish disabler, depends on :varnish inverted flag
43+
.shell_add('magento2.sh', :magento2) # M2 Nginx Config Flag, depends on :magento2 flag
44+
.shell_add('magento2-install.sh', [:magento2, :install]) # M2 Installer, depends on :magento2 and :install
45+
.shell_add('magento2-developer.sh', [:magento2, :install, :developer]) # M2 Developer options, depends on :magento2, :install, :developer
46+
.shell_add('shell.sh', :shell) # Fish shell installer, depends on :shell flag
47+
.shell_add('hello.sh') # Final message with connection instructions
48+
49+
# Loads config.rb from the same directory where Vagrantfile is in
50+
box_config.load(File.join(current_file.dirname, 'config.rb.dst'))
51+
box_config.load(File.join(current_file.dirname, 'config.rb'))
52+
53+
AutoNetwork.default_pool = box_config.get(:network)
54+
55+
if box_config.flag?(:php7)
56+
config.vm.box = 'hypernode_php7'
57+
config.vm.box_url = 'http://vagrant.hypernode.com/customer/php7/catalog.json'
58+
else
59+
config.vm.box = 'hypernode_php5'
60+
config.vm.box_url = 'http://vagrant.hypernode.com/customer/php5/catalog.json'
61+
end
62+
63+
config.ssh.forward_agent = true
64+
65+
config.vm.provider :virtualbox do |v, o|
66+
v.memory = box_config.get(:memory)
67+
v.cpus = box_config.get(:cpu)
68+
end
69+
70+
config.vm.provider :lxc do |lxc|
71+
lxc.customize 'cgroup.memory.limit_in_bytes', box_config.get(:memory).to_s + 'M'
72+
end
73+
74+
# Disable default /vagrant mount as we use custom user for box
75+
config.vm.synced_folder '.', '/vagrant/', disabled: true
76+
77+
config.vm.synced_folder box_config.get(:directory), '/data/web', type: 'nfs_guest', create: true,
78+
linux__nfs_options: %w(rw no_subtree_check all_squash insecure async),
79+
map_uid: box_config.get(:uid).to_s,
80+
map_gid: box_config.get(:gid).to_s,
81+
owner: box_config.get(:user),
82+
group: box_config.get(:group)
83+
84+
box_config.shell_list.each do |file|
85+
config.vm.provision 'shell', path: 'vagrant/provisioning/' + file, env: {
86+
VAGRANT_UID: box_config.get(:uid).to_s,
87+
VAGRANT_GID: box_config.get(:gid).to_s,
88+
VAGRANT_USER: box_config.get(:user),
89+
VAGRANT_GROUP: box_config.get(:group),
90+
VAGRANT_HOSTNAME: box_config.get(:hostname)
91+
}
92+
end
93+
94+
config.hostmanager.enabled = true
95+
config.hostmanager.manage_host = true
96+
config.hostmanager.ignore_private_ip = false
97+
config.hostmanager.include_offline = true
98+
99+
config.vm.define 'hypernode' do |node|
100+
node.vm.hostname = box_config.get(:hostname)
101+
node.vm.network :private_network, auto_network: true
102+
node.hostmanager.aliases = box_config.get(:domains)
103+
end
104+
105+
end

config.rb.dst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name 'magento-hypernode'
2+
hostname name + '.box'
3+
domains %w(www.magento-hypernode.box)
4+
profiler true
5+
developer true
6+
directory 'server'

vagrant/inline/config.rb

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
module VagrantApp
2+
class Config < BasicObject
3+
@@options = ::Hash.new
4+
5+
def self.option(option, default)
6+
@@options[option] = default
7+
class_eval do
8+
define_method option do |value = nil|
9+
access(option, value)
10+
end
11+
end
12+
self
13+
end
14+
15+
def initialize
16+
@config = ::Hash.new
17+
@shell = []
18+
end
19+
20+
def set(key, value)
21+
@config[key] = value
22+
end
23+
24+
def get(key)
25+
unless @@options.key?(key)
26+
raise 'Undefined configuration option: ' + key
27+
end
28+
29+
unless @config.key?(key)
30+
return @@options[key]
31+
end
32+
33+
@config[key]
34+
end
35+
36+
# Configuration flag check
37+
def flag?(key, inverse = false)
38+
result = true
39+
if key.instance_of?(::Array)
40+
for sub_key in key
41+
result &&= flag?(sub_key, inverse)
42+
end
43+
elsif !key.nil?
44+
result = (get(key) === true)
45+
46+
if inverse
47+
result = !result
48+
end
49+
end
50+
51+
result
52+
end
53+
54+
def access(key, value = nil)
55+
unless value === nil
56+
set(key, value)
57+
end
58+
59+
get(key)
60+
end
61+
62+
def self.method_missing(method, value = nil)
63+
access(method, value)
64+
end
65+
66+
def load(file)
67+
unless ::File::exists?(file)
68+
return false
69+
end
70+
71+
instance_eval ::File.read(file), file
72+
end
73+
74+
def shell_add(script, flags = nil, inverse = false)
75+
@shell << {file: script, flags: flags, inverse: inverse}
76+
self
77+
end
78+
79+
def shell_list
80+
scripts = @shell.select do |info|
81+
flag?(info[:flags])
82+
end
83+
scripts.collect do |info|
84+
info[:file]
85+
end
86+
end
87+
end
88+
end

vagrant/inline/nfs-plugin.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'vagrant-nfs_guest/guests/ubuntu/cap/nfs_server'
2+
3+
module VagrantPlugins
4+
module VagrantAppPlugin
5+
module Cap
6+
class NFSServer < ::VagrantPlugins::SyncedFolderNFSGuest::GuestUbuntu::Cap::NFSServer
7+
def self.nfs_server_install(machine)
8+
super machine
9+
machine.communicate.sudo("ufw disable")
10+
end
11+
end
12+
end
13+
14+
class Plugin < Vagrant.plugin("2")
15+
name "Ubuntu guest capability plugin for fixing firewall issue of nfs guest"
16+
17+
guest_capability(:ubuntu, :nfs_server_install) do
18+
Cap::NFSServer
19+
end
20+
end
21+
end
22+
end

vagrant/provisioning/composer.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
set -e
4+
[ -f "/usr/local/bin/composer" ] || php -r "readfile('https://getcomposer.org/installer');" \
5+
| php -- --install-dir=/usr/local/bin --filename=composer

vagrant/provisioning/developer.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
3+
set -e
4+
AS_USER="sudo -u ${VAGRANT_USER}"
5+
HOME_DIR=$(getent passwd ${VAGRANT_USER} | cut -d ':' -f6)
6+
7+
$AS_USER tee ${HOME_DIR}/nginx/server.devmode <<"CONFIG"
8+
set $developermode "1";
9+
CONFIG
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
echo "Disabling Varnish caching..."
6+
7+
# Create a vcl that tells Varnish to cache nothing
8+
cat > /etc/varnish/default.vcl <<- EOM
9+
vcl 4.0;
10+
backend default {
11+
.host = "127.0.0.1";
12+
.port = "8080";
13+
}
14+
15+
sub vcl_recv {
16+
return(pass);
17+
}
18+
EOM
19+
20+
service varnish restart

vagrant/provisioning/hello.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
echo "Welcome to Hypernode Vagrant Box!"
4+
echo "You can login now with in order to use your box:"
5+
echo "\$ ssh ${VAGRANT_USER}@${VAGRANT_HOSTNAME} -A"
6+
echo "To access database, you can use the following credentials in your app:"
7+
HOME_DIR=$(getent passwd ${VAGRANT_USER} | cut -d ':' -f6)
8+
MYSQLPASSWORD=$(awk -F "=" '/password/ {print $2}' ${HOME_DIR}/.my.cnf | sed -e 's/^[ \t]*//')
9+
10+
echo "Username: ${VAGRANT_USER}"
11+
echo "Password: $MYSQLPASSWORD"

vagrant/provisioning/hypernode.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
set -e
3+
4+
truncate -s 0 /var/mail/app
5+
6+
AS_USER="sudo -u ${VAGRANT_USER}"
7+
HOME_DIR=$(getent passwd ${VAGRANT_USER} | cut -d ':' -f6)
8+
$AS_USER mkdir -p "${HOME_DIR}/.ssh"
9+
$AS_USER touch "${HOME_DIR}/.ssh/authorized_keys"
10+
chmod 700 "${HOME_DIR}/.ssh"
11+
chmod 600 "${HOME_DIR}/.ssh/authorized_keys"
12+
13+
if ssh-add -L >/dev/null 2>/dev/null; then
14+
ssh-add -L >> ${HOME_DIR}/.ssh/authorized_keys;
15+
fi
16+
17+
cat << EOF >> ${HOME_DIR}/.ssh/authorized_keys
18+
EOF
19+
20+
rm -f "/var/lib/varnish/`hostname`"
21+
ln -s /var/lib/varnish/xxxxx-dummytag-vagrant.nodes.hypernode.io/ "/var/lib/varnish/`hostname`"
22+
23+
rm -rf /etc/cron.d/hypernode-fpm-monitor
24+
25+
# Copy default nginx configs to synced nginx directory if the files don't exist
26+
if [ -d /etc/hypernode/defaults/nginx/ ]; then
27+
su ${VAGRANT_USER} -c 'find /etc/hypernode/defaults/nginx -type f | xargs -I {} cp -n {} /data/web/nginx/'
28+
fi
29+
30+
# Update magerun to the latest version
31+
/usr/local/bin/n98-magerun -q self-update || true
32+
/usr/local/bin/n98-magerun2 -q self-update || true

0 commit comments

Comments
 (0)