---
# This playbook coverts docker to go from loopback to direct-lvm (the Red Hat recommended way to run docker)
#  in AWS.  This adds an additional EBS volume and creates the Volume Group on this EBS volume to use.
#
#  To run:
#  1. Source your AWS credentials (make sure it's the corresponding AWS account) into your environment
#    export AWS_ACCESS_KEY_ID='XXXXX'
#    export AWS_SECRET_ACCESS_KEY='XXXXXX'
#
# 2. run the playbook:
#   ansible-playbook -e 'cli_tag_name=<tag-name>' -e "cli_volume_size=30" docker_loopback_to_direct_lvm.yml
#
#  Example:
#   ansible-playbook -e 'cli_tag_name=ops-master-12345' -e "cli_volume_size=30" docker_loopback_to_direct_lvm.yml
#
#  Notes:
#  * By default this will do a 30GB volume.
#  * iops are calculated by Disk Size * 30.  e.g ( 30GB * 30) = 900 iops
#  * This will remove /var/lib/docker!
#  * You may need to re-deploy docker images after this is run (like monitoring)
#

- name: Fix docker to have a provisioned iops drive
  hosts: "tag_Name_{{ cli_tag_name }}"
  user: root
  connection: ssh
  gather_facts: no

  vars:
    cli_volume_type: gp2
    cli_volume_size: 30

  pre_tasks:
  - fail:
      msg: "This playbook requires {{item}} to be set."
    when: "{{ item }} is not defined or {{ item }} == ''"
    with_items:
    - cli_tag_name
    - cli_volume_size

  - debug:
      var: hosts

  - name: start docker
    service:
      name: docker
      state: started

  - name: Determine if loopback
    shell: docker info | grep 'Data file:.*loop'
    register: loop_device_check
    ignore_errors: yes

  - debug:
      var: loop_device_check

  - name: fail if we don't detect loopback
    fail:
      msg:  loopback not detected! Please investigate manually.
    when: loop_device_check.rc == 1

  - name: stop zagg client monitoring container
    service:
      name: oso-rhel7-zagg-client
      state: stopped
    ignore_errors: yes

  - name: stop pcp client monitoring container
    service:
      name: oso-f22-host-monitoring
      state: stopped
    ignore_errors: yes

  - name: stop docker
    service:
      name: docker
      state: stopped

  - name: delete /var/lib/docker
    command: rm -rf /var/lib/docker

  - name: remove /var/lib/docker
    command: rm -rf /var/lib/docker

  - name: check to see if /dev/xvdb exists
    command: test -e /dev/xvdb
    register: xvdb_check
    ignore_errors: yes

  - debug: var=xvdb_check

  - name: fail if /dev/xvdb already exists
    fail:
      msg: /dev/xvdb already exists.  Please investigate
    when: xvdb_check.rc == 0

  - name: Create a volume and attach it
    delegate_to: localhost
    ec2_vol:
      state: present
      instance: "{{ ec2_id }}"
      region: "{{ ec2_region }}"
      volume_size: "{{ cli_volume_size | default(30, True)}}"
      volume_type: "{{ cli_volume_type }}"
      device_name: /dev/xvdb
    register: vol

  - debug: var=vol

  - name: tag the vol with a name
    delegate_to: localhost
    ec2_tag: region={{ ec2_region }} resource={{ vol.volume_id }}
    args:
      tags:
        Name: "{{ ec2_tag_Name }}"
        clusterid: "{{ ec2_tag_clusterid }}"
    register: voltags

  - name: Wait for volume to attach
    pause:
      seconds: 30

  - name: copy the docker-storage-setup config file
    copy:
      src: docker-storage-setup
      dest: /etc/sysconfig/docker-storage-setup
      owner: root
      group: root
      mode: 0664

  - name: docker storage setup
    command: docker-storage-setup
    register: setup_output

  - debug: var=setup_output

  - name: start docker
    command: systemctl start docker.service
    register: dockerstart

  - debug: var=dockerstart

