feat(scripts): deploy helper scripts

Deploy arbitrary helper scripts to use them in configuration file
This commit is contained in:
Hatifnatt 2020-11-30 18:10:54 +03:00
parent a82e3efa7c
commit 5fc37fa6fb
7 changed files with 94 additions and 0 deletions

View File

@ -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.

View File

@ -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:

View File

@ -9,3 +9,5 @@ keepalived:
config:
global_defs:
smtp_server: localhost
scripts_dir: /etc/keepalived
scripts: {}

View File

@ -3,5 +3,6 @@
include:
- .package
- .scripts
- .config
- .service

View File

@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# vim: ft=sls
include:
- .manage

View File

@ -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 %}

View File

@ -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