Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Dear Monks,

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.

#!/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__
Many thanks in advance!

T.M.


In reply to psql splitter for php by TheloniusMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2024-04-20 06:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found