]>
git.proxmox.com Git - librados2-perl.git/blob - PVE/RADOS.pm
12 our @ISA = qw(Exporter);
14 # Items to export into callers namespace by default. Note: do not export
15 # names by default without a very good reason. Use EXPORT_OK instead.
16 # Do not simply export all your public functions/methods/constants.
18 # This allows declaration use PVE::RADOS ':all';
19 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
21 our %EXPORT_TAGS = ( 'all' => [ qw(
25 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
34 XSLoader
::load
('PVE::RADOS', $VERSION);
39 my ($fh, $cmd, $data) = @_;
41 local $SIG{PIPE
} = 'IGNORE';
43 my $bin = pack "a L/a*", $cmd, $data || '';
44 my $res = syswrite $fh, $bin;
46 die "write data failed - $!\n" if !defined($res);
50 my ($fh, $expect_result) = @_;
54 local $SIG{PIPE
} = 'IGNORE';
56 while (length($head) < 5) {
57 last if !sysread $fh, $head, 5 - length($head), length($head);
59 die "partial read\n" if length($head) < 5;
61 my ($cmd, $len) = unpack "a L", $head;
64 while (length($data) < $len) {
65 last if !sysread $fh, $data, $len - length($data), length($data);
67 die "partial data read\n" if length($data) < $len;
70 die $data if $cmd eq 'E' && $data;
71 die "got unexpected result\n" if $cmd ne '>';
74 return wantarray ?
($cmd, $data) : $data;
78 my ($class, %params) = @_;
80 socketpair(my $child, my $parent, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
81 || die "socketpair: $!";
85 die "unable to fork - $!\n" if !defined($cpid);
92 $self->{cpid
} = $cpid;
93 $self->{child
} = $child;
96 my ($cmd, $msg) = &$readdata($child);
97 die $msg if $cmd eq 'E';
98 die "internal error- got unexpected result" if $cmd ne 'S';
107 my $timeout = delete $params{timeout
} || 5;
111 $conn = pve_rados_create
() ||
112 die "unable to create RADOS object\n";
114 pve_rados_conf_set
($conn, 'client_mount_timeout', $timeout);
116 foreach my $k (keys %params) {
117 pve_rados_conf_set
($conn, $k, $params{$k});
120 pve_rados_connect
($conn);
123 &$writedata($parent, 'E', $err);
126 &$writedata($parent, 'S');
128 $self->{conn
} = $conn;
131 my ($cmd, $data) = &$readdata($parent);
137 if ($cmd eq 'M') { # rados monitor commands
138 $res = pve_rados_mon_command
($self->{conn
}, [ $data ]);
139 } elsif ($cmd eq 'C') { # class methods
140 my $aref = decode_json
($data);
141 my $method = shift @$aref;
142 $res = encode_json
($self->$method(@$aref));
144 die "invalid command\n";
148 &$writedata($parent, 'E', $err);
151 &$writedata($parent, '>', $res);
164 #print "$$: DESTROY WAIT0\n";
165 eval { &$writedata($self->{child
}, 'Q'); };
166 my $res = waitpid($self->{cpid
}, 0);
167 #print "$$: DESTROY WAIT $res\n";
169 #print "$$: DESTROY SHUTDOWN\n";
170 pve_rados_shutdown
($self->{conn
}) if $self->{conn
};
175 my ($self, @args) = @_;
178 my $data = encode_json
(['cluster_stat', @args]);
179 &$writedata($self->{child
}, 'C', $data);
180 return decode_json
(&$readdata($self->{child
}, 1));
182 return pve_rados_cluster_stat
($self->{conn
});
186 # example1: { prefix => 'get_command_descriptions'})
187 # example2: { prefix => 'mon dump', format => 'json' }
189 my ($self, $cmd) = @_;
191 $cmd->{format
} = 'json' if !$cmd->{format
};
193 my $json = encode_json
($cmd);
195 &$writedata($self->{child
}, 'M', $json);
197 my $raw = &$readdata($self->{child
}, 1);
199 if ($cmd->{format
} && $cmd->{format
} eq 'json') {
200 return length($raw) ? decode_json
($raw) : undef;
211 PVE::RADOS - Perl bindings for librados
217 my $rados = PVE::RADOS::new();
218 my $stat = $rados->cluster_stat();
219 my $res = $rados->mon_command({ prefix => 'mon dump', format => 'json' });
223 Perl bindings for librados.
231 Dietmar Maurer, E<lt>dietmar@proxmox.com<gt>