[Patches] [PATCH] Bug 6913: More options for koha-create and koha-remove

koha-patchbot at kohaaloha.com koha-patchbot at kohaaloha.com
Wed Dec 21 07:35:03 NZDT 2011


From: Jared Camins-Esakov <jcamins at cpbibliography.com>
Date: Sat, 8 Oct 2011 15:34:24 -0400
Subject: [PATCH] Bug 6913: More options for koha-create and koha-remove
Content-Type: text/plain; charset="UTF-8"

This commit makes koha-create more flexible by adding a number of command-line
and configuration options.

The biggest change is to add support for pre-determined MySQL credentials using
a file in /etc/koha/passwd (for security reasons, if you use this feature, make
sure the file is not world-readable). The configuration file has the following
format:
instance:user:password:database
The database is optional, but the other two are both required if you want to
specify MySQL credentials. If no credentials are specified for a given instance,
the standard koha_instance username and an auto-generated password are used.

The following additional command line options are now available:
* --use-db - use a pre-existing database, see also --database
* --database - specify the name of the mysql database (still defaults to
  koha_instance)
* --passwdfile - specify a custom (Koha) passwd file for MySQL credentials

The following additional variables are now available (for use in
koha-sites.conf or a custom config file):
* OPACPORT (previously added but not documented) - port on which the OPAC
  should be served
* OPACPREFIX - prefix for the OPAC's virtual host
* OPACSUFFIX - suffix for the OPAC's virtual host

This commit also adds the --keep-mysql option to koha-remove, which, if
specified, will prevent koha-remove from deleting the database or MySQL user
when removing an instance.
---
 debian/docs/koha-create.xml |   57 +++++++++++++++++++++++++++++++++++--
 debian/docs/koha-remove.xml |   19 ++++++++++--
 debian/scripts/koha-create  |   66 +++++++++++++++++++++++++++++++++++-------
 debian/scripts/koha-remove  |   16 ++++++++++-
 4 files changed, 140 insertions(+), 18 deletions(-)

diff --git a/debian/docs/koha-create.xml b/debian/docs/koha-create.xml
index a3e1520..ec1e426 100644
--- a/debian/docs/koha-create.xml
+++ b/debian/docs/koha-create.xml
@@ -24,11 +24,13 @@
   <refsynopsisdiv>
     <cmdsynopsis>
       <command>koha-create</command>
-      <arg choice="req"><option>--create-db</option>|<option>--request-db</option>|<option>--populate-db</option></arg>
+      <arg choice="req"><option>--create-db</option>|<option>--request-db</option>|<option>--populate-db</option>|<option>--use-db</option></arg>
       <arg><option>--marcflavor</option> marc21|normarc|unimarc</arg>
       <arg><option>--zebralang</option> en|nb|fr</arg>
       <arg><option>--defaultsql</option> /path/to/some.sql</arg>
       <arg><option>--configfile</option> /path/to/config</arg>
+      <arg><option>--passwdfile</option> /path/to/passwd</arg>
+      <arg><option>--database</option> database</arg>
       <arg><option>--adminuser</option> n</arg>
       <arg choice="req" rep="norepeat"><replaceable>instancename</replaceable></arg>
     </cmdsynopsis>
@@ -40,7 +42,7 @@
     <varlistentry>
       <term><option>--create-db</option></term>
       <listitem>
-        <para>Create a new datbase on localhost. If the database you want to use does not reside on localhost, you  can  use <option>--request-db</option> and <option>--populate-db</option>, see below.</para>
+        <para>Create a new database on localhost. If the database you want to use does not reside on localhost, you can use <option>--request-db</option> and <option>--populate-db</option>, see below. If the database already exists, you can use <option>--use-db</option>, see below.</para>
       </listitem>
     </varlistentry>
     
@@ -54,7 +56,14 @@
     <varlistentry>
       <term><option>--populate-db</option></term>
       <listitem>
-        <para>When you have run koha-create with the <option>--request-db</option> option, and a database has been set up, you can finnish your installation by running <command>koha-create</command> with this option.</para>
+        <para>When you have run koha-create with the <option>--request-db</option> option, and a database has been set up, you can finish your installation by running <command>koha-create</command> with this option.</para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
+      <term><option>--use-db</option></term>
+      <listitem>
+        <para>When the database you want to use has already been populated, you can run <command>koha-create</command> with this option to take care of all the other set-up configuration.</para>
       </listitem>
     </varlistentry>
 
@@ -66,6 +75,13 @@
     </varlistentry>
 
     <varlistentry>
+      <term><option>--database</option></term>
+      <listitem>
+        <para>MySQL database to use.</para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>--defaultsql</option></term>
       <listitem>
         <para>Path to an SQL-file other than the one pointed to in the default or alternative config file. Corresponds to the DEFAULTSQL config file variable.</para>
@@ -80,6 +96,13 @@
     </varlistentry>
 
     <varlistentry>
+      <term><option>--passwdfile</option></term>
+      <listitem>
+        <para>Path to an alternative passwd file. This file is in the same format as the default (Koha) passwd file, see below for details.</para>
+      </listitem>
+    </varlistentry>
+
+    <varlistentry>
       <term><option>--zebralang</option></term>
       <listitem>
         <para>Primary language for Zebra indexing. Corresponds to the ZEBRA_LANGUAGE config file variable.</para>
@@ -107,6 +130,27 @@
       </varlistentry>
     
       <varlistentry>
+        <term><option>OPACPORT</option></term>
+        <listitem>
+          <para>The port for the OPAC.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>OPACPREFIX</option></term>
+        <listitem>
+          <para>This is inserted before the instance name  when  forming  Apache ServerName.  For subdomains, make sure it ends with a period.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
+        <term><option>OPACSUFFIX</option></term>
+        <listitem>
+          <para>This  is inserted after the instance name, but before the domain name, when forming Apache ServerName.</para>
+        </listitem>
+      </varlistentry>
+
+      <varlistentry>
         <term><option>INTRAPORT</option></term>
         <listitem>
           <para>The port for the intranet.  If you set this to empty or 80, then you must also define INTRAPREFIX or INTRASUFFIX.</para>
@@ -169,6 +213,13 @@
         <para>Configuration variables are read from this file, if it exists.</para>
       </listitem>
     </varlistentry>
+
+    <varlistentry>
+      <term><option><filename>/etc/koha/passwd</filename></option></term>
+      <listitem>
+        <para>Pre-selected database credentials are read from this file, if it exists. The format for this file is instancename:username:passwd:database. Database is optional, but the other three are required if you want to use pre-determined database user credentials.</para>
+      </listitem>
+    </varlistentry>
   </variablelist>
   </refsect1>
 
diff --git a/debian/docs/koha-remove.xml b/debian/docs/koha-remove.xml
index ee2b266..4e272f8 100644
--- a/debian/docs/koha-remove.xml
+++ b/debian/docs/koha-remove.xml
@@ -23,14 +23,27 @@
 
   <refsynopsisdiv>
     <cmdsynopsis>
-      <command>koha-remove</command> <arg choice="req" rep="repeat"><replaceable>instancename</replaceable></arg>
+      <command>koha-remove</command>
+      <arg><option>--keep-mysql</option></arg>
+      <arg choice="req" rep="repeat"><replaceable>instancename</replaceable></arg>
     </cmdsynopsis>
   </refsynopsisdiv>
 
+  <refsect1><title>Options</title>
+  <variablelist>
+    <varlistentry>
+      <term><option>--keep-mysql</option></term>
+      <listitem>
+        <para>Leave the MySQL database and user when removing the Koha instance.</para>
+      </listitem>
+    </varlistentry>
+  </variablelist>
+  </refsect1>
+
   <refsect1><title>Description</title>
   <para>Remove a Koha instance.</para>
   </refsect1>
-  
+
   <refsect1><title>See also</title>
   <simplelist type="inline">
     <member><command>koha-disable(8)</command></member>
@@ -39,7 +52,7 @@
     <member><command>koha-restore(8)</command></member>
   </simplelist>
   </refsect1>
-  
+
 </refentry>
 
 </article>
diff --git a/debian/scripts/koha-create b/debian/scripts/koha-create
index b52cf90..bb2b642 100755
--- a/debian/scripts/koha-create
+++ b/debian/scripts/koha-create
@@ -19,11 +19,12 @@
 
 set -e
 
-usage="Usage: $0 [--create-db|--request-db|--populate-db] \
+usage="Usage: $0 [--create-db|--request-db|--populate-db|--use-db] \
     [--marcflavor marc21|normarc|unimarc] \
     [--zebralang en|nb|fr] \
     [--defaultsql /path/to/some.sql] \
-    [--configfile /path/to/config] [--adminuser n] instancename"
+    [--configfile /path/to/config] [--passwdfile /path/to/passwd] \
+    [--database database] [--adminuser n] instancename"
 
 die() {
     echo "$@" 1>&2
@@ -76,6 +77,8 @@ getinstancemysqldatabase() {
 # Set defaults and read config file, if it exists.
 DOMAIN=""
 OPACPORT="80"
+OPACPREFIX=""
+OPACSUFFIX=""
 INTRAPORT="8080"
 INTRAPREFIX=""
 INTRASUFFIX=""
@@ -83,14 +86,15 @@ DEFAULTSQL=""
 ZEBRA_MARC_FORMAT="marc21"
 ZEBRA_LANGUAGE="en"
 ADMINUSER="1"
+PASSWDFILE="/etc/koha/passwd"
 if [ -e /etc/koha/koha-sites.conf ]
 then
     . /etc/koha/koha-sites.conf
 fi
 
-[ $# -ge 2 ] && [ $# -le 12 ] || die $usage
+[ $# -ge 2 ] && [ $# -le 16 ] || die $usage
 
-TEMP=`getopt -o crpm:l:d:f:a: -l create-db,request-db,populate-db,marcflavor:,zebralang:,defaultsql:,configfile:,adminuser: \
+TEMP=`getopt -o crpm:l:d:f:a: -l create-db,request-db,populate-db,use-db,marcflavor:,zebralang:,defaultsql:,configfile:,passwdfile:,adminuser: \
      -n "$0" -- "$@"`
 
 # Note the quotes around `$TEMP': they are essential!
@@ -107,10 +111,13 @@ while true ; do
 		-c|--create-db) op=create ; shift ;;
 		-r|--request-db) op=request ; shift ;;
 		-p|--populate-db) op=populate ; shift ;;
+        -u|--use-db) op=use ; shift ;;
 		-m|--marcflavor) CLO_ZEBRA_MARC_FORMAT="$2" ; shift 2 ;;
 		-l|--zebralang) CLO_ZEBRA_LANGUAGE="$2" ; shift 2 ;;
 		-d|--defaultsql) CLO_DEFAULTSQL="$2" ; shift 2 ;;
 		-f|--configfile) configfile="$2" ; shift 2 ;;
+        -s|--passwdfile) CLO_PASSWDFILE="$2" ; shift 2 ;;
+        -b|--database) CLO_DATABASE="$2" ; shift 2 ;;
 		-a|--adminuser) CLO_ADMINUSER="$2" ; shift 2 ;;
 		--) shift ; break ;;
 		*) die "Internal error processing command line arguments" ;;
@@ -145,26 +152,54 @@ if [ "$CLO_ADMINUSER" != "" ]
 then
     ADMINUSER="$CLO_ADMINUSER"
 fi
+if [ "$CLO_PASSWDFILE" != "" ]
+then
+    PASSWDFILE="$CLO_PASSWDFILE"
+fi
 
 name="$1"
 
-opacdomain="$name$DOMAIN"
+opacdomain="$OPACPREFIX$name$OPACSUFFIX$DOMAIN"
 intradomain="$INTRAPREFIX$name$INTRASUFFIX$DOMAIN"
 
 
-mysqldb="koha_$name"
+if [ `cat $PASSWDFILE | grep "^$name:"` ]
+then
+    passwdline=`cat $PASSWDFILE | grep "^$name:"`
+    mysqluser=`echo $passwdline | cut -d ":" -f 2`
+    mysqlpwd=`echo $passwdline | cut -d ":" -f 3`
+    mysqldb=`echo $passwdline | cut -d ":" -f 4`
+fi
+
+# The order of precedence for MySQL database name is:
+# default < passwd file < command line
+if [ "$mysqldb" = "" ]
+then
+    mysqldb="koha_$name"
+fi
+if [ "$CLO_DATABASE" != "" ]
+then
+    mysqldb="$CLO_DATABASE"
+fi
+
+if [ "$mysqluser" = "" ]
+then
+    mysqluser="koha_$name"
+fi
 mysqlhost="$(getmysqlhost)"
-mysqluser="koha_$name"
 
-if [ "$op" = create ] || [ "$op" = request ]
+if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
 then
-    mysqlpwd="$(pwgen -1)"
+    if [ "$mysqlpwd" = "" ]
+    then
+        mysqlpwd="$(pwgen -1)"
+    fi
 else
     mysqlpwd="$(getinstancemysqlpassword $name)"
 fi
 
 
-if [ "$op" = create ] || [ "$op" = request ]
+if [ "$op" = create ] || [ "$op" = request ] || [ "$op" = use ]
 then
     # Create new user and group.
     username="$name-koha"
@@ -197,6 +232,15 @@ FLUSH PRIVILEGES;
 eof
     fi #`
 
+    if [ "$op" = use ]
+    then
+        mysql --defaults-extra-file=/etc/mysql/koha-common.cnf --force <<eof
+CREATE USER \`$mysqluser\`@'%' IDENTIFIED BY '$mysqlpwd';
+GRANT ALL PRIVILEGES ON \`$mysqldb\`.* TO \`$mysqluser\`;
+FLUSH PRIVILEGES;
+eof
+    fi #`
+
     # Generate and install Apache site-available file and log dir.
     generate_config_file apache-site.conf.in \
         "/etc/apache2/sites-available/$name"
@@ -283,7 +327,7 @@ eof
 fi
 
 
-if [ "$op" = create ] || [ "$op" = populate ]
+if [ "$op" = create ] || [ "$op" = populate ] || [ "$op" = use ]
 then
     # Reconfigure Apache.
     a2ensite "$name"
diff --git a/debian/scripts/koha-remove b/debian/scripts/koha-remove
index b9e5c2a..09720ea 100755
--- a/debian/scripts/koha-remove
+++ b/debian/scripts/koha-remove
@@ -19,16 +19,30 @@
 
 set -e
 
+args=$(getopt -l keep-mysql -o k -n $0 -- "$@")
+set -- $args
+while [ ! -z "$1" ]
+do
+    case "$1" in
+         -k|--keep-mysql) keepmysql=1; exit;;
+          *) break;;
+    esac
+    shift
+done
+
 
 for name in "$@"
 do
     echo "Removing Koha instance $name"
 
-    mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
+    if [ "$keepmysql" != "1" ]
+    then
+        mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof
 DROP USER \`koha_$name\`;
 DROP DATABASE \`koha_$name\`;
 FLUSH PRIVILEGES;
 eof
+    fi #`
     
     koha-stop-zebra $name
     [ -f "/etc/apache2/sites-available/$name" ]  && \
-- 
1.7.2.5


More information about the Patches mailing list