]>
git.proxmox.com Git - librados2-perl.git/blob - PVE/RADOS.pm
76c9d7b9d0d9477067b143880206e7f7fa642ba7
11 use PVE
::RPCEnvironment
;
15 my $rados_default_timeout = 5;
18 our @ISA = qw(Exporter);
20 # Items to export into callers namespace by default. Note: do not export
21 # names by default without a very good reason. Use EXPORT_OK instead.
22 # Do not simply export all your public functions/methods/constants.
24 # This allows declaration use PVE::RADOS ':all';
25 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
27 our %EXPORT_TAGS = ( 'all' => [ qw(
31 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
40 XSLoader
::load
('PVE::RADOS', $VERSION);
43 my ($fh, $cmd, $data) = @_;
45 local $SIG{PIPE
} = 'IGNORE';
47 my $bin = pack "a L/a*", $cmd, $data || '';
48 my $res = syswrite $fh, $bin;
50 die "write data failed - $!\n" if !defined($res);
58 local $SIG{PIPE
} = 'IGNORE';
60 while (length($head) < 5) {
61 last if !sysread $fh, $head, 5 - length($head), length($head);
63 die "partial read\n" if length($head) < 5;
65 my ($cmd, $len) = unpack "a L", $head;
68 while (length($data) < $len) {
69 last if !sysread $fh, $data, $len - length($data), length($data);
71 die "partial data read\n" if length($data) < $len;
73 return wantarray ?
($cmd, $data) : $data;
76 my $kill_worker = sub {
79 return if !$self->{cpid
};
80 return if $self->{__already_killed
};
82 $self->{__already_killed
} = 1;
84 close($self->{child
}) if defined($self->{child
});
86 kill(9, $self->{cpid
});
87 waitpid($self->{cpid
}, 0);
91 my ($self, $cmd, $data, $expect_tag) = @_;
93 $expect_tag = '>' if !$expect_tag;
97 &$writedata($self->{child
}, $cmd, $data) if $expect_tag ne 'S';
98 ($restag, $raw) = &$readdata($self->{child
});
100 eval { PVE
::Tools
::run_with_timeout
($self->{timeout
}, $code); };
102 &$kill_worker($self);
105 if ($restag eq 'E') {
107 die "unknown error\n";
110 die "got unexpected result\n" if $restag ne $expect_tag;
116 my ($class, %params) = @_;
118 my $rpcenv = PVE
::RPCEnvironment
::get
();
120 socketpair(my $child, my $parent, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
121 || die "socketpair: $!";
125 die "unable to fork - $!\n" if !defined($cpid);
129 my $timeout = delete $params{timeout
} || $rados_default_timeout;
131 $self->{timeout
} = $timeout;
133 if ($cpid) { # parent
136 $self->{cpid
} = $cpid;
137 $self->{child
} = $child;
139 &$sendcmd($self, undef, undef, 'S'); # wait for sync
144 PVE
::INotify
::inotify_close
();
146 if (my $atfork = $rpcenv->{atfork
}) {
156 $conn = pve_rados_create
() ||
157 die "unable to create RADOS object\n";
159 pve_rados_conf_set
($conn, 'client_mount_timeout', $timeout);
161 foreach my $k (keys %params) {
162 pve_rados_conf_set
($conn, $k, $params{$k});
165 pve_rados_connect
($conn);
168 &$writedata($parent, 'E', $err);
171 &$writedata($parent, 'S');
173 $self->{conn
} = $conn;
176 my ($cmd, $data) = &$readdata($parent);
182 if ($cmd eq 'M') { # rados monitor commands
183 $res = pve_rados_mon_command
($self->{conn
}, [ $data ]);
184 } elsif ($cmd eq 'C') { # class methods
185 my $aref = decode_json
($data);
186 my $method = shift @$aref;
187 $res = encode_json
($self->$method(@$aref));
189 die "invalid command\n";
193 &$writedata($parent, 'E', $err);
196 &$writedata($parent, '>', $res);
209 #print "$$: DESTROY WAIT0\n";
210 &$kill_worker($self);
211 #print "$$: DESTROY WAIT\n";
213 #print "$$: DESTROY SHUTDOWN\n";
214 pve_rados_shutdown
($self->{conn
}) if $self->{conn
};
219 my ($self, @args) = @_;
222 my $data = encode_json
(['cluster_stat', @args]);
223 my $raw = &$sendcmd($self, 'C', $data);
224 return decode_json
($raw);
226 return pve_rados_cluster_stat
($self->{conn
});
230 # example1: { prefix => 'get_command_descriptions'})
231 # example2: { prefix => 'mon dump', format => 'json' }
233 my ($self, $cmd) = @_;
235 $cmd->{format
} = 'json' if !$cmd->{format
};
237 my $json = encode_json
($cmd);
239 my $raw = &$sendcmd($self, 'M', $json);
241 if ($cmd->{format
} && $cmd->{format
} eq 'json') {
242 return length($raw) ? decode_json
($raw) : undef;
253 PVE::RADOS - Perl bindings for librados
259 my $rados = PVE::RADOS::new();
260 my $stat = $rados->cluster_stat();
261 my $res = $rados->mon_command({ prefix => 'mon dump', format => 'json' });
265 Perl bindings for librados.
273 Dietmar Maurer, E<lt>dietmar@proxmox.com<gt>