3

Our vagrant setup includes a Vagrant file that installs some softwares at the time of vm provisioning. For this we have the archive of softwares that we want to install and some installation scipts. These software archives and installation scripts directory, we are syncing in vagrant file. Once the installation is done, Vagrant file have a trigger to unsync softwares archive and installation scripts directory. When this trigger is called, we are unsyncing /vagrant directory as well our software archives and installation scripts directory.

/vagrant directory unsyncs successfully, but softwares archive and installation scripts directory is not getting unsynced.

I suspect, the way I am syncing the software archives and installtion scripts directory is not correct. Kindly suggest how to properly unsync software archives and installation scripts directory.


Please find below simplified Vagrantfile and provisioning shell script :-

Vagrantfile :-

SOFTWARES_DIR = "/Users/dev/bundles/softwares"

Vagrant.configure("2") do |config|

    config.vm.define "sampleMachine" do |sm|

            #trigger that will unsync /vagrant and /softwares directory
            sm.trigger.after :up do |trigger|
                trigger.name = "Unsync script"
                trigger.info = "Unsyncing /vagrant, /softwares directory"

                #unsyncing of /vagrant directory working fine
                sm.vm.synced_folder ".", "/vagrant", disabled: true 

                #unsyncing previously synced /softwares directory, once the installation of softwares is done
                #but it's not unsyncing
                sm.vm.synced_folder "#{SOFTWARES_DIR}", "/softwares", disabled: true 
            end

            sm.vm.box = "centos/7"
            sm.vm.hostname = "machineWithUnsyncedFolders"
            sm.vm.network "private_network", ip: "192.168.33.191"

            #folders synced for installing softwares at the time of machine provisioning
            #is there any alternate way of syncing, so that unsycing works
            sm.vm.synced_folder "#{SOFTWARES_DIR}", "/softwares", id: "softwares", :nfs => true, :mount_options => ['nolock,vers=3,udp,noatime']

            #this script will use files that are synced in above statement for installing softwares 
            sm.vm.provision "shell", path: "script.sh"
    end
end

script.sh :-

echo "provisioning vm for development setup"

function installSoftwares(){
        local MACHINE_NAME=`hostname`
        echo "machine name is >$MACHINE_NAME<, installing below softwares :-"
        ls -1 /softwares
}

installSoftwares

When directories are listed in root directory, vagrant directory is not there, but softwares directory is still there, which means it is not unsynced in the Vagrant file trigger.

Please find below listing of directories in root directory in vm :-

vagrant ssh
[vagrant@machineWithUnsyncedFolders ~]$ cd /
[vagrant@machineWithUnsyncedFolders /]$ ls -1
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
softwares
srv
sys
tmp
usr
var
[vagrant@machineWithUnsyncedFolders /]$

Please find below vagrant up logs :-

~/othervms/vagrantExamples/vbProvider/removeSyncedFolderAfterUse $

ls -a
.       ..      Vagrantfile script.sh

~/othervms/vagrantExamples/vbProvider/removeSyncedFolderAfterUse $

vagrant up
Bringing machine 'sampleMachine' up with 'virtualbox' provider...
==> sampleMachine: Importing base box 'centos/7'...
==> sampleMachine: Matching MAC address for NAT networking...
==> sampleMachine: Checking if box 'centos/7' version '1804.02' is up to date...
==> sampleMachine: Setting the name of the VM: removeSyncedFolderAfterUse_sampleMachine_1553489790050_21418
==> sampleMachine: Fixed port collision for 22 => 2222. Now on port 2200.
==> sampleMachine: Clearing any previously set network interfaces...
==> sampleMachine: Preparing network interfaces based on configuration...
    sampleMachine: Adapter 1: nat
    sampleMachine: Adapter 2: hostonly
==> sampleMachine: Forwarding ports...
    sampleMachine: 22 (guest) => 2200 (host) (adapter 1)
