From cfaee39e706e2998f023d4693aa49ed29ae7fd27 Mon Sep 17 00:00:00 2001 From: Benedikt Koeppel Date: Sat, 18 Feb 2012 21:02:26 +0100 Subject: [PATCH 1/3] new type 'mysql_database' installs a database on an existing mysql_server remotely --- conf/type/__mysql_database/gencode-remote | 48 ++++++++++++++++++ conf/type/__mysql_database/man.text | 49 +++++++++++++++++++ conf/type/__mysql_database/parameter/optional | 3 ++ 3 files changed, 100 insertions(+) create mode 100755 conf/type/__mysql_database/gencode-remote create mode 100644 conf/type/__mysql_database/man.text create mode 100644 conf/type/__mysql_database/parameter/optional diff --git a/conf/type/__mysql_database/gencode-remote b/conf/type/__mysql_database/gencode-remote new file mode 100755 index 00000000..fdfb1501 --- /dev/null +++ b/conf/type/__mysql_database/gencode-remote @@ -0,0 +1,48 @@ +#!/bin/sh +# +# 2012 Benedikt Koeppel (code@benediktkoeppel.ch) +# +# 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 --database was specified +if [ -f "$__object/parameter/database" ]; then + database="$(cat "$__object/parameter/database")" +else # otherwise use the object id as database name + database="$__object_id" +fi + +mysql -u root -p <<-EOF + CREATE DATABASE IF NOT EXISTS $database +EOF + +# if --user was specified +if [ -f "$__object/parameter/user" ]; then + user="$(cat "$__object/parameter/user")" + + # if --password was specified + if [ -f "$__object/parameter/password" ]; then + password="$(cat "$__object/parameter/password")" + mysql -u root -p <<-EOF + GRANT ALL PRIVILEGES ON $database.* to '$user'@'localhost' IDENTIFIED BY '$password'; + EOF + else + mysql -u root -p <<-EOF + GRANT ALL PRIVILEGES ON $database.* to '$user'@'localhost'; + EOF + fi +fi diff --git a/conf/type/__mysql_database/man.text b/conf/type/__mysql_database/man.text new file mode 100644 index 00000000..3c66eeab --- /dev/null +++ b/conf/type/__mysql_database/man.text @@ -0,0 +1,49 @@ +cdist-type__mysql_database(7) +============================= +Benedikt Koeppel + + +NAME +---- +cdist-type__mysql_database - Manage a MySQL database + + +DESCRIPTION +----------- +This cdist type allows you to install a MySQL database. + + +REQUIRED PARAMETERS +------------------- +None. + +OPTIONAL PARAMETERS +------------------- +database:: + The name of the database to install + defaults to the object id + +user:: + A user that should have access to the database + +password:: + The password for the user who manages the database + + +EXAMPLES +-------- + +-------------------------------------------------------------------------------- +__mysql_database "cdist" --database "cdist" --user "myuser" --password "mypwd" +-------------------------------------------------------------------------------- + + +SEE ALSO +-------- +- cdist-type(7) + + +COPYING +------- +Copyright \(C) 2012 Benedikt Koeppel. Free use of this software is +granted under the terms of the GNU General Public License version 3 (GPLv3). diff --git a/conf/type/__mysql_database/parameter/optional b/conf/type/__mysql_database/parameter/optional new file mode 100644 index 00000000..ac60a873 --- /dev/null +++ b/conf/type/__mysql_database/parameter/optional @@ -0,0 +1,3 @@ +database +user +password From c67c0cf12dd76a87de17e8812f8948f2b7ac094d Mon Sep 17 00:00:00 2001 From: Benedikt Koeppel Date: Sat, 18 Feb 2012 23:10:56 +0100 Subject: [PATCH 2/3] fixed mysql_database type can now install a database, and create a user who manages that database --- conf/type/__mysql_database/gencode-remote | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/conf/type/__mysql_database/gencode-remote b/conf/type/__mysql_database/gencode-remote index fdfb1501..9001ac99 100755 --- a/conf/type/__mysql_database/gencode-remote +++ b/conf/type/__mysql_database/gencode-remote @@ -26,9 +26,11 @@ else # otherwise use the object id as database name database="$__object_id" fi -mysql -u root -p <<-EOF +cat <<-EOFF +mysql -u root <<-EOF CREATE DATABASE IF NOT EXISTS $database EOF +EOFF # if --user was specified if [ -f "$__object/parameter/user" ]; then @@ -37,12 +39,17 @@ if [ -f "$__object/parameter/user" ]; then # if --password was specified if [ -f "$__object/parameter/password" ]; then password="$(cat "$__object/parameter/password")" - mysql -u root -p <<-EOF + cat <<-EOFF + mysql -u root <<-EOF GRANT ALL PRIVILEGES ON $database.* to '$user'@'localhost' IDENTIFIED BY '$password'; - EOF +EOF +EOFF else - mysql -u root -p <<-EOF + password="" + cat <<-EOFF + mysql -u root <<-EOF GRANT ALL PRIVILEGES ON $database.* to '$user'@'localhost'; - EOF +EOF +EOFF fi fi From cd8e2869883efd3eedd1b78a78227259565c6731 Mon Sep 17 00:00:00 2001 From: Benedikt Koeppel Date: Tue, 21 Feb 2012 07:03:03 +0100 Subject: [PATCH 3/3] renamed --database to --name --- conf/type/__mysql_database/gencode-remote | 4 ++-- conf/type/__mysql_database/man.text | 4 ++-- conf/type/__mysql_database/parameter/optional | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/conf/type/__mysql_database/gencode-remote b/conf/type/__mysql_database/gencode-remote index 9001ac99..c0e862f3 100755 --- a/conf/type/__mysql_database/gencode-remote +++ b/conf/type/__mysql_database/gencode-remote @@ -20,8 +20,8 @@ # # if --database was specified -if [ -f "$__object/parameter/database" ]; then - database="$(cat "$__object/parameter/database")" +if [ -f "$__object/parameter/name" ]; then + database="$(cat "$__object/parameter/name")" else # otherwise use the object id as database name database="$__object_id" fi diff --git a/conf/type/__mysql_database/man.text b/conf/type/__mysql_database/man.text index 3c66eeab..f184a30e 100644 --- a/conf/type/__mysql_database/man.text +++ b/conf/type/__mysql_database/man.text @@ -19,7 +19,7 @@ None. OPTIONAL PARAMETERS ------------------- -database:: +name:: The name of the database to install defaults to the object id @@ -34,7 +34,7 @@ EXAMPLES -------- -------------------------------------------------------------------------------- -__mysql_database "cdist" --database "cdist" --user "myuser" --password "mypwd" +__mysql_database "cdist" --name "cdist" --user "myuser" --password "mypwd" -------------------------------------------------------------------------------- diff --git a/conf/type/__mysql_database/parameter/optional b/conf/type/__mysql_database/parameter/optional index ac60a873..756afee7 100644 --- a/conf/type/__mysql_database/parameter/optional +++ b/conf/type/__mysql_database/parameter/optional @@ -1,3 +1,3 @@ -database +name user password