{%- if pillar['mount'] is defined -%}
  {%- if pillar['mount']['ext4'] is defined -%}

    {%- for mount in pillar['mount']['ext4'] -%}
      {%- set device=pillar['mount']['ext4'][mount]['device'] -%}
{{ mount }}:
  mount.mounted:
    - device: {{ device }}
    - fstype: ext4
    - mkmnt: True
    - opts: rw,relatime,data=ordered
    - hidden_opts: rw,relatime,data=ordered
    - dump: 0
    - pass_num: 2
    - persist: True
    {%- endfor -%}
  {% endif %}

autofs-pkg:
  pkg.installed:
    - name: autofs
  service.running:
    - enable: true
    - name: autofs
    - watch:
      - file: /etc/autofs/auto.master
      {%- if pillar['mount']['sshfs'] is defined %}
      - file: /etc/autofs/auto.ssh
      {% endif -%}
      {%- if pillar['mount']['nfs'] is defined %}
      - file: /etc/autofs/auto.nfs
      {% endif %}

autofs-master:
  file.managed:
    - name: /etc/autofs/auto.master
    - source: salt://systems/core/mount/auto.master
    - user: root
    - group: root
    - mode: 644

  {%- if pillar['mount']['sshfs'] is defined %}
fuse2:
  pkg.installed
sshfs:
  pkg.installed
fuse-module:
  kmod.present:
    - name: fuse
    - persist: true

/etc/autofs/auto.ssh:
  file.managed:
    - source: salt://systems/core/mount/auto.ssh
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
      sshfs: {{pillar['mount']['sshfs']}}

mount_host.actcur.com:
  ssh_known_hosts.present:
    - name: host.actcur.com
    - hash_known_hosts: False
    - user: root
    - enc: ecdsa-sha2-nistp256
    - key: "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBCnvAIi9HiYDsQCHPWPQrgdLTANveZ3K9v1+0iJDA2yEo67EfkFl7O9Go/pVkOoSjV+eWKQ9A7Az7aMY1tc9ur0="

    {% for mount in pillar['mount']['sshfs'] %}
      {%- if pillar['mount']['sshfs'][mount]['user'] is defined -%}
        {%- set user=pillar['mount']['sshfs'][mount]['user'] -%}
      {%- else -%}
        {%- set user="mount" -%}
      {%- endif -%}
      {%- set name=pillar['mount']['sshfs'][mount]['name'] %}
{{ mount }}_key:
  file.managed:
    - name: /root/.ssh/{{user}}_key
    - source: salt://secure/keys/mount/{{user}}_key
    - user: root
    - group: root
    - mode: 600
    - makedirs: true

{{ mount }}_link:
  file.symlink:
    - name: {{ mount }}
    - target: /mnt/autofs/ssh/{{name}}
    - force: true
    - makedirs: true
    {%- endfor -%}
  {% endif %}

  {%- if pillar['mount']['nfs'] is defined -%}
nfs-utils:
  pkg.installed

rpcbind:
  service.running:
    - enable: true

nfs-client.target:
  service.running:
    - enable: true

remote-fs.target:
  service.running:
    - enable: true

/etc/autofs/auto.nfs:
  file.managed:
    - source: salt://systems/core/mount/auto.nfs
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - context:
      nfs: {{pillar['mount']['nfs']}}

{% for mount in pillar['mount']['nfs'] %}
      {%- set name=pillar['mount']['nfs'][mount]['name'] %}

{{ mount }}_link:
  file.symlink:
    - name: {{ mount }}
    - target: /mnt/autofs/ssh/{{name}}
    - force: true
    - makedirs: true
    {%- endfor -%}

  {% endif %}
{%- endif -%}