Skip to content

Commit bc5d74b

Browse files
committed
Install Flask API via Ansible
In a previous commit we have installed docker. For the API to be able to run, we need also python3 and several packages. We install them (if not yet present). Afterwards, the `flaskAPIwDocker` directory is coopied to the remote host and run with `docker-compose`. Therefore, this ansible script will set up a basic ubuntu system, install everything necessary for the API to run and start it up with one command only. Further: * added typical entries (IDE specific, build stuff, ...) to gitignore (cf., * https://www.gitignore.io/api/go,code,linux,macos,windows,eclipse,jetbrains)
1 parent 45ed109 commit bc5d74b

12 files changed

Lines changed: 133 additions & 17 deletions

File tree

.gitignore

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
nodeAPI
2-
2+
**/__pycache__/
33
# Created by https://www.gitignore.io/api/go,code,linux,macos,windows,eclipse,jetbrains
44
# Edit at https://www.gitignore.io/?templates=go,code,linux,macos,windows,eclipse,jetbrains
55

6-
### Build Artifacts & Generated Stuff ###
7-
fenrir
8-
deployment/e2e-k8s-job/*.yaml
9-
venv
10-
116
### Code ###
127
.vscode/*
138
# !.vscode/settings.json

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Added
9-
* ansible playbook that provisions a cron-job that runs every midnight and should trigger aggregator
10-
and routines
9+
* ansible playbook that provisions a cron-job that runs every midnight and should trigger aggregator and routines
10+
* ansible role to install docker and docker-compose
1111

1212
[Unreleased]: https://github.com/maxisses/Covid-19API/compare/feature/add-aggregation-cron-job?expand=1

ansible/README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ In the command line run
88
```shell
99
ansible-playbook playbook.yaml
1010
```
11-
to get everything set up.
11+
to get everything set up. That is:
12+
* create a cron job
13+
* install docker (if not installed)
14+
* install python3 (if not installed) and necessary packages
15+
* run the `flaskAPIwDocker` in a docker container
16+
* Profit! ;)
1217

1318
You can specify user, remote IP, host-key check, etc. optionally, like this:
1419
```shell
@@ -17,12 +22,44 @@ ansible-playbook \
1722
--key-file ~/.ssh/<key-file>.pem \ # provide SSH key
1823
--ssh-common-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' \ # skip host key checking
1924
-i <inventory-file> \ # use different inventory file
25+
-b \ # become
2026
-vvvv \ # very verbose
2127
playbook.yaml
2228
```
2329

30+
**Note**, that the setup is designed to run on an Ubuntu Linux with user `ubuntu` since ansible tasks
31+
are using apt only (this may change in future versions). The provisioning can easily be adapted by
32+
changing all fields with `CHANGE` mentions.
33+
2434
## Test
2535
For testing, within the specific role, execute:
2636
```shell
27-
molecule converge # --debug optionally
37+
molecule converge [--debug]
38+
```
39+
40+
## Example Using AWS EC2 Instance
41+
### Prerequisites
42+
* AWS Account
43+
* ansible is installed on your local system
44+
45+
### Next Steps
46+
* in the AWS console, create an EC2 instance with public IP and SSH keypair
47+
* copy the private key and change permissions to "read only" (`400`)
48+
* note the public IP of the EC2 instance
49+
* modify the `./ansible/inventory` file to use your IP
50+
* from local, run:
51+
```shell script
52+
ansible-playbook \
53+
-u ubuntu \
54+
--key-file <path-to-key-file>.pem \
55+
--ssh-common-args '-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null' \
56+
-i <inventory-file> \
57+
-b \
58+
-vvvv \
59+
./ansible/playbook.yaml
60+
```
61+
* wait a few minutes
62+
After that, you can access the API via `<publicIP>:5000/<endpoint>`, e.g.,
63+
```shell script
64+
curl 35.180.191.100:5000/get_total_cases
2865
```

ansible/inventory

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
[targets]
2-
localhost ansible_connection=local
1+
[remote]
2+
35.180.178.217 # CHANGE
3+
[local]
4+
localhost ansible_connection=local

ansible/playbook.yaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
- hosts: all
3-
remote_user: user #TODO
3+
remote_user: ubuntu # CHANGE
44
become_user: root
55
roles:
66
- role: cron-job
7-
- role: geerlingguy.docker
7+
- role: geerlingguy.docker
8+
- role: flask-api

ansible/roles/cron-job/tasks/main.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
src: ../../../../fillDatabase/aggregate.py
55
dest: "{{ project_remote_dir }}"
66
owner: ubuntu # CHANGE
7-
mode: '0644'
7+
mode: '0755'
88

99
- name: Install cron
1010
become: yes
@@ -18,4 +18,4 @@
1818
name: "Aggregate case data"
1919
minute: 0
2020
hour: 0
21-
job: "{{ aggregator_command }} > /dev/null 2>&1"
21+
job: "{{ aggregator_command }} > /dev/null 2>&1"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
project_remote_dir: /home/ubuntu # CHANGE
3+
api_image_name: flaskapiwdocker_web_1
4+
postgres_image_name: flaskapiwdocker_postgresql_1
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
- name: Perform Pre-Installation Tasks
3+
include_tasks: prerequisites.yaml
4+
5+
- name: Tear down existing services
6+
docker_compose:
7+
project_src: "{{ project_remote_dir }}/flaskAPIwDocker"
8+
state: absent
9+
10+
- name: Create and start services
11+
docker_compose:
12+
project_src: flaskAPIwDocker
13+
register: output
14+
15+
- debug:
16+
var: output
17+
18+
- name: Run `docker-compose up` again
19+
docker_compose:
20+
project_src: "{{ project_remote_dir }}/flaskAPIwDocker"
21+
build: no
22+
register: output
23+
24+
- debug:
25+
var: output
26+
27+
- assert:
28+
that: "not output.changed "
29+
30+
- name: Stop all services
31+
docker_compose:
32+
project_src: "{{ project_remote_dir }}/flaskAPIwDocker"
33+
build: no
34+
stopped: yes
35+
register: output
36+
37+
- debug:
38+
var: output
39+
40+
- assert:
41+
that:
42+
- "not web.{{ api_image_name }}.state.running"
43+
44+
- name: Restart all services
45+
docker_compose:
46+
project_src: "{{ project_remote_dir }}/flaskAPIwDocker"
47+
build: no
48+
restarted: yes
49+
register: output
50+
51+
- debug:
52+
var: output
53+
54+
- assert:
55+
that:
56+
- "web.{{ api_image_name }}.state.running"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
- name: Install Python plus utility packages
3+
become: yes
4+
apt:
5+
name:
6+
- python3-pip
7+
- python3-virtualenv
8+
- python3-dev
9+
- virtualenv
10+
- python3-docker
11+
12+
- name: "Install Python3 docker compose"
13+
pip:
14+
name: docker-compose
15+
16+
- name: "Copy API project directory to remote host's home"
17+
copy:
18+
src: ../../../../flaskAPIwDocker
19+
dest: "{{ project_remote_dir }}"
20+
owner: ubuntu # CHANGE
21+
mode: '0644'

ansible/roles/geerlingguy.docker/defaults/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ docker_yum_gpg_key: https://download.docker.com/linux/centos/gpg
3030
# A list of users who will be added to the docker group.
3131
docker_users:
3232
- root
33-
- ubuntu
33+
- ubuntu # CHANGE

0 commit comments

Comments
 (0)