Skip to content

Commit

Permalink
remote: add nfs as a template option
Browse files Browse the repository at this point in the history
NFS is untested.
Adds ability to append a path to the remote name.
  • Loading branch information
saltydk committed Aug 4, 2023
1 parent e52c6b8 commit a984ef2
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 15 deletions.
8 changes: 8 additions & 0 deletions roles/remote/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ rclone_port_lookup: "{{ port_lookup_rclone.meta.port
if (port_lookup_rclone.meta.port is defined) and (port_lookup_rclone.meta.port | trim | length > 0)
else '5572' }}"
rclone_remort_port: "{{ lookup('vars', 'rclone_remote_' + item.remote + '_port', default=rclone_port_lookup) }}"
rclone_remote_name: "{{ item.remote.split(':')[0] if ':' in item.remote else item.remote }}"
rclone_remote_with_path: "{{ item.remote if ':' in item.remote else item.remote + ':' }}"

################################
# Rclone VFS Refresh
Expand All @@ -31,3 +33,9 @@ rclone_remort_port: "{{ lookup('vars', 'rclone_remote_' + item.remote + '_port',
rclone_vfs_refresh_interval: 10800
rclone_vfs_refresh_command: |-
/usr/bin/rclone rc vfs/refresh recursive=true --url http://localhost:{{ rclone_remort_port }} _async=true
################################
# NFS
################################

nfs_opts: "nofail,noatime,nolock,intr,tcp,actimeo=1800"
7 changes: 6 additions & 1 deletion roles/remote/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@
- name: Remote Tasks
ansible.builtin.include_tasks: "remote.yml"
with_items: "{{ rclone.remotes }}"
when: rclone_remote_is_defined and use_remote
when: rclone_remote_is_defined and use_remote and item.template != "nfs"

- name: Remote Tasks (NFS)
ansible.builtin.include_tasks: "nfs.yml"
with_items: "{{ rclone.remotes }}"
when: rclone_remote_is_defined and use_remote and item.template == "nfs"
66 changes: 66 additions & 0 deletions roles/remote/tasks/nfs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#########################################################################
# Title: Saltbox: Remote Role | Remote Tasks (NFS) #
# Author(s): salty #
# URL: https://github.com/saltyorg/Saltbox #
# -- #
#########################################################################
# GNU General Public License v3.0 #
#########################################################################
---
- name: "Remote (NFS) | Set Variables"
ansible.builtin.set_fact:
mount_path: "/mnt/remote/{{ rclone_remote_name }}"

- name: "Remote (NFS) | Ensure '{{ mount_path }}' is not mounted"
ansible.posix.mount:
path: "{{ mount_path }}"
state: unmounted

- name: "Remote (NFS) | Check if '{{ mount_path }}' exists"
ansible.builtin.stat:
path: "{{ mount_path }}"
register: mount_path_stat
ignore_errors: true

- name: "Remote (NFS) | Tasks for '{{ mount_path }}' path"
when: (mount_path_stat is failed) or (mount_path_stat.stat.exists)
block:
- name: "Remote (NFS) | Remove empty directories in '{{ mount_path }}'"
ansible.builtin.shell: "find '{{ mount_path }}' -type d -empty -delete"
ignore_errors: true

- name: "Remote (NFS) | Recursively find '{{ mount_path }}' files"
ansible.builtin.find:
paths: '{{ mount_path }}'
hidden: true
recurse: true
register: mount_path_stat_files
ignore_errors: true

- name: "Remote (NFS) | Backup non-empty '{{ mount_path }}' path"
ansible.builtin.shell: "mv {{ mount_path }} {{ mount_path }}_{{ '%Y-%m-%d_%H.%M.%S' | strftime(ansible_date_time['epoch'] | int) }}"
ignore_errors: true
when: (mount_path_stat_files.matched | int > 0)

- name: "Remote (NFS) | Remove '{{ mount_path }}'"
ansible.builtin.file:
path: "{{ mount_path }}"
state: absent

- name: Remote (NFS) | Create mount directory
ansible.builtin.file:
path: "{{ mount_path }}"
state: directory
owner: "{{ user.name }}"
group: "{{ user.name }}"
mode: "0775"
recurse: true

- name: Remote (NFS) | Mount NFS volume
ansible.posix.mount:
src: "{{ rclone_remote_with_path }}"
path: "{{ mount_path }}"
opts: "{{ nfs_opts }}"
boot: true
state: mounted
fstype: nfs
16 changes: 8 additions & 8 deletions roles/remote/tasks/remote.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
---
- name: "Remote | Set Variables"
ansible.builtin.set_fact:
_service_file: "{{ rclone_service_template + item.remote }}.service"
_service_refresh: "{{ rclone_service_template + item.remote }}_refresh"
mount_path: "/mnt/remote/{{ item.remote }}"
_service_file: "{{ rclone_service_template + rclone_remote_name }}.service"
_service_refresh: "{{ rclone_service_template + rclone_remote_name }}_refresh"
mount_path: "/mnt/remote/{{ rclone_remote_name }}"

- name: Remote | Get next available port within the range of '5572-5672' # noqa fqcn[action]
find_open_port:
Expand Down Expand Up @@ -194,14 +194,14 @@

- name: Remote | Set 'rclone_remote_is_configured' variable
ansible.builtin.set_fact:
rclone_remote_is_configured: "{{ (item.remote + ':') in rclone_config_remotes }}"
rclone_remote_is_configured: "{{ (rclone_remote_name + ':') in rclone_config_remotes }}"

- name: Remote | Tasks when Rclone remote is configured
when: rclone_remote_is_configured
block:
- name: "Remote | Test Rclone remote '{{ item.remote }}'"
- name: "Remote | Test Rclone remote '{{ rclone_remote_name }}'"
ansible.builtin.shell: |
rclone lsd '{{ item.remote }}:' \
rclone lsd '{{ rclone_remote_with_path }}' \
--user-agent='{{ user_agent }}'
become: true
become_user: "{{ user.name }}"
Expand Down Expand Up @@ -243,12 +243,12 @@
- name: "Remote | Display error message when Rclone remote is not working properly"
ansible.builtin.fail:
msg:
- "Rclone remote '{{ item.remote }}:' is not working properly."
- "Rclone remote '{{ rclone_remote_with_path }}' is not working properly."
- "{{ rclone_test.stderr }}"
when: (not rclone_remote_is_working)

- name: "Remote | Display error message when Rclone remote is not configured properly"
ansible.builtin.fail:
msg:
- "Rclone remote '{{ item.remote }}:' is not configured properly."
- "Rclone remote '{{ rclone_remote_with_path }}' is not configured properly."
when: (not rclone_remote_is_configured)
4 changes: 2 additions & 2 deletions roles/remote/templates/dropbox.j2
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ ExecStart=/usr/bin/rclone mount \
--vfs-read-chunk-size-limit=2G \
--vfs-read-chunk-size={{ '32M' if item.vfs_cache.enabled else '64M' }} \
-v \
{{ item.remote }}:{{ lookup('vars', (item.remote | replace('-', '_') | lower) + '_folder', default="") }} /mnt/remote/{{ item.remote }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ item.remote }}
{{ rclone_remote_with_path }} /mnt/remote/{{ rclone_remote_name }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ rclone_remote_name }}
Restart=on-abort
RestartSec=5
StartLimitInterval=60s
Expand Down
4 changes: 2 additions & 2 deletions roles/remote/templates/google.j2
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ ExecStart=/usr/bin/rclone mount \
--vfs-read-chunk-size-limit=2G \
--vfs-read-chunk-size={{ '32M' if item.vfs_cache.enabled else '64M' }} \
-v \
{{ item.remote }}:{{ lookup('vars', (item.remote | replace('-', '_') | lower) + '_folder', default="") }} /mnt/remote/{{ item.remote }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ item.remote }}
{{ rclone_remote_with_path }} /mnt/remote/{{ rclone_remote_name }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ rclone_remote_name }}
Restart=on-abort
RestartSec=5
StartLimitInterval=60s
Expand Down
4 changes: 2 additions & 2 deletions roles/remote/templates/sftp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ ExecStart=/usr/bin/rclone mount \
--vfs-fast-fingerprint \
{% endif %}
-v \
{{ item.remote }}:{{ lookup('vars', (item.remote | replace('-', '_') | lower) + '_folder', default='/mnt/local' if item.remote == 'feeder' else '') }} /mnt/remote/{{ item.remote }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ item.remote }}
{{ rclone_remote_with_path + ('/mnt/local' if item.remote == 'feeder' else '') }} /mnt/remote/{{ rclone_remote_name }}
ExecStop=/bin/fusermount3 -uz /mnt/remote/{{ rclone_remote_name }}
Restart=on-abort
RestartSec=5
StartLimitInterval=60s
Expand Down

0 comments on commit a984ef2

Please sign in to comment.