From 981d4f9072f8bf6a96668f86aeb1494fa3603c83 Mon Sep 17 00:00:00 2001 From: Stefan Morgenthaler Date: Thu, 15 Dec 2022 17:40:53 +0100 Subject: [PATCH] Allow to installation via OS package manager or pip (#106) * allow to installation via os package manager or pip * Run a second time to install via package manager * Check for EPEL if distro package requested. * Split dep lists to avoid installing build deps when using distro pkg Co-authored-by: Stefan Morgenthaler Co-authored-by: Manu --- README.md | 3 ++- defaults/main.yml | 5 ++-- molecule/default/Dockerfile.j2 | 4 +-- molecule/default/converge.yml | 1 + tasks/Archlinux.yml | 1 - tasks/Debian.yml | 1 - tasks/RedHat.yml | 1 - tasks/install_package.yml | 18 +++++++++++++ tasks/install_pip.yml | 39 ++++++++++++++++++++++++++++ tasks/main.yml | 46 ++++------------------------------ vars/Archlinux.yml | 10 ++++++-- vars/Debian.yml | 12 ++++++--- vars/Fedora.yml | 12 ++++++--- vars/ManjaroLinux.yml | 19 ++++++++++++-- vars/RedHat-8.yml | 12 ++++++--- vars/RedHat-9.yml | 12 ++++++--- vars/RedHat.yml | 12 ++++++--- vars/main.yml | 1 + 18 files changed, 140 insertions(+), 69 deletions(-) delete mode 100644 tasks/Archlinux.yml delete mode 100644 tasks/Debian.yml delete mode 100644 tasks/RedHat.yml create mode 100644 tasks/install_package.yml create mode 100644 tasks/install_pip.yml diff --git a/README.md b/README.md index e121b69..415c63f 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,8 @@ $ git clone https://github.com/borgbase/ansible-role-borgbackup.git roles/ansibl - `ssh_key_file`: Path to a private ssh key file (default is `.ssh/id_ed25519`). It generates a ed25519 key if the file doesn't exist yet. - `borg_version`: Force a specific borg version to be installed - `borgmatic_version`: Force a specific borgmatic version to be installed - +- `borg_install_method`: By default `pip` is used to install borgmatic. To install via your distributions package manager set this to `package` and (if needed) overwrite the `borg_distro_packages` variable to contain your distributions package names required to install borgmatic. Note that many distributions ship outdated versions of borgbackup and borgmatic; use at your own risk. +- `borg_distro_packages`: contains the names of distributions packages for `borg(backup)` and `borgmatic`, only used if `borg_install_method` is set to `package`. ## Contributing diff --git a/defaults/main.yml b/defaults/main.yml index f734524..6918f3f 100755 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -35,7 +35,6 @@ borgmatic_cron_minute: "{{ 59 | random(seed=inventory_hostname) }}" borgmatic_cron_checks_day: "{{ range(1, 28) | random(seed=inventory_hostname) }}" borgmatic_cron_checks_hour: "{{ range(9, 24) | random(seed=inventory_hostname) }}" borgmatic_cron_checks_minute: "{{ 59 | random(seed=inventory_hostname) }}" - - borg_version: false -borgmatic_version: false \ No newline at end of file +borgmatic_version: false +borg_install_method: pip diff --git a/molecule/default/Dockerfile.j2 b/molecule/default/Dockerfile.j2 index 59f32cd..0b331c5 100644 --- a/molecule/default/Dockerfile.j2 +++ b/molecule/default/Dockerfile.j2 @@ -14,10 +14,10 @@ ENV {{ var }} {{ value }} {% endfor %} {% endif %} -RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python3 sudo bash ca-certificates iproute2 python3-apt aptitude && apt-get clean; \ +RUN if [ $(command -v apt-get) ]; then apt-get update && apt-get install -y python3 python3-pip sudo bash ca-certificates iproute2 python3-apt aptitude && apt-get clean; \ elif [ $(command -v dnf) ]; then dnf makecache && dnf --assumeyes install /usr/bin/python3 /usr/bin/python3-config /usr/bin/dnf-3 sudo bash iproute && dnf clean all; \ elif [ $(command -v yum) ]; then yum makecache fast && yum install -y /usr/bin/python /usr/bin/python2-config sudo yum-plugin-ovl bash iproute && sed -i 's/plugins=0/plugins=1/g' /etc/yum.conf && yum clean all; \ elif [ $(command -v zypper) ]; then zypper refresh && zypper install -y python sudo bash python-xml iproute2 && zypper clean -a; \ elif [ $(command -v apk) ]; then apk update && apk add --no-cache python sudo bash ca-certificates; \ - elif [ $(command -v pacman) ]; then pacman --noconfirm -Suy python sudo openssh; \ + elif [ $(command -v pacman) ]; then pacman --noconfirm -Suy python python-pip sudo openssh; \ elif [ $(command -v xbps-install) ]; then xbps-install -Syu && xbps-install -y python sudo bash ca-certificates iproute2 && xbps-remove -O; fi diff --git a/molecule/default/converge.yml b/molecule/default/converge.yml index 5477b13..e5b47d2 100644 --- a/molecule/default/converge.yml +++ b/molecule/default/converge.yml @@ -38,6 +38,7 @@ - name: users hostname: database1.example.org port: 5433 + borg_install_method: pip post_tasks: - name: Install yamllint for checking config file diff --git a/tasks/Archlinux.yml b/tasks/Archlinux.yml deleted file mode 100644 index ed97d53..0000000 --- a/tasks/Archlinux.yml +++ /dev/null @@ -1 +0,0 @@ ---- diff --git a/tasks/Debian.yml b/tasks/Debian.yml deleted file mode 100644 index 73b314f..0000000 --- a/tasks/Debian.yml +++ /dev/null @@ -1 +0,0 @@ ---- \ No newline at end of file diff --git a/tasks/RedHat.yml b/tasks/RedHat.yml deleted file mode 100644 index ed97d53..0000000 --- a/tasks/RedHat.yml +++ /dev/null @@ -1 +0,0 @@ ---- diff --git a/tasks/install_package.yml b/tasks/install_package.yml new file mode 100644 index 0000000..f153ecb --- /dev/null +++ b/tasks/install_package.yml @@ -0,0 +1,18 @@ +--- +- name: Check if EPEL repo is enabled, if installation from distro is requested + when: ansible_os_family == 'RedHat' + block: + - name: Get list of installed packages + ansible.builtin.package_facts: + manager: auto + - name: Ensure EPEL is enabled + ansible.builtin.assert: + that: + - "'epel-release' in ansible_facts.packages" + fail_msg: Need EPEL repo to install via distro package. + +- name: Install borgmatic and borg via distribution package manager + package: + name: "{{ item }}" + state: present + loop: "{{ borg_distro_packages }}" diff --git a/tasks/install_pip.yml b/tasks/install_pip.yml new file mode 100644 index 0000000..1f72f39 --- /dev/null +++ b/tasks/install_pip.yml @@ -0,0 +1,39 @@ +--- +- name: Install build dependencies + package: + name: "{{ borg_pip_packages }}" + state: present + +- name: Create virtualenv for borg # noqa package-latest + pip: + name: + - pip + - setuptools + state: latest + virtualenv: /opt/borgmatic + virtualenv_command: "{{ python_bin }} -m venv" + +- name: Install dependent Python Packages + pip: + name: "{{ borg_dependent_python_packages }}" + virtualenv: /opt/borgmatic + when: borg_dependent_python_packages is defined + +- name: Install main Python Packages + pip: + name: "{{ item.name }}" + version: "{{ item.version | default(omit, true) }}" + virtualenv: /opt/borgmatic + when: borg_python_packages is defined + loop: "{{ borg_python_packages }}" + +- name: Create borgmatic command in /usr/local/bin + copy: + content: | + #!/bin/bash + . /opt/borgmatic/bin/activate + borgmatic "$@" + dest: /usr/local/bin/borgmatic + owner: root + group: root + mode: "0755" diff --git a/tasks/main.yml b/tasks/main.yml index fcd63f4..4b67356 100755 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -16,50 +16,14 @@ - "{{ ansible_os_family }}.yml" - "{{ ansible_lsb.id }}.yml" -- name: Run OS-specific tasks - include_tasks: "{{ item }}" - with_first_found: - - "{{ ansible_os_family }}-{{ ansible_distribution_major_version }}.yml" - - "{{ ansible_os_family }}.yml" - -- name: Install required System Packages +- name: Install general dependencies (cron and openssh) package: - name: "{{ borg_packages }}" + name: "{{ borg_dep_packages }}" state: present -- name: Create virtualenv for borg # noqa package-latest - pip: - name: - - pip - - setuptools - state: latest - virtualenv: /opt/borgmatic - virtualenv_command: "{{ python_bin }} -m venv" - -- name: Install dependent Python Packages - pip: - name: "{{ borg_dependent_python_packages }}" - virtualenv: /opt/borgmatic - when: borg_dependent_python_packages is defined - -- name: Install main Python Packages - pip: - name: "{{ item.name }}" - version: "{{ item.version | default(omit, true) }}" - virtualenv: /opt/borgmatic - when: borg_python_packages is defined - loop: "{{ borg_python_packages }}" - -- name: Create borgmatic command in /usr/local/bin - copy: - content: | - #!/bin/bash - . /opt/borgmatic/bin/activate - borgmatic "$@" - dest: /usr/local/bin/borgmatic - owner: root - group: root - mode: "0755" +- name: Install Borg and Borgmatic + ansible.builtin.include_tasks: + file: install_{{ borg_install_method }}.yml - name: Ensure root has SSH key. user: diff --git a/vars/Archlinux.yml b/vars/Archlinux.yml index 8b0027d..0ec7e01 100644 --- a/vars/Archlinux.yml +++ b/vars/Archlinux.yml @@ -1,11 +1,17 @@ --- -borg_packages: +borg_dep_packages: - cronie - - gcc - openssh + +borg_pip_packages: + - gcc - pkgconfig - python-pip - python-setuptools +borg_distro_packages: + - borg + - borgmatic + python_bin: python3 pip_bin: pip3 diff --git a/vars/Debian.yml b/vars/Debian.yml index 5dbed10..03d7d87 100644 --- a/vars/Debian.yml +++ b/vars/Debian.yml @@ -1,5 +1,9 @@ --- -borg_packages: +borg_dep_packages: + - openssh-client + - cron + +borg_pip_packages: - libssl-dev - libacl1-dev - libacl1 @@ -10,8 +14,10 @@ borg_packages: - python3-pkgconfig - python3-msgpack - python3-venv - - openssh-client - - cron + +borg_distro_packages: + - borgbackup + - borgmatic python_bin: python3 pip_bin: pip3 diff --git a/vars/Fedora.yml b/vars/Fedora.yml index 9f12d89..6a2166b 100644 --- a/vars/Fedora.yml +++ b/vars/Fedora.yml @@ -1,5 +1,9 @@ --- -borg_packages: +borg_dep_packages: + - cronie + - openssh-clients + +borg_pip_packages: - libacl-devel - libacl - gcc @@ -10,8 +14,10 @@ borg_packages: - python3-devel - python3-setuptools - python3-Cython - - openssh-clients - - cronie + +borg_distro_packages: + - borgbackup + - borgmatic python_bin: python3 pip_bin: pip3 diff --git a/vars/ManjaroLinux.yml b/vars/ManjaroLinux.yml index a4dfa5d..52e70f6 100644 --- a/vars/ManjaroLinux.yml +++ b/vars/ManjaroLinux.yml @@ -1,8 +1,23 @@ --- -borg_packages: - - borgmatic +borg_dep_packages: - openssh - cronie +borg_pip_packages: # untested + - libssl-dev + - libacl1-dev + - libacl1 + - build-essential + - python3-setuptools + - python3-dev + - python3-pip + - python3-pkgconfig + - python3-msgpack + - python3-venv + +borg_distro_packages: + - borg + - borgmatic + python_bin: python3 pip_bin: pip3 diff --git a/vars/RedHat-8.yml b/vars/RedHat-8.yml index 7ee2d7e..4673e05 100644 --- a/vars/RedHat-8.yml +++ b/vars/RedHat-8.yml @@ -1,5 +1,9 @@ --- -borg_packages: +borg_dep_packages: + - openssh-clients + - cronie + +borg_pip_packages: - libacl-devel - libacl - gcc @@ -10,8 +14,10 @@ borg_packages: - python3-devel - python3-setuptools - python3-virtualenv - - openssh-clients - - cronie + +borg_distro_packages: + - borgbackup + - borgmatic python_bin: python3 pip_bin: pip3 diff --git a/vars/RedHat-9.yml b/vars/RedHat-9.yml index bbf20e6..50112d3 100644 --- a/vars/RedHat-9.yml +++ b/vars/RedHat-9.yml @@ -1,5 +1,9 @@ --- -borg_packages: +borg_dep_packages: + - cronie + - openssh-clients + +borg_pip_packages: - libacl-devel - libacl - gcc @@ -10,8 +14,10 @@ borg_packages: - python3-devel - python3-setuptools # - python3-virtualenv - - openssh-clients - - cronie + +borg_distro_packages: + - borgbackup + - borgmatic python_bin: python3 pip_bin: pip3 diff --git a/vars/RedHat.yml b/vars/RedHat.yml index bd2fd08..7c5f8b6 100644 --- a/vars/RedHat.yml +++ b/vars/RedHat.yml @@ -1,5 +1,9 @@ --- -borg_packages: +borg_dep_packages: + - cronie + - openssh-clients + +borg_pip_packages: - libacl-devel - libacl - gcc @@ -9,8 +13,10 @@ borg_packages: - python36-wheel - python36-devel - python-setuptools - - openssh-clients - - cronie + +borg_distro_packages: + - borgbackup + - borgmatic python_bin: python3 pip_bin: pip3 diff --git a/vars/main.yml b/vars/main.yml index 9cf3949..9f34550 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -2,6 +2,7 @@ borg_dependent_python_packages: - cython - pkgconfig + borg_python_packages: - name: borgbackup version: "{{ borg_version }}"