File Coverage

blib/lib/WWW/Docker/API/System.pm
Criterion Covered Total %
statement 26 27 96.3
branch 4 8 50.0
condition n/a
subroutine 8 8 100.0
pod 2 5 40.0
total 40 48 83.3


line stmt bran cond sub pod time code
1             package WWW::Docker::API::System;
2             # ABSTRACT: Docker Engine System API
3              
4 10     10   79 use Moo;
  10         27  
  10         74  
5 10     10   4396 use Carp qw( croak );
  10         74  
  10         822  
6 10     10   5883 use namespace::clean;
  10         179995  
  10         75  
7              
8             our $VERSION = '0.101';
9              
10             =head1 SYNOPSIS
11              
12             my $docker = WWW::Docker->new;
13              
14             # System information
15             my $info = $docker->system->info;
16             say "Docker version: " . $info->{ServerVersion};
17              
18             # API version
19             my $version = $docker->system->version;
20             say "API version: " . $version->{ApiVersion};
21              
22             # Health check
23             my $pong = $docker->system->ping;
24              
25             # Monitor events
26             my $events = $docker->system->events(
27             since => time() - 3600,
28             );
29              
30             # Disk usage
31             my $df = $docker->system->df;
32              
33             =head1 DESCRIPTION
34              
35             This module provides access to Docker system-level operations including daemon
36             information, version detection, health checks, and event monitoring.
37              
38             Accessed via C<< $docker->system >>.
39              
40             =cut
41              
42             has client => (
43             is => 'ro',
44             required => 1,
45             weak_ref => 1,
46             );
47              
48             =attr client
49              
50             Reference to L client. Weak reference to avoid circular dependencies.
51              
52             =cut
53              
54             sub info {
55 1     1 0 1563 my ($self) = @_;
56 1         16 return $self->client->get('/info');
57             }
58              
59             =method info
60              
61             my $info = $system->info;
62              
63             Get system-wide information about the Docker daemon.
64              
65             Returns hashref with keys including:
66              
67             =over
68              
69             =item * C - Docker version
70              
71             =item * C - Total number of containers
72              
73             =item * C - Total number of images
74              
75             =item * C - Storage driver
76              
77             =item * C - Total memory
78              
79             =back
80              
81             =cut
82              
83             sub version {
84 1     1 1 1536 my ($self) = @_;
85 1         15 return $self->client->get('/version');
86             }
87              
88             =method version
89              
90             my $version = $system->version;
91              
92             Get version information about the Docker daemon and API.
93              
94             Returns hashref with keys including C, C, C,
95             C, C, and C.
96              
97             =cut
98              
99             sub ping {
100 1     1 0 74 my ($self) = @_;
101 1         15 my $result = $self->client->get('/_ping');
102 1         24 return $result;
103             }
104              
105             =method ping
106              
107             my $pong = $system->ping;
108              
109             Health check endpoint. Returns C string if daemon is responsive.
110              
111             =cut
112              
113             sub events {
114 2     2 1 88 my ($self, %opts) = @_;
115 2         7 my $callback = delete $opts{callback};
116 2         6 my %params;
117 2 50       11 $params{since} = $opts{since} if defined $opts{since};
118 2 50       10 $params{until} = $opts{until} if defined $opts{until};
119 2 50       7 $params{filters} = $opts{filters} if defined $opts{filters};
120 2 50       7 if ($callback) {
121 0         0 return $self->client->stream_get('/events',
122             params => \%params,
123             callback => $callback,
124             );
125             }
126 2         29 return $self->client->get('/events', params => \%params);
127             }
128              
129             =method events
130              
131             # Bounded query (since+until) — returns arrayref of event hashrefs:
132             my $events = $system->events(
133             since => 1234567890,
134             until => 1234567900,
135             filters => { type => ['container'] },
136             );
137              
138             # Real-time streaming — invokes callback for each event as it arrives:
139             $system->events(
140             filters => { type => ['container'] },
141             callback => sub {
142             my ($event) = @_;
143             printf "Event: %s %s\n", $event->{Type}, $event->{Action};
144             },
145             );
146              
147             Get real-time events from the Docker daemon.
148              
149             When C is provided, events are read incrementally from the socket and
150             the callback is invoked once per event as JSON objects arrive. This is required
151             for long-lived (unbounded) streams; without a C the response body is
152             buffered in memory, which is only safe when C bounds the response.
153              
154             Options:
155              
156             =over
157              
158             =item * C - Show events created since this timestamp
159              
160             =item * C - Show events created before this timestamp
161              
162             =item * C - Hashref of filters (e.g., C<< { type => ['container', 'image'] } >>)
163              
164             =item * C - CodeRef invoked with each decoded event hashref (enables streaming mode)
165              
166             =back
167              
168             =cut
169              
170             sub df {
171 1     1 0 25 my ($self) = @_;
172 1         10 return $self->client->get('/system/df');
173             }
174              
175             =method df
176              
177             my $usage = $system->df;
178              
179             Get data usage information (disk usage by images, containers, and volumes).
180              
181             Returns hashref with C, C, C, and C arrays.
182              
183             =cut
184              
185             =seealso
186              
187             =over
188              
189             =item * L - Main Docker client
190              
191             =back
192              
193             =cut
194              
195             1;