- name: Create s3-mirror group
  group: gid=442 name=s3-mirror
  tags:
  - s3-mirror

- name: Create s3-mirror user
  user: group=s3-mirror name=s3-mirror comment="s3-mirror user" home=/home/s3-mirror shell=/bin/bash
  tags:
  - s3-mirror

- name: Create needed directories
  file: path={{item}} owner=s3-mirror group=s3-mirror state=directory mode=0755
  with_items:
  - /var/log/s3-mirror
  - /var/lib/s3-mirror
  - /var/run/s3-mirror
  - /home/s3-mirror/.aws
  #- /var/log/s3-mirror-logs
  #- /var/log/s3-mirror-logs/bucket
  tags:
  - s3-mirror

- name: aws credentials file
  template: src=credentials dest=/home/s3-mirror/.aws/credentials owner=s3-mirror group=s3-mirror mode=0600
  tags:
  - s3-mirror

- name: aws config file
  copy: src=config dest=/home/s3-mirror/.aws/config owner=s3-mirror group=s3-mirror mode=0600
  tags:
  - s3-mirror

- name: Install packages
  package: name={{ item }} state=present
  with_items:
  - awscli
  #  - mirrormanager2-client
  tags:
  - s3-mirror

- name: s3 script
  copy: src={{item}} dest=/usr/local/bin/{{item}} owner=s3-mirror group=s3-mirror mode=0755
  with_items:
  - s3.sh
  tags:
  - s3-mirror

- name: s3sync cron
  cron: name="s3sync" minute="0" hour="0,11" user="s3-mirror"
        job='/usr/local/bin/lock-wrapper s3sync /usr/local/bin/s3.sh 2>&1 | /usr/local/bin/nag-once s3.sh 1d 2>&1'
        cron_file=s3.sh
  when: env != 'staging' and inventory_hostname.startswith('mm-backend01.')
  tags:
  - s3-mirror

#- name: s3sync-logs cron
#  cron: name="s3sync-logs" hour="0" user="root"
#        job='/usr/local/bin/lock-wrapper s3sync-logs "/bin/sleep $((${RANDOM} \% 300)); /usr/local/bin/s3sync-logs >> /var/log/s3-mirror-logs/s3sync-logs.log 2>&1" | /usr/local/bin/nag-once s3mirror-logs 12h 2>&1'
#        cron_file=s3sync-logs
#        state=absent