==> sampleMachine: Booting VM...
==> sampleMachine: Waiting for machine to boot. This may take a few minutes...
    sampleMachine: SSH address: 127.0.0.1:2200
    sampleMachine: SSH username: vagrant
    sampleMachine: SSH auth method: private key
    sampleMachine:
    sampleMachine: Vagrant insecure key detected. Vagrant will automatically replace
    sampleMachine: this with a newly generated keypair for better security.
    sampleMachine:
    sampleMachine: Inserting generated public key within guest...
    sampleMachine: Removing insecure key from the guest if it's present...
    sampleMachine: Key inserted! Disconnecting and reconnecting using new SSH key...
==> sampleMachine: Machine booted and ready!
==> sampleMachine: Checking for guest additions in VM...
    sampleMachine: No guest additions were detected on the base box for this VM! Guest
    sampleMachine: additions are required for forwarded ports, shared folders, host only
    sampleMachine: networking, and more. If SSH fails on this machine, please install
    sampleMachine: the guest additions and repackage the box to continue.
    sampleMachine:
    sampleMachine: This is not an error message; everything may continue to work properly,
    sampleMachine: in which case you may ignore this message.
==> sampleMachine: Setting hostname...
==> sampleMachine: Configuring and enabling network interfaces...
==> sampleMachine: Exporting NFS shared folders...
==> sampleMachine: Preparing to edit /etc/exports. Administrator privileges will be required...
The nfsd service does not appear to be running.
Starting the nfsd service
==> sampleMachine: Mounting NFS shared folders...
==> sampleMachine: Running provisioner: shell...
    sampleMachine: Running: /var/folders/3y/zgjk378n4r968pv_q095ttkh0000gn/T/vagrant-shell20190325-42456-bm7v10.sh
    sampleMachine: provisioning vm for development setup
    sampleMachine: machine name is >machineWithUnsyncedFolders<, installing below softwares :-
    sampleMachine: apache-hive-1.1.0-bin.tar.gz
    sampleMachine: apache-hive-1.2.2-bin.tar.gz
    sampleMachine: apache-hive-2.1.1-bin.tar.gz
    sampleMachine: apache-hive-2.3.3-bin.tar.gz
    sampleMachine: apache-hive-3.1.0-bin.tar.gz
    sampleMachine: apache-maven-3.6.0-bin.tar.gz
    sampleMachine: apache-tomcat-7.0.90.tar.gz
    sampleMachine: hadoop-2.6.0.tar.gz
    sampleMachine: hadoop-2.8.4.tar.gz
    sampleMachine: hadoop-2.9.0.tar.gz
    sampleMachine: jdk-8u171-linux-x64.tar.gz
    sampleMachine: mysql-connector-java-5.1.47.tar.gz
    sampleMachine: scala-2.11.12.tgz
    sampleMachine: spark-2.3.1-bin-hadoop2.7.tgz
    sampleMachine: spark-2.4.0-bin-hadoop2.7.tgz
==> sampleMachine: Running action triggers after up ...
==> sampleMachine: Running trigger: Unsync script...
==> sampleMachine: Unsyncing /vagrant, /softwares directory

~/othervms/vagrantExamples/vbProvider/removeSyncedFolderAfterUse $

vagrant ssh
[vagrant@machineWithUnsyncedFolders ~]$ whoami
vagrant
[vagrant@machineWithUnsyncedFolders ~]$ hostname
machineWithUnsyncedFolders
[vagrant@machineWithUnsyncedFolders ~]$ pwd
/home/vagrant
[vagrant@machineWithUnsyncedFolders ~]$ cd /
[vagrant@machineWithUnsyncedFolders /]$ pwd
/
[vagrant@machineWithUnsyncedFolders /]$ ls -1
bin
boot
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
softwares
srv
sys
tmp
usr
var
[vagrant@machineWithUnsyncedFolders /]$

How to unsync custom folder properly.

mogli
  • 1,549
  • 4
  • 29
  • 57

0 Answers0