# the defaults on this image are user:manager, password:schooltool
- name: Get the schooltool container if it is missing
  docker: image=ghunt/schooltool:latest
          ports=7080:7080
          state=present
          pull=missing
  when: schooltool_install and docker_install and internet_available 


- name: Configure schooltool to run in docker container under systemd
  template: backup=yes
            src={{ item.src }}
            dest={{ item.dest }}
            owner=root
            group=root
            mode={{ item.mode }}
  with_items:
    -  dest: '/etc/systemd/system/schooltool.service'
       src: 'schooltool.service'
       mode: '0644'
    -  dest: '/etc/{{ apache_config_dir }}/schooltool.conf'
       src:  'schooltool.conf'
       mode: '0644'

# create a schooltool user who can have limited capability
- name: Make a user for schooltool
  user: name=schooltool
        shell=/sbin/nologin
        createhome=no

# create the permanent storage directory, and give world write access
# -- will eventually change the docker container to give itself write access and demote itself
- name: create Permanent Storage for persistent schooltool data
  file: path=/var/lib/schooltool
        state=directory
        owner=schooltool
        mode=0777

#Experience teaches that enabling schooltool is more likely to succeed after docker restart
- name: Restart docker
  service: name=docker
           state=restarted
           enabled=yes
  when: schooltool_enabled | bool

- name: Enable schooltool
  service: name=schooltool
           state=started
           enabled=yes
  when: schooltool_enabled | bool

- name: Disable schooltool
  service: name=schooltool
           state=stopped
           enabled=no
  when: not schooltool_enabled

- name: Add 'schooltool' variable values to {{ iiab_ini_file }}
  ini_file:
    path: "{{ iiab_ini_file }}"
    section: schooltool
    option: "{{ item.option }}"
    value: "{{ item.value }}"
  with_items:
    - option: name
      value: Schooltool
    - option: description
      value: '"SchoolTool is an open source, web based student information system designed for schools in the developing world, with strong support for translation, localization and automated deployment and updates."'
    - option: enabled
      value: "{{ schooltool_enabled }}"
