Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
E-VANCE committed Jul 21, 2021
0 parents commit 5d3f6ae
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
23 changes: 23 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
# rclone_arch can be defined as an architecture (e.g. arm, mips, 386) listed at https://github.com/ncw/rclone/releases
rclone_arch: amd64

# Release of rclone to use – 'default' or 'beta' are accepted
rclone_release: stable

rclone_version: '{{ ansible_local.rclone.version | d("0.0.0") }}'

# Defaults in case no variables for OS are chosen
rclone_setup_tmp_dir: "/tmp/rclone_setup"

# The location to install the config file if configured
rclone_config_location: "/root/.config/rclone/rclone.conf"

install_manpages: true

packages:
- unzip

man_pages:
owner: root
group: root
16 changes: 16 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
galaxy_info:
author: Henning Orth / E-VANCE
description: Use rclone with Trellis
license: "license (MIT)"
min_ansible_version: 2.4
platforms:
- name: Ubuntu
versions:
- trusty
- xenial
- bionic
galaxy_tags:
- networking
- system
dependencies: []
9 changes: 9 additions & 0 deletions tasks/beta.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
- name: Get rclone beta version
unarchive:
src: "{{ beta_version_url | default('https://beta.rclone.org/v' + rclone_version + '/rclone-v' + rclone_version + '-linux-' + rclone_arch + '.zip') }}"
dest: "{{ rclone_setup_tmp_dir }}"
remote_src: true
mode: 0744
list_files: true
creates: "{{ rclone_setup_tmp_dir }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}"
77 changes: 77 additions & 0 deletions tasks/install-bin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
- name: Update repositories cache on Ubuntu
apt:
update_cache: true
become: true

- name: Install required packages
package:
name: '{{ item }}'
state: present
become: true
with_items: '{{ packages }}'

- name: Remove temporary working directory
file:
path: "{{ rclone_setup_tmp_dir }}"
state: absent
tags:
- molecule-idempotence-notest

- name: Create temporary working directory
file:
path: "{{ rclone_setup_tmp_dir }}"
state: directory
mode: '0775'

- name: Do beta install
include_tasks: beta.yml
when: rclone_release == 'beta'

- name: Do stable install
include_tasks: stable.yml
when: rclone_release == 'stable'

- name: Get list of files extracted from the rclone archive
find:
path: "{{ rclone_setup_tmp_dir }}"
depth: 2
file_type: directory
register: rclone_archive

- name: Copy rclone binary
copy:
src: "{{ rclone_setup_tmp_dir }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}/rclone"
dest: "/usr/local/bin/rclone"
mode: '0755'
owner: root
group: root
remote_src: true
become: true

- name: Make dir for local manpages
file:
path: '{{ man_pages.path }}'
state: directory
mode: '0775'
owner: '{{ man_pages.owner }}'
group: '{{ man_pages.group }}'
become: true
when: install_manpages

- name: Copy rclone manpage
copy:
src: "{{ rclone_setup_tmp_dir }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}/rclone.1"
dest: "{{ man_pages.path }}/rclone.1"
mode: '0644'
owner: root
group: root
remote_src: true
become: true
when: install_manpages

- name: Update mandb
command: mandb
become: true
changed_when: false
when: install_manpages
81 changes: 81 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
- name: Gather OS specific variables
include_vars: "{{ item }}"
with_first_found:
- '{{ ansible_distribution }}-{{ ansible_distribution_major_version }}.yml'
- '{{ ansible_distribution }}.yml'
- '{{ ansible_os_family }}.yml'
tags:
- vars

- name: Create directory for ansible custom facts
ansible.builtin.file:
state: directory
recurse: true
path: /etc/ansible/facts.d

- name: Create facts file from template
ansible.builtin.template:
src: 'etc/ansible/facts.d/rclone.fact.j2'
dest: /etc/ansible/facts.d/rclone.fact
mode: '0755'
register: rclone__register_facts

- name: Re-read facts after adding custom fact
become: true
ansible.builtin.setup:
filter: ansible_local

- block:
- name: Check latest stable rclone version number
uri:
url: https://downloads.rclone.org/version.txt
return_content: true
register:
rclone_latest_version
check_mode: false
- name: Set variable to latest stable version number
set_fact:
rclone_version: "{{ rclone_latest_version.content | replace ('rclone v', '', 1) | trim }}"
when:
- rclone_version | length == 0 or rclone_version == "0.0.0"
- rclone_release == 'stable'

- block:
- name: Check latest beta rclone version number
uri:
url: https://beta.rclone.org/version.txt
return_content: true
register:
rclone_latest_beta_version
- name: Set variable for beta version
set_fact:
rclone_version: "{{ rclone_latest_beta_version.content | replace ('rclone v', '', 1) | trim }}"
beta_version_url: https://beta.rclone.org/rclone-beta-latest-linux-{{ rclone_arch }}.zip
when:
- rclone_version | length == 0 or rclone_version == "0.0.0"
- rclone_release == 'beta'

- name: Install the binary
include_tasks: install-bin.yml
when: (((not ansible_local.rclone.installed|d())|bool) or (ansible_local.rclone.installed_version|d("0.0.0")) != rclone_version)

- name: Install configs
block:
- name: Create config directory
file:
path: '{{ rclone_config_location | dirname }}'
state: directory
mode: '0700'

- name: Set up rclone config
template:
src: rclone.conf.j2
dest: '{{ rclone_config_location }}'
mode: 0600
become: true
when: rclone_configs is defined

- name: Update Ansible facts if they were modified
action: setup
when: rclone__register_facts is changed
8 changes: 8 additions & 0 deletions tasks/stable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: Get rclone stable version {{ rclone_version }}
unarchive:
src: https://downloads.rclone.org/v{{ rclone_version }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}.zip
dest: "{{ rclone_setup_tmp_dir }}"
remote_src: true
mode: 0744
creates: "{{ rclone_setup_tmp_dir }}/rclone-v{{ rclone_version }}-linux-{{ rclone_arch }}"
29 changes: 29 additions & 0 deletions templates/etc/ansible/facts.d/rclone.fact.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!{{ ansible_python['executable'] }}
# -*- coding: utf-8 -*-

# Copyright (C) 2018 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2018 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

# {{ ansible_managed }}

from __future__ import print_function
from json import dumps
import os
import subprocess

def cmd_exists(cmd):
return any(
os.access(os.path.join(path, cmd), os.X_OK)
for path in os.environ["PATH"].split(os.pathsep)
)

output = {'configured': True,
'installed': cmd_exists('rclone')}

if output['installed']:
output['installed_version'] = subprocess.check_output(
['rclone', 'version'], stderr=subprocess.STDOUT
).decode('utf-8').replace('\n', ' ').split(' ')[1].lstrip('v')

print(dumps(output, sort_keys=True, indent=4))
7 changes: 7 additions & 0 deletions templates/rclone.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% for config in rclone_configs %}
[{{ config.name }}]
{% for key, value in config.properties.items() %}
{{ key }} = {{ value }}
{% endfor %}

{% endfor %}
9 changes: 9 additions & 0 deletions vars/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
packages:
- unzip
- man-db

man_pages:
owner: root
group: root
path: '/usr/local/share/man/man1'

0 comments on commit 5d3f6ae

Please sign in to comment.