2 ###############################################################################
7 mig-env - This tool is for tracking and setting environment variables used by
8 B<mig> and its sub-tools.
12 B<mig-env> <create|use> <migration_schema>
14 B<mig-env> <show> [migration_schema]
16 B<mig-env> <clone> [orig_migration_schema] [new_migration_schema]
24 For most invocations, B<mig-env> will either create or use a migration-specific
25 file (~/.kmig/<migration_schema>.env) for setting the following environment
32 The name of the migration schema. In practice, this will match the name of the
37 The base working directory for containing migration data, scripts, and other
42 The IP address or hostname for the MySQL/MariaDB database used for a migration.
46 The TCP port for the database.
50 The user to use for the database.
54 The password to use for the database.
58 The name of the actual database/schema. In practice, this will match the
59 migration schema or Koha instance name, prefixed with 'koha_'.
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
67 It may also create the migration work directory if necessary.
73 =item B<create> <schema>
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).
81 This command will spawn a bash shell that executes the corresponding
82 ~/.kmig/<schema>.env script for setting up environment variables encoded during
85 =item B<show> [schema]
87 This command will show the contents of the corresponding ~/.kmig/<schema>.env
88 script, or, if no schema is specified, then it will list pertinent variables in
89 the current environment if they exist.
91 =item B<clone> [orig schema] [new schema]
93 FIXME: need to re-think this in a MySQL/MariaDB/Koha context
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
101 This command will list migration schemas found in ~/.kmig
105 Display the documentation you're reading now.
111 ###############################################################################
117 HOME MYSQL_HOST MYSQL_TCP_PORT MYSQL_USER MYSQL_DATABASE MYSQL_PW
118 MIGSCHEMA MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
121 use File::Path qw(make_path);
123 my $mig_bin = "$FindBin::Bin/";
124 use lib "$FindBin::Bin/";
126 pod2usage(-verbose => 2) if ! $ARGV[0];
128 my $migration_schema = $ARGV[1] || '';
129 my $filename = "$HOME/.kmig/$migration_schema.env";
132 pod2usage(-verbose => 2);
135 pod2usage(-verbose => 2);
138 pod2usage(-verbose => 1) if ! $ARGV[1];
142 pod2usage(-verbose => 1) if ! $ARGV[2];
143 $migration_schema = $ARGV[2] || '';
144 $filename = "$HOME/.kmig/$migration_schema.env";
148 pod2usage(-verbose => 1) if ! $ARGV[1];
150 exec '/bin/bash', '--init-file', $filename;
152 die "\n$filename does not exist\n";
157 exec '/bin/cat', $filename;
159 print `env | sort | egrep 'MIG|PG'`;
163 opendir(my $dh, "$HOME/.kmig") || die "can't open $HOME/.kmig: $!";
164 while (readdir $dh) {
178 print "Re-Creating $filename\n";
179 print `cat $filename`;
181 print "Creating $filename\n";
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>;
193 $MIGWORKDIR = $migworkdir_default;
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>;
201 $MIGGITDIR = $miggitdir_default;
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;
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>;
226 $MYSQL_HOST = $mysql_host_default;
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;
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;
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>;
250 $MYSQL_USER = $mysql_user_default;
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>;
258 $MYSQL_PW = $mysql_pw_default;
261 # create files and directories if needed
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 MIGCMD=kmig\n";
276 print FILE "export MIGENVPROMPT=$migration_schema\n";
277 print FILE "export MIGSCHEMA=$migration_schema\n";
278 print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
279 print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
280 print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
281 print FILE "export MIGGITDIR=$MIGGITDIR\n";
282 print FILE "alias wcd='cd `mig wdir`'\n";
283 print FILE "alias gcd='cd `mig gdir`'\n";
284 print FILE "alias scd='cd `mig sdir`'\n";
285 print FILE "source ~/.profile\n";
286 print FILE "env | sort | egrep 'MYSQL|MIG'\n";
287 print FILE 'echo shell PID = $$' . "\n";
289 chmod 0600, $filename; # TODO: race condition worth worrying about? couldn't get sysopen to work
293 my $orig_migration_schema = $ARGV[1] || '';
294 my $orig_filename = "$HOME/.kmig/$orig_migration_schema.env";
295 `cp $orig_filename $filename`;
296 `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`;
297 `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`;