|
| 1 | +packageRemoteCluster; |
| 2 | + |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | +use Data::Dumper; |
| 6 | +use Net::OpenSSH; |
| 7 | +use Cwd; |
| 8 | + |
| 9 | +subnew |
| 10 | +{ |
| 11 | +my ($class,$config_fname) =@_; |
| 12 | +open(my$config,'<',$config_fname); |
| 13 | +my@config_lines = <$config>; |
| 14 | +my@nodes = (); |
| 15 | + |
| 16 | +# Parse connection options from ssh_config |
| 17 | +my$node; |
| 18 | +foreach (@config_lines) |
| 19 | +{ |
| 20 | +if (/^Host (.+)/) |
| 21 | +{ |
| 22 | +if ($node->{'host'}){ |
| 23 | +push(@nodes,$node); |
| 24 | +$node = {}; |
| 25 | +} |
| 26 | +$node->{'host'} =$1; |
| 27 | +} |
| 28 | +elsif (/\s*([^\s]+)\s*([^\s]+)\s*/) |
| 29 | +{ |
| 30 | +$node->{'cfg'}->{$1} =$2; |
| 31 | +} |
| 32 | +} |
| 33 | +push(@nodes,$node); |
| 34 | + |
| 35 | +# print Dumper(@nodes); |
| 36 | + |
| 37 | +my$self = { |
| 38 | +nnodes=>scalar@nodes, |
| 39 | +nodes=> \@nodes, |
| 40 | +}; |
| 41 | + |
| 42 | +bless$self,$class; |
| 43 | +return$self; |
| 44 | +} |
| 45 | + |
| 46 | +subrun |
| 47 | +{ |
| 48 | +my ($self,$node_id,$cmd) =@_; |
| 49 | +my$node =$self->{nodes}[$node_id]; |
| 50 | +my$opts =$node->{cfg}; |
| 51 | + |
| 52 | +print"===\n"; |
| 53 | +print Dumper($opts); |
| 54 | +print"===\n"; |
| 55 | + |
| 56 | +my$ssh = Net::OpenSSH->new( |
| 57 | +$opts->{HostName}, |
| 58 | +port=>$opts->{Port}, |
| 59 | +user=>$opts->{User}, |
| 60 | +key_path=>$opts->{IdentityFile} =~/"([^"]*)"/, |
| 61 | +master_opts=> [-o=>"StrictHostKeyChecking=no"] |
| 62 | +); |
| 63 | + |
| 64 | +my@ls =$ssh->capture($cmd); |
| 65 | + |
| 66 | +print Dumper(@ls); |
| 67 | + |
| 68 | +} |
| 69 | + |
| 70 | +subinit |
| 71 | +{ |
| 72 | +my ($self) =@_; |
| 73 | +my$nodes =$self->{nodes}; |
| 74 | + |
| 75 | +foreachmy$node (@$nodes) |
| 76 | +{ |
| 77 | +$node->init(hba_permit_replication=> 0); |
| 78 | +} |
| 79 | +} |
| 80 | + |
| 81 | +subdetach |
| 82 | +{ |
| 83 | +my ($self) =@_; |
| 84 | +my$nodes =$self->{nodes}; |
| 85 | + |
| 86 | +foreachmy$node (@$nodes) |
| 87 | +{ |
| 88 | +delete$node->{_pid}; |
| 89 | +} |
| 90 | +} |
| 91 | + |
| 92 | +subconfigure |
| 93 | +{ |
| 94 | +my ($self) =@_; |
| 95 | +my$nodes =$self->{nodes}; |
| 96 | + |
| 97 | +my$connstr =join(',',map {"${\$_->connstr('postgres') }" }@$nodes); |
| 98 | +my$raftpeers =join(',',map {join(':',$_->{id},$_->host,$_->{raftport}) }@$nodes); |
| 99 | + |
| 100 | +foreachmy$node (@$nodes) |
| 101 | +{ |
| 102 | +my$id =$node->{id}; |
| 103 | +my$host =$node->host; |
| 104 | +my$pgport =$node->port; |
| 105 | +my$raftport =$node->{raftport}; |
| 106 | + |
| 107 | +$node->append_conf("postgresql.conf",qq( |
| 108 | +listen_addresses = '$host' |
| 109 | +unix_socket_directories = '' |
| 110 | +port =$pgport |
| 111 | +max_prepared_transactions = 200 |
| 112 | +max_connections = 200 |
| 113 | +max_worker_processes = 100 |
| 114 | +wal_level = logical |
| 115 | +fsync = off |
| 116 | +max_wal_senders = 10 |
| 117 | +wal_sender_timeout = 0 |
| 118 | +max_replication_slots = 10 |
| 119 | +shared_preload_libraries = 'raftable,multimaster' |
| 120 | +multimaster.workers = 10 |
| 121 | +multimaster.queue_size = 10485760 # 10mb |
| 122 | +multimaster.node_id =$id |
| 123 | +multimaster.conn_strings = '$connstr' |
| 124 | +multimaster.use_raftable = true |
| 125 | +multimaster.ignore_tables_without_pk = true |
| 126 | +multimaster.twopc_min_timeout = 60000 |
| 127 | +raftable.id =$id |
| 128 | +raftable.peers = '$raftpeers' |
| 129 | +)); |
| 130 | + |
| 131 | +$node->append_conf("pg_hba.conf",qq( |
| 132 | +local replication all trust |
| 133 | +host replication all 127.0.0.1/32 trust |
| 134 | +host replication all ::1/128 trust |
| 135 | +)); |
| 136 | +} |
| 137 | +} |
| 138 | + |
| 139 | +substart |
| 140 | +{ |
| 141 | +my ($self) =@_; |
| 142 | +my$nodes =$self->{nodes}; |
| 143 | + |
| 144 | +foreachmy$node (@$nodes) |
| 145 | +{ |
| 146 | +$node->start(); |
| 147 | +} |
| 148 | +} |
| 149 | + |
| 150 | +substop |
| 151 | +{ |
| 152 | +my ($self) =@_; |
| 153 | +my$nodes =$self->{nodes}; |
| 154 | + |
| 155 | +foreachmy$node (@$nodes) |
| 156 | +{ |
| 157 | +$node->stop(); |
| 158 | +} |
| 159 | +} |
| 160 | + |
| 161 | +subpsql |
| 162 | +{ |
| 163 | +my ($self,$index,@args) =@_; |
| 164 | +my$node =$self->{nodes}->[$index]; |
| 165 | +return$node->psql(@args); |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | +my$cluster = new RemoteCluster('ssh-config'); |
| 170 | + |
| 171 | +print$cluster->{'nnodes'} ."\n"; |
| 172 | + |
| 173 | +$cluster->run(1,'ls -la'); |
| 174 | + |
| 175 | +1; |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |