/[rserv]/bin/SlaveInit
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /bin/SlaveInit

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Tue Aug 5 21:10:28 2003 UTC (20 years, 9 months ago) by dpavlin
Branch: MAIN
Changes since 1.4: +35 -25 lines
command line options cleanup and cosistency fix, modification to work
under "use strict;"

1 dpavlin 1.5 #!/usr/bin/perl -w
2 dpavlin 1.1 # SlaveInit
3     # Vadim Mikheev, (c) 2000, PostgreSQL Inc.
4    
5     eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
6     & eval 'exec perl -S $0 $argv:q'
7     if 0;
8    
9 dpavlin 1.5 use strict;
10 dpavlin 1.1 use Pg;
11     use Getopt::Long;
12 dpavlin 1.3 use Sys::Hostname;
13 dpavlin 1.1
14     $| = 1;
15    
16 dpavlin 1.5 my ($debug,$verbose,$quiet) = (0,0,0);
17     my ($help,$masterhost,$masterport,$masteruser,$masterpassword,
18     $slavehost,$slaveport,$slaveuser,$slavepassword);
19    
20     my $result = GetOptions(
21     "debug!" => \$debug, "verbose!" => \$verbose,
22     "quiet!" => \$quiet, "help" => \$help,
23     "masterhost=s" => \$masterhost, "masterport=i" => \$masterport,
24     "masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword,
25     "slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,
26     "slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,
27     );
28    
29     if (defined($help) || (scalar(@ARGV) < 2)) {
30     print "Usage: $0 [options] masterdb slavedb
31     Options:
32     --masterhost=hostname --masterport=port
33     --masteruser=username --masterpassword=string
34     --slavehost=hostname --slaveport=port
35     --slaveuser=username --slavepassword=string
36     ";
37     exit ((scalar(@ARGV) < 2)? 1:0);
38 dpavlin 1.1 }
39    
40 dpavlin 1.3 my $master = $ARGV[0] || "master";
41     my $slave = $ARGV[1] || "slave";
42 dpavlin 1.1
43     my $sinfo = "dbname=$slave";
44 dpavlin 1.5 $sinfo = "$sinfo host=$slavehost" if (defined($slavehost));
45     $sinfo = "$sinfo port=$slaveport" if (defined($slaveport));
46     $sinfo = "$sinfo user=$slaveuser" if (defined($slaveuser));
47     $sinfo = "$sinfo password=$slavepassword" if (defined($slavepassword));
48 dpavlin 1.3
49     my $minfo = "dbname=$master";
50 dpavlin 1.5 $minfo = "$minfo host=$masterhost" if (defined($masterhost));
51     $minfo = "$minfo port=$masterport" if (defined($masterport));
52     $minfo = "$minfo user=$masteruser" if (defined($masteruser));
53     $minfo = "$minfo password=$masterpassword" if (defined($masterpassword));
54 dpavlin 1.1
55     sub RollbackAndQuit {
56     my $conn = shift @_;
57    
58     print STDERR $conn->errorMessage;
59     $conn->exec("ROLLBACK");
60     exit (-1);
61     }
62    
63 dpavlin 1.3 # First, lets add the needed information in the slave database
64    
65 dpavlin 1.1 print("Connecting to $sinfo\n") if ($debug || $verbose);
66     my $conn = Pg::connectdb($sinfo);
67     if ($conn->status != PGRES_CONNECTION_OK) {
68     print STDERR "Failed opening $sinfo\n";
69     exit 1;
70     }
71    
72 dpavlin 1.5 $result = $conn->exec("BEGIN");
73 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
74    
75     $result = $conn->exec("set transaction isolation level serializable");
76     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
77    
78     $result = $conn->exec("create table _RSERV_SLAVE_TABLES_" .
79 dpavlin 1.3 " (tname name not null, cname name not null, reloid oid not null, key int4 not null)");
80 dpavlin 1.1 RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
81    
82     $result = $conn->exec("create table _RSERV_SLAVE_SYNC_" .
83     " (syncid int4, synctime timestamp)");
84     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
85    
86 dpavlin 1.3
87     # next, let's tell the master that the slave is set up.
88    
89     print("Connecting to $minfo\n") if ($debug || $verbose);
90 dpavlin 1.5 my $mconn = Pg::connectdb($minfo);
91 dpavlin 1.3 if ($mconn->status != PGRES_CONNECTION_OK) {
92     print STDERR "Failed opening $minfo\n";
93     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
94     # exit 1;
95     }
96    
97 dpavlin 1.5 my $host = defined($slavehost) ? $slavehost : hostname;
98 dpavlin 1.3
99     $result = $mconn->exec("INSERT INTO _RSERV_SERVERS_ (host,dbase) VALUES ('$host','$slave')");
100     if ($result->resultStatus ne PGRES_COMMAND_OK) {
101     print STDERR $mconn->errorMessage;
102     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
103     # exit (-1);
104     }
105    
106    
107     # then commit the slave transaction
108    
109 dpavlin 1.1 $result = $conn->exec("COMMIT");
110     RollbackAndQuit($conn) if ($result->resultStatus ne PGRES_COMMAND_OK);
111    
112 dpavlin 1.3
113    
114 dpavlin 1.1 exit (0);

  ViewVC Help
Powered by ViewVC 1.1.26