From b335731d5dbeb35d8cd452e0b64ce5888de0c8b9 Mon Sep 17 00:00:00 2001 From: Daniel Speichert Date: Sun, 23 Feb 2014 19:58:31 -0500 Subject: [PATCH] Vagrant development environment --- .gitignore | 4 ++ README.md | 1 + Vagrantfile | 78 ++++++++++++++++++++++++++++ ansible/README.md | 8 +++ ansible/ansible-in-vm.sh | 41 +++++++++++++++ ansible/inventory | 1 + ansible/playbook.yml | 19 +++++++ ansible/tasks/DevAAC.yml | 11 ++++ ansible/tasks/mysql.yml | 11 ++++ ansible/tasks/nginx.yml | 12 +++++ ansible/tasks/php5-fpm.yml | 32 ++++++++++++ ansible/tasks/tfs.yml | 15 ++++++ ansible/templates/config.php | 13 +++++ ansible/templates/nginx-vhost.conf | 26 ++++++++++ ansible/templates/php5-fpm-pool.conf | 14 +++++ public_html/app.js | 2 +- 16 files changed, 287 insertions(+), 1 deletion(-) create mode 100644 Vagrantfile create mode 100644 ansible/README.md create mode 100644 ansible/ansible-in-vm.sh create mode 100644 ansible/inventory create mode 100644 ansible/playbook.yml create mode 100644 ansible/tasks/DevAAC.yml create mode 100644 ansible/tasks/mysql.yml create mode 100644 ansible/tasks/nginx.yml create mode 100644 ansible/tasks/php5-fpm.yml create mode 100644 ansible/tasks/tfs.yml create mode 100644 ansible/templates/config.php create mode 100644 ansible/templates/nginx-vhost.conf create mode 100644 ansible/templates/php5-fpm-pool.conf diff --git a/.gitignore b/.gitignore index 2648e02..23dcb83 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,9 @@ vendor/ # PHPStorm .idea/ +# Vagrant +.vagrant/ +vagrant_ansible_inventory_default + # Config public_html/config.php diff --git a/README.md b/README.md index 141e2b8..8541765 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Vagrantfile b/Vagrantfile new file mode 100644 index 0000000..7555812 --- /dev/null +++ b/Vagrantfile @@ -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 diff --git a/ansible/README.md b/ansible/README.md new file mode 100644 index 0000000..ffa901f --- /dev/null +++ b/ansible/README.md @@ -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. diff --git a/ansible/ansible-in-vm.sh b/ansible/ansible-in-vm.sh new file mode 100644 index 0000000..deb02da --- /dev/null +++ b/ansible/ansible-in-vm.sh @@ -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} diff --git a/ansible/inventory b/ansible/inventory new file mode 100644 index 0000000..e56ea71 --- /dev/null +++ b/ansible/inventory @@ -0,0 +1 @@ +127.0.0.1 \ No newline at end of file diff --git a/ansible/playbook.yml b/ansible/playbook.yml new file mode 100644 index 0000000..ce43207 --- /dev/null +++ b/ansible/playbook.yml @@ -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 + \ No newline at end of file diff --git a/ansible/tasks/DevAAC.yml b/ansible/tasks/DevAAC.yml new file mode 100644 index 0000000..8958ef9 --- /dev/null +++ b/ansible/tasks/DevAAC.yml @@ -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 + diff --git a/ansible/tasks/mysql.yml b/ansible/tasks/mysql.yml new file mode 100644 index 0000000..7864e06 --- /dev/null +++ b/ansible/tasks/mysql.yml @@ -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 + + \ No newline at end of file diff --git a/ansible/tasks/nginx.yml b/ansible/tasks/nginx.yml new file mode 100644 index 0000000..4bbeec0 --- /dev/null +++ b/ansible/tasks/nginx.yml @@ -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 + + \ No newline at end of file diff --git a/ansible/tasks/php5-fpm.yml b/ansible/tasks/php5-fpm.yml new file mode 100644 index 0000000..ea120aa --- /dev/null +++ b/ansible/tasks/php5-fpm.yml @@ -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 diff --git a/ansible/tasks/tfs.yml b/ansible/tasks/tfs.yml new file mode 100644 index 0000000..3a2c3f4 --- /dev/null +++ b/ansible/tasks/tfs.yml @@ -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 + diff --git a/ansible/templates/config.php b/ansible/templates/config.php new file mode 100644 index 0000000..ad69c0c --- /dev/null +++ b/ansible/templates/config.php @@ -0,0 +1,13 @@ + '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); diff --git a/ansible/templates/nginx-vhost.conf b/ansible/templates/nginx-vhost.conf new file mode 100644 index 0000000..5ab1710 --- /dev/null +++ b/ansible/templates/nginx-vhost.conf @@ -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; + } +} diff --git a/ansible/templates/php5-fpm-pool.conf b/ansible/templates/php5-fpm-pool.conf new file mode 100644 index 0000000..e49c97d --- /dev/null +++ b/ansible/templates/php5-fpm-pool.conf @@ -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 diff --git a/public_html/app.js b/public_html/app.js index d7a435d..380624a 100644 --- a/public_html/app.js +++ b/public_html/app.js @@ -5,7 +5,7 @@ function PageUrl(page) { return "pages/" + page + ".html"; } function ApiUrl(link) { - return "http://duots.dondaniello.com/api/" + link; + return "/api/" + link; } /*