TheloniusMonk has asked for the wisdom of the Perl Monks concerning the following question:
I am working on a migration project, where there will be parallel run for all the migrated servers for a period of time. One of these servers is a LAPP stack with cron jobs written in bash and php.
During the parallel run, it is necessary to be able to turn off production scripts in the old environment as soon as the migrated versions are tested because there are as yet unmigrated shared storage devices in the landscape.
So, although the storage will be updated by the new servers, instead of the old, for such scripts, a psql splitter is needed in the case of psql.
For bash, I have a working solution: In the bash scripts I alias psql to ~/bin/psql.pl, and this script contacts two databases instead of one, transparently to the bash script (see below).
My challenge for today is to do the same for php scripts. I.E., create a similar Perl script as the below, but find a way to alias calls like $link = pg_connect('host=localhost dbname=DBNAME user=USERNAME password=XYZ');
So that subsequent references to $link will send the SQL to two databases similarly to the below solution for bash. I can't begin to write the phppsql.pl for this not knowing how the interface will look. Bear in mind there are a large number of such php scripts.
Many thanks in advance!#!/usr/bin/perl use strict; use warnings; my $g=main->new; $g->init; $g->parse; $g->enact; package main; sub new { return bless {}, shift; } sub init { my $this = shift; $this->{host1} = '<new-server>.com'; $this->{host2} = '<old-server>.com'; } sub parse { my $this = shift; my $cmd = join(' ', @ARGV); my ($host); if ($cmd =~ s/\-h\s*(\S*)//) { $host = $1; unless ($host eq $this->{host1}) { $this->{host1} = $host; $this->{host2} = ''; } } if ($cmd =~ s/\-c\s*(.*)$//) { $this->{sql} = $1; } else { $this->{sql} = ''; } $this->{cmd1} = '/bin/psql -h ' . $this->{host1} . ' ' . $cmd;; $this->{host2} and $this->{cmd2} = '/bin/psql -h ' . $this->{host2} . ' ' . $cm +d; } sub enact { my $this = shift; if ($this->{sql}) { my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph $this->{sql} . "\n"; close $ph; waitpid $pid, 0; } } else { my @stdin = <STDIN>; my $pid = open my $ph, "| $this->{cmd1}" or die $!; print $ph @stdin; close $ph; waitpid $pid, 0; if ($this->{host2}) { my $pid = open my $ph, "| $this->{cmd2} 2>&1 >/dev/null" o +r die $!; print $ph @stdin; close $ph; waitpid $pid, 0; } } } 1; __END__
T.M.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: psql splitter for php
by tobyink (Canon) on Jan 22, 2021 at 17:07 UTC | |
Re: psql splitter for php
by erix (Prior) on Jan 22, 2021 at 20:58 UTC | |
Re: psql splitter for php
by betmatt (Scribe) on Jan 22, 2021 at 17:58 UTC | |
Re: psql splitter for php
by LanX (Saint) on Jan 22, 2021 at 16:04 UTC | |
| |
Re: psql splitter for php
by Anonymous Monk on Jan 22, 2021 at 12:23 UTC | |
|