# Copyright (c) 2018, Red Hat, Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
#
#
---
#------------------------------------------------------------------------------
# Setup an Ceph cluster that will be used by cinderlib's functional tests
#------------------------------------------------------------------------------
- hosts: all
  vars:
    ansible_become: yes
  tasks:
      # Leave pyparsing, as it's needed by tox through the packaging library.
      - name: Remove Python packages unnecessary for cinderlib
        pip:
          name: ['glanceclient', 'novaclient', 'swiftclient', 'barbicanclient',
                 'cursive', 'keystoneauth1', 'keystonemiddleware', 'webob',
                 'keystoneclient', 'castellan', 'oslo_reports', 'oslo_policy',
                 'oslo_messaging', 'osprofiler', 'oauth2client', 'paste',
                 'oslo_middleware', 'routes', 'jsonschema', 'os-win',
                 'oslo_upgradecheck', 'googleapiclient', 'pastedeploy']
          state: absent

      - name: Install ceph requirements
        package:
          name:
            - ceph-common
            - python3-rados
            - python3-rbd
          state: present

      - name: Install Docker
        package:
          name: 'docker.io'
          state: present

      - name: Start Docker
        service:
            name: docker
            state: started

      - name: Start Ceph demo
        command: |
          docker run -d
          --name ceph-demo
          -e MON_IP=127.0.0.1
          -e CEPH_PUBLIC_NETWORK=127.0.0.1/0
          -e DEMO_DAEMONS="osd mds"
          --net=host
          --volume /etc/ceph:/etc/ceph
          --privileged
          ceph/daemon:latest-pacific
          demo

      - name: Wait for ceph.conf
        # Start of the file
        wait_for:
            path: /etc/ceph/ceph.conf
            search_regex: '\[global\]'
        # End of the file
      - wait_for:
            path: /etc/ceph/ceph.conf
            search_regex: '^osd data ='

      - name: Set ceph features in config
        lineinfile:
            path: /etc/ceph/ceph.conf
            insertafter: '\[global\]'
            firstmatch: yes
            line: 'rbd default features = 3'
            state: present

      - name: Set ceph keyring mode
        file:
            path: /etc/ceph/ceph.client.admin.keyring
            mode: 0644

      - name: Create rbd pool
        command: |
          docker exec ceph-demo ceph osd pool create rbd 8

      - shell: cat /etc/ceph/ceph.conf
        register: ceph_conf
