Skip to content

Commit

Permalink
Vagrant development environment
Browse files Browse the repository at this point in the history
  • Loading branch information
DSpeichert committed Feb 24, 2014
1 parent 6cce475 commit b335731
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ vendor/
# PHPStorm
.idea/

# Vagrant
.vagrant/
vagrant_ansible_inventory_default

# Config
public_html/config.php
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Hacking
=====
* [Slim](http://slimframework.com) framework [documentation](http://docs.slimframework.com/)
* API documentation is awesome thanks to [Swagger](https://helloreverb.com/developers/swagger). Put [Swagger Annotations](http://zircote.com/swagger-php/annotations.html) in the code!
* You can use [Vagrant](http://www.vagrantup.com/) to setup a development machine. [Install Vagrant](http://www.vagrantup.com/downloads), execute ```vagrant up``` in project root and connect to [http://localhost:8044/](http://localhost:8044/)
* Swagger docs are dynamically server at /api-docs

You can generate Swagger API docs manually:
Expand Down
78 changes: 78 additions & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
# All Vagrant configuration is done here. The most common configuration
# options are documented and commented below. For a complete reference,
# please see the online documentation at vagrantup.com.

# Every Vagrant virtual environment requires a box to build off of.
config.vm.box = "saucy64"

# The url from where the 'config.vm.box' box will be fetched if it
# doesn't already exist on the user's system.
config.vm.box_url = "http://cloud-images.ubuntu.com/vagrant/saucy/current/saucy-server-cloudimg-amd64-vagrant-disk1.box"

# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
config.vm.network :forwarded_port, guest: 80, host: 8044

# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network :private_network, ip: "192.168.33.10"

# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network :public_network

# If true, then any SSH connections made will enable agent forwarding.
# Default value: false
# config.ssh.forward_agent = true

# 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"

# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider :virtualbox do |vb|
# # Don't boot with headless mode
# vb.gui = true
#
# # Use VBoxManage to customize the VM. For example to change memory:
vb.customize ["modifyvm", :id, "--cpuexecutioncap", "50"]
vb.customize ["modifyvm", :id, "--cpus", "2"]
vb.customize ["modifyvm", :id, "--memory", "2048"]
end
#
# View the documentation for the provider you're using for more
# information on available options.

# Use rbconfig to determine if we're on a windows host or not.
require 'rbconfig'
is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
if is_windows
# Provisioning configuration for shell script.
config.vm.provision "shell" do |sh|
sh.path = "ansible/ansible-in-vm.sh"
sh.args = "ansible/playbook.yml ansible/inventory"
end
else
# Provisioning configuration for Ansible (for Mac/Linux hosts).
config.vm.provision "ansible" do |ansible|
ansible.playbook = "ansible/playbook.yml"
ansible.inventory_path = "ansible/inventory"
ansible.sudo = true
end
end

end
8 changes: 8 additions & 0 deletions ansible/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
This is a directory holding [Ansible](http://docs.ansible.com/) playbook that is used to provision the development environment created by [Vagrant](http://www.vagrantup.com/).


Run ```vagrant up``` in the root directory (above this one) to start a Virtual Machine with this project. Then connect to [http://localhost:8044/](http://localhost:8044/).

Remember that first you need to [install Vagrant](http://www.vagrantup.com/downloads) and [Virtual Box](https://www.virtualbox.org/wiki/Downloads).

This directory is not needed for a real deployment, you can safely delete it.
41 changes: 41 additions & 0 deletions ansible/ansible-in-vm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
#
# Windows shell provisioner for Ansible playbooks, based on KSid's
# windows-vagrant-ansible: https://github.com/KSid/windows-vagrant-ansible
#
# @todo - Allow proxy configuration to be passed in via Vagrantfile config.
#
# @see README.md
# @author Jeff Geerling, 2014
# @version 1.0
#

# Uncomment if behind a proxy server.
# export {http,https,ftp}_proxy='http://username:password@proxy-host:80'

ANSIBLE_PLAYBOOK=$1
ANSIBLE_HOSTS=$2
TEMP_HOSTS="/tmp/ansible_hosts"

if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
echo "Cannot find Ansible playbook."
exit 1
fi

if [ ! -f /vagrant/$ANSIBLE_HOSTS ]; then
echo "Cannot find Ansible hosts."
exit 2
fi

# Install Ansible and its dependencies if it's not installed already.
if [ ! -f /usr/bin/ansible ]; then
echo "Installing Ansible dependencies and Git"
aptitude install -y git python python-pip python-dev
echo "Installing Ansible"
pip install ansible
fi

cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} && chmod -x ${TEMP_HOSTS}
echo "Running Ansible provisioner defined in Vagrantfile."
ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --extra-vars "is_windows=true" --connection=local
rm ${TEMP_HOSTS}
1 change: 1 addition & 0 deletions ansible/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
127.0.0.1
19 changes: 19 additions & 0 deletions ansible/playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
- hosts: all
tasks:
- include: tasks/mysql.yml
- include: tasks/nginx.yml
- include: tasks/php5-fpm.yml
- include: tasks/tfs.yml
- include: tasks/DevAAC.yml

handlers:
- name: restart nginx
service: name=nginx state=restarted

- name: restart php5-fpm
service: name=php5-fpm state=restarted

- name: restart mysql
service: name=mysql state=restarted

11 changes: 11 additions & 0 deletions ansible/tasks/DevAAC.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Install composer
shell: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin
creates=/usr/local/bin/composer.phar

- name: Install requirements with composer
shell: cd /vagrant && composer.phar install

- name: Copy config.php
template: src=templates/config.php dest=/home/vagrant/config.php

11 changes: 11 additions & 0 deletions ansible/tasks/mysql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
- name: Install Mysql package
apt: pkg={{ item }} state=present
with_items:
- mysql-server
- python-mysqldb

- name: Start Mysql Service
service: name=mysql state=started enabled=true


12 changes: 12 additions & 0 deletions ansible/tasks/nginx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- name: Install nginx
apt: pkg=nginx state=present

- name: Copy nginx configuration for DevAAC
template: src=templates/nginx-vhost.conf dest=/etc/nginx/sites-available/default
notify: restart nginx

- name: Start nginx Service
service: name=nginx state=started enabled=true


32 changes: 32 additions & 0 deletions ansible/tasks/php5-fpm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
- name: Install php5-fpm
apt: pkg={{ item }} state=present
with_items:
- php5-fpm
- php5-apcu
- php5-mysqlnd
- php5-json
- php5-cli

- name: Set php.ini display_errors
lineinfile: dest=/etc/php5/fpm/php.ini line="display_errors = On" regexp="^display_errors =" state=present
notify: restart php5-fpm

- name: Set php.ini default_timezone
lineinfile: dest=/etc/php5/fpm/php.ini line="date.timezone = America/New_York" regexp="date.timezone =" state=present
notify: restart php5-fpm

- name: Magically append vagrant-specific config.php for DevAAC
lineinfile: dest=/etc/php5/fpm/php.ini line="^auto_prepend_file =" regexp="auto_prepend_file = /home/vagrant/config.php" state=present
notify: restart php5-fpm

- name: Disable default pool
command: mv /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/www.disabled creates=/etc/php5/fpm/pool.d/www.disabled
notify: restart php5-fpm

- name: Copy php5-fpm configuration
template: src=templates/php5-fpm-pool.conf dest=/etc/php5/fpm/pool.d/DevAAC.conf
notify: restart php5-fpm

- name: Start php5-fpm Service
service: name=php5-fpm state=started enabled=true
15 changes: 15 additions & 0 deletions ansible/tasks/tfs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
- name: Download latest TFS from git
git: repo=https://github.com/otland/forgottenserver.git
dest=/home/vagrant/forgottenserver

- name: Create database user for TFS
mysql_user: name=forgottenserver password="" priv=forgottenserver.*:ALL state=present check_implicit_admin=yes

- name: Create database for TFS
mysql_db: name=forgottenserver state=present

- name: Import MySQL database from schema.sql
shell: (cat /home/vagrant/forgottenserver/schema.sql | mysql forgottenserver) && touch /home/vagrant/.imported_db
creates=/home/vagrant/.imported_db

13 changes: 13 additions & 0 deletions ansible/templates/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
// AT MINIMUM UNCOMMENT AND CONFIGURE ONE OF THE FOLLOWING OPTIONS:

// OPTION ONE: READ MYSQL CONFIG FROM config.lua - RECOMMENDED
define('TFS_CONFIG', '/home/vagrant/forgottenserver/config.lua');

// OPTION TWO: SPECIFY MYSQL CONNECTION DETAILS HERE
// define('TFS_CONFIG', array('mysqlHost' => '127.0.0.1', 'mysqlDatabase' => 'tfs', 'mysqlUser' => 'tfs', 'mysqlPass' => 'tfs'));
// THIS OPTION IS DISCOURAGED AS SOME CODE MIGHT DEPEND ON OTHER VALUES FROM TFS CONFIG

// IF YOU WANT TO CHANGE SOMETHING ELSE, BE SMART AND FOLLOW THE PATTERN
define('CORS_ALLOW_ORIGIN', '*');
define('ENABLE_DEBUG', true);
26 changes: 26 additions & 0 deletions ansible/templates/nginx-vhost.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server {
listen 80 default_server;
server_name _;
root /vagrant/public_html;

client_max_body_size 64M;

location / {
index index.html index.php;
try_files $uri $uri/ /index.php;
}

location ~* \.(gif|jpg|jpeg|png|css|js)$ {
expires -1;
}

location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm_DevAAC.sock;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
14 changes: 14 additions & 0 deletions ansible/templates/php5-fpm-pool.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[DevAAC]
listen = /var/run/php5-fpm_DevAAC.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
user = vagrant
group = vagrant
pm = dynamic
pm.max_children = 10
pm.start_servers = 1
pm.min_spare_servers = 1
pm.max_spare_servers = 3
pm.max_requests = 500
#php_admin_value[open_basedir] = /vagrant/:/tmp
2 changes: 1 addition & 1 deletion public_html/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function PageUrl(page) {
return "pages/" + page + ".html";
}
function ApiUrl(link) {
return "http://duots.dondaniello.com/api/" + link;
return "/api/" + link;
}

/*
Expand Down

0 comments on commit b335731

Please sign in to comment.