DevOps Se­ries An­si­ble De­ploy­ment of Elo­va­tion

The Elo­va­tion Ruby on Rails ap­pli­ca­tion was writ­ten by Chris­tos Hrousis. It can be used to track the re­sults of two-player games like chess, ta­ble ten­nis and foos­ball. This 14th ar­ti­cle in this se­ries tells us how to in­stall the Elo­va­tion Ruby on Rails ap

OpenSource For You - - Contents - By: Shak­thi Kan­nan The au­thor is a free soft­ware en­thu­si­ast and blogs at shak­thi­maan.com.

The Elo­va­tion ap­pli­ca­tion uses the Elo rat­ing sys­tem cre­ated by Ar­pad Elo, a Hun­gar­ian-born Amer­i­can physics pro­fes­sor. You can also use the Trueskill rat­ing sys­tem for teams with mul­ti­ple play­ers and still pro­vide rank­ings for in­di­vid­ual play­ers. Elo­va­tion re­quires at least Ruby on Rails 5.1 and uses the Post­greSQL data­base for its back­end. It is free and open source soft­ware and has been re­leased un­der the MIT li­cence.

GNU/Linux

An Ubuntu 16.04.1 LTS guest vir­tual ma­chine (VM) in­stance us­ing KVM/QEMU has been cho­sen to set up Elo­va­tion.

The host sys­tem is a Parabola GNU/Linux-li­bre x86_64 sys­tem, and An­si­ble is in­stalled on the host sys­tem us­ing the dis­tri­bu­tion pack­age man­ager. The ver­sion of An­si­ble used is 2.4.3.0 as in­di­cated be­low:

$ an­si­ble --ver­sion an­si­ble 2.4.3.0 con­fig file = /etc/an­si­ble/an­si­ble.cfg con­fig­ured mod­ule search path = [u’/home/shak­thi/.an­si­ble/ plug­ins/mod­ules’, u’/usr/share/an­si­ble/plug­ins/mod­ules’]

an­si­ble python mod­ule lo­ca­tion = /usr/lib/python2.7/sitepack­ages/an­si­ble ex­e­cutable lo­ca­tion = /usr/bin/an­si­ble python ver­sion = 2.7.14 (de­fault, Jan 5 2018, 10:41:29) [GCC 7.2.1 20171224]

You should add an en­try to the /etc/hosts file for the guest ‘Ubuntu’ VM as fol­lows:

192.168.122.244 ubuntu

On the host sys­tem, let’s cre­ate a project direc­tory struc­ture to store the An­si­ble play­books and in­ven­tory, as fol­lows:

an­si­ble/in­ven­tory/kvm/ /play­books/con­fig­u­ra­tion/

The Ubuntu 16.04.1 LTS server has Python 3 by de­fault, and hence we can use the same with An­si­ble. The in­ven­tory/ kvm/in­ven­tory file con­tains the fol­low­ing:

[elo­va­tion-host] ubuntu an­si­ble_host=192.168.122.244 an­si­ble_­con­nec­tion=ssh an­si­ble_user=ubuntu an­si­ble_­pass­word=ubuntu123

[elo­va­tion-host:vars] an­si­ble_python_in­ter­preter=/usr/bin/python3

You should now be able to is­sue com­mands, us­ing An­si­ble, to the guest OS. For ex­am­ple:

$ an­si­ble -i in­ven­tory/kvm/in­ven­tory ubuntu -m ping

ubuntu | SUC­CESS => { “changed”: false, “ping”: “pong” }

De­pen­den­cies

The apt-daily.ser­vice runs by de­fault on a new in­stal­la­tion of Ubuntu 16.04.1 LTS. If we need to in­stall soft­ware, the APT lock held by this ser­vice needs to be re­moved. Hence, we need to first stop this ser­vice. The APT soft­ware pack­age repos­i­tory is then up­dated be­fore in­stalling the de­pen­den­cies to set up Ruby.

--name: In­stall de­pen­den­cies hosts: ubuntu be­come: yes be­come_method: sudo gath­er_­facts: true tags: [apt]

tasks:

- name: Stop apt-daily.ser­vice shell: sys­tem­ctl kill --kill-who=all apt-daily. ser­vice

- name: Up­date the soft­ware pack­age repos­i­tory apt: up­date_­cache: yes

- name: In­stall de­pen­den­cies pack­age: name: “{{ item }}” state: lat­est with­_items: - git The above play­book can be ex­e­cuted as fol­lows:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags apt -vv -K

The -vv rep­re­sents the ver­bosity of the An­si­ble out­put. You can use up to four ‘v’s. The -K op­tion prompts for the sudo pass­word for the ubuntu user.

Rbenv

Let’s use Rbenv to set up Ruby on this vir­tual in­stance. It al­lows the in­stal­la­tion of mul­ti­ple Ruby ver­sions, and the ver­sion in pro­duc­tion can be se­lected. The source repo of Rbenv is cloned first, and then con­fig­ure and make are ex­e­cuted. The Rbenv PATH and ini­tial­i­sa­tion are then up­dated in the ~/.bashrc file.

- name: Build rbenv hosts: ubuntu tags: [rbenv]

tasks:

- name: Get rbenv git: repo: ‘https://github.com/rbenv/rbenv.git’ dest: “/home/{{ an­si­ble_user }}/.rbenv”

- name: Build rbenv shell: “cd /home/{{ an­si­ble_user }}/.rbenv && src/ con­fig­ure && make -C src”

- name: Set rbenv PATH line­in­file: path: “/home/{{ an­si­ble_user }}/.bashrc” state: present line: ‘ex­port PATH=”$HOME/.rbenv/bin:$PATH”’

- name: rbenv init line­in­file: path: “/home/{{ an­si­ble_user }}/.bashrc” state: present line: ‘eval “$(rbenv init -)”’

The play­book to set up Rbenv can be in­voked as fol­lows:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags rbenv -K

A sam­ple ex­e­cu­tion out­put is shown be­low for ref­er­ence:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags rbenv -K

SUDO pass­word:

PLAY [In­stall de­pen­den­cies] ********************************* *************************************************************

TASK [Gath­er­ing Facts] ************************************** ************************************************************* ok: [ubuntu]

PLAY [Build rbenv] ****************************************** *************************************************************

TASK [Gath­er­ing Facts] ************************************** ************************************************************* ok: [ubuntu]

TASK [Get rbenv] ******************************************** ************************************************************* ok: [ubuntu]

TASK [Build rbenv] ****************************************** ************************************************************* changed: [ubuntu]

TASK [Set rbenv PATH] *************************************** ************************************************************* ok: [ubuntu]

TASK [rbenv init] ******************************************* ************************************************************* ok: [ubuntu]

PLAY [Build ruby] ******************************************* ************************************************************* ****************************

TASK [Gath­er­ing Facts] ************************************** ************************************************************* ok: [ubuntu]

PLAY [Post­gresql] ******************************************* *************************************************************

TASK [Gath­er­ing Facts] ************************************** ************************************************************* ok: [ubuntu]

PLAY [Elo­va­tion] ******************************************** ************************************************************* TASK [Gath­er­ing Facts] ************************************** ************************************************************* ok: [ubuntu]

PLAY RE­CAP ************************************************** ************************************************************* ubuntu : ok=9 changed=1 un­reach­able=0 failed=0

Ruby

Ruby-build is a com­mand line util­ity to in­stall Ruby. Its repos­i­tory needs to be cloned into the Rbenv plug­ins folder. Ruby 2.4.0 is then in­stalled us­ing the rbenv com­mand, and the same is set as the de­fault global Ruby ver­sion in the fol­low­ing play­book:

- name: Build ruby hosts: ubuntu tags: [ruby]

vars: rben­v_­root: “/home/{{ an­si­ble_user }}/.rbenv”

tasks:

- name: Cre­ate rbenv plug­ins shell: $SHELL -lc “mkdir -p {{ rben­v_­root }}/plug­ins”

- name: Get ruby-build git: repo: ‘https://github.com/rbenv/ruby-build.git’ dest: “{{ rben­v_­root }}/plug­ins/ruby-build”

- name: In­stall Ruby 2.4.0 shell: $SHELL -lc “{{ rben­v_­root }}/bin/rbenv in­stall 2.4.0”

- name: Set Ruby 2.4.0 as Global shell: $SHELL -lc “{{ rben­v_­root }}/bin/rbenv global 2.4.0”

The above play­book can be in­voked with the ‘ruby’ tags op­tion as shown be­low:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags ruby -vv -K

Post­greSQL

Elo­va­tion uses the Post­greSQL data­base, by de­fault, as its back­end data store. Af­ter up­dat­ing the APT soft­ware pack­age repos­i­tory, we can in­stall the Post­greSQL server and a few other de­pen­den­cies. The Post­greSQL data­base server is started, and the An­si­ble play­book waits for the data­base to lis­ten on Port 5432. A sep­a­rate ap­pli­ca­tion user ac­count is cre­ated in the data­base. Al­though we hard-coded the pass­word in this ex­am­ple, in pro­duc­tion, it is rec­om­mended that you

use Vault to en­crypt and de­crypt the pass­words with An­si­ble. The lo­cal au­then­ti­ca­tion is changed from peer to md5 in the Post­greSQL pg_hba.conf con­fig­u­ra­tion file, and the data­base is restarted. The An­si­ble play­book again waits for the server to lis­ten on Port 5432, as shown be­low:

- name: Post­gresql hosts: ubuntu be­come: yes be­come_method: sudo tags: [post­gresql]

tasks:

- name: Up­date the soft­ware pack­age repos­i­tory apt: up­date_­cache: yes

- name: In­stall de­pen­den­cies pack­age: name: “{{ item }}” state: lat­est with­_items: - python3-psy­copg2 - post­gresql - post­gresql-con­trib - libpq-dev

- name: Start data­base server sys­temd: name: post­gresql state: started

- wait­_­for: port: 5432

- name: Cre­ate ap­pli­ca­tion user shell: sudo -u post­gres cre­a­teuser -s pguser

- name: Set pass­word for ap­pli­ca­tion user shell: sudo -u post­gres psql -c “AL­TER USER pguser WITH pass­word ‘pguser123’”

- name: Re­place peer auth line­in­file: path: /etc/post­gresql/9.5/main/pg_hba.conf reg­exp: ‘^lo­cal all al l peer’ line: ‘lo­cal all all md5’

- name: Restart data­base server sys­temd: name: post­gresql state: restarted - wait­_­for: port: 5432

The above play­book to in­stall and con­fig­ure the Post­greSQL data­base server can be run as shown be­low:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags post­gresql -vv -K

Elo­va­tion

The fi­nal step is to set up the Elo­va­tion Ruby on Rails ap­pli­ca­tion. The bundler soft­ware is used to track gems and the ver­sions re­quired by the Rails ap­pli­ca­tion. It is in­stalled first and then the Elo­va­tion repos­i­tory is cloned to the

HOME folder. The con­fig/data­base.yml file is cre­ated and the data­base cre­den­tials are up­dated. The bun­dle in­stall com­mand is ex­e­cuted to fetch and in­stall the re­quired gems for the ap­pli­ca­tion. The data­base is cre­ated for the ap­pli­ca­tion and the mi­gra­tions are ex­e­cuted to cre­ate the nec­es­sary ta­bles. The en­tire play­book to set up Elo­va­tion is as fol­lows:

- name: Elo­va­tion hosts: ubuntu tags: [elo­va­tion]

vars: rben­v_­root: “/home/{{ an­si­ble_user }}/.rbenv”

tasks:

- name: In­stall bundler shell: “{{ rben­v_­root }}/shims/gem in­stall bundler”

- name: Get elo­va­tion git: repo: ‘https://github.com/elo­va­tion/elo­va­tion.git’ dest: “/home/{{ an­si­ble_user }}/elo­va­tion”

- name: Cre­ate data­base.yml copy: src: “/home/{{ an­si­ble_user }}/elo­va­tion/con­fig/ data­base.yml.ex­am­ple” dest: “/home/{{ an­si­ble_user }}/elo­va­tion/con­fig/ data­base.yml” re­mote_src: yes

- name: Add data­base cre­den­tials for de­vel­op­ment data­base line­in­file: path: “/home/{{ an­si­ble_user }}/elo­va­tion/con­fig/ data­base.yml” in­sertafter: “data­base: elo­va­tion_de­vel­op­ment” line: “user­name: pguser\n pass­word: pguser123”

- name: Add data­base cre­den­tials for test data­base line­in­file:

path: “/home/{{ an­si­ble_user }}/elo­va­tion/con­fig/ data­base.yml” in­sertafter: “data­base: elo­va­tion_test” line: “user­name: pguser\n pass­word: pguser123”

- name: bun­dle in­stall shell: “{{ rben­v_­root }}/shims/bun­dle in­stall” args: chdir: “/home/{{ an­si­ble_user }}/elo­va­tion”

- name: Cre­ate data­base

shell: “RAILS_ENV=’de­vel­op­ment’ {{ rben­v_­root }}/shims/ bun­dle exec rake db:cre­ate” args:

chdir: “/home/{{ an­si­ble_user }}/elo­va­tion”

- name: Mi­grate data­base

shell: “RAILS_ENV=’de­vel­op­ment’ {{ rben­v_­root }}/shims/ bun­dle exec rake db:mi­grate” args:

chdir: “/home/{{ an­si­ble_user }}/elo­va­tion”

The above play­book can be ex­e­cuted as fol­lows:

$ an­si­ble-play­book -i in­ven­tory/kvm/in­ven­tory play­books/ con­fig­u­ra­tion/elo­va­tion.yml --tags elo­va­tion -vv -K

You can then log in to the VM, and man­u­ally start the RAILS ap­pli­ca­tion us­ing the fol­low­ing com­mand:

$ cd elo­va­tion

$ RAILS_ENV=”de­vel­op­ment” bun­dle exec rails server --bind­ing=192.168.122.244

The ap­pli­ca­tion will lis­ten on 192.168.122.244:3000, which you can open in a browser on the host sys­tem. You will see the home page, as shown in Fig­ure 1.

You can add a player by pro­vid­ing the name and e-mail ad­dress, as shown in Fig­ure 2.

You can start a new game us­ing the Trueskill rat­ing sys­tem, as shown in Fig­ure 3.

You can also cre­ate a new game with the Elo rat­ing sys­tem, as shown in Fig­ure 4.

Fig­ure 4: Cre­at­ing a new game with Elo rat­ing

Fig­ure 2: Adding a player

Fig­ure 3: Cre­at­ing a new game

Fig­ure 1: Home page

Newspapers in English

Newspapers from India

© PressReader. All rights reserved.