]>
git.proxmox.com Git - librados2-perl.git/blob - PVE/RADOS.pm
10 use PVE
::RPCEnvironment
;
14 our @ISA = qw(Exporter);
16 # Items to export into callers namespace by default. Note: do not export
17 # names by default without a very good reason. Use EXPORT_OK instead.
18 # Do not simply export all your public functions/methods/constants.
20 # This allows declaration use PVE::RADOS ':all';
21 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
23 our %EXPORT_TAGS = ( 'all' => [ qw(
27 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
36 XSLoader
::load
('PVE::RADOS', $VERSION);
41 my ($fh, $cmd, $data) = @_;
43 local $SIG{PIPE
} = 'IGNORE';
45 my $bin = pack "a L/a*", $cmd, $data || '';
46 my $res = syswrite $fh, $bin;
48 die "write data failed - $!\n" if !defined($res);
52 my ($fh, $expect_result) = @_;
56 local $SIG{PIPE
} = 'IGNORE';
58 while (length($head) < 5) {
59 last if !sysread $fh, $head, 5 - length($head), length($head);
61 die "partial read\n" if length($head) < 5;
63 my ($cmd, $len) = unpack "a L", $head;
66 while (length($data) < $len) {
67 last if !sysread $fh, $data, $len - length($data), length($data);
69 die "partial data read\n" if length($data) < $len;
72 die $data if $cmd eq 'E' && $data;
73 die "got unexpected result\n" if $cmd ne '>';
76 return wantarray ?
($cmd, $data) : $data;
80 my ($class, %params) = @_;
82 my $rpcenv = PVE
::RPCEnvironment
::get
();
84 socketpair(my $child, my $parent, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
85 || die "socketpair: $!";
89 die "unable to fork - $!\n" if !defined($cpid);
96 $self->{cpid
} = $cpid;
97 $self->{child
} = $child;
100 my ($cmd, $msg) = &$readdata($child);
101 die $msg if $cmd eq 'E';
102 die "internal error- got unexpected result" if $cmd ne 'S';
107 PVE
::INotify
::inotify_close
();
109 if (my $atfork = $rpcenv->{atfork
}) {
117 my $timeout = delete $params{timeout
} || 5;
121 $conn = pve_rados_create
() ||
122 die "unable to create RADOS object\n";
124 pve_rados_conf_set
($conn, 'client_mount_timeout', $timeout);
126 foreach my $k (keys %params) {
127 pve_rados_conf_set
($conn, $k, $params{$k});
130 pve_rados_connect
($conn);
133 &$writedata($parent, 'E', $err);
136 &$writedata($parent, 'S');
138 $self->{conn
} = $conn;
141 my ($cmd, $data) = &$readdata($parent);
147 if ($cmd eq 'M') { # rados monitor commands
148 $res = pve_rados_mon_command
($self->{conn
}, [ $data ]);
149 } elsif ($cmd eq 'C') { # class methods
150 my $aref = decode_json
($data);
151 my $method = shift @$aref;
152 $res = encode_json
($self->$method(@$aref));
154 die "invalid command\n";
158 &$writedata($parent, 'E', $err);
161 &$writedata($parent, '>', $res);
174 #print "$$: DESTROY WAIT0\n";
175 eval { &$writedata($self->{child
}, 'Q'); };
176 my $res = waitpid($self->{cpid
}, 0);
177 #print "$$: DESTROY WAIT $res\n";
179 #print "$$: DESTROY SHUTDOWN\n";
180 pve_rados_shutdown
($self->{conn
}) if $self->{conn
};
185 my ($self, @args) = @_;
188 my $data = encode_json
(['cluster_stat', @args]);
189 &$writedata($self->{child
}, 'C', $data);
190 return decode_json
(&$readdata($self->{child
}, 1));
192 return pve_rados_cluster_stat
($self->{conn
});
196 # example1: { prefix => 'get_command_descriptions'})
197 # example2: { prefix => 'mon dump', format => 'json' }
199 my ($self, $cmd) = @_;
201 $cmd->{format
} = 'json' if !$cmd->{format
};
203 my $json = encode_json
($cmd);
205 &$writedata($self->{child
}, 'M', $json);
207 my $raw = &$readdata($self->{child
}, 1);
209 if ($cmd->{format
} && $cmd->{format
} eq 'json') {
210 return length($raw) ? decode_json
($raw) : undef;
221 PVE::RADOS - Perl bindings for librados
227 my $rados = PVE::RADOS::new();
228 my $stat = $rados->cluster_stat();
229 my $res = $rados->mon_command({ prefix => 'mon dump', format => 'json' });
233 Perl bindings for librados.
241 Dietmar Maurer, E<lt>dietmar@proxmox.com<gt>