From 1bb5421879a4bff5ea5cf893c322ba63b17dcbb5 Mon Sep 17 00:00:00 2001 From: Xavier Rousset Date: Fri, 6 Jul 2018 18:11:07 +0200 Subject: [PATCH] PHRAS-2140 Check OS for SMB or NFS folder strategy --- Vagrantfile | 65 +++++++++++++------ .../ansible/inventories/GetIpAdresses.cmd | 23 +++++++ 2 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 resources/ansible/inventories/GetIpAdresses.cmd diff --git a/Vagrantfile b/Vagrantfile index 26a4c88d81..2940d1f7d2 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -1,16 +1,4 @@ Vagrant.require_version ">= 1.5" -$php = [ "5.6", "7.0", "7.1", "7.2" ] -$phpVersion = ENV['phpversion'] ? ENV['phpversion'] : "5.6"; - -unless Vagrant.has_plugin?('vagrant-hostmanager') - raise "vagrant-hostmanager is not installed! Please run\n vagrant plugin install vagrant-hostmanager\n\n" -end - -unless $php.include?($phpVersion) - raise "You should specify php version before running vagrant\n\n (Available : 5.6, 7.0, 7.1, 7.2 | default => 5.6)\n\n Exemple: phpversion='7.0' vagrant up \n\n" -end - -$root = File.dirname(File.expand_path(__FILE__)) # Check to determine whether we're on a windows or linux/os-x host, # later on we use this to launch ansible in the supported way @@ -26,6 +14,28 @@ def which(cmd) return nil end +if which('ip') + $env = "mac" +else if which('ifconfig') + $env = "linux" + else + $env = "windows" + end +end + +$php = [ "5.6", "7.0", "7.1", "7.2" ] +$phpVersion = ENV['phpversion'] ? ENV['phpversion'] : "5.6"; + +unless Vagrant.has_plugin?('vagrant-hostmanager') + raise "vagrant-hostmanager is not installed! Please run\n vagrant plugin install vagrant-hostmanager\n\n" +end + +unless $php.include?($phpVersion) + raise "You should specify php version before running vagrant\n\n (Available : 5.6, 7.0, 7.1, 7.2 | default => 5.6)\n\n Exemple: phpversion='7.0' vagrant up \n\n" +end + +$root = File.dirname(File.expand_path(__FILE__)) + def config_net(config) config.hostmanager.aliases = [ $hostname + ".vb", @@ -41,11 +51,20 @@ def config_net(config) # vboxnet0 can be changed to use a specific private_network config.vm.network :private_network, type: "dhcp" config.vm.provider "virtualbox" do |vb| - vb.customize ["modifyvm", :id, "--hostonlyadapter2", "vboxnet0"] + + if $env == "mac" || $env == "linux" + vb.customize ["modifyvm", :id, "--hostonlyadapter2", "vboxnet0"] + else + vb.customize ["modifyvm", :id, "--hostonlyadapter2", "VirtualBox Host-Only Ethernet Adapter"] + end end config.hostmanager.ip_resolver = proc do |vm, resolving_vm| if vm.id - `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.split()[1] + if $env == "mac" || $env == "linux" + `VBoxManage guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.split()[1] + else + `"C:/Program Files/Oracle/VirtualBox/VBoxManage" guestproperty get #{vm.id} "/VirtualBox/GuestInfo/Net/1/V4/IP"`.split()[1] + end end end end @@ -53,11 +72,15 @@ end # By default, the name of the VM is the project's directory name $hostname = File.basename($root).downcase -if which('ip') + +if $env == "mac" # $hostIps = `ip addr show | grep inet | grep -v inet6 | cut -d' ' -f6 | cut -d'/' -f1`.split("\n"); $hostIps = `ip addr show | sed -nE 's/[[:space:]]*inet ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$/\\1/p'`.split("\n"); -else - $hostIps = `ifconfig | sed -nE 's/[[:space:]]*inet ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$/\\1/p'`.split("\n"); +else if $env == "linux" + $hostIps = `ifconfig | sed -nE 's/[[:space:]]*inet ([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3})(.*)$/\\1/p'`.split("\n"); + else + $hostIps = `resources/ansible/inventories/GetIpAdresses.cmd` + end end Vagrant.configure("2") do |config| @@ -115,5 +138,9 @@ Vagrant.configure("2") do |config| # config.vm.provision :shell, run: "always", path: "resources/ansible/windows-always.sh", args: ["default"] end - config.vm.synced_folder "./", "/vagrant", type: "nfs" -end + if $env == "mac" || $env == "linux" + config.vm.synced_folder "./", "/vagrant", type: "nfs", mount_options: ['rw', 'vers=3', 'tcp', 'fsc'] + else + config.vm.synced_folder "./", "/vagrant", type: "smb", mount_options: ["vers=3.02","mfsymlinks"] + end +end \ No newline at end of file diff --git a/resources/ansible/inventories/GetIpAdresses.cmd b/resources/ansible/inventories/GetIpAdresses.cmd new file mode 100644 index 0000000000..629750f581 --- /dev/null +++ b/resources/ansible/inventories/GetIpAdresses.cmd @@ -0,0 +1,23 @@ +@echo off +setlocal +setlocal enabledelayedexpansion +rem throw away everything except the IPv4 address line +for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do ( + rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78" + rem split on : and get 2nd token + for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do ( + rem we have " 192.168.42.78" + rem split on . and get 4 tokens (octets) + for /f "tokens=1-4 delims=." %%c in ("%%b") do ( + set _o1=%%c + set _o2=%%d + set _o3=%%e + set _o4=%%f + rem strip leading space from first octet + set _3octet=!_o1:~1!.!_o2!.!_o3!. + echo !_3octet! + ) + ) + ) +rem add additional commands here +endlocal \ No newline at end of file