Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,10 @@ workingPath/
*.ini
!travisTests.ini

ansible/inventory
ansible/settings.yml
ansible/*.retry

*.swp
*.gch
*.exe
Expand Down
3 changes: 3 additions & 0 deletions ansible/ansible.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[defaults]
module_lang = en_US.UTF-8

17 changes: 17 additions & 0 deletions ansible/primary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
- name: Get halite code on server
hosts: all
vars_files:
- settings.yml
roles:
- common

- name: Setup combined website and database server
hosts: all
become: yes
vars_files:
- settings.yml
roles:
- webserver
- dbserver

42 changes: 42 additions & 0 deletions ansible/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Introduction

This defines an [Ansible](http://www.ansible.com/) playbook that will deploy
the Halite website and database onto a single server. The playbook assumes
a basically fresh Ubuntu 16.04 OS install and that it will be the single use
for the server.

WARNING: Do not deploy this to a server you are using for other purposes. This
will probably interfere with whatever you are already doing on the server.

# Prerequisites

On the server you need a user that the Halite code will live under, can be
accessed with SSH and has sudo permissions. Python must also be installed,
`sudo apt install python` will do that.

Locally you need ansible installed, `pip install ansible`.

An `inventory` file in this directory with the line:
MY.SERVER.HOSTNAME remote_user="USERNAME"

Where MY.SERVER.HOSTNAME is either the domain or ip address to your server and
USERNAME is the user on the server for the Halite code.

Also a `settings.yml` file filled in with the settings shown in
`settings_example.yml`.

If you will be using ssh password authentication instead of key based
authentication, you also need `sshpass` installed locally, `sudo apt install
sshpass`.

# Running the playbook

After getting the prerequisites setup above, to deploy to the server simply run:

ansible-playbook -i inventory primary.yml

If you need to specify an ssh password, use the -k option. Similarly if a sudo
password is need use -K. For example if you need both ssh and sudo passwords it
would look something like:

ansible-playbook -i inventory primary.yml -k -K
7 changes: 7 additions & 0 deletions ansible/roles/common/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- name: Get halite code
git:
repo: "{{ halite_src }}"
version: "{{ halite_version }}"
dest: "{{ halite_dest }}"

39 changes: 39 additions & 0 deletions ansible/roles/dbserver/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
- name: Install apt packages
apt: name={{ item }} update_cache=yes cache_valid_time=7200
with_items:
- mysql-server
- python-mysqldb

- name: Check mysql using password
command: mysql -u root -e "SELECT * FROM mysql.user WHERE User='root' AND plugin!='mysql_native_password';"
register: mysql_use_password
changed_when: mysql_use_password.stdout != ""

- name: Use password for mysql root auth
when: mysql_use_password.stdout != ""
command: mysql -u root -e "UPDATE mysql.user SET plugin = 'mysql_native_password' WHERE User='root'; FLUSH PRIVILEGES;"

- name: Set mysql root password
mysql_user:
name: root
password: "{{ mysql_root_pw }}"

- name: Set .my.cnf file
template: src=my.cnf.j2 dest={{ item.dst }} owner={{ item.owner }} mode=0640
with_items:
- { dst: "/home/{{ remote_user }}/.my.cnf", owner: "{{ remote_user }}" }
- { dst: /root/.my.cnf, owner: root }

- name: Check halite database exists
command: mysql -e 'use Halite'
register: database_check
changed_when: database_check.rc != 0
failed_when: database_check.rc != 0 and not database_check.stderr.startswith("ERROR 1049")

- name: Create halite database
when: database_check.rc != 0
mysql_db:
name: Halite
state: import
target: /home/{{ remote_user }}/Halite/website/sql/schema.sql
3 changes: 3 additions & 0 deletions ansible/roles/dbserver/templates/my.cnf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[client]
user=root
password={{ mysql_root_pw }}
6 changes: 6 additions & 0 deletions ansible/roles/webserver/handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
- name: reload apache2
service: name=apache2 state=reloaded

- name: restart apache2
service: name=apache2 state=restarted
74 changes: 74 additions & 0 deletions ansible/roles/webserver/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
- name: Add apt repository
apt_repository: repo="ppa:ondrej/php"

- name: Install apt packages
apt: name={{item}} update_cache=yes cache_valid_time=7200
with_items:
- apache2
- libssl-dev
- php5.6
- php5.6-mysql
- python3
- python3-pip
- zip

- name: Install pip packages
pip: name={{item}} executable=pip3
with_items:
- trueskill
- boto
- paramiko
- pymysql

- name: Install composer
shell: curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
args:
creates: /usr/local/bin/composer

- name: Install php packages
become: true
become_user: "{{ remote_user }}"
shell: composer install
args:
chdir: "{{ halite_dest }}/website/"
creates: "{{ halite_dest }}/website/vendor/"

- name: Symlink to webserver directory
file:
src: "{{ halite_dest }}"
dest: /var/www/Halite
state: link

- name: Configure apache
template: src=apache.conf.j2 dest=/etc/apache2/sites-enabled/000-default.conf
notify: reload apache2

- name: Enable apache modules
apache2_module: name={{ item }} state=present
with_items:
- rewrite
- expires
notify: restart apache2

- name: Set php max upload size
lineinfile:
dest: /etc/php/5.6/apache2/php.ini
line: upload_max_filesize = 150M
regexp: "^upload_max_filesize ="
state: present
notify: restart apache2

- name: Set php max post size
lineinfile:
dest: /etc/php/5.6/apache2/php.ini
line: post_max_size = 151M
regexp: "^post_max_size ="
state: present
notify: restart apache2

- name: Set halite.ini
template:
src: halite.ini.j2
dest: "{{ halite_dest }}/halite.ini"
owner: "{{ remote_user }}"
14 changes: 14 additions & 0 deletions ansible/roles/webserver/templates/apache.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<VirtualHost *:80>
ServerName {{ webdomain }}

ServerAdmin webmaster@{{ webdomain }}
DocumentRoot /var/www/Halite/website

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory "/var/www/Halite/website">
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
6 changes: 6 additions & 0 deletions ansible/roles/webserver/templates/halite.ini.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[database]
hostname = localhost
username = root
password = {{ mysql_root_pw }}
name = Halite

17 changes: 17 additions & 0 deletions ansible/settings_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
# Halite code repository to use. Change as appropriate if you want to install
# something other than the public source.
halite_src: https://github.com/HaliteChallenge/Halite.git

# The branch or revision to use from the above repository.
halite_version: HEAD

# Where to put the code
halite_dest: /home/{{ remote_user }}/Halite

# domain name to use for the website
webdomain: halite.MYDOMAIN

# password to use for the mysql root user.
mysql_root_pw: MAKEMESECRET