Skip to content

Instantly share code, notes, and snippets.

@Blizzke
Created January 3, 2016 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blizzke/0bc3ec65d9a7c058d99d to your computer and use it in GitHub Desktop.
Save Blizzke/0bc3ec65d9a7c058d99d to your computer and use it in GitHub Desktop.
# -*- mode: ruby -*-
# vi: set ft=ruby :
HOSTNAME = "irrelevant.dev" # VM/nginx hostname
IP = "172.16.190.10" # One of the IPs of your host only virtualbox network
SSHPORT = 12222 # Set to a custom port, not taken on your machine
Vagrant.configure(2) do |config|
config.vm.box = "internal-dvp"
config.vm.box_url = "http://internal.url/base.box"
config.vm.hostname = HOSTNAME
config.ssh.port = SSHPORT
# Change the SSH port to what is configured
config.vm.network :forwarded_port, :guest => 22, :host => SSHPORT, :id => "ssh-custom"
config.vm.network :forwarded_port, :guest => 22, :host => 2222, :id => "ssh", :disabled => "true"
# Define and use a host-only network. Leave the adapter part, otherwise it tries to reconfigure the primary one and hangs!
config.vm.network "private_network", :ip => IP, :adapter => 2
config.vm.provider "virtualbox" do |vb|
# Uncomment to show the VM "display"
# vb.gui = true
# http://stackoverflow.com/questions/20194128/vagrant-crashes-depending-on-physical-network
# vb.customize ['modifyvm', :id, '--natdnshostresolver1', 'on']
# vb.customize ["modifyvm", :id, "--natnet1", PRIVATE_RANGE]
vb.customize ['modifyvm', :id, '--memory', "512"]
vb.customize ['modifyvm', :id, '--cpus', "1"]
vb.customize ['modifyvm', :id, '--name', config.vm.hostname]
end
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# one time scripts (root)
config.vm.provision :shell, :path => "vagrant/do-composer.sh"
# Pass on the hostname so it can be added in the nginx config (root)
config.vm.provision "shell" do |s|
s.path = "vagrant/www-setup.sh"
s.args = [HOSTNAME]
end
# Store the github client access token so that we don't have to log in separately and then init the configuration
$script = <<SCRIPT
echo Configuring github authentication...
/usr/local/bin/composer config -g github-oauth.github.com <token>
echo Setting up environment...
cd /vagrant
SCRIPT
config.vm.provision "shell", :inline => $script, :privileged => false
config.vm.provision :shell, :path => "vagrant/www-composer-update.sh", :privileged => false
# needs to go as last because it requires a configured yii
config.vm.provision :shell, :path => "vagrant/sap-setup-db.sh"
end
@Blizzke
Copy link
Author

Blizzke commented Jan 3, 2016

www-composer-update.sh

#!/bin/bash 
#
# If the vendor folder is not found yet, run a composer update 
#

# Since composer appears to be extremely slow when it is actually downloading things using the git protocol,
# make it default to https
# http://www.elcoderino.com/installing-updating-composer-dependencies-is-very-slow-or-time-outs/
/usr/local/bin/composer config -g github-protocols https

# Yii2 requires the asset plugin, which needs to be installed globally at this point
/usr/local/bin/composer global require "fxp/composer-asset-plugin:dev-master"

if [ ! -d /vagrant/vendor ]; then
  echo "composer: vendor folder not found, updating dependencies"
  cd /vagrant/
  /usr/local/bin/composer -v update --prefer-dist
else
  echo "composer: vendor folder found, skipping update"
fi

@Blizzke
Copy link
Author

Blizzke commented Jan 3, 2016

sap-setup-db.sh

#!/bin/bash

if [ ! -f ~/.my.cnf ]; then
  echo "mysql: Creating personal settings file"
  echo [client]       >> ~/.my.cnf
  echo password=root  >> ~/.my.cnf
fi

DB=databasename

echo "mysql: verifying if $DB exists"
EXISTS=`mysql --batch --skip-column-names -e "SHOW DATABASES LIKE '$DB'" | grep $DB`
if [[ "$EXISTS" != "$DB" ]]; then
  echo "mysql: $DB not found, creating"
  mysql --batch --skip-column-names -e "CREATE DATABASE $DB CHARACTER SET utf8 COLLATE utf8_general_ci"

  # DB created, start migration
  cd /vagrant
  /usr/bin/php yii migrate --interactive=0
  /usr/bin/php yii migrate --interactive=0 --migrationPath=vendor/bedezign/yii2-audit/src/migrations
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment