From a89b6aff93eec5da9962dd74a2805cedd9195e7e Mon Sep 17 00:00:00 2001 From: Jeff Mahoney Date: Thu, 24 Feb 2022 14:32:54 -0500 Subject: [PATCH] Improve usage on SUSE releases (#155) * Do not install init script on SUSE systems (#152) The last SUSE releases that didn't support systemd were SLE 11 and openSUSE 11.x. The telegraf packages on the Open Build Service all contain systemd service files. openSUSE 11.x is long since dead and buried and SLE 11 has been out of general support since mid-2019. There are no other users of the script and it can be removed. The dependencies required in the molecule configuration can also be removed. * Fix Python package dependencies on SUSE releases (#153) The naming of the packages for the Python libxml and xml modules on SUSE releases is a bit of a mess across releases. SLE12 ships with Python 3.4 which is not new enough for Ansible to use so Python 2 must be used. Those packages are named python-xml and python-libxml2. SLE15 ships with Python 3.6 but the package containing the libxml2 module is named python3-libxml2-python. The xml module is part of python3-base and will be present if the interpreter is present. On later releases, the package containing the libxml2 module has been renamed python3-libxml2 but it still offers an alias for the old name. * Add default repository handling for SUSE releases (#154) The repositories for SUSE releases are in well-known locations and requiring the user to specify them is unnecessary. If the user wishes to override the default repo using the telegraf_zypper_baseurl that will still be honored. --- README.md | 2 +- defaults/main.yml | 6 +- files/telegraf.init | 212 ----------------------------------- molecule/default/prepare.yml | 2 - tasks/Suse.yml | 37 ++++-- 5 files changed, 34 insertions(+), 225 deletions(-) delete mode 100644 files/telegraf.init diff --git a/README.md b/README.md index a5be438..750d100 100644 --- a/README.md +++ b/README.md @@ -235,7 +235,7 @@ _Supporting Windows is an best effort (I don't have the possibility to either te ## openSUSE specific Variables -* `telegraf_zypper_baseurl`: The URL to the openSUSE repository that hosts Telegraf (for example, for openSUSE Leap: "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Leap_{{ ansible_distribution_version }}/") +* `telegraf_zypper_baseurl`: The URL to the openSUSE repository that hosts Telegraf (for example, for openSUSE Leap: "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Leap_{{ ansible_distribution_version }}/"). If this is unspecified, a default repository will be used. ## MacOS specific Variables diff --git a/defaults/main.yml b/defaults/main.yml index a06f9bc..a9cea11 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -79,7 +79,11 @@ telegraf_yum_baseurl: rocky: "https://repos.influxdata.com/rhel/{{ telegraf_redhat_releasever }}/$basearch/stable" telegraf_yum_gpgkey: "https://repos.influxdata.com/influxdb.key" -telegraf_zypper_baseurl: "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Factory/" +telegraf_zypper_repos: + "opensuse tumbleweed": "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Factory/" + "default": "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Factory/" + "sles": "http://download.opensuse.org/repositories/devel:/languages:/go/SLE_{{ ansible_distribution_major_version }}_SP{{ ansible_distribution_release }}/" + "opensuse leap": "http://download.opensuse.org/repositories/devel:/languages:/go/openSUSE_Leap_{{ ansible_distribution_version }}/" telegraf_win_install_dir: 'C:\Telegraf' telegraf_win_logfile: 'C:\\Telegraf\\telegraf.log' diff --git a/files/telegraf.init b/files/telegraf.init deleted file mode 100644 index 668dc54..0000000 --- a/files/telegraf.init +++ /dev/null @@ -1,212 +0,0 @@ -#! /usr/bin/env bash - -# chkconfig: 2345 99 01 -# description: Telegraf daemon - -### BEGIN INIT INFO -# Provides: telegraf -# Required-Start: $all -# Required-Stop: $remote_fs $syslog -# Default-Start: 2 3 4 5 -# Default-Stop: 0 1 6 -# Short-Description: Start telegraf at boot time -### END INIT INFO - -# this init script supports three different variations: -# 1. New lsb that define start-stop-daemon -# 2. Old lsb that don't have start-stop-daemon but define, log, pidofproc and killproc -# 3. Centos installations without lsb-core installed -# -# In the third case we have to define our own functions which are very dumb -# and expect the args to be positioned correctly. - -# Command-line options that can be set in /etc/default/telegraf. These will override -# any config file values. -TELEGRAF_OPTS= - -USER=telegraf -GROUP=telegraf - -if [ -r /lib/lsb/init-functions ]; then - source /lib/lsb/init-functions -fi - -DEFAULT=/etc/default/telegraf - -if [ -r $DEFAULT ]; then - set -o allexport - source $DEFAULT - set +o allexport -fi - -if [ -z "$STDOUT" ]; then - STDOUT=/dev/null -fi -if [ ! -f "$STDOUT" ]; then - mkdir -p `dirname $STDOUT` -fi - -if [ -z "$STDERR" ]; then - STDERR=/var/log/telegraf/telegraf.log -fi -if [ ! -f "$STDERR" ]; then - mkdir -p `dirname $STDERR` -fi - -OPEN_FILE_LIMIT=65536 - -function pidofproc() { - if [ $# -ne 3 ]; then - echo "Expected three arguments, e.g. $0 -p pidfile daemon-name" - fi - - if [ ! -f "$2" ]; then - return 1 - fi - - local pidfile=`cat $2` - - if [ "x$pidfile" == "x" ]; then - return 1 - fi - - if ps --pid "$pidfile" | grep -q $(basename $3); then - return 0 - fi - - return 1 -} - -function killproc() { - if [ $# -ne 3 ]; then - echo "Expected three arguments, e.g. $0 -p pidfile signal" - fi - - pid=`cat $2` - - kill -s $3 $pid -} - -function log_failure_msg() { - echo "$@" "[ FAILED ]" -} - -function log_success_msg() { - echo "$@" "[ OK ]" -} - -# Process name ( For display ) -name=telegraf - -# Daemon name, where is the actual executable -daemon=/usr/bin/telegraf - -# pid file for the daemon -pidfile=/var/run/telegraf/telegraf.pid -piddir=`dirname $pidfile` - -if [ ! -d "$piddir" ]; then - mkdir -p $piddir - chown $USER:$GROUP $piddir -fi - -# Configuration file -config=/etc/telegraf/telegraf.conf -confdir=/etc/telegraf/telegraf.d - -# If the daemon is not there, then exit. -[ -x $daemon ] || exit 5 - -case $1 in - start) - # Checked the PID file exists and check the actual status of process - if [ -e $pidfile ]; then - pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?" - # If the status is SUCCESS then don't need to start again. - if [ "x$status" = "x0" ]; then - log_failure_msg "$name process is running" - exit 0 # Exit - fi - fi - - # Bump the file limits, before launching the daemon. These will carry over to - # launched processes. - ulimit -n $OPEN_FILE_LIMIT - if [ $? -ne 0 ]; then - log_failure_msg "set open file limit to $OPEN_FILE_LIMIT" - fi - - log_success_msg "Starting the process" "$name" - if command -v startproc >/dev/null; then - startproc -u "$USER" -g "$GROUP" -p "$pidfile" -q -- "$daemon" -pidfile "$pidfile" -config "$config" -config-directory "$confdir" $TELEGRAF_OPTS - elif which start-stop-daemon > /dev/null 2>&1; then - start-stop-daemon --chuid $USER:$GROUP --start --quiet --pidfile $pidfile --exec $daemon -- -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR & - else - su -s /bin/sh -c "nohup $daemon -pidfile $pidfile -config $config -config-directory $confdir $TELEGRAF_OPTS >>$STDOUT 2>>$STDERR &" $USER - fi - log_success_msg "$name process was started" - ;; - - stop) - # Stop the daemon. - if [ -e $pidfile ]; then - pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?" - if [ "$status" = 0 ]; then - if killproc -p $pidfile SIGTERM && /bin/rm -rf $pidfile; then - log_success_msg "$name process was stopped" - else - log_failure_msg "$name failed to stop service" - fi - fi - else - log_failure_msg "$name process is not running" - fi - ;; - - reload) - # Reload the daemon. - if [ -e $pidfile ]; then - pidofproc -p $pidfile $daemon > /dev/null 2>&1 && status="0" || status="$?" - if [ "$status" = 0 ]; then - if killproc -p $pidfile SIGHUP; then - log_success_msg "$name process was reloaded" - else - log_failure_msg "$name failed to reload service" - fi - fi - else - log_failure_msg "$name process is not running" - fi - ;; - - restart) - # Restart the daemon. - $0 stop && sleep 2 && $0 start - ;; - - status) - # Check the status of the process. - if [ -e $pidfile ]; then - if pidofproc -p $pidfile $daemon > /dev/null; then - log_success_msg "$name Process is running" - exit 0 - else - log_failure_msg "$name Process is not running" - exit 1 - fi - else - log_failure_msg "$name Process is not running" - exit 3 - fi - ;; - - version) - $daemon version - ;; - - *) - # For invalid arguments, print the usage message. - echo "Usage: $0 {start|stop|restart|status|version}" - exit 2 - ;; -esac diff --git a/molecule/default/prepare.yml b/molecule/default/prepare.yml index c0578b1..c923b34 100644 --- a/molecule/default/prepare.yml +++ b/molecule/default/prepare.yml @@ -28,8 +28,6 @@ - name: "Installing packages on Suse" zypper: name: - - sysvinit-tools - - insserv-compat - aaa_base state: present when: diff --git a/tasks/Suse.yml b/tasks/Suse.yml index 5b63b4c..4cafef6 100644 --- a/tasks/Suse.yml +++ b/tasks/Suse.yml @@ -12,16 +12,40 @@ state: present become: yes -- name: "Suse | Install repo dependencies" +- name: "Suse | Install repo dependencies for Python 2" zypper: - name: ["python-libxml2", "python-xml"] + name: + - python-libxml2 + - python-xml update_cache: True state: present register: are_telegraf_dependencies_packages_installed until: are_telegraf_dependencies_packages_installed is succeeded become: yes + when: ansible_python.version.major == 2 -- name: "Suse | Install basic repo file" +- name: "Suse | Install repo dependencies for Python >= 3" + zypper: + name: + - python3-libxml2-python + update_cache: True + state: present + register: are_telegraf_dependencies_packages_installed + until: are_telegraf_dependencies_packages_installed is succeeded + become: yes + when: ansible_python.version.major >= 3 + +- name: "Suse | Add default Open Build Service repository" + zypper_repository: + repo: "{{ telegraf_zypper_repos[ansible_distribution|lower] | default(telegraf_zypper_repos['default']) }}" + name: "telegraf" + state: present + runrefresh: True + auto_import_keys: True + become: yes + when: telegraf_zypper_baseurl is not defined + +- name: "Suse | Add specified package repository" zypper_repository: repo: "{{ telegraf_zypper_baseurl }}" name: "telegraf" @@ -29,6 +53,7 @@ runrefresh: True auto_import_keys: True become: yes + when: telegraf_zypper_baseurl is defined - name: "Suse | Install Telegraf" zypper: @@ -49,9 +74,3 @@ owner: root - path: /var/log/telegraf owner: telegraf - -- name: "Suse | Install init file" - copy: - src: telegraf.init - dest: /etc/init.d/telegraf - mode: 0755