---
# System prepended metadata

title: CREDENTIALS

---

# CREDENTIALS

Team:
* Daniil Manakovskiy (d.manakovskiy@innopolis.university)
* Sergey Makarov (s.makarov@innopolis.university)
* Maks Kureikin (m.kureykin@innopolis.university)

[TOC]

## Apllication description
The application used is a bit modified Innopolis University's sport course service (https://sport.innopolis.university). 

Original source code: https://github.com/WinnerOK/SWP_sport_back
Modified source code: https://github.com/SgMakarov/SWP_sport_back

## Machines
Zerotier network: `af415e486fac1c46`
### InnoVM (for CI)
ip (innopolis net): `10.90.138.210`
ip (zerotier): `192.168.196.167`
login: `root`
password: `R00tR00t`

### InnoVM (web1)
ip (innopolis net): `10.90.138.215`
ip (zerotier): `192.168.196.14`
login: `root`
password: `R00tR00t`

### InnoVM (web2)
ip (innopolis net): `10.90.138.216`
ip (zerotier): `192.168.196.25`
login: `root`
password: `R00tR00t`

### InnoVM (db)
ip (innopolis net) `10.90.138.217`
ip (zerotier): `192.168.196.120`
login: `root`
password: `R00tR00t`

### InnoVM (backup)
ip (innopolis net) `10.90.138.219`
ip (zerotier): `192.168.196.232`
login: `root`
password: `RootRott123_`

### AWS (load balancer)

ip (public): `3.131.64.143`
ip (zerotier): `192.168.196.10`
login: ubuntu

## Services
### CI
* ssh: ubuntu@ci.snafinal.sgmakarov.ru
* jenkins: `ciadmin`:`jfkdls;a`
* gh token: `d9683b5ae03224b868ce1f1b4f6d752c8a317d21`

### DB
user: `pguser`
password: `qwertypassword`
database: `sport_database`

### Sport
username: `root`
password: `qwertyytrewq`


## To backup

### from CI:
* `/var/lib/jenkins`
### from web[1-2]
* `/var/app/`
### from load balancer
* `/etc/haproxy`
### from DB
* `/var/db`


## /etc/hosts/
192.168.196.167	ci_server
192.168.196.14	web_server1
192.168.196.25 web_server2
192.168.196.120	db_server
192.168.196.10  balancer_server
192.168.196.232 backup_server

# CONFIGS

## Load balancer

![](https://i.imgur.com/p4THF2o.png)

![](https://i.imgur.com/CK1CpRV.png)

![](https://i.imgur.com/Qr1f2Fa.png)

## Bacula

![](https://i.imgur.com/Lmun8BV.png)

![](https://i.imgur.com/gj1OVq9.jpg)

![](https://i.imgur.com/YYsZYsM.jpg)

![](https://i.imgur.com/GXIxIAv.png)

Here are represented only full level backups available for manual start up:
![](https://i.imgur.com/Z2nWziF.jpg)

![](https://i.imgur.com/dAECpiM.jpg)

![](https://i.imgur.com/ibNLwcq.jpg)

## Jenkins

We have installed the following plugins and their dependencies:
* all default plugins
* Blue Ocean
* Ansible plugin
* Docker plugin
* Git plugin

Jenkinsfile:

```jenkinsfile
pipeline {
  agent any
  stages {
    stage('Test') {
      post {
        cleanup {
          echo 'Cleanup...'
          sh 'docker-compose down -v --rmi all && rm docker-compose.yml'
        }

      }
      steps {
        echo 'Testing..'
        sh '''
          ln -s docker-compose-test.yml docker-compose.yml
          echo "Building test images..."
          docker-compose build --no-cache
          docker-compose up -d
          echo "Waiting for database to go up..."
          docker-compose exec -T adminpanel bash -c "while !</dev/tcp/db/5432; do sleep 1; done;"  
          echo "Making migrations..."
          docker-compose exec -T adminpanel python manage.py makemigrations
          docker-compose exec -T adminpanel python manage.py migrate
          echo "Running tests..."
          docker-compose exec -T adminpanel pytest
          echo "done"
          '''
      }
    }

    stage('Build') {
      environment {
        PYTHON_VERSION = credentials('python-version')
      }
      steps {
        echo 'Building..'
        script {
          dockerInstanceDjango = docker.build("winnerokay/sna-app", '-t latest --build-arg PYTHON_VERSION=$PYTHON_VERSION ./adminpage')
          dockerInstanceNginx = docker.build("winnerokay/sna-app-nginx", '-t latest ./nginx')
        }

      }
    }

    stage('Migrate') {
      environment {
        ENV_FILE = credentials('production-envfile')
      }
      steps {
        echo 'Making migrations to the db...'
        sh ' docker run --env-file $ENV_FILE -e POSTGRES_SERVER=$(getent hosts db_server | awk \'{ print $1 }\') winnerokay/sna-app:latest bash -c "python manage.py makemigrations && python manage.py migrate"'
      }
    }

    stage('Push to registry') {
      environment {
        registryCredentialSet = 'dockerhub'
      }
      steps {
        echo 'Publishing....'
        script {
          docker.withRegistry('', registryCredentialSet){
            dockerInstanceDjango.push("${env.GIT_COMMIT}")
            dockerInstanceDjango.push("latest")
            dockerInstanceNginx.push("${env.GIT_COMMIT}")
            dockerInstanceNginx.push("latest")
          }
        }

      }
    }

    stage('Deploy') {
      environment {
        ansibleInventoryFile = credentials('ansible-inventory-web')
        productionEnvfile = credentials('production-envfile')
      }
      steps {
        echo 'Deploying...'
        ansiblePlaybook(
          playbook: "deploy_web.yml",
          inventory: "$ansibleInventoryFile",
          extras: '-e envfilePath=$productionEnvfile'
        )
      }
    }

  }
}

```

## Ansible

### Inventory
```ini
[snafinal_web]
# web 1
10.90.138.215
# web 2
10.90.138.216

[snafinal_db]
10.90.138.217

[snafinal_ci]
10.90.138.210

[snafinal_backup]
10.90.138.219

[snafinal_loadbalancer]


[snafinal_loadbalancer:vars]
ansible_user=ubuntu

[snafinal:children]
snafinal_web
snafinal_db
snafinal_loadbalancer
snafinal_ci
snafinal_backup

[snafinal:vars]
ansible_user=ansible
ansible_password=qwertyytrewq

```

### Add all hosts to `/etc/hosts`
```yaml
- name: add addresses
  hosts: snafinal
  become: true
  tasks:
    - name: Ensure CI server present
      lineinfile:
        path: /etc/hosts
        regexp: 'ci_server'
        line: 192.168.196.167	ci_server
    
    - name: Ensure web1 server present
      lineinfile:
        path: /etc/hosts
        regexp: 'web_server1'
        line: 192.168.196.14	web_server1

    - name: Ensure web2 server present
      lineinfile:
        path: /etc/hosts
        regexp: 'web_server2'
        line: 192.168.196.25 web_server2

    - name: Ensure db server present
      lineinfile:
        path: /etc/hosts
        regexp: 'db_server'
        line: 192.168.196.120	db_server

    - name: Ensure balancer server present
      lineinfile:
        path: /etc/hosts
        regexp: 'balancer_server'
        line: 192.168.196.10  balancer_server

    - name: Ensure backup server present
      lineinfile:
        path: /etc/hosts
        regexp: 'backup_server'
        line: 192.168.196.232 backup_server
   
```

### Setup bacula client
```yaml
- name: setup bacula
  hosts: snafinal_web:snafinal_db:snafinal_ci:snafinal_loadbalancer
  become: true
  tasks:
    - name: Update apt repos
      apt:
        update_cache: yes
    
    - name: Install bacula client
      apt:
        state: present
        name:
          - bacula-client
```

### Install docker
```yaml
---
- name: docker_setup
  hosts: snafinal_web
  become: true
  tasks:
     - name: download docker
       get_url:
         url: https://get.docker.com
         dest: /tmp/get_docker.sh
         mode: '0770'
  
     - name: install docker
       shell: /tmp/get_docker.sh

     - name: download docker-compose
       get_url:
         url: "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-Linux-x86_64"
         dest: /usr/local/bin/docker-compose
         mode: '0770'

```


### Add to zerotier network
```yaml
---
- name: setup_zerotier
  hosts: snafinal
  tasks:

    - name: Download zerotier
      get_url:
        url: https://install.zerotier.com
        dest: /tmp/get_zerotier.sh
        mode: '0770'
    
    - name: Install zerotier
      become: true
      shell: /tmp/get_zerotier.sh

    - name: Join network
      become: true
      shell: zerotier-cli join af415e486fac1c46

    - name: Print zerotier IDs
      become: true
      command: zerotier-cli info
      register: info
    
    - debug: msg="{{info.stdout}}"

```

### Start database
```yaml
- name: start postgres
  hosts: snafinal_db
  tasks:
    - name: Make db_data folder
      become: true
      file:
        path: /var/db/data
        state: directory
    
    
    - name: Run database container
      become: true
      shell: docker run -d -p 5432:5432 --name db -v /var/db/data:/var/lib/postgresql/data -e POSTGRES_USER=pguser -e POSTGRES_PASSWORD=qwertypassword -e POSTGRES_DB=sport_database --restart unless-stopped postgres:11-alpine
```

### Deploy web
```yaml
- name: deploy web app
  hosts: snafinal_web
  become: true
  vars:
    envfilePath: .env
  tasks:

    - name: Get running containers
      shell: docker ps -aq
      register: running_containers

    - name: Stop all docker containers
      shell: docker stop $(docker ps -aq)
      when: running_containers.stdout | length > 0

    - name: Delete all stopped docker containers
      shell: docker rm $(docker ps -aq)
      when: running_containers.stdout | length > 0

    - name: Remove unused/dangling image
      shell: docker image prune -af

    - name: Get db_server ip
      shell: "getent hosts db_server | awk '{ print $1 }'"
      register: db_server_ip

    - name: Copy production compose
      copy:
        src: ./docker-compose-sna-prod.yml
        dest: /root/docker-compose.yml
    
    - name: Copy production env
      copy:
        src: "{{ envfilePath }}"
        dest: /root/.env

    - name: Run docker-compose
      shell: docker-compose -f /root/docker-compose.yml up -d
      environment:
        POSTGRES_SERVER: "{{ db_server_ip.stdout }}"

    - name: Collect statics 
      shell: docker-compose -f /root/docker-compose.yml exec -T adminpanel bash -c './manage.py collectstatic --noinput'

    - name: Restart nginx
      shell: docker-compose -f /root/docker-compose.yml restart nginx
```