Skip to content

Instantly share code, notes, and snippets.

@KarlBaumann
Created February 23, 2016 11:37
Show Gist options
  • Save KarlBaumann/f638d81f2a7498004ef2 to your computer and use it in GitHub Desktop.
Save KarlBaumann/f638d81f2a7498004ef2 to your computer and use it in GitHub Desktop.
# -*- mode: ruby -*-
# vi: set ft=ruby :
# The full LAMP stack with phpMyAdmin is installed regardless of needing a DB for a project
# todo: Add shared network folder with host--easy of moving files between host/guest
# These values could be prompted for, if and until that's written, add them below
# VAGRANT_API_VERSION: The API version
# VM_DISPLAY_NAME: The name applied to the VM in VituralBox
# MY_SQL_VERSION: The server version
# PMA_VERSION: The phpMyAdmin version
# PMA: The phpMyAdmin file (assumes .tar.bz2)
# PMA_INSTALL_DIR: The target install directory
# STUDIO_VERSION: The Zend Studio version number only
# STUDIO: The Zend Studio file (assumes .tar.gz)
# COURSE_FILE: The course projects archive (this is a standard naming convention)
# COURSE_HAS_DB: Boolean on whether we have course database
# COURSE_DB_NAME: The course database name
# COURSE_SQL_PATH: The course sql path (<name>.sql)
# COURSE_NAME: The course name
# S3_COURSE_PATH: The S3 bucket path
# WORKSPACE: The target workspace
VAGRANTFILE_API_VERSION = "2"
VM_DISPLAY_NAME = "Security - Provisioning"
MY_SQL_VERSION = "5.5"
PMA_VERSION = "4.4.14"
PMA = "phpMyAdmin-4.4.14-english"
PMA_INSTALL_DIR = "/etc/phpmyadmin"
STUDIO_VERSION = "13.0.0"
STUDIO="ZendStudio-13.0.0-linux.gtk.x86_64"
COURSE_FILE="course-projects.tar.bz2"
COURSE_HAS_DB = "true"
COURSE_DB_NAME = "security"
COURSE_SQL_PATH = "course.sql"
COURSE_NAME = "security-training"
S3_COURSE_PATH = "security"
WORKSPACE="/home/vagrant/Zend/workspace"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
if Vagrant.has_plugin?("vagrant-cachier")
config.cache.auto_detect = true
end
config.vm.hostname = COURSE_NAME
config.vm.box = "box-cutter/ubuntu1404-desktop"
config.vm.synced_folder ".", "/vagrant", disabled: false
config.vm.provider "virtualbox" do |vb|
vb.gui = true
vb.customize ["modifyvm", :id, "--memory", "1024"]
vb.customize ["modifyvm", :id, "--name", VM_DISPLAY_NAME]
vb.name = VM_DISPLAY_NAME
end
#Provision Zend desktop image
config.vm.provision "shell", inline: <<BACKGROUND
printf 'Set Zend desktop background image...'
#If the logo exists, don't pull it.
if ! [[ -f '/usr/share/backgrounds/zend.png' ]]
then
wget -q https://s3.amazonaws.com/zend-training/#{S3_COURSE_PATH}/zend.png
cp zend.png /usr/share/backgrounds/warty-final-ubuntu.png
rm zend.png
fi
echo '[DONE: Setting up background image]'
BACKGROUND
#Provision Timezone
config.vm.provision "shell", inline: <<TIMEZONE
printf 'Set timezone to US Pacific Time...'
export DEBIAN_FRONTEND=noninteractive
NEW_SERVER_TZ=America/Los_Angeles
printf $NEW_SERVER_TZ | tee /etc/timezone
dpkg-reconfigure tzdata > /dev/null 2>&1
echo '[DONE: Setting timezone]'
TIMEZONE
#Provision Apache
config.vm.provision "shell", inline: <<INSTALL_APACHE
printf 'Installing Apache ...'
apt-get -yqq update
apt-get -yqq install apache2 apache2-utils > /dev/null 2>&1
a2enmod rewrite > /dev/null 2>&1
echo 'ServerName localhost.local' >> /etc/apache2/apache2.conf
service apache2 restart > /dev/null 2>&1
echo '[DONE: Install Apache]'
INSTALL_APACHE
#Provision MySQL
config.vm.provision "shell", inline: <<INSTALL_MYSQL
printf 'Installing MySQL...'
debconf-set-selections <<< 'mysql-server-#{MY_SQL_VERSION} mysql-server/root_password password vagrant'
debconf-set-selections <<< 'mysql-server-#{MY_SQL_VERSION} mysql-server/root_password_again password vagrant'
apt-get -yqq install mysql-server --force-yes > /dev/null 2>&1
apt-get -yqq install mysql-client --force-yes > /dev/null 2>&1
echo '[DONE: Install MySQL]'
INSTALL_MYSQL
#Provision PHP
config.vm.provision "shell", inline: <<INSTALL_PHP
printf 'Installing PHP...'
apt-get -yqq install python-software-properties > /dev/null 2>&1
add-apt-repository ppa:ondrej/php-7.0 > /dev/null 2>&1
apt-get -yqq update
apt-get -yqq install php7.0 php7.0-cli libapache2-mod-php7.0 php7.0-mysql > /dev/null 2>&1
service apache2 restart > /dev/null 2>&1
echo '[DONE: Install PHP]'
INSTALL_PHP
#Provision phpMyAdmin
config.vm.provision "shell", inline: <<INSTALL_PHPMYADMIN
printf 'Installing phpMyAdmin...'
#This conditional speeds up testing if phpMyAdmin is already installed
if ! [[ -d #{PMA_INSTALL_DIR} ]]
then
wget -q https://files.phpmyadmin.net/phpMyAdmin/#{PMA_VERSION}/#{PMA}.tar.bz2
tar xjf #{PMA}.tar.bz2 --directory .
mv #{PMA} #{PMA_INSTALL_DIR}
mv #{PMA_INSTALL_DIR}/config.sample.inc.php #{PMA_INSTALL_DIR}/config.inc.php
fi
echo '<VirtualHost *:80>
ServerName phpmyadmin
DocumentRoot #{PMA_INSTALL_DIR}
<Directory #{PMA_INSTALL_DIR}>
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>' > /etc/apache2/sites-available/phpmyadmin.conf
echo '127.0.0.1 phpmyadmin' >> /etc/hosts
echo 'Set phpMyconfig params...'
#This line removes the end PHP tag
sed -i "s/?>//g" #{PMA_INSTALL_DIR}/config.inc.php
#These lines change existing config--cannot start the command "s" segment value with a "[". It thinks it's a regex character class
sed -i "s/'auth_type'] = 'cookie'/'auth_type'] = 'config'/g" #{PMA_INSTALL_DIR}/config.inc.php
sed -i "s/'AllowNoPassword'] = false/'AllowNoPassword'] = true/g" #{PMA_INSTALL_DIR}/config.inc.php
#These lines are missing in config. We'll add them here
echo '$cfg["Servers"][$i]["user"] = "root";' >> #{PMA_INSTALL_DIR}/config.inc.php
echo '$cfg["Servers"][$i]["password"] = "vagrant";' >> #{PMA_INSTALL_DIR}/config.inc.php
a2ensite phpmyadmin > /dev/null 2>&1
service apache2 restart > /dev/null 2>&1
echo '[DONE: Install phpMyAdmin]'
INSTALL_PHPMYADMIN
#Provision Java and Studio
config.vm.provision "shell", inline: <<INSTALL_ZEND_STUDIO
printf 'Installing Oracle Java. This will take time...'
sleep 5
add-apt-repository ppa:webupd8team/java > /dev/null 2>&1
apt-get -yqq update
echo debconf shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
echo debconf shared/accepted-oracle-license-v1-1 seen true | /usr/bin/debconf-set-selections
apt-get -yqq install oracle-java8-installer > /dev/null 2>&1
echo '[DONE: Install Oracle Java]'
#This conditional speeds up testing if Studio is already installed
if ! [[ -d 'ZendStudio' ]]
then
printf 'Installing Zend Studio. This will take time...'
wget -q http://downloads.zend.com/studio-eclipse/#{STUDIO_VERSION}/#{STUDIO}.tar.gz
tar xzf #{STUDIO}.tar.gz --directory .
chown -R vagrant:vagrant ZendStudio
echo '[DONE: Install Zend Studio]'
fi
INSTALL_ZEND_STUDIO
#Provision Course Project(s) and required software
#This is course-specific provisioning
config.vm.provision "shell", inline: <<INSTALL_COURSE_PROJECT
printf 'Setting up course project...'
if ! [[ -f #{COURSE_FILE} ]]
then
wget -q https://s3.amazonaws.com/zend-training/#{S3_COURSE_PATH}/#{COURSE_FILE}
mkdir -p #{WORKSPACE}
tar xjf #{COURSE_FILE} --directory #{WORKSPACE}
chown -R vagrant:www-data #{WORKSPACE}
chmod -R 775 #{WORKSPACE}
fi
printf 'Create virtual hosts for the project...'
list=$(ls #{WORKSPACE})
for item in $list;
do
echo "
<VirtualHost *:80>
ServerName $item
DocumentRoot #{WORKSPACE}/$item
<Directory #{WORKSPACE}/$item/>
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
</Directory>
</VirtualHost>" > /etc/apache2/sites-available/$item.conf
echo "127.0.0.1 $item" >> /etc/hosts
a2ensite $item.conf > /dev/null 2>&1
done
service apache2 reload > /dev/null 2>&1
#Install nmap port scanner
printf 'Installing nmap port scanner...'
apt-get -yqq install nmap
#Install the course DB if necessary and it doesn't already exist
printf 'Setting up the course DB if necessary'
if [ #{COURSE_HAS_DB} == "true" ]
then
wget -q https://s3.amazonaws.com/zend-training/#{S3_COURSE_PATH}/#{COURSE_SQL_PATH}
printf 'Bootstrap the course MySql database...'
mysql -uroot -pvagrant -e "CREATE DATABASE #{COURSE_DB_NAME};"
mysql -uroot -pvagrant #{COURSE_DB_NAME} < #{COURSE_SQL_PATH};
fi
echo '[DONE: Set up course project in Apache]'
INSTALL_COURSE_PROJECT
#Provision environment setup
config.vm.provision "shell", inline: <<SETUPENV
printf 'Setting up the environment...'
list=$(ls #{WORKSPACE})
echo "<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<div class="container">
<h1>Welcome to Zend Training</h1>
<h3>Web applications of the course:</h3>" > /var/www/html/index.html
for item in $list;
do
echo "<a href="http://$item">$item</a></br>" >> /var/www/html/index.html
done
echo "<a href="http://phpmyadmin">phpMyAdmin</a></br>" >> /var/www/html/index.html
echo "<a href="http://php.net">PHP Manual</a></br>" >> /var/www/html/index.html
echo "
</body>
</html>" >> /var/www/html/index.html
echo '[DONE: Environment setup]'
SETUPENV
#Provision cleanup
config.vm.provision "shell", inline: <<CLEANUP
printf 'Executing cleanup detail...'
apt-get clean
apt-get autoclean
if [[ -f #{STUDIO}.tar.gz ]]
then
rm #{STUDIO}.tar.gz
fi
rm #{PMA}.tar.bz2
rm #{COURSE_FILE}
echo '[DONE: Cleanup]'
CLEANUP
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment