let's not name these differently than the emig.d/ counterparts
[migration-tools.git] / kmig.d / bin / mig-env
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-env - This tool is for tracking and setting environment variables used by
8 B<mig> and its sub-tools.
9
10 =head1 SYNOPSIS
11
12 B<mig-env> <create|use> <migration_schema>
13
14 B<mig-env> <show> [migration_schema]
15
16 B<mig-env> <clone> [orig_migration_schema] [new_migration_schema]
17
18 B<mig-env> <list>
19
20 B<mig-env> <help>
21
22 =head1 DESCRIPTION
23
24 For most invocations, B<mig-env> will either create or use a migration-specific
25 file (~/.mig/<migration_schema>.env) for setting the following environment
26 variables:
27
28 =over 15
29
30 =item MIGSCHEMA
31
32 The name of the migration schema.  In practice, this will match the name of the
33 Koha instance.
34
35 =item MIGWORKDIR
36
37 The base working directory for containing migration data, scripts, and other
38 files.
39
40 =item MYSQL_HOST
41
42 The IP address or hostname for the MySQL/MariaDB database used for a migration.
43
44 =item MYSQL_TCP_PORT
45
46 The TCP port for the database.
47
48 =item MYSQL_USER
49
50 The user to use for the database.
51
52 =item MYSQL_PW
53
54 The password to use for the database.
55
56 =item MYSQL_DATABASE
57
58 The name of the actual database/schema.  In practice, this will match the
59 migration schema or Koha instance name, prefixed with 'koha_'.
60
61 =back
62
63 This script may also setup a symlink from a specified Git repository to a
64 scripts/ directory within the migration work directory.  The default for this is
65 ~/git/migration-work/MIGSCHEMA --> MIGWORKDIR/scripts
66
67 It may also create the migration work directory if necessary.
68
69 =head1 COMMANDS
70
71 =over 15
72
73 =item B<create> <schema>
74
75 This invocation will prompt for various values and create a .env file for the
76 specified migration schema, and a symlink between the specified Git repository
77 and migration work directory (which will also be created if needed).
78
79 =item B<use> <schema>
80
81 This command will spawn a bash shell that executes the corresponding
82 ~/.mig/<schema>.env script for setting up environment variables encoded during
83 B<create>.
84
85 =item B<show> [schema]
86
87 This command will show the contents of the corresponding ~/.mig/<schema>.env
88 script, or, if no schema is specified, then it will list pertinent variables in
89 the current environment if they exist.
90
91 =item B<clone> [orig schema] [new schema]
92
93 FIXME: need to re-think this in a MySQL/MariaDB/Koha context
94
95 This command will create a "shallow" clone of the orig schema, in that it will
96 share database credentials as well as git and data directories, but will have a
97 separate schema name.
98
99 =item B<list>
100
101 This command will list migration schemas found in ~/.mig
102
103 =item B<help>
104
105 Display the documentation you're reading now.
106
107 =back
108
109 =cut
110
111 ###############################################################################
112
113 use strict;
114 use 5.012;
115 use Switch;
116 use Env qw(
117     HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
118     MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
119 );
120 use Pod::Usage;
121 use File::Path qw(make_path);
122 use FindBin;
123 my $mig_bin = "$FindBin::Bin/";
124 use lib "$FindBin::Bin/";
125
126 pod2usage(-verbose => 2) if ! $ARGV[0];
127
128 my $migration_schema = $ARGV[1] || '';
129 my $filename = "$HOME/.mig/$migration_schema.env";
130 switch($ARGV[0]) {
131     case "--help" {
132         pod2usage(-verbose => 2);
133     }
134     case "help" {
135         pod2usage(-verbose => 2);
136     }
137     case "create" {
138         pod2usage(-verbose => 1) if ! $ARGV[1];
139         mig_env_create();
140     }
141     case "clone" {
142         pod2usage(-verbose => 1) if ! $ARGV[2];
143         $migration_schema = $ARGV[2] || '';
144         $filename = "$HOME/.mig/$migration_schema.env";
145         mig_env_clone();
146     }
147     case "use" {
148         pod2usage(-verbose => 1) if ! $ARGV[1];
149         if (-e $filename) {
150             exec '/bin/bash', '--init-file', $filename;
151         } else {
152             die "\n$filename does not exist\n";
153         }
154     }
155     case "show" {
156         if (-e $filename) {
157             exec '/bin/cat', $filename;
158         } else {
159             print `env | sort | egrep 'MIG|PG'`;
160         }
161     }
162     case "list" {
163         opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
164         while (readdir $dh) {
165             if (/^(.*)\.env$/) {
166                 print "$1\n";
167             }
168         }
169         closedir $dh;
170     }
171     else {
172         pod2usage(1);
173     }
174 }
175
176 sub mig_env_create {
177     if (-e $filename) {
178         print "Re-Creating $filename\n";
179         print `cat $filename`;
180     } else {
181         print "Creating $filename\n";
182     }
183     print "\n";
184
185     # directories
186
187     $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
188     my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
189     print "Main work directory (default $migworkdir_default): ";
190     my $MIGWORKDIR = <STDIN>;
191     chomp $MIGWORKDIR;
192     if (! $MIGWORKDIR) {
193         $MIGWORKDIR = $migworkdir_default;
194     }
195     $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
196     my $miggitdir_default = "${MIGBASEGITDIR}/$migration_schema/";
197     print "git repo for migration-specific scripts (default $miggitdir_default): ";
198     my $MIGGITDIR = <STDIN>;
199     chomp $MIGGITDIR;
200     if (! $MIGGITDIR) {
201         $MIGGITDIR = $miggitdir_default;
202     }
203
204     # MySQL/MariaDB
205
206     my $mysqlhost; my $mysqldb; my $mysqlport;
207     my $mysqluser; my $mysqlpass;
208     if (-e '/usr/sbin/koha-list' && `/usr/sbin/koha-list` =~ $migration_schema
209         && `sudo -nl xmlstarlet` =~ 'xmlstarlet') {
210         my $kohaconfig="/etc/koha/sites/$migration_schema/koha-conf.xml";
211         $mysqlhost=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig`;
212         $mysqldb=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig`;
213         $mysqlport=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/port' $kohaconfig`;
214         $mysqluser=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig`;
215         $mysqlpass=`sudo -n xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig`;
216         chomp $mysqlhost; chomp $mysqldb; chomp $mysqlport;
217         chomp $mysqluser; chomp $mysqlpass;
218     }
219
220     $MYSQL_HOST = $mysqlhost || 'localhost' unless $MYSQL_HOST;
221     my $mysql_host_default = $MYSQL_HOST;
222     print "MYSQL_HOST (default $mysql_host_default): ";
223     $MYSQL_HOST = <STDIN>;
224     chomp $MYSQL_HOST;
225     if (! $MYSQL_HOST) {
226         $MYSQL_HOST = $mysql_host_default;
227     }
228     $MYSQL_TCP_PORT = $mysqlport || 3306 unless $MYSQL_TCP_PORT;
229     my $mysql_port_default = $MYSQL_TCP_PORT;
230     print "MYSQL_TCP_PORT (default $mysql_port_default): ";
231     $MYSQL_TCP_PORT = <STDIN>;
232     chomp $MYSQL_TCP_PORT;
233     if (! $MYSQL_TCP_PORT) {
234         $MYSQL_TCP_PORT = $mysql_port_default;
235     }
236     $MYSQL_DATABASE = $mysqldb || 'koha_demo' unless $MYSQL_DATABASE;
237     my $mysql_database_default = $MYSQL_DATABASE;
238     print "MYSQL_DATABASE (default $mysql_database_default): ";
239     $MYSQL_DATABASE = <STDIN>;
240     chomp $MYSQL_DATABASE;
241     if (! $MYSQL_DATABASE) {
242         $MYSQL_DATABASE = $mysql_database_default;
243     }
244     $MYSQL_USER = $mysqluser || $MYSQL_DATABASE unless $MYSQL_USER;
245     my $mysql_user_default = $MYSQL_USER;
246     print "MYSQL_USER (default $mysql_user_default): ";
247     my $MYSQL_USER = <STDIN>;
248     chomp $MYSQL_USER;
249     if (! $MYSQL_USER) {
250         $MYSQL_USER = $mysql_user_default;
251     }
252     $MYSQL_PW = $mysqlpass || $MYSQL_USER unless $MYSQL_PW;
253     my $mysql_pw_default = $MYSQL_PW;
254     print "MYSQL_PW (default $mysql_pw_default): ";
255     my $MYSQL_PW = <STDIN>;
256     chomp $MYSQL_PW;
257     if (! $MYSQL_PW) {
258         $MYSQL_PW = $mysql_pw_default;
259     }
260
261     # create files and directories if needed
262
263     mkdir "$HOME/.mig";
264     make_path($MIGGITDIR, { verbose => 1 });
265     `touch $MIGGITDIR/README`;
266     make_path($MIGWORKDIR, { verbose => 1 });
267     symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
268     open FILE, ">$filename";
269     print FILE "export MYSQL_HOST=$MYSQL_HOST\n";
270     print FILE "export MYSQL_TCP_PORT=$MYSQL_TCP_PORT\n";
271     print FILE "export MYSQL_DATABASE=$MYSQL_DATABASE\n";
272     print FILE "export MYSQL_USER=$MYSQL_USER\n";
273     #TODO - brittle; need to escape the password string
274     print FILE "export MYSQL_PW=$MYSQL_PW\n";
275     print FILE "export MIGENVPROMPT=$migration_schema\n";
276     print FILE "export MIGSCHEMA=$migration_schema\n";
277     print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
278     print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
279     print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
280     print FILE "export MIGGITDIR=$MIGGITDIR\n";
281     print FILE "alias wcd='cd `mig wdir`'\n";
282     print FILE "alias gcd='cd `mig gdir`'\n";
283     print FILE "alias scd='cd `mig sdir`'\n";
284     print FILE "source ~/.profile\n";
285     print FILE "env | sort | egrep 'MYSQL|MIG'\n";
286     print FILE 'echo shell PID = $$' . "\n";
287     close FILE;
288     chmod 0600, $filename; # TODO: race condition worth worrying about? couldn't get sysopen to work
289 }
290
291 sub mig_env_clone {
292     my $orig_migration_schema = $ARGV[1] || '';
293     my $orig_filename = "$HOME/.kmig/$orig_migration_schema.env";
294     `cp $orig_filename $filename`;
295     `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`;
296     `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`;
297 }
298