From b24f1f849b18ae5a27d583a974e75c628ffccf59 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Sun, 14 Nov 2021 13:37:49 +0100 Subject: [PATCH] added cleanup on softwares --- states/_modules/software.py | 18 ++++++++++++++++++ states/_states/software.py | 13 +++++++++++++ states/golang/install.sls | 12 +++++++++--- states/influxdb/install.sls | 6 ++++++ states/lego/defaults.yaml | 2 +- states/lego/install.sls | 16 ++++++++++------ states/nextcloud/install.sls | 8 +++++++- states/pycharm/install.sls | 6 ++++++ states/qrz/install.sls | 6 ++++++ states/rclone/defaults.yaml | 2 +- states/rclone/install.sls | 12 +++++++++--- states/syncthing/install.sls | 15 +++++++++++---- states/telegraf/install.sls | 6 ++++++ states/telegram/install.sls | 8 +++++++- 14 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 states/_modules/software.py create mode 100644 states/_states/software.py diff --git a/states/_modules/software.py b/states/_modules/software.py new file mode 100644 index 0000000..6e6e7f9 --- /dev/null +++ b/states/_modules/software.py @@ -0,0 +1,18 @@ +#!/usr/bin/python3 + +import re +import os + +def cleanup(name=None, path=None, version=None): + res = [] + version = version.replace(".", "\.") + keep_pattern = f"^{name}-(?!{version}).*$" + regex = re.compile(keep_pattern) + for f in os.listdir(path): + if regex.match(f): + to_remove = f"{path}/{f}" + res.append(to_remove) + __salt__["file.remove"](to_remove) + if len(res) > 1: + return res + return None diff --git a/states/_states/software.py b/states/_states/software.py new file mode 100644 index 0000000..79c5ff3 --- /dev/null +++ b/states/_states/software.py @@ -0,0 +1,13 @@ +#!/usr/bin/python3 + +def cleanup(name=None, path=None, version=None): + ret = {'name': name, + 'changes': {}, + 'result': True, + 'comment': 'No changes been made'} + + res = __salt__['software.cleanup'](name, path, version) + if res is not None: + ret['changes']["folders"] = res + + return ret diff --git a/states/golang/install.sls b/states/golang/install.sls index 597ae1e..cc7553d 100644 --- a/states/golang/install.sls +++ b/states/golang/install.sls @@ -2,17 +2,23 @@ {%- from "golang/map.jinja" import golang with context %} golang-archive-extract: archive.extracted: - - name: {{ golang.release_dir }}/go{{ golang.version }} + - name: {{ golang.release_dir }}/go-{{ golang.version }} - source: {{ golang.mirror }}/go{{ golang.version }}.{{ golang.os }}-{{ golang.arch }}.tar.gz - skip_verify: true - enforce_toplevel: false - options: --transform 's/^go\///g' - - if_missing: {{ golang.release_dir }}/go{{ golang.version }} + - if_missing: {{ golang.release_dir }}/go-{{ golang.version }} golang-binary-symlink: file.symlink: - name: {{ golang.install_dir }}/go - - target: {{ golang.release_dir }}/go{{ golang.version }} + - target: {{ golang.release_dir }}/go-{{ golang.version }} - force: true - require: - archive: golang-archive-extract + +golang-cleanup: + software.cleanup: + - name: go + - path: {{ golang.release_dir }} + - version: {{ golang.version }} diff --git a/states/influxdb/install.sls b/states/influxdb/install.sls index 34525fa..e171f5c 100644 --- a/states/influxdb/install.sls +++ b/states/influxdb/install.sls @@ -34,6 +34,12 @@ influxdb-archive-extract: - watch_in: - service: influxdb-service +influxdb-cleanup: + software.cleanup: + - name: influxdb + - path: {{ influxdb.release_dir }} + - version: {{ influxdb.version }} + influxdb-bin-symlink: file.symlink: - name: {{ influxdb.install_dir }}/influxdb diff --git a/states/lego/defaults.yaml b/states/lego/defaults.yaml index a494136..03e74c8 100644 --- a/states/lego/defaults.yaml +++ b/states/lego/defaults.yaml @@ -4,6 +4,6 @@ lego: install_dir: /usr/local/apps release_dir: /usr/local/apps/releases mirror: https://github.com/go-acme/lego/releases/download - version: v4.0.1 + version: 4.0.1 os: linux arch: amd64 diff --git a/states/lego/install.sls b/states/lego/install.sls index 7050d66..f475ea5 100644 --- a/states/lego/install.sls +++ b/states/lego/install.sls @@ -1,19 +1,23 @@ --- {%- from "lego/map.jinja" import lego with context %} -{%- if not salt['file.file_exists']( lego.install_dir +"/lego-" + lego.version) %} lego-archive-extract: archive.extracted: - - name: {{ lego.release_dir }}/lego-{{ lego.version }}-{{ lego.os }}-{{ lego.arch }} - - source: {{ lego.mirror }}/{{ lego.version }}/lego_{{ lego.version }}_{{ lego.os }}_{{ lego.arch }}.tar.gz + - name: {{ lego.release_dir }}/lego-{{ lego.version }} + - source: {{ lego.mirror }}/v{{ lego.version }}/lego_v{{ lego.version }}_{{ lego.os }}_{{ lego.arch }}.tar.gz - skip_verify: true - enforce_toplevel: false - - if_missing: {{ lego.release_dir }}/lego-{{ lego.version }}-{{ lego.os }}-{{ lego.arch }} + - if_missing: {{ lego.release_dir }}/lego-{{ lego.version }} lego-binary-symlink: file.symlink: - name: /usr/local/bin/lego - - target: {{ lego.release_dir }}/lego-{{ lego.version }}-{{ lego.os }}-{{ lego.arch }}/lego + - target: {{ lego.release_dir }}/lego-{{ lego.version }}/lego - force: true - require: - archive: lego-archive-extract -{%- endif %} + +lego-cleanup: + software.cleanup: + - name: lego + - path: {{ lego.release_dir }} + - version: {{ lego.version }} diff --git a/states/nextcloud/install.sls b/states/nextcloud/install.sls index 09cab9d..3da1d56 100644 --- a/states/nextcloud/install.sls +++ b/states/nextcloud/install.sls @@ -18,8 +18,14 @@ nextcloud-install-link: - target: {{ nextcloud.release_dir }}/nextcloud-{{ nextcloud.version }} - force: true +nextcloud-cleanup: + software.cleanup: + - name: nextcloud + - path: {{ nextcloud.release_dir }} + - version: {{ nextcloud.version }} + nextcloud-datadirectory: file.directory: - - name: {{ nextcloud.config.datadirectory}} + - name: {{ nextcloud.config.datadirectory }} - user: {{ nextcloud.user }} - group: {{ nextcloud.group }} diff --git a/states/pycharm/install.sls b/states/pycharm/install.sls index 2f729e6..1a5ead0 100644 --- a/states/pycharm/install.sls +++ b/states/pycharm/install.sls @@ -17,6 +17,12 @@ pycharm-link: - target: {{ pycharm.release_dir }}/pycharm-community-{{ pycharm.version }} - force: true +pycharm-cleanup: + software.cleanup: + - name: pycharm-community + - path: {{ pycharm.release_dir }} + - version: {{ pycharm.version }} + pycharm-shortcut: file.managed: - name: /usr/share/applications/jetbrains-pycharm-ce.desktop diff --git a/states/qrz/install.sls b/states/qrz/install.sls index ca0a242..40b40c6 100644 --- a/states/qrz/install.sls +++ b/states/qrz/install.sls @@ -18,3 +18,9 @@ qrz-binary-symlink: - archive: qrz-archive-extract - watch_in: - service: qrz-service + +qrz-cleanup: + software.cleanup: + - name: qrz + - path: {{ qrz.release_dir }} + - version: {{ qrz.version }} diff --git a/states/rclone/defaults.yaml b/states/rclone/defaults.yaml index 5538dbe..c5254d9 100644 --- a/states/rclone/defaults.yaml +++ b/states/rclone/defaults.yaml @@ -4,6 +4,6 @@ rclone: install_dir: /usr/local/apps release_dir: /usr/local/apps/releases mirror: https://downloads.rclone.org - version: v1.50.2 + version: 1.50.2 os: linux arch: amd64 diff --git a/states/rclone/install.sls b/states/rclone/install.sls index 2e26015..6bb8fb8 100644 --- a/states/rclone/install.sls +++ b/states/rclone/install.sls @@ -3,15 +3,21 @@ rclone-archive-extract: archive.extracted: - name: {{ rclone.release_dir }} - - source: {{ rclone.mirror }}/{{ rclone.version }}/rclone-{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}.zip + - source: {{ rclone.mirror }}/v{{ rclone.version }}/rclone-v{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}.zip - skip_verify: true - enforce_toplevel: false - - if_missing: {{ rclone.release_dir }}/rclone-{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}/rclone + - if_missing: {{ rclone.release_dir }}/rclone-v{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}/rclone rclone-binary-symlink: file.symlink: - name: /usr/local/bin/rclone - - target: {{ rclone.release_dir }}/rclone-{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}/rclone + - target: {{ rclone.release_dir }}/rclone-v{{ rclone.version }}-{{ rclone.os }}-{{ rclone.arch }}/rclone - force: true - require: - archive: rclone-archive-extract + +rclone-cleanup: + software.cleanup: + - name: rclone + - path: {{ rclone.release_dir }} + - version: v{{ rclone.version }} diff --git a/states/syncthing/install.sls b/states/syncthing/install.sls index 0203488..04c3399 100644 --- a/states/syncthing/install.sls +++ b/states/syncthing/install.sls @@ -2,18 +2,25 @@ {%- from "syncthing/map.jinja" import syncthing with context %} syncthing-archive-extract: archive.extracted: - - name: {{ syncthing.release_dir }} + - name: {{ syncthing.release_dir }}/syncthing-{{ syncthing.version }} - source: {{ syncthing.mirror }}/v{{ syncthing.version }}/syncthing-{{ syncthing.os }}-{{ syncthing.arch }}-v{{ syncthing.version }}.tar.gz - enforce_toplevel: false - skip_verify: true - archive_format: tar - - if_missing: {{ syncthing.release_dir }}/syncthing-{{ syncthing.os }}-{{ syncthing.arch }}-v{{ syncthing.version }} + - options: --transform 's/syncthing-{{ syncthing.os }}-{{ syncthing.arch }}-v{{ syncthing.version }}/syncthing-{{ syncthing.version }}/g' --strip 1 + - if_missing: {{ syncthing.release_dir }}/syncthing-{{ syncthing.version }} - watch_in: - service: syncthing-service syncthing-bin-symlink: file.symlink: - name: {{ syncthing.install_dir }}/syncthing - - target: {{ syncthing.release_dir }}/syncthing-{{ syncthing.os }}-{{ syncthing.arch }}-v{{ syncthing.version }} + - target: {{ syncthing.release_dir }}/syncthing-{{ syncthing.version }} - watch_in: - - service: syncthing-service \ No newline at end of file + - service: syncthing-service + +syncthing-cleanup: + software.cleanup: + - name: syncthing + - path: {{ syncthing.release_dir }} + - version: {{ syncthing.version }} \ No newline at end of file diff --git a/states/telegraf/install.sls b/states/telegraf/install.sls index 8e94c1a..82ccd89 100644 --- a/states/telegraf/install.sls +++ b/states/telegraf/install.sls @@ -29,3 +29,9 @@ telegraf-defaults-file: - template: jinja - watch_in: - service: telegraf-service + +telegraf-cleanup: + software.cleanup: + - name: telegraf + - path: {{ telegraf.release_dir }} + - version: {{ telegraf.version }} diff --git a/states/telegram/install.sls b/states/telegram/install.sls index 6f00ba5..42ffc1f 100644 --- a/states/telegram/install.sls +++ b/states/telegram/install.sls @@ -46,4 +46,10 @@ telegram-shortcut: - mode: 644 - template: jinja - require: - - archive: telegram-archive-extract \ No newline at end of file + - archive: telegram-archive-extract + +telegram-cleanup: + software.cleanup: + - name: telegram + - path: {{ telegram.release_dir }} + - version: {{ telegram.version }} \ No newline at end of file