let's not name these differently than the emig.d/ counterparts
[migration-tools.git] / kmig.d / bin / mig-env
diff --git a/kmig.d/bin/mig-env b/kmig.d/bin/mig-env
new file mode 100755 (executable)
index 0000000..a8b5329
--- /dev/null
@@ -0,0 +1,298 @@
+#!/usr/bin/perl -w
+###############################################################################
+=pod
+
+=head1 NAME
+
+mig-env - This tool is for tracking and setting environment variables used by
+B<mig> and its sub-tools.
+
+=head1 SYNOPSIS
+
+B<mig-env> <create|use> <migration_schema>
+
+B<mig-env> <show> [migration_schema]
+
+B<mig-env> <clone> [orig_migration_schema] [new_migration_schema]
+
+B<mig-env> <list>
+
+B<mig-env> <help>
+
+=head1 DESCRIPTION
+
+For most invocations, B<mig-env> will either create or use a migration-specific
+file (~/.mig/<migration_schema>.env) for setting the following environment
+variables:
+
+=over 15
+
+=item MIGSCHEMA
+
+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 MYSQL_HOST
+
+The IP address or hostname for the MySQL/MariaDB database used for a migration.
+
+=item MYSQL_TCP_PORT
+
+The TCP port for the database.
+
+=item MYSQL_USER
+
+The user to use for the database.
+
+=item MYSQL_PW
+
+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
+
+This script may also setup a symlink from a specified Git repository to a
+scripts/ directory within the migration work directory.  The default for this is
+~/git/migration-work/MIGSCHEMA --> MIGWORKDIR/scripts
+
+It may also create the migration work directory if necessary.
+
+=head1 COMMANDS
+
+=over 15
+
+=item B<create> <schema>
+
+This invocation will prompt for various values and create a .env file for the
+specified migration schema, and a symlink between the specified Git repository
+and migration work directory (which will also be created if needed).
+
+=item B<use> <schema>
+
+This command will spawn a bash shell that executes the corresponding
+~/.mig/<schema>.env script for setting up environment variables encoded during
+B<create>.
+
+=item B<show> [schema]
+
+This command will show the contents of the corresponding ~/.mig/<schema>.env
+script, or, if no schema is specified, then it will list pertinent variables in
+the current environment if they exist.
+
+=item B<clone> [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<list>
+
+This command will list migration schemas found in ~/.mig
+
+=item B<help>
+
+Display the documentation you're reading now.
+
+=back
+
+=cut
+
+###############################################################################
+
+use strict;
+use 5.012;
+use Switch;
+use Env qw(
+    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);
+use FindBin;
+my $mig_bin = "$FindBin::Bin/";
+use lib "$FindBin::Bin/";
+
+pod2usage(-verbose => 2) if ! $ARGV[0];
+
+my $migration_schema = $ARGV[1] || '';
+my $filename = "$HOME/.mig/$migration_schema.env";
+switch($ARGV[0]) {
+    case "--help" {
+        pod2usage(-verbose => 2);
+    }
+    case "help" {
+        pod2usage(-verbose => 2);
+    }
+    case "create" {
+        pod2usage(-verbose => 1) if ! $ARGV[1];
+        mig_env_create();
+    }
+    case "clone" {
+        pod2usage(-verbose => 1) if ! $ARGV[2];
+        $migration_schema = $ARGV[2] || '';
+        $filename = "$HOME/.mig/$migration_schema.env";
+        mig_env_clone();
+    }
+    case "use" {
+        pod2usage(-verbose => 1) if ! $ARGV[1];
+        if (-e $filename) {
+            exec '/bin/bash', '--init-file', $filename;
+        } else {
+            die "\n$filename does not exist\n";
+        }
+    }
+    case "show" {
+        if (-e $filename) {
+            exec '/bin/cat', $filename;
+        } else {
+            print `env | sort | egrep 'MIG|PG'`;
+        }
+    }
+    case "list" {
+        opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
+        while (readdir $dh) {
+            if (/^(.*)\.env$/) {
+                print "$1\n";
+            }
+        }
+        closedir $dh;
+    }
+    else {
+        pod2usage(1);
+    }
+}
+
+sub mig_env_create {
+    if (-e $filename) {
+        print "Re-Creating $filename\n";
+        print `cat $filename`;
+    } else {
+        print "Creating $filename\n";
+    }
+    print "\n";
+
+    # directories
+
+    $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
+    my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
+    print "Main work directory (default $migworkdir_default): ";
+    my $MIGWORKDIR = <STDIN>;
+    chomp $MIGWORKDIR;
+    if (! $MIGWORKDIR) {
+        $MIGWORKDIR = $migworkdir_default;
+    }
+    $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
+    my $miggitdir_default = "${MIGBASEGITDIR}/$migration_schema/";
+    print "git repo for migration-specific scripts (default $miggitdir_default): ";
+    my $MIGGITDIR = <STDIN>;
+    chomp $MIGGITDIR;
+    if (! $MIGGITDIR) {
+        $MIGGITDIR = $miggitdir_default;
+    }
+
+    # 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;
+    }
+
+    $MYSQL_HOST = $mysqlhost || 'localhost' unless $MYSQL_HOST;
+    my $mysql_host_default = $MYSQL_HOST;
+    print "MYSQL_HOST (default $mysql_host_default): ";
+    $MYSQL_HOST = <STDIN>;
+    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 = <STDIN>;
+    chomp $MYSQL_TCP_PORT;
+    if (! $MYSQL_TCP_PORT) {
+        $MYSQL_TCP_PORT = $mysql_port_default;
+    }
+    $MYSQL_DATABASE = $mysqldb || 'koha_demo' unless $MYSQL_DATABASE;
+    my $mysql_database_default = $MYSQL_DATABASE;
+    print "MYSQL_DATABASE (default $mysql_database_default): ";
+    $MYSQL_DATABASE = <STDIN>;
+    chomp $MYSQL_DATABASE;
+    if (! $MYSQL_DATABASE) {
+        $MYSQL_DATABASE = $mysql_database_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 = <STDIN>;
+    chomp $MYSQL_USER;
+    if (! $MYSQL_USER) {
+        $MYSQL_USER = $mysql_user_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 = <STDIN>;
+    chomp $MYSQL_PW;
+    if (! $MYSQL_PW) {
+        $MYSQL_PW = $mysql_pw_default;
+    }
+
+    # create files and directories if needed
+
+    mkdir "$HOME/.mig";
+    make_path($MIGGITDIR, { verbose => 1 });
+    `touch $MIGGITDIR/README`;
+    make_path($MIGWORKDIR, { verbose => 1 });
+    symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
+    open FILE, ">$filename";
+    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";
+    print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
+    print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
+    print FILE "export MIGGITDIR=$MIGGITDIR\n";
+    print FILE "alias wcd='cd `mig wdir`'\n";
+    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 '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/.kmig/$orig_migration_schema.env";
+    `cp $orig_filename $filename`;
+    `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`;
+    `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`;
+}
+