toward renaming mig to emig and tweaking the directory layout
[migration-tools.git] / emig.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.  Convention has this being a single lowercased
33 word or acronym identifying the library, prefixed with 'm_'.
34
35 =item MIGWORKDIR
36
37 The base working directory for containing migration data, scripts, and other
38 files.
39
40 =item PGHOST
41
42 The IP address or hostname for the PostgreSQL database used for a migration.
43
44 =item PGPORT
45
46 The TCP port for the PostgreSQL database.
47
48 =item PGUSER
49
50 The PostgreSQL user to use for the database.
51
52 =item PGDATABASE
53
54 The name of the actual database containing the migration schema.
55
56 =back
57
58 This script may also setup a symlink from a specified Git repository to a
59 scripts/ directory within the migration work directory.  The default for this is
60 ~/git/migration-work/MIGSCHEMA --> MIGWORKDIR/scripts
61
62 It may also create the migration work directory if necessary.
63
64 =head1 COMMANDS
65
66 =over 15
67
68 =item B<create> <schema>
69
70 This invocation will prompt for various values and create a .env file for the
71 specified migration schema, and a symlink between the specified Git repository
72 and migration work directory (which will also be created if needed).
73
74 =item B<use> <schema>
75
76 This command will spawn a bash shell that executes the corresponding
77 ~/.mig/<schema>.env script for setting up environment variables encoded during
78 B<create>.
79
80 =item B<show> [schema]
81
82 This command will show the contents of the corresponding ~/.mig/<schema>.env
83 script, or, if no schema is specified, then it will list pertinent variables in
84 the current environment if they exist.
85
86 =item B<clone> [orig schema] [new schema]
87
88 This command will create a "shallow" clone of the orig schema, in that it will
89 share database credentials as well as git and data directories, but will have a
90 separate schema name.
91
92 =item B<list>
93
94 This command will list migration schemas found in ~/.mig
95
96 =item B<help>
97
98 Display the documentation you're reading now.
99
100 =back
101
102 =cut
103
104 ###############################################################################
105
106 use strict;
107 use 5.012;
108 use Switch;
109 use Env qw(
110     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
111     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
112 );
113 use Pod::Usage;
114 use File::Path qw(make_path);
115 use FindBin;
116 my $mig_bin = "$FindBin::Bin/";
117 use lib "$FindBin::Bin/";
118
119 pod2usage(-verbose => 2) if ! $ARGV[0];
120
121 my $migration_schema = $ARGV[1] || '';
122 my $filename = "$HOME/.mig/$migration_schema.env";
123 switch($ARGV[0]) {
124     case "--help" {
125         pod2usage(-verbose => 2);
126     }
127     case "help" {
128         pod2usage(-verbose => 2);
129     }
130     case "create" {
131         pod2usage(-verbose => 1) if ! $ARGV[1];
132         mig_env_create();
133     }
134     case "clone" {
135         pod2usage(-verbose => 1) if ! $ARGV[2];
136         $migration_schema = $ARGV[2] || '';
137         $filename = "$HOME/.mig/$migration_schema.env";
138         mig_env_clone();
139     }
140     case "use" {
141         pod2usage(-verbose => 1) if ! $ARGV[1];
142         if (-e $filename) {
143             exec '/bin/bash', '--init-file', $filename;
144         } else {
145             die "\n$filename does not exist\n";
146         }
147     }
148     case "show" {
149         if (-e $filename) {
150             exec '/bin/cat', $filename;
151         } else {
152             print `env | sort | egrep 'MIG|PG'`;
153         }
154     }
155     case "list" {
156         opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
157         while (readdir $dh) {
158             if (/^(.*)\.env$/) {
159                 print "$1\n";
160             }
161         }
162         closedir $dh;
163     }
164     else {
165         pod2usage(1);
166     }
167 }
168
169 sub mig_env_create {
170     if (-e $filename) {
171         print "Re-Creating $filename\n";
172         print `cat $filename`;
173     } else {
174         print "Creating $filename\n";
175     }
176     print "\n";
177
178     # directories
179
180     $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
181     my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
182     print "Main work directory (default $migworkdir_default): ";
183     my $MIGWORKDIR = <STDIN>;
184     chomp $MIGWORKDIR;
185     if (! $MIGWORKDIR) {
186         $MIGWORKDIR = $migworkdir_default;
187     }
188     $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
189     my $miggitdir_default = "${MIGBASEGITDIR}/$migration_schema/";
190     print "git repo for migration-specific scripts (default $miggitdir_default): ";
191     my $MIGGITDIR = <STDIN>;
192     chomp $MIGGITDIR;
193     if (! $MIGGITDIR) {
194         $MIGGITDIR = $miggitdir_default;
195     }
196
197     # PostgreSQL
198
199     $PGHOST = 'localhost' unless $PGHOST;
200     my $pghost_default = $PGHOST;
201     print "PGHOST (default $pghost_default): ";
202     $PGHOST = <STDIN>;
203     chomp $PGHOST;
204     if (! $PGHOST) {
205         $PGHOST = $pghost_default;
206     }
207     $PGPORT = 5432 unless $PGPORT;
208     my $pgport_default = $PGPORT;
209     print "PGPORT (default $pgport_default): ";
210     $PGPORT = <STDIN>;
211     chomp $PGPORT;
212     if (! $PGPORT) {
213         $PGPORT = $pgport_default;
214     }
215     $PGDATABASE = 'evergreen' unless $PGDATABASE;
216     my $pgdatabase_default = $PGDATABASE;
217     print "PGDATABASE (default $pgdatabase_default): ";
218     $PGDATABASE = <STDIN>;
219     chomp $PGDATABASE;
220     if (! $PGDATABASE) {
221         $PGDATABASE = $pgdatabase_default;
222     }
223     $PGUSER = $PGDATABASE unless $PGUSER;
224     my $pguser_default = $PGUSER;
225     print "PGUSER (default $pguser_default): ";
226     my $PGUSER = <STDIN>;
227     chomp $PGUSER;
228     if (! $PGUSER) {
229         $PGUSER = $pguser_default;
230     }
231
232     # create files and directories if needed
233
234     mkdir "$HOME/.mig";
235     make_path($MIGGITDIR, { verbose => 1 });
236     `touch $MIGGITDIR/README`;
237     make_path($MIGWORKDIR, { verbose => 1 });
238     symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
239     open FILE, ">$filename";
240     print FILE "export PGHOST=$PGHOST\n";
241     print FILE "export PGPORT=$PGPORT\n";
242     print FILE "export PGDATABASE=$PGDATABASE\n";
243     print FILE "export PGUSER=$PGUSER\n";
244     print FILE "export PGOPTIONS='-c search_path=$migration_schema,public,evergreen'\n";
245     print FILE "export MIGENVPROMPT=$migration_schema\n";
246     print FILE "export MIGSCHEMA=$migration_schema\n";
247     print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
248     print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
249     print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
250     print FILE "export MIGGITDIR=$MIGGITDIR\n";
251     print FILE "alias wcd='cd `mig wdir`'\n";
252     print FILE "alias gcd='cd `mig gdir`'\n";
253     print FILE "alias scd='cd `mig sdir`'\n";
254     print FILE "source ~/.profile\n";
255     print FILE "env | sort | egrep 'PG|MIG'\n";
256     print FILE 'echo shell PID = $$' . "\n";
257     close FILE;
258 }
259
260 sub mig_env_clone {
261     my $orig_migration_schema = $ARGV[1] || '';
262     my $orig_filename = "$HOME/.mig/$orig_migration_schema.env";
263     `cp $orig_filename $filename`;
264     `sed -i 's/export PGOPTIONS=.*/export PGOPTIONS='"'"'-c search_path=$migration_schema,public,evergreen'"'"'/' $filename`;
265     `sed -i 's/export MIGENVPROMPT=.*/export MIGENVPROMPT=$migration_schema/' $filename`;
266     `sed -i 's/export MIGSCHEMA=.*/export MIGSCHEMA=$migration_schema/' $filename`;
267 }
268