This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Vagrant improvements: automatically installation of "vagrant-hostsupd…

…ater", unique crossplatform provisioner, improved Vagrantfile
  • Loading branch information...
BR0kEN- committed Nov 22, 2015
1 parent 4f0187b commit e2524f3ce701e745ed726bcd7eaadafc02ee00d6
@@ -3,32 +3,24 @@
tmproot: /tmp
webroot: /var/www
vm:
box_url: http://128.199.55.125/ubuntu14.04LTS_cibox.box
network:
private_network: 192.168.56.132
forwarded_port:
CocLsFE7aIwA:
host: 8081
guest: 80
CocLsFE7aIwE:
host: 8083
guest: 3306
provider:
virtualbox:
modifyvm:
natdnshostresolver1: "on"
memory: "2048"
cpus: "2"
lxc:
box: fgrehm/precise64-lxc
box_url: https://atlas.hashicorp.com/fgrehm/boxes/precise64-lxc/versions/1.2.0/providers/lxc.box
ip: 192.168.56.132
forwarded_port:
CocLsFE7aIwA:
host: 8081
guest: 80
CocLsFE7aIwE:
host: 8083
guest: 3306
virtualbox:
box: http://128.199.55.125/ubuntu14.04LTS_cibox.box
cpus: 2
memory: 2048
natdnshostresolver1: "on"
lxc:
box: https://atlas.hashicorp.com/fgrehm/boxes/precise64-lxc/versions/1.2.0/providers/lxc.box
synced_folder:
- id: drupal
source: ./
- source: ./
target: /var/www
exclude:
- .vagrant/
- .idea/
- .git/
ssh:
username: vagrant
@@ -55,7 +55,7 @@
- name: Is Vagrant machine will be provisioned?
set_fact:
vagrant: yes
when: limit is defined and limit == "vagrant"
when: ansible_ssh_host is defined and ansible_ssh_host == "127.0.0.1"

- include: tasks/project-variable.yml
when: not vagrant
@@ -91,5 +91,5 @@
- name: Update config.yml
lineinfile:
dest: "../{{ project }}/config.yml"
line: "project: {{ project }}\nsite_url: http://{{ project }}.dev\ncmf: {{ cmf }}"
line: "cmf: {{ cmf }}\nproject: {{ project }}\nsite_url: http://{{ project }}.dev"
insertafter: "^---$"
@@ -15,6 +15,7 @@ docroot/build_reports/
!tests/
# Ansible provision scripts for VM.
!provisioning/
!provisioners/

# Ignore hidden files.
.*
@@ -1,49 +1,42 @@
# Drupal Vagrant box for CIBox support
# CIBox

## Installation

