]>
git.proxmox.com Git - librados2-perl.git/blob - PVE/RADOS.pm
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);
45 my ($fh, $cmd, $data) = @_;
47 local $SIG{PIPE
} = 'IGNORE';
49 my $bin = pack "a L/a*", $cmd, $data || '';
50 my $res = syswrite $fh, $bin;
52 die "write data failed - $!\n" if !defined($res);
60 local $SIG{PIPE
} = 'IGNORE';
62 while (length($head) < 5) {
63 last if !sysread $fh, $head, 5 - length($head), length($head);
65 die "partial read\n" if length($head) < 5;
67 my ($cmd, $len) = unpack "a L", $head;
70 while (length($data) < $len) {
71 last if !sysread $fh, $data, $len - length($data), length($data);
73 die "partial data read\n" if length($data) < $len;
75 return wantarray ?
($cmd, $data) : $data;
78 my $kill_worker = sub {
81 return if !$self->{cpid
};
82 return if $self->{__already_killed
};
84 $self->{__already_killed
} = 1;
86 close($self->{child
}) if defined($self->{child
});
88 kill(9, $self->{cpid
});
89 waitpid($self->{cpid
}, 0);
93 my ($self, $cmd, $data, $expect_tag) = @_;
95 $expect_tag = '>' if !$expect_tag;
99 &$writedata($self->{child
}, $cmd, $data) if $expect_tag ne 'S';
100 ($restag, $raw) = &$readdata($self->{child
});
102 eval { PVE
::Tools
::run_with_timeout
($self->{timeout
}, $code); };
104 &$kill_worker($self);
107 if ($restag eq 'E') {
109 die "unknown error\n";
112 die "got unexpected result\n" if $restag ne $expect_tag;
118 my ($class, %params) = @_;
120 my $rpcenv = PVE
::RPCEnvironment
::get
();
122 socketpair(my $child, my $parent, AF_UNIX
, SOCK_STREAM
, PF_UNSPEC
)
123 || die "socketpair: $!";
127 die "unable to fork - $!\n" if !defined($cpid);
131 my $timeout = delete $params{timeout
} || $rados_default_timeout;
133 $self->{timeout
} = $timeout;
135 if ($cpid) { # parent
138 $self->{cpid
} = $cpid;
139 $self->{child
} = $child;
141 &$sendcmd($self, undef, undef, 'S'); # wait for sync
146 PVE
::INotify
::inotify_close
();
148 if (my $atfork = $rpcenv->{atfork
}) {
158 $conn = pve_rados_create
() ||
159 die "unable to create RADOS object\n";
161 pve_rados_conf_set
($conn, 'client_mount_timeout', $timeout);
163 foreach my $k (keys %params) {
164 pve_rados_conf_set
($conn, $k, $params{$k});
167 pve_rados_connect
($conn);
170 &$writedata($parent, 'E', $err);
173 &$writedata($parent, 'S');
175 $self->{conn
} = $conn;
178 my ($cmd, $data) = &$readdata($parent);
184 if ($cmd eq 'M') { # rados monitor commands
185 $res = pve_rados_mon_command
($self->{conn
}, [ $data ]);
186 } elsif ($cmd eq 'C') { # class methods
187 my $aref = decode_json
($data);
188 my $method = shift @$aref;
189 $res = encode_json
($self->$method(@$aref));
191 die "invalid command\n";
195 &$writedata($parent, 'E', $err);
198 &$writedata($parent, '>', $res);
211 #print "$$: DESTROY WAIT0\n";
212 &$kill_worker($self);
213 #print "$$: DESTROY WAIT\n";
215 #print "$$: DESTROY SHUTDOWN\n";
216 pve_rados_shutdown
($self->{conn
}) if $self->{conn
};
221 my ($self, @args) = @_;
224 my $data = encode_json
(['cluster_stat', @args]);
225 my $raw = &$sendcmd($self, 'C', $data);
226 return decode_json
($raw);
228 return pve_rados_cluster_stat
($self->{conn
});
232 # example1: { prefix => 'get_command_descriptions'})
233 # example2: { prefix => 'mon dump', format => 'json' }
235 my ($self, $cmd) = @_;
237 $cmd->{format
} = 'json' if !$cmd->{format
};
239 my $json = encode_json
($cmd);
241 my $raw = &$sendcmd($self, 'M', $json);
243 if ($cmd->{format
} && $cmd->{format
} eq 'json') {
244 return length($raw) ? decode_json
($raw) : undef;
255 PVE::RADOS - Perl bindings for librados
261 my $rados = PVE::RADOS::new();
262 my $stat = $rados->cluster_stat();
263 my $res = $rados->mon_command({ prefix => 'mon dump', format => 'json' });
267 Perl bindings for librados.
275 Dietmar Maurer, E<lt>dietmar@proxmox.com<gt>