From 3020ee91c232f722dd65b983cf22a84f45975c1d Mon Sep 17 00:00:00 2001 From: Daniel Fancsali Date: Sat, 27 Feb 2021 11:00:52 +0000 Subject: [PATCH] Defer explorer logic --- cdist/conf/type/__mysql/gencode-remote | 20 ------- .../conf/type/__mysql_database/explorer/state | 33 ------------ .../conf/type/__mysql_database/gencode-remote | 20 ++++--- cdist/conf/type/__mysql_user/explorer/state | 54 ------------------- cdist/conf/type/__mysql_user/gencode-remote | 32 ++++++++--- 5 files changed, 40 insertions(+), 119 deletions(-) delete mode 100755 cdist/conf/type/__mysql/gencode-remote delete mode 100755 cdist/conf/type/__mysql_database/explorer/state delete mode 100755 cdist/conf/type/__mysql_user/explorer/state diff --git a/cdist/conf/type/__mysql/gencode-remote b/cdist/conf/type/__mysql/gencode-remote deleted file mode 100755 index 5464795f..00000000 --- a/cdist/conf/type/__mysql/gencode-remote +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh -e -# -# 2021 Daniel Fancsali (fancsali@gmail.com) -# -# This file is part of cdist. -# -# cdist is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# cdist is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with cdist. If not, see . -# - diff --git a/cdist/conf/type/__mysql_database/explorer/state b/cdist/conf/type/__mysql_database/explorer/state deleted file mode 100755 index 79858695..00000000 --- a/cdist/conf/type/__mysql_database/explorer/state +++ /dev/null @@ -1,33 +0,0 @@ -#!/bin/sh -e -# -# 2020 Ander Punnar (ander-at-kvlt-dot-ee) -# -# This file is part of cdist. -# -# cdist is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# cdist is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with cdist. If not, see . -# - -if [ -f "$__object/parameter/name" ] -then - name="$( cat "$__object/parameter/name" )" -else - name="$__object_id" -fi - -if [ -n "$( mysql -B -N -e "show databases like '$name'" )" ] -then - echo 'present' -else - echo 'absent' -fi diff --git a/cdist/conf/type/__mysql_database/gencode-remote b/cdist/conf/type/__mysql_database/gencode-remote index 1bdb2b11..ad22cc6e 100755 --- a/cdist/conf/type/__mysql_database/gencode-remote +++ b/cdist/conf/type/__mysql_database/gencode-remote @@ -18,15 +18,9 @@ # along with cdist. If not, see . # -state_is="$( cat "$__object/explorer/state" )" state_should="$( cat "$__object/parameter/state" )" -if [ "$state_is" = "$state_should" ] -then - exit 0 -fi - if [ -f "$__object/parameter/name" ] then name="$( cat "$__object/parameter/name" )" @@ -34,6 +28,20 @@ else name="$__object_id" fi +# Figure out the current state +# Not an explorer, as otherwise there'd be issues around the dependencies +if [ -n "$( $__remote_exec $__target_host "mysql -B -N -e \"show databases like '$name'\"" )" ] +then + state_is="present" +else + state_is="absent" +fi + +if [ "$state_is" = "$state_should" ] +then + exit 0 +fi + case "$state_should" in present) echo "mysql -e 'create database \`$name\`'" diff --git a/cdist/conf/type/__mysql_user/explorer/state b/cdist/conf/type/__mysql_user/explorer/state deleted file mode 100755 index 6817ee9d..00000000 --- a/cdist/conf/type/__mysql_user/explorer/state +++ /dev/null @@ -1,54 +0,0 @@ -#!/bin/sh -e -# -# 2020 Ander Punnar (ander-at-kvlt-dot-ee) -# -# This file is part of cdist. -# -# cdist is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# cdist is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with cdist. If not, see . -# - -if [ -f "$__object/parameter/name" ] -then - name="$( cat "$__object/parameter/name" )" -else - name="$__object_id" -fi - -if [ -f "$__object/parameter/password" ] -then - password="$( cat "$__object/parameter/password" )" -else - password='' -fi - -host="$( cat "$__object/parameter/host" )" - -check_user="$( mysql -B -N -e "select user from mysql.user where user = '$name' and host = '$host'" )" - -if [ -n "$check_user" ] -then - if [ -n "$password" ] - then - check_password="$( mysql -B -N -e "select user from mysql.user where user = '$name' and host = '$host' and password = password( '$password' )" )" - fi - - if [ -n "$password" ] && [ -z "$check_password" ] - then - echo 'change-password' - else - echo 'present' - fi -else - echo 'absent' -fi diff --git a/cdist/conf/type/__mysql_user/gencode-remote b/cdist/conf/type/__mysql_user/gencode-remote index 5f13bc87..4fd1a049 100755 --- a/cdist/conf/type/__mysql_user/gencode-remote +++ b/cdist/conf/type/__mysql_user/gencode-remote @@ -18,15 +18,9 @@ # along with cdist. If not, see . # -state_is="$( cat "$__object/explorer/state" )" state_should="$( cat "$__object/parameter/state" )" -if [ "$state_is" = "$state_should" ] -then - exit 0 -fi - if [ -f "$__object/parameter/name" ] then name="$( cat "$__object/parameter/name" )" @@ -49,6 +43,32 @@ else fi fi +# Current state +# Not an exploerer, to avoid issues with mysql not installed YET +check_user="$( $__remote_exec $__target_host "mysql -B -N -e \"select user from mysql.user where user = '$name' and host = '$host'\"" )" + +if [ -n "$check_user" ] +then + if [ -n "$password" ] + then + check_password="$( $__remote_exec $__target_host "mysql -B -N -e \"select user from mysql.user where user = '$name' and host = '$host' and password = password( '$password' )\"" )" + fi + + if [ -n "$password" ] && [ -z "$check_password" ] + then + state_is='change-password' + else + state_is='present' + fi +else + state_is='absent' +fi + +if [ "$state_is" = "$state_should" ] +then + exit 0 +fi + if [ "$state_is" = 'absent' ] && [ "$state_should" = 'present' ] then echo "mysql -e 'create user \`$name\`@\`$host\` identified by \"$password\"'"