From b416be73144cfb534bd652c69f913cea3314a47d Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Thu, 9 Apr 2020 17:51:54 -0400 Subject: [PATCH] first cut of kmig-env, and KMig.pm comment tweak Signed-off-by: Jason Etheridge --- kmig.d/bin/KMig.pm | 4 +- kmig.d/bin/kmig-env | 160 ++++++++++++++++++++++++++++++--------------------- 2 files changed, 98 insertions(+), 66 deletions(-) diff --git a/kmig.d/bin/KMig.pm b/kmig.d/bin/KMig.pm index 64d72af..45dd172 100644 --- a/kmig.d/bin/KMig.pm +++ b/kmig.d/bin/KMig.pm @@ -51,7 +51,9 @@ sub die_if_no_env_migschema { } sub check_for_db_migschema { - return 1; # the schema is the same as the database name, which is the same as the koha instance name, in most cases + # the schema is the same as the database name, which is the same + # as the koha instance name prefixed with 'koha_', in most cases + return 1; } sub check_db_migschema_for_migration_tables { diff --git a/kmig.d/bin/kmig-env b/kmig.d/bin/kmig-env index dceec4f..fce49bc 100755 --- a/kmig.d/bin/kmig-env +++ b/kmig.d/bin/kmig-env @@ -4,54 +4,59 @@ =head1 NAME -mig-env - This tool is for tracking and setting environment variables used by -B and its sub-tools. +kmig-env - This tool is for tracking and setting environment variables used by +B and its sub-tools. =head1 SYNOPSIS -B +B -B [migration_schema] +B [migration_schema] -B [orig_migration_schema] [new_migration_schema] +B [orig_migration_schema] [new_migration_schema] -B +B -B +B =head1 DESCRIPTION -For most invocations, B will either create or use a migration-specific -file (~/.mig/.env) for setting the following environment +For most invocations, B will either create or use a migration-specific +file (~/.kmig/.env) for setting the following environment variables: =over 15 =item MIGSCHEMA -The name of the migration schema. Convention has this being a single lowercased -word or acronym identifying the library, prefixed with 'm_'. +The name of the migration schema. In practice, this will match the name of the +Koha instance. =item MIGWORKDIR The base working directory for containing migration data, scripts, and other files. -=item PGHOST +=item MYSQL_HOST -The IP address or hostname for the PostgreSQL database used for a migration. +The IP address or hostname for the MySQL/MariaDB database used for a migration. -=item PGPORT +=item MYSQL_TCP_PORT -The TCP port for the PostgreSQL database. +The TCP port for the database. -=item PGUSER +=item MYSQL_USER -The PostgreSQL user to use for the database. +The user to use for the database. -=item PGDATABASE +=item MYSQL_PW -The name of the actual database containing the migration schema. +The password to use for the database. + +=item MYSQL_DATABASE + +The name of the actual database/schema. In practice, this will match the +migration schema or Koha instance name, prefixed with 'koha_'. =back @@ -74,24 +79,26 @@ and migration work directory (which will also be created if needed). =item B This command will spawn a bash shell that executes the corresponding -~/.mig/.env script for setting up environment variables encoded during +~/.kmig/.env script for setting up environment variables encoded during B. =item B [schema] -This command will show the contents of the corresponding ~/.mig/.env +This command will show the contents of the corresponding ~/.kmig/.env script, or, if no schema is specified, then it will list pertinent variables in the current environment if they exist. =item B [orig schema] [new schema] +FIXME: need to re-think this in a MySQL/MariaDB/Koha context + This command will create a "shallow" clone of the orig schema, in that it will share database credentials as well as git and data directories, but will have a separate schema name. =item B -This command will list migration schemas found in ~/.mig +This command will list migration schemas found in ~/.kmig =item B @@ -107,8 +114,8 @@ use strict; use 5.012; use Switch; use Env qw( - HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA - MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR + HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW + MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR ); use Pod::Usage; use File::Path qw(make_path); @@ -119,7 +126,7 @@ use lib "$FindBin::Bin/"; pod2usage(-verbose => 2) if ! $ARGV[0]; my $migration_schema = $ARGV[1] || ''; -my $filename = "$HOME/.mig/$migration_schema.env"; +my $filename = "$HOME/.kmig/$migration_schema.env"; switch($ARGV[0]) { case "--help" { pod2usage(-verbose => 2); @@ -134,7 +141,7 @@ switch($ARGV[0]) { case "clone" { pod2usage(-verbose => 1) if ! $ARGV[2]; $migration_schema = $ARGV[2] || ''; - $filename = "$HOME/.mig/$migration_schema.env"; + $filename = "$HOME/.kmig/$migration_schema.env"; mig_env_clone(); } case "use" { @@ -153,7 +160,7 @@ switch($ARGV[0]) { } } case "list" { - opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!"; + opendir(my $dh, "$HOME/.kmig") || die "can't open $HOME/.kmig: $!"; while (readdir $dh) { if (/^(.*)\.env$/) { print "$1\n"; @@ -194,54 +201,77 @@ sub mig_env_create { $MIGGITDIR = $miggitdir_default; } - # PostgreSQL + # MySQL/MariaDB + + my $mysqlhost; my $mysqldb; my $mysqlport; + my $mysqluser; my $mysqlpass; + if (-e '/usr/sbin/koha-list' && `/usr/sbin/koha-list` =~ $migration_schema + && `sudo -nl xmlstarlet` =~ 'xmlstarlet') { + my $kohaconfig="/etc/koha/sites/$migration_schema/koha-conf.xml"; + $mysqlhost=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig`; + $mysqldb=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig`; + $mysqlport=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/port' $kohaconfig`; + $mysqluser=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig`; + $mysqlpass=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig`; + chomp $mysqlhost; chomp $mysqldb; chomp $mysqlport; + chomp $mysqluser; chomp $mysqlpass; + } - $PGHOST = 'localhost' unless $PGHOST; - my $pghost_default = $PGHOST; - print "PGHOST (default $pghost_default): "; - $PGHOST = ; - chomp $PGHOST; - if (! $PGHOST) { - $PGHOST = $pghost_default; + $MYSQL_HOST = $mysqlhost || 'localhost' unless $MYSQL_HOST; + my $mysql_host_default = $MYSQL_HOST; + print "MYSQL_HOST (default $mysql_host_default): "; + $MYSQL_HOST = ; + chomp $MYSQL_HOST; + if (! $MYSQL_HOST) { + $MYSQL_HOST = $mysql_host_default; + } + $MYSQL_TCP_PORT = $mysqlport || 3306 unless $MYSQL_TCP_PORT; + my $mysql_port_default = $MYSQL_TCP_PORT; + print "MYSQL_TCP_PORT (default $mysql_port_default): "; + $MYSQL_TCP_PORT = ; + chomp $MYSQL_TCP_PORT; + if (! $MYSQL_TCP_PORT) { + $MYSQL_TCP_PORT = $mysql_port_default; } - $PGPORT = 5432 unless $PGPORT; - my $pgport_default = $PGPORT; - print "PGPORT (default $pgport_default): "; - $PGPORT = ; - chomp $PGPORT; - if (! $PGPORT) { - $PGPORT = $pgport_default; + $MYSQL_DATABASE = $mysqldb || 'koha_demo' unless $MYSQL_DATABASE; + my $mysql_database_default = $MYSQL_DATABASE; + print "MYSQL_DATABASE (default $mysql_database_default): "; + $MYSQL_DATABASE = ; + chomp $MYSQL_DATABASE; + if (! $MYSQL_DATABASE) { + $MYSQL_DATABASE = $mysql_database_default; } - $PGDATABASE = 'evergreen' unless $PGDATABASE; - my $pgdatabase_default = $PGDATABASE; - print "PGDATABASE (default $pgdatabase_default): "; - $PGDATABASE = ; - chomp $PGDATABASE; - if (! $PGDATABASE) { - $PGDATABASE = $pgdatabase_default; + $MYSQL_USER = $mysqluser || $MYSQL_DATABASE unless $MYSQL_USER; + my $mysql_user_default = $MYSQL_USER; + print "MYSQL_USER (default $mysql_user_default): "; + my $MYSQL_USER = ; + chomp $MYSQL_USER; + if (! $MYSQL_USER) { + $MYSQL_USER = $mysql_user_default; } - $PGUSER = $PGDATABASE unless $PGUSER; - my $pguser_default = $PGUSER; - print "PGUSER (default $pguser_default): "; - my $PGUSER = ; - chomp $PGUSER; - if (! $PGUSER) { - $PGUSER = $pguser_default; + $MYSQL_PW = $mysqlpass || $MYSQL_USER unless $MYSQL_PW; + my $mysql_pw_default = $MYSQL_PW; + print "MYSQL_PW (default $mysql_pw_default): "; + my $MYSQL_PW = ; + chomp $MYSQL_PW; + if (! $MYSQL_PW) { + $MYSQL_PW = $mysql_pw_default; } # create files and directories if needed - mkdir "$HOME/.mig"; + mkdir "$HOME/.kmig"; make_path($MIGGITDIR, { verbose => 1 }); `touch $MIGGITDIR/README`; make_path($MIGWORKDIR, { verbose => 1 }); symlink $MIGGITDIR, "$MIGWORKDIR/scripts"; open FILE, ">$filename"; - print FILE "export PGHOST=$PGHOST\n"; - print FILE "export PGPORT=$PGPORT\n"; - print FILE "export PGDATABASE=$PGDATABASE\n"; - print FILE "export PGUSER=$PGUSER\n"; - print FILE "export PGOPTIONS='-c search_path=$migration_schema,public,evergreen'\n"; + print FILE "export MYSQL_HOST=$MYSQL_HOST\n"; + print FILE "export MYSQL_TCP_PORT=$MYSQL_TCP_PORT\n"; + print FILE "export MYSQL_DATABASE=$MYSQL_DATABASE\n"; + print FILE "export MYSQL_USER=$MYSQL_USER\n"; + #TODO - brittle; need to escape the password string + print FILE "export MYSQL_PW=$MYSQL_PW\n"; print FILE "export MIGENVPROMPT=$migration_schema\n"; print FILE "export MIGSCHEMA=$migration_schema\n"; print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n"; @@ -252,16 +282,16 @@ sub mig_env_create { print FILE "alias gcd='cd `mig gdir`'\n"; print FILE "alias scd='cd `mig sdir`'\n"; print FILE "source ~/.profile\n"; - print FILE "env | sort | egrep 'PG|MIG'\n"; + print FILE "env | sort | egrep 'MYSQL|MIG'\n"; print FILE 'echo shell PID = $$' . "\n"; close FILE; + chmod 0600, $filename; # TODO: race condition worth worrying about? couldn't get sysopen to work } sub mig_env_clone { my $orig_migration_schema = $ARGV[1] || ''; - my $orig_filename = "$HOME/.mig/$orig_migration_schema.env"; + my $orig_filename = "$HOME/.kmig/$orig_migration_schema.env"; `cp $orig_filename $filename`; - `sed -i 's/export PGOPTIONS=.*/export PGOPTIONS='"'"'-c search_path=$migration_schema,public,evergreen'"'"'/' $filename`; `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`; `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`; } -- 1.7.2.5