|
| 1 | +package RemoteNode; |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Net::OpenSSH; |
| 6 | + |
| 7 | +sub new |
| 8 | +{ |
| 9 | + my ($class, $name, $sshopts) = @_; |
| 10 | + |
| 11 | + my $self = { |
| 12 | + _port => $sshopts->{Port}, |
| 13 | + _host => $sshopts->{HostName}, |
| 14 | + _user => $sshopts->{User}, |
| 15 | + _keypath => $sshopts->{IdentityFile} =~ /"([^"]*)"/, |
| 16 | + _pgdata => "/home/$sshopts->{User}/pg_cluster/data_5432", |
| 17 | + _pgbin => "/home/$sshopts->{User}/pg_cluster/install/bin", |
| 18 | + }; |
| 19 | + |
| 20 | + bless $self, $class; |
| 21 | + |
| 22 | + # kill postgres here to ensure |
| 23 | + # predictable initial state. |
| 24 | + $self->execute("pkill -9 postgres || true"); |
| 25 | + |
| 26 | + return $self; |
| 27 | +} |
| 28 | + |
| 29 | +sub execute |
| 30 | +{ |
| 31 | + my ($self, $cmd) = @_; |
| 32 | + |
| 33 | + # XXX: reuse connection |
| 34 | + my $ssh = Net::OpenSSH->new( |
| 35 | + host => $self->{_host}, |
| 36 | + port => $self->{_port}, |
| 37 | + user => $self->{_user}, |
| 38 | + key_path => $self->{_keypath}, |
| 39 | + master_opts => [-o => "StrictHostKeyChecking=no"] |
| 40 | + ); |
| 41 | + |
| 42 | + print "# running \"$cmd\":\n"; |
| 43 | + |
| 44 | + my $output = $ssh->capture($cmd); |
| 45 | + |
| 46 | + $ssh->error and |
| 47 | + warn "operation didn't complete successfully: ". $ssh->error; |
| 48 | + |
| 49 | + # XXX: tab and colorize output |
| 50 | + print $output; |
| 51 | + print "---\n"; |
| 52 | + |
| 53 | + return $?; |
| 54 | +} |
| 55 | + |
| 56 | +sub init |
| 57 | +{ |
| 58 | + my ($self, %params) = @_; |
| 59 | + my $pgbin = $self->{_pgbin}; |
| 60 | + my $pgdata = $self->{_pgdata}; |
| 61 | + |
| 62 | + $self->execute("rm -rf $pgdata"); |
| 63 | + $self->execute("env LANG=C LC_ALL=C $pgbin/initdb -D $pgdata -A trust -N"); |
| 64 | + |
| 65 | + $self->execute("echo 'fsync = off' >> $pgdata/postgresql.conf"); |
| 66 | + $self->execute("echo 'host all all 0.0.0.0/0 trust' >> $pgdata/pg_hba.conf"); |
| 67 | +} |
| 68 | + |
| 69 | +sub start |
| 70 | +{ |
| 71 | + my ($self) = @_; |
| 72 | + my $pgbin = $self->{_pgbin}; |
| 73 | + my $pgdata = $self->{_pgdata}; |
| 74 | + |
| 75 | + $self->execute("$pgbin/pg_ctl -w -D $pgdata -l $pgdata/log start"); |
| 76 | +} |
| 77 | + |
| 78 | +sub stop |
| 79 | +{ |
| 80 | + my ($self, $mode) = @_; |
| 81 | + my $pgbin = $self->{_pgbin}; |
| 82 | + my $pgdata = $self->{_pgdata}; |
| 83 | + |
| 84 | + $self->execute("$pgbin/pg_ctl -w -D $pgdata -m $mode stop"); |
| 85 | +} |
| 86 | + |
| 87 | +sub restart |
| 88 | +{ |
| 89 | + my ($self) = @_; |
| 90 | + my $pgbin = $self->{_pgbin}; |
| 91 | + my $pgdata = $self->{_pgdata}; |
| 92 | + |
| 93 | + $self->execute("$pgbin/pg_ctl -w -D $pgdata restart"); |
| 94 | +} |
| 95 | + |
| 96 | +sub append_conf |
| 97 | +{ |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +sub psql |
| 102 | +{ |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +# XXX: test |
| 107 | +my $node = new RemoteNode('node0', { |
| 108 | + 'Port' => '2200', |
| 109 | + 'IdentityFile' => '"/Users/stas/code/postgres_cluster/contrib/mmts/testeaux/.vagrant/machines/node1/virtualbox/private_key"', |
| 110 | + 'IdentitiesOnly' => 'yes', |
| 111 | + 'LogLevel' => 'FATAL', |
| 112 | + 'PasswordAuthentication' => 'no', |
| 113 | + 'StrictHostKeyChecking' => 'no', |
| 114 | + 'HostName' => '127.0.0.1', |
| 115 | + 'User' => 'vagrant', |
| 116 | + 'UserKnownHostsFile' => '/dev/null' |
| 117 | + }); |
| 118 | + |
| 119 | +$node->execute("ls -ls"); |
| 120 | +$node->init; |
| 121 | +$node->start; |
| 122 | + |
| 123 | + |
0 commit comments