Ok, I dug a little deeper in the VVV approach and tried to replicate their database syncing method with the following steps:
- Added the following directory structure in my main project directory:
database/
├─ backups/
├─ data/
├─ import-sql.sh
├─ init-custom.sql.sample (not used)
└─ init.sql (not used)
-
Changed the sync folder in the wordpress_sites loop in my Vagrantfile into:
config.vm.synced_folder “…/database/”, "/srv/database"
if File.exists?(File.join(’…/database/data/mysql_upgrade_info’)) then
config.vm.synced_folder “database/data/”, “/var/lib/mysql”, :mount_options => [ “dmode=777”, “fmode=777” ]
end -
Added the following directory structure and files in my ansible directory:
config/
├─ homebin/
├── db_backup
├── vagrant_destroy
├── vagrant_halt
└── vagrant_suspend
and added the following to the Vagrantfile:
config.vm.synced_folder "config/", "/srv/config"
-
Added a provision directory in the ansible directory with an edited version of the VVV’s provision.sh file:
#!/bin/bash
provision.sh
This file is specified in Vagrantfile and is loaded by Vagrant as the primary
provisioning script whenever the commands
vagrant up
,vagrant provision
,or
vagrant reload
are used. It provides all of the default packages andconfigurations included with Varying Vagrant Vagrants.
If MySQL is installed, go through the various imports and service tasks.
exists_mysql="$(service mysql status)"
if [[ “mysql: unrecognized service” != “${exists_mysql}” ]]; then# IMPORT SQL # Setup MySQL by importing an init file that creates necessary # users and databases that our vagrant setup relies on. # mysql -u root -proot < /srv/database/init.sql # echo "Initial MySQL prep..." # Process each mysqldump SQL file in database/backups to import # an initial data set for MySQL. /srv/database/import-sql.sh
else
echo -e "\nMySQL is not installed. No databases imported."
fi
and added the following to the Vagrantfile:
if File.exists?(File.join(ANSIBLE_PATH,'provision','provision.sh')) then
config.vm.provision :shell, :path => File.join( "provision", "provision.sh" )
end
-
Installed the vagrant-triggers plugin and added the following at the bottom of my Vagrantfile:
if Vagrant.has_plugin? 'vagrant-triggers’
config.trigger.before :halt, :stdout => true do
run "vagrant ssh -c ‘vagrant_halt’"
end
config.trigger.before :suspend, :stdout => true do
run "vagrant ssh -c ‘vagrant_suspend’"
end
config.trigger.before :destroy, :stdout => true do
run "vagrant ssh -c ‘vagrant_destroy’"
end
else
puts 'vagrant-triggers missing, please install the plugin:'
puts 'vagrant plugin install vagrant-triggers’
end
Running vagrant up
doesn’t give any errors besides stdin: is not a tty
==> default: Running provisioner: shell...
default: Running: /var/folders/x8/l1j6fgb91s37px9tplwmfdmdn2c64s/T/vagrant-shell20150824-7848-13ekkph.sh
==> default: stdin: is not a tty
==> default:
==> default: Start MySQL Database Import
==> default: No custom databases to import
No databases are imported yet, but that makes sense because one has to be exported first on vagrant halt
That’s where I’m stuck right now, the vagrant_destroy
, vagrant_halt
& vagrant_suspend
commands are called from within the vagrant ssh in the vagrant-triggers but how does Vagrant ‘know’ that these commands are stored inside the config/homebin directory?
When running vagrant halt
I get the following error right now:
==> default: Running triggers before halt...
==> default: Executing command "vagrant ssh -c vagrant_halt"...
==> default: bash: vagrant_halt: command not found
==> default: Command execution finished.
The command "vagrant ssh -c 'vagrant_halt'" returned a failed exit code. The
error output is shown below:
bash: vagrant_halt: command not found
How do I ‘load’ these commands into my Vagrantfile?
Thanks again and sorry for the long post!