Vagrant – Automating PHP/MySQL Installation with bash/slack for continuous deployment

In: General|Linux|MySQL|PHP

11 Dec 2011

Vagrant is simply a wrapper around Virtualbox headless that allows for provisioning virtual machines with support for puppet, chef-solo, chef, and bash. This allows you to automate the deployment and sandboxing of development sites. Additional base box images can be found at vagrantbox.es.

Installing Vagrant

apt-get install -y ruby1.9.1 ruby1.9.1-dev
ln -svf /usr/bin/ruby1.9.1 /etc/alternatives/ruby

gem install vagrant
vagrant box add base http://puppetlabs.s3.amazonaws.com/pub/squeeze64.box

mkdir ~/vagrant/test && ~/vagrant/test;
vagrant init base

Change the config for the virtual box instance to be bootstrapped with a bash script.

echo "
Vagrant::Config.run do |config|
  config.vm.box = "base"

  config.vm.define :web do |web_config|
    web_config.vm.box = "base"
    web_config.vm.forward_port("http", 80, 28080)
    web_config.vm.provision :shell, :path => "slack.sh web"
  end
end
" > Vagrantfile

On a remote/local server configure slack, hit enter on prompts to use defaults for ssh-keygen and use without a passphrase.

useradd -m slack
ssh-keygen -f~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub

Replace the contents of the RSA file in the following slack.sh file in place of <place_rsa_key_contents_here>. Additionally change the ~/.ssh/config ip in Hostname to the relevant ip to where slack is enabled.


#!/bin/bash
echo "export DEBIAN_FRONTEND=noninteractive" > ~/base.install.sh
echo "apt-get -q update" >> ~/base.install.sh
#echo "apt-get -q -y upgrade" >> ~/base.install.sh
echo "apt-get -q -y install slack locales-all" >> ~/base.install.sh
echo "mkdir -p ~/.ssh;" >> ~/base.install.sh

echo "
cat > ~/.ssh/slack.id_rsa < <-EOD
-----BEGIN RSA PRIVATE KEY-----
<place_rsa_key_contents_here>
-----END RSA PRIVATE KEY-----
EOD
" >> ~/base.install.sh
echo "chmod 0600 ~/.ssh/slack.id_rsa" >> ~/base.install.sh

echo "
cat > ~/.ssh/config < <-EOD
Host slack
  HostName 192.168.0.1
  User slack
  Port 22
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  PasswordAuthentication no
  IdentityFile ~/.ssh/slack.id_rsa
  IdentitiesOnly yes
EOD
" >> ~/base.install.sh

echo "Installing";
sudo -i chmod +x ~/base.install.sh
sudo -i ~/base.install.sh | tee install.log

slack --verbose --source slack:~/project web

At this point you have enough to start the bootstrap and configure slack as you please without being prompted.

Sample slack install

The files within your slack install

.
|-- etc
|   `-- roles.conf
`-- roles
    `-- web
        `-- files
        |   |-- etc
        |   |   |-- apache2
        |   |   |   |-- sites-available
        |   |   |   |   |-- project.com
        `-- scripts
            |-- postinstall
            |-- preinstall
slack@bb1:~/project$ cat etc/roles.conf 
debian: web

slack@bb1:~/project$ cat roles/web/scripts/preinstall 
#!/bin/bash

echo 'Starting installation' | wall

# install packages
apt-get -q -y --force-yes install subversion git-core apache2 memcached exim4 \
php-codesniffer php-doc php5-imagick php5-memcache libapache2-mod-php5 \
php-pear php5-cli php5-common php5-curl php5-dbg php5-dev php5-gd php5-gmp \
php5-imap php5-mcrypt php5-mhash php5-mysql php5-sqlite php5-tidy php5-xmlrpc \
php5-xsl php5-xdebug php-apc php5-memcached libelastica-php5 php5-zmq \
munin-node munin-plugins-extra mon tripwire pwgen

INSTALLER_LOG=/var/log/non-interactive-installer.log

installnoninteractive(){
  sudo bash -c "DEBIAN_FRONTEND=noninteractive aptitude install -q -y $* >> $INSTALLER_LOG"
}

installnoninteractive mysql-server

# Alternatively you can set the mysql password with debconf-set-selections
MYSQL_PASS=$(pwgen -s 12 1);
mysql -uroot -e "UPDATE mysql.user SET password=PASSWORD('${MYSQL_PASS}') WHERE user='root'; FLUSH PRIVILEGES;";
echo "MySQL Password set to '${MYSQL_PASS}'. Remember to delete ~/.mysql.passwd" | tee ~/.mysql.passwd;


slack@bb1:~/project$ cat roles/web/scripts/postinstall 
#!/bin/bash

echo 'Starting post installation' | wall

# force hostname
hostname -F /etc/hostname

a2ensite project.com

# disable sites
a2dissite default
a2dissite default-ssl

# restart apache
/etc/init.d/apache2 restart

Running

vagrant up
vagrant ssh

More examples of installing MySQL unattended can be found at Installing Java & MySQL – unattended/non-interactive installation

Comment Form

About this blog

I have been a developer for roughly 10 years and have worked with an extensive range of technologies. Whilst working for relatively small companies, I have worked with all aspects of the development life cycle, which has given me a broad and in-depth experience.