Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Flags example: __mysql #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions type/__mysql/man.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cdist-type__postfix(7)
======================

NAME
----
cdist-type__mysql - Install MySQL or MariaDB


DESCRIPTION
-----------
This space intentionally left blank.


REQUIRED PARAMETERS
-------------------
None.


OPTIONAL PARAMETERS
-------------------
None.


EXAMPLES
--------

.. code-block:: sh

__mysql


AUTHORS
-------
Daniel Fancsali <[email protected]>


COPYING
-------
Copyright \(C) 2022 Daniel Fancsali. 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.
31 changes: 31 additions & 0 deletions type/__mysql/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh -e
#
# 2022 Daniel Fancsali ([email protected])
#
# 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 <http://www.gnu.org/licenses/>.
#


__MYSQL=${__MYSQL-mariadb} # Set default value

if [ "$__MYSQL" = "mysql" ]; then
__package mysql-server --state present
elif [ "$__MYSQL" = "mariadb" ]; then
__package mariadb-server --state present
else
echo "Neither the environment variable __MYSQL nor its default is set."
exit 1
fi
1 change: 1 addition & 0 deletions type/__mysql/parameter/boolean
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mariadb
Empty file added type/__mysql/singleton
Empty file.
20 changes: 14 additions & 6 deletions type/__mysql_database/gencode-remote
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,30 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

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" )"
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\`'"
Expand Down
5 changes: 5 additions & 0 deletions type/__mysql_database/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#


# Whatever the details, we'll need MySQL/MariaDB
__mysql


if [ -f "$__object/parameter/user" ]
then
user="$( cat "$__object/parameter/user" )"
Expand Down
23 changes: 23 additions & 0 deletions type/__mysql_privileges/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh -e
#
# 2021 Daniel Fancsali ([email protected])
#
# 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 <http://www.gnu.org/licenses/>.
#


# We'll need MySQL/MariaDB
__mysql
32 changes: 26 additions & 6 deletions type/__mysql_user/gencode-remote
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,9 @@
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

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" )"
Expand All @@ -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\"'"
Expand Down
23 changes: 23 additions & 0 deletions type/__mysql_user/manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/sh -e
#
# 2021 Daniel Fancsali ([email protected])
#
# 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 <http://www.gnu.org/licenses/>.
#


# We'll need MySQl/MariaDb to be installed
__mysql