From 5fc37fa6fb319ef8c718b1e8e4979bce77282021 Mon Sep 17 00:00:00 2001 From: Hatifnatt Date: Mon, 30 Nov 2020 18:10:54 +0300 Subject: [PATCH] feat(scripts): deploy helper scripts Deploy arbitrary helper scripts to use them in configuration file --- docs/README.rst | 5 ++++ keepalived/config/file.sls | 2 ++ keepalived/defaults.yaml | 2 ++ keepalived/init.sls | 1 + keepalived/scripts/init.sls | 5 ++++ keepalived/scripts/manage.sls | 48 +++++++++++++++++++++++++++++++++++ pillar.example | 31 ++++++++++++++++++++++ 7 files changed, 94 insertions(+) create mode 100644 keepalived/scripts/init.sls create mode 100644 keepalived/scripts/manage.sls diff --git a/docs/README.rst b/docs/README.rst index 1084e76..35fccda 100644 --- a/docs/README.rst +++ b/docs/README.rst @@ -57,6 +57,11 @@ Installs the keepalived package. ^^^^^^^^^^^^^^^^^^^^^ This state manages the file ``keepalived.conf`` under ``/etc/keepalived`` (template found in "keepalived/files"). The configuration is populated by values in "keepalived/map.jinja" based on the package's default values (and RedHat, Debian, Suse and Arch family distribution specific values), which can then be overridden by values of the same name in pillar. +``keepalived.scripts`` +^^^^^^^^^^^^^^^^^^^^^^ +Put arbitrary helper scripts on the minion. Default scripts location: ``/etc/keepalived`` +This state can deploy script, set its permissions like file mode, user and group, but it won't create new user / group if they do not exist. + ``keepalived.service`` ^^^^^^^^^^^^^^^^^^^^^^ This state ensures that keepalived service is running. diff --git a/keepalived/config/file.sls b/keepalived/config/file.sls index 9e0e351..de743d1 100644 --- a/keepalived/config/file.sls +++ b/keepalived/config/file.sls @@ -4,11 +4,13 @@ {#- Get the `tplroot` from `tpldir` #} {%- set tplroot = tpldir.split('/')[0] %} {%- set sls_package_install = tplroot ~ '.package.install' %} +{%- set sls_scripts_manage = tplroot ~ '.scripts.manage' %} {%- from tplroot ~ "/map.jinja" import keepalived with context %} {%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} include: - {{ sls_package_install }} + - {{ sls_scripts_manage }} keepalived-config-file-file-managed: file.managed: diff --git a/keepalived/defaults.yaml b/keepalived/defaults.yaml index 78abbf9..f267924 100644 --- a/keepalived/defaults.yaml +++ b/keepalived/defaults.yaml @@ -9,3 +9,5 @@ keepalived: config: global_defs: smtp_server: localhost + scripts_dir: /etc/keepalived + scripts: {} diff --git a/keepalived/init.sls b/keepalived/init.sls index 858a8e6..9e7159b 100644 --- a/keepalived/init.sls +++ b/keepalived/init.sls @@ -3,5 +3,6 @@ include: - .package + - .scripts - .config - .service diff --git a/keepalived/scripts/init.sls b/keepalived/scripts/init.sls new file mode 100644 index 0000000..9537eda --- /dev/null +++ b/keepalived/scripts/init.sls @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +include: + - .manage diff --git a/keepalived/scripts/manage.sls b/keepalived/scripts/manage.sls new file mode 100644 index 0000000..28544d6 --- /dev/null +++ b/keepalived/scripts/manage.sls @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +# vim: ft=sls + +{#- Get the `tplroot` from `tpldir` #} +{%- set tplroot = tpldir.split('/')[0] %} +{%- set sls_package_install = tplroot ~ '.package.install' %} +{%- from tplroot ~ "/map.jinja" import keepalived with context %} +{%- from tplroot ~ "/libtofs.jinja" import files_switch with context %} + +include: + - {{ sls_package_install }} + +{#- Don't create scripts_dir if no scripts defined #} +{%- if 'scripts' in keepalived and keepalived.scripts %} +keepalived-scripts-manage-file-directory: + file.directory: + - name: {{ keepalived.scripts_dir }} + - makedirs: true + - require: + - sls: {{ sls_package_install }} +{%- endif %} + +{%- for script,data in keepalived.scripts|dictsort %} + {%- set ensure = data.get('ensure', present) %} + {%- if ensure == 'present' %} +keepalived-scripts-manage-file-managed-{{ script }}: + file.managed: + - name: {{ data.get('dst_file', keepalived.scripts_dir ~ '/' ~ script) }} + - user: {{ data.get('user', 'root') }} + - group: {{ data.get('group', 'root') }} + - mode: {{ data.get('mode', '755') }} + - template: {{ data.get('template_engine', 'jinja') }} + {%- if 'contents' in data %} + - contents: | + {{ data.contents|indent(width=8) }} + {%- elif 'template_file' in data %} + - source: {{ files_switch([data.template_file]) }} + - context: + data: {{ data.context|tojson }} + {%- endif %} + - require: + - sls: {{ sls_package_install }} + {%- elif ensure == 'absent' %} +keepalived-scripts-manage-file-absent-{{ script }}: + file.absent: + - name: {{ data.get('dst_file', keepalived.scripts_dir ~ '/' ~ script) }} + {%- endif %} +{%- endfor %} diff --git a/pillar.example b/pillar.example index ee2f892..389318f 100644 --- a/pillar.example +++ b/pillar.example @@ -148,3 +148,34 @@ keepalived: script: '"killall -0 apache"' interval: 2 weight: 10 + # put helper scripts on the minon + # defaut directory where scripts will be saved if full path not specified + scripts_dir: /etc/keepalived + scripts: + # item name, will be used as file name if full path not specified + check_sshd.sh: + # present - create script + # absent - remove file + ensure: present + # user and group for script file, default is root:root + # note: it's required to use existing user and group + user: root + group: root + # file mode, default is 755 + mode: '755' + # full path for script, optional + # if not defined "scripts_dir + '/' + script" will be used as file name + dst_file: /etc/keepalived/check_sshd.sh + # 'contents' have more priority than 'template_file', + # if 'contents' present, 'template_file' won't be used, + # but one of them is mandatory + contents: | + #!/usr/bin/env bash + pidof sshd + # source template for script + template_file: check_sshd.sh + # template engine to use for rendering, default is jinja + template_engine: jinja + # dict with arbitrary data that will be passed to template as 'data' variable + context: + foo: bar