---
- hosts: nodes

  roles:
  - role: postgres
    pg_port: 15432
    pg_repo: https://github.com/postgrespro/postgres_cluster.git
    pg_destroy_and_init: true

  tasks:
  - name: build tsdtm
    shell: "make clean && make -j {{makejobs}} install"
    args:
      chdir: "{{pg_src}}/contrib/pg_tsdtm"
    environment:
      PG_CONFIG: "{{pg_dst}}/bin/pg_config"

  - name: enable dtm extension on datanodes
    lineinfile:
      dest: "{{pg_datadir}}/postgresql.conf"
      line: "{{item}}"
      state: present
    with_items:
    - "shared_preload_libraries = 'pg_dtm'"

  - name: restart postgrespro
    command: "{{pg_dst}}/bin/pg_ctl restart -w -D {{pg_datadir}} -l {{pg_datadir}}/pg.log"
    environment:
      LD_LIBRARY_PATH: "{{pg_dst}}/lib/"

- hosts: clients
  tasks:

  - name: copy transfers source
    copy: src=../{{item}} dest=~/{{item}} mode=0755
    with_items:
      - "dtmbench.cpp"

  - name: clone pqxx
    git: repo=https://github.com/Ambrosys/pqxx.git
      dest=~/pqxx
      accept_hostkey=yes
      update=no
      force=yes
    register: pqxx

  - name: build pqxx
    shell: "./configure --prefix=/home/{{ansible_ssh_user}}/pg_cluster/install/ --enable-shared && make -j4 && make install"
    args:
      chdir: "~/pqxx"
    environment:
      PATH: "/home/{{ansible_ssh_user}}/pg_cluster/install/bin/:{{ansible_env. PATH}}"
    # when: pqxx.changed

  - name: compile dtmbench
    shell: "g++ -g -Wall -O2 -o dtmbench dtmbench.cpp -lpqxx -lpq -pthread -L/home/{{ansible_ssh_user}}/pg_cluster/install/lib/ -I/home/{{ansible_ssh_user}}/pg_cluster/install/include/"

  - name: move dtmbench to bin
    shell: "mv dtmbench ~/pg_cluster/install/bin/dtmbench"