- [Vagrant](https://www.vagrantup.com/downloads.html)
- [VirtualBox](https://www.virtualbox.org/wiki/Downloads)
- Vagrant plugins:
- [vagrant plugin install vagrant-hostsupdater](https://github.com/cogitatio/vagrant-hostsupdater)
- [vagrant plugin install vagrant-host-shell](https://github.com/phinze/vagrant-host-shell)

## Usage

Start and provision the virtual machine:
### Up

```shell
vagrant up
vagrant ssh
```

Site re-installation from scratch:
### Re-install

```shell
./ansible.sh reinstall
```

- By default your site will be accessible using this URL: `http://drupal.192.168.56.132.xip.io/`.
- If `xip.io` not working - create a row with `192.168.56.112 drupal.192.168.56.132.xip.io` in `/etc/hosts` or just use another ServerName in www.yml

## Tools

- XDebug
- Drush
- Selenium 2
- Composer
- Adminer
- Composer
- XDebug
- XHProf
- PHP Daemon
- PHP, SASS, JS sniffers/lints/hints
- Selenium 2
- Sniffers, lints and hints for PHP, CSS, JS

### Adminer

Adminer for MySQL administration (credentials drupal:drupal and root:root): `http://192.168.56.112.xip.io/adminer.php`.
**Adminer** for MySQL administration (credentials `drupal/drupal` and `root/root`): `http://192.168.56.112.xip.io/adminer.php`.

### PHP Profiler XHProf

It is installed by default, but to use it as Devel module integration use:
It is installed by default, but to use it as `Devel` module integration use:

```shell
drush en devel -y
@@ -86,12 +79,11 @@ sudo /etc/init.d/apparmor reload
and run the box by command:

```shell
VAGRANT_CI=yes vagrant up
VAGRANT_CI=lxc vagrant up
```

### Windows Containers

- Install [Cygwin](https://servercheck.in/blog/running-ansible-within-windows) according to provided steps.
- Run Cygwin as administrator.
- Add `export VAGRANT_DETECTED_OS=cygwin` to `~/.bash_profile` using Cygwin CLI.
- Use default flow to up Vagrant.
- Run `Cygwin` as administrator.
- Use default flow to [up Vagrant](#up).
@@ -2,78 +2,79 @@
# vi: set ft=ruby :
VAGRANTFILE_API_VERSION = "2"

require 'yaml'
require 'rbconfig'
require "yaml"

isWindows = RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/
isWindows = Vagrant::Util::Platform.windows?
configValues = YAML::load_file("config.yml")

if !configValues['vm']['provider']['virtualbox'].empty?
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
ENV["VAGRANT_DEFAULT_PROVIDER"] = ENV.has_key?("VAGRANT_CI") && ENV["VAGRANT_CI"].include?("lxc") ? "lxc" : "virtualbox"

if isWindows
ENV["VAGRANT_DETECTED_OS"] = "cygwin"
# Prevent closing connection.
# https://github.com/ansible/ansible/issues/6363#issuecomment-49349902
ENV["ANSIBLE_SSH_ARGS"] = "-o ControlMaster=no"
end

if !ENV['VAGRANT_CI'].nil?
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'lxc'
if not Vagrant.has_plugin?("vagrant-hostsupdater")
system "vagrant plugin install vagrant-hostsupdater"
end

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = File.basename(configValues['vm']['box_url'])
config.vm.box_url = "#{configValues['vm']['box_url']}"
# Dynamically load provisioners.
Dir["provisioners/*"].each do |file|
file += "/#{File.basename(file)}.rb"

if !ENV['VAGRANT_CI'].nil?
config.vm.box = "#{configValues['vm']['provider']['lxc']['box']}"
if File.exist?(file)
require_relative file
end
end

if configValues['vm']['hostname'].to_s != ''
config.vm.hostname = "#{configValues['vm']['hostname']}"
end
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box_url = "#{configValues["vm"][ENV["VAGRANT_DEFAULT_PROVIDER"]]["box"]}"
config.vm.box = File.basename(config.vm.box_url)
config.vm.hostname = "#{configValues["project"]}.dev"
config.vm.usable_port_range = (10200..10500)

# If hostsupdater plugin is installed, add all servernames as aliases.
if Vagrant.has_plugin?("vagrant-hostsupdater")
config.hostsupdater.aliases = ["#{configValues['project']}.dev"]
end
config.hostsupdater.aliases = [config.vm.hostname]

if configValues['vm']['network']['private_network'].to_s != ''
config.vm.network "private_network",
:ip => "#{configValues['vm']['network']['private_network']}",
:lxc__bridge_name => "lxcbr0"
config.vm.network "private_network",
:ip => "#{configValues["vm"]["ip"]}",
:lxc__bridge_name => "lxcbr0"

config.vm.provider :virtualbox do |vb|
configValues["vm"]["virtualbox"].each do |key, value|
vb.customize ["modifyvm", :id, "--#{key}", "#{value}"]
end
end

configValues['vm']['network']['forwarded_port'].each do |i, port|
if port['guest'] != '' && port['host'] != ''
configValues["vm"]["forwarded_port"].each do |i, port|
if port["guest"] != "" && port["host"] != ""
config.vm.network :forwarded_port,
:host => port['host'].to_i,
:guest => port['guest'].to_i,
:auto_correct => true
end
end

for folder in configValues['vm']['synced_folder'];
config.vm.synced_folder folder['source'], folder['target'],
:id => folder['id'],
for folder in configValues["vm"]["synced_folder"];
config.vm.synced_folder folder["source"], folder["target"],
:id => folder["target"],
# Only using "rsync" we able to affect on file permissions inside of VM using "chmod" on Windows host.
:type => isWindows ? "rsync" : "nfs",
:rsync__auto => "true",
:rsync__args => ["--verbose", "--archive", "--delete", "-z", "--chmod=ugo=rwX"],
:rsync__exclude => folder['exclude'],
:rsync__exclude => folder["excluded_paths"],
:linux__nfs_options => ["rw", "no_subtree_check", "no_root_squash", "async"]
end

config.vm.usable_port_range = (10200..10500)

if !configValues['vm']['provider']['virtualbox'].empty?
config.vm.provider :virtualbox do |virtualbox|
configValues['vm']['provider']['virtualbox']['modifyvm'].each do |key, value|
virtualbox.customize ["modifyvm", :id, "--#{key}", "#{value}"]
end
end
end
config.vm.provision :cibox,
:controller => "./ansible.sh",
:playbook => "provisioning/provision"

config.vm.provision :host_shell, :inline => "./ansible.sh provisioning/provision --limit=vagrant"
config.vm.provision :shell, :inline => "/var/www/ansible.sh reinstall"
config.vm.provision :shell,
:inline => "/var/www/ansible.sh reinstall"

config.ssh.username = configValues['vm']['ssh']['username']
config.ssh.password = configValues['vm']['ssh']['username']
config.ssh.shell = "sh"
config.ssh.insert_key = "false"
config.ssh.insert_key = false
config.ssh.forward_agent = true
end
@@ -0,0 +1,17 @@
module VagrantPlugins
module CIBox
class Plugin < Vagrant.plugin("2")
name "cibox"

config :cibox, :provisioner do
require_relative "config"
Config
end

provisioner :cibox do
require_relative "provisioner"
Provisioner
end
end
end
end
@@ -0,0 +1,20 @@
module VagrantPlugins::CIBox
class Config < Vagrant.plugin("2", :config)
attr_accessor :playbook
attr_accessor :controller

def validate(machine)
errors = _detected_errors

unless controller
errors << ':cibox provisioner requires :controller to be set.'
end

unless playbook
errors << ':cibox provisioner requires :playbook to be set.'
end

{'CIBox Provisioner' => errors}
end
end
end
@@ -0,0 +1,84 @@
module VagrantPlugins::CIBox
class Provisioner < Vagrant.plugin("2", :provisioner)
def provision
Vagrant::Util::Subprocess.execute(
"bash",
"-c",
"#{config.controller} #{config.playbook} --limit=#{@machine.name}",
:workdir => @machine.env.root_path.to_s,
:notify => [:stdout, :stderr],
:env => environment_variables,
) do |io_name, data|
@machine.env.ui.info(data, {
:new_line => false,
:prefix => false,
:color => io_name == :stdout ? :green : :red,
})
end
end

protected

def environment_variables
environment_variables = {}
environment_variables["ANSIBLE_SSH_ARGS"] = ansible_ssh_args
environment_variables["ANSIBLE_HOSTS"] = ansible_hosts
environment_variables["PATH"] = ENV["VAGRANT_OLD_ENV_PATH"]

return environment_variables
end

def ansible_ssh_args
ansible_ssh_args = []
ansible_ssh_args << "-o ForwardAgent=yes" if @machine.ssh_info[:forward_agent]
ansible_ssh_args << "-o StrictHostKeyChecking=no"
ansible_ssh_args << ENV["ANSIBLE_SSH_ARGS"]

return ansible_ssh_args.join(' ')
end

# Auto-generate "safe" inventory file based on Vagrantfile.
def ansible_hosts
inventory_content = "# Generated by CIBox\n"

# By default, in Cygwin, user's home directory is "/home/<USERNAME>" and it is not the same
# that "C:\Users\<USERNAME>". All used software (Ansible (~/.ansible), Vagrant (~/.vagrant.d),
# Virtualbox (~/.VirtualBox), SSH (~/.ssh)) uses correct for Windows path and this breaks a
# lot of Linux commands (chmod - one of them and we need to use to set correct permissions to
# SSH private key).
if Vagrant::Util::Platform.cygwin?
ENV["HOME"] = Vagrant::Util::Subprocess.execute("cygpath", "-wH").stdout.chomp.gsub("\\", "/") + "/" + Etc.getlogin()
end

@machine.env.active_machines.each do |active_machine|
begin
m = @machine.env.machine(*active_machine)

if !m.ssh_info.nil?
inventory_content += "#{m.name} ansible_ssh_host=#{m.ssh_info[:host]} ansible_ssh_port=#{m.ssh_info[:port]} ansible_ssh_user=#{m.ssh_info[:username]} ansible_ssh_private_key_file=#{m.ssh_info[:private_key_path][0].gsub(ENV["HOME"], "~")}\n"
else
@logger.error("Auto-generated inventory: Impossible to get SSH information for machine '#{m.name} (#{m.provider_name})'. This machine should be recreated.")
# Let a note about this missing machine
inventory_content += "# MISSING: '#{m.name}' machine was probably removed without using Vagrant. This machine should be recreated.\n"
end
rescue Vagrant::Errors::MachineNotFound => e
@logger.info("Auto-generated inventory: Skip machine '#{active_machine[0]} (#{active_machine[1]})', which is not configured for this Vagrant environment.")
end
end

inventory_dir = Pathname.new(File.join(@machine.env.local_data_path.join, %w(provisioners cibox ansible)))
FileUtils.mkdir_p(inventory_dir) unless File.directory?(inventory_dir)
inventory_file = Pathname.new(File.join(inventory_dir, 'inventory'))

Mutex.new.synchronize do
if !File.exists?(inventory_file) or inventory_content != File.read(inventory_file)
inventory_file.open('w') do |file|
file.write(inventory_content)
end
end
end

return inventory_file
end
end
end

0 comments on commit e2524f3

Please sign in to comment.