Skip to content

Commit

Permalink
Initial version of the role
Browse files Browse the repository at this point in the history
kysrpex committed May 30, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 705eb80 commit 6886ed0
Showing 8 changed files with 277 additions and 0 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Ansible role: rustus

A role that installs a [rustus](https://github.com/s3rius/rustus) server.

## Requirements

No specific requirements, the role is self-contained.

## Role variables

Role variables are documented in the forms of comments on [defaults/main.yml](defaults/main.yml)

## Dependencies

None.

## Example Playbook

```yaml
- name: Install and configure rustus
hosts: all
vars:
rustus_instances:
# Runs rustus instances as systemd services
- name: uploads
# user that rustus will run as
user: myuser
# group that rustus will run as
group: mygroup
# arguments passed to rustus
args:
# see https://s3rius.github.io/rustus/configuration/
- --host "0.0.0.0"
- --port 1081
- --data-dir /data/uploads
- --hooks-http-urls "https://my-app.example.org/api/upload"
- --hooks-http-proxy-headers "Cookie"
roles:
usegalaxy-eu.rustus
```
## License
See [LICENSE.md](LICENSE.md)
## Author Information
This role was created by contributors of the [Galaxy Project](https://galaxyproject.org/). Check the [contributors page](https://github.com/usegalaxy-eu/ansible-rustus/graphs/contributors) for detailed information.
## Acknowledgments
This role stems from [ansible-role-tusd](https://galaxy.ansible.com/galaxyproject/tusd).
27 changes: 27 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---

# Use 'latest' to install the latest stable release from https://github.com/s3rius/rustus/releases, or a valid tag for
# a specific tagged release (e.g. 0.7.2)
rustus_version: latest

# Path of the rustus binary
rustus_path: /usr/local/bin/rustus

# Owner, group, mode of the rustus binary
# rustus_owner: omit
# rustus_group: omit
# rustus_mode: 0755

# Systemd
# If using Linux, you can install systemd service units to start and manage your rustus instances
rustus_systemd: true
rustus_systemd_target: "network.target time-sync.target" # add here more targets as needed

# Configuration for systemd-managed rustus instances - see the README for syntax
rustus_instances: []

# This is used to download the correct release tarball, which are named like
# 'rustus_{{ rustus_version }}_{{ rustus_os }}_{{ rustus_arch }}.tar.gz
# You should generally not have to set this directly.
rustus_os: "{{ ansible_system | lower }}"
rustus_arch: "{{ ansible_architecture }}"
13 changes: 13 additions & 0 deletions handlers/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---

- name: Reload systemd
ansible.builtin.systemd:
daemon_reload: true

- name: Restart modified rustus instances
ansible.builtin.service:
name: rustus-{{ item.item.name }}.service
state: restarted
loop: "{{ __rustus_modified_instances.results }}"
loop_control:
label: "{{ item.item }}"
26 changes: 26 additions & 0 deletions meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
galaxy_info:
namespace: usegalaxy_eu
role_name: rustus
author: José Manuel Domínguez
description: Install and run instances of rustus, an implementation of the tus file upload protocol in Rust.
company: The Galaxy Project
license: MIT
min_ansible_version: "2.11"
platforms:
- name: GenericLinux
versions:
- all
- name: macOS
versions:
- all
- name: MacOSX
versions:
- all
galaxy_tags:
- system
- web
- tus
- upload
- http
dependencies: []
108 changes: 108 additions & 0 deletions tasks/install.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---

- name: Resolve latest release of rustus
when: rustus_version == 'latest'
block:
- name: Retrieve release tag for the latest stable release of rustus
ansible.builtin.uri:
url: https://api.github.com/repos/s3rius/rustus/releases/latest
return_content: true
register: __rustus_release_latest_response

- name: Overwrite rustus release number
ansible.builtin.set_fact:
rustus_version: "{{ __rustus_release_latest_response.json.tag_name }}"

- name: Determine whether rustus should be installed or upgraded
block:
- name: Default to not installing or upgrading rustus
ansible.builtin.set_fact:
__rustus_install: false

- name: Determine whether a rustus binary is already installed
ansible.builtin.shell: type -p rustus || echo none
register: __rustus_installed
changed_when: false

- name: Check whether a rustus binary exists in the configured path for installation
ansible.builtin.stat:
path: "{{ rustus_path }}"
register: __rustus_path_exists

- name: Error out on path mismatch (installed binary not matching the installation path)
when: __rustus_installed.stdout != "none" and __rustus_installed.stdout != rustus_path
ansible.builtin.fail:
msg: |
A rustus binary already exists in {{ __rustus_installed.stdout }}, which does not match
the installation path {{ rustus_path }}. Please remove the existing binary or change the
installation path to {{ __rustus_installed.stdout }} before continuing.
- name: Compare sha256sums of the installed binary and the desired version.
when: __rustus_installed.stdout != "none"
block:
- name: Compute sha256sum of existing rustus binary.
ansible.builtin.command: "sha256sum {{ __rustus_installed.stdout }}"
register: __rustus_sha256sum_installed
changed_when: false

- name: Download and read sha256sum of desired rustus version.
block:
- name: Allocate temporary file.
ansible.builtin.tempfile:
state: file
prefix: ansible.usegalaxy-eu.rustus.
register: __rustus_tempfile_sha256
changed_when: false

- name: Download sha256sum.
ansible.builtin.get_url:
url: "https://github.com/s3rius/rustus/releases/download/\
{{ rustus_version }}/rustus-{{ rustus_version }}-{{ rustus_os }}-{{ rustus_arch }}.sha256"
dest: "{{ __rustus_tempfile_sha256.path }}"
mode: "0644"
changed_when: false

- name: Read sha256sum.
ansible.builtin.shell: |
set -o pipefail
< {{ __rustus_tempfile_sha256.path }} tr -s ' ' | cut -d ' ' -f 1
register: __rustus_sha256sum_desired
changed_when: false

- name: Compare sha256 sums.
ansible.builtin.set_fact:
__rustus_install: true
when: __rustus_sha256sum_installed != __rustus_sha256sum_desired
changed_when: false

- name: Schedule rustus installation if no binary was found.
ansible.builtin.set_fact:
__rustus_install: true
when: __rustus_installed.stdout == "none"


- name: Install rustus.
when: __rustus_install
block:
- name: Allocate temporary directory for downloading and extracting rustus.
ansible.builtin.tempfile:
prefix: ansible.usegalaxy-eu.rustus.
state: directory
changed_when: false
register: __rustus_install_tempdir

- name: Extract rustus.
ansible.builtin.unarchive:
remote_src: true
src: "https://github.com/s3rius/rustus/releases/download/{{ rustus_version }}/rustus-{{ rustus_version }}-{{ rustus_os }}-{{ rustus_arch }}.tar.gz"
dest: "{{ __rustus_install_tempdir.path }}/"
changed_when: false

- name: Install the rustus binary.
ansible.builtin.copy:
remote_src: true
src: "{{ __rustus_install_tempdir.path }}/rustus"
dest: "{{ rustus_path }}"
owner: "{{ rustus_owner | default(omit) }}"
group: "{{ rustus_group | default(omit) }}"
mode: "{{ rustus_mode | default('0755') }}"
8 changes: 8 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---

- name: Import install tasks
ansible.builtin.import_tasks: install.yml

- name: Include systemd tasks
ansible.builtin.include_tasks: systemd.yml
when: rustus_systemd
24 changes: 24 additions & 0 deletions tasks/systemd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---

# Using the systemd instance functionality doesn't work here beacuse the user/group cannot be an env var, but should not
# necessarily be the same for all instances, so we resort to creating individual service units for each instance.
- name: Install rustus service units
ansible.builtin.template:
src: rustus.service.j2
dest: /etc/systemd/system/rustus-{{ item.name }}.service
mode: "0644"
loop: "{{ rustus_instances }}"
register: __rustus_modified_instances
notify:
- Reload systemd
- Restart modified rustus instances

- name: Flush handlers
ansible.builtin.meta: flush_handlers

- name: Ensure configured rustus instances are enabled and running
ansible.builtin.service:
name: rustus-{{ item.name }}.service
state: started
enabled: true
loop: "{{ rustus_instances }}"
19 changes: 19 additions & 0 deletions templates/rustus.service.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
###
### This file is maintained by Ansible - CHANGES WILL BE OVERWRITTEN
###

[Unit]
Description=rustus server for {{ item.name }}
After={{ rustus_systemd_target }}

[Service]
UMask=022
Type=simple
User={{ item.user }}
Group={{ item.group }}
TimeoutStartSec=10
ExecStart={{ rustus_path }} {{ item.args | join(' ') }}
Restart=always

[Install]
WantedBy=multi-user.target

0 comments on commit 6886ed0

Please sign in to comment.