forked from os-autoinst/os-autoinst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmapi.pm
278 lines (201 loc) · 7.62 KB
/
mmapi.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# Copyright 2015-2021 SUSE LLC
# SPDX-License-Identifier: GPL-2.0-or-later
## Multi-Machine API
package mmapi;
use Mojo::Base 'Exporter', -signatures;
our @EXPORT = qw(get_children_by_state get_children get_parents
get_job_info get_job_autoinst_url get_job_autoinst_vars
wait_for_children wait_for_children_to_start api_call
api_call_2 handle_api_error get_current_job_id
);
require bmwqemu;
use Mojo::UserAgent;
use Mojo::URL;
our $retry_count = $ENV{OS_AUTOINST_MMAPI_RETRY_COUNT} // 30;
our $retry_interval = $ENV{OS_AUTOINST_MMAPI_RETRY_INTERVAL} // 10;
our $poll_interval = $ENV{OS_AUTOINST_MMAPI_POLL_INTERVAL} // 1;
our $url;
# private ua
my $ua;
my $app;
# define HTTP return codes which are not treated as errors by api_call/api_call_2/handle_api_error
my $CODES_EXPECTED_BY_DEFAULT = {200 => 1, 409 => 1};
# define HTTP return codes which are not treated as errors by functions of mmapi itself
my $CODES_EXPECTED_BY_MMAPI = {200 => 1};
sub _init () {
# init $ua and $url
my $host = $bmwqemu::vars{OPENQA_URL};
my $secret = $bmwqemu::vars{JOBTOKEN};
return unless $host && $secret;
$url = Mojo::URL->new($host =~ '/' ? $host : "http://$host");
# Relative paths are appended to the existing one
$url->path('/api/v1/');
$ua = Mojo::UserAgent->new;
# add JOBTOKEN header secret
$ua->on(start => sub ($ua, $tx) { $tx->req->headers->add('X-API-JobToken' => $secret) });
}
sub set_app ($app_arg) {
_init;
$ua->server->app($app = $app_arg);
}
=head2 api_call_2
Queries openQA's multi-machine API and returns the resulting Mojo::Transaction::HTTP object.
=cut
sub api_call_2 ($method, $action, $params = undef, $expected_codes = undef) {
_init unless $ua;
bmwqemu::mydie('Missing mandatory options') unless $method && $action && $ua;
my $ua_url = $url->clone;
$ua_url->path($action);
$ua_url->query($params) if $params;
my $tries = $retry_count;
my ($tx, $res);
while ($tries--) {
$tx = $ua->$method($ua_url);
$res = $tx->res;
last if $res->code && ($expected_codes // $CODES_EXPECTED_BY_DEFAULT)->{$res->code};
bmwqemu::diag("api_call_2 failed, retries left: $tries of $retry_count");
sleep $retry_interval;
}
return $tx;
}
=head2 api_call
Queries openQA's multi-machine API and returns the result as Mojo::Message::Response object.
=cut
sub api_call (@args) { api_call_2(@args)->res }
=head2 handle_api_error
Returns a truthy value if the specified Mojo::Transaction::HTTP object has an error.
Logs the errors if a log context is specified.
=cut
sub handle_api_error ($tx, $log_ctx, $expected_codes) {
my $err = $tx->error;
return 0 unless $err;
my $url = $tx->req->url;
my $code = $err->{code};
return 0 if $code && ($expected_codes // $CODES_EXPECTED_BY_DEFAULT)->{$code};
$err->{message} .= "; URL was $url" if $url;
bmwqemu::diag($code
? "$log_ctx: $code response: $err->{message}"
: "$log_ctx: Connection error: $err->{message}") if $log_ctx;
return 1;
}
=head2 get_children_by_state
my $children = get_children_by_state('done');
print $children->[0]
Returns an array ref containing ids of children in given state.
=cut
sub get_children_by_state ($state) {
my $tx = api_call_2(get => "mm/children/$state", undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'get_children_by_state', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/jobs');
}
=head2 get_children
my $children = get_children();
print keys %$children;
Returns a hash ref containing { id => state } pair for each child job.
=cut
sub get_children () {
my $tx = api_call_2(get => 'mm/children', undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'get_children', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/jobs');
}
=head2 get_parents
my $parents = get_parents
print $parents->[0]
Returns an array ref containing ids of parent jobs.
=cut
sub get_parents () {
my $tx = api_call_2(get => 'mm/parents', undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'get_parents', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/jobs');
}
=head2 get_job_info
my $info = get_job_info($target_id);
print $info->{settings}->{DESKTOP}
Returns a hash containin job information provided by openQA server.
=cut
sub get_job_info ($target_id) {
my $tx = api_call_2(get => "jobs/$target_id", undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'get_job_info', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/job');
}
=head2 get_job_autoinst_url
my $url = get_job_autoinst_url($target_id);
Returns url of os-autoinst webserver for job $target_id or C<undef> on failure.
=cut
sub get_job_autoinst_url ($target_id) {
my $tx = api_call_2(get => 'workers', undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'get_job_autoinst_url', $CODES_EXPECTED_BY_MMAPI);
my $workers = $tx->res->json('/workers') // [];
for my $worker (@$workers) {
if ($worker->{jobid} && $target_id == $worker->{jobid} && $worker->{host} && $worker->{instance} && $worker->{properties}{JOBTOKEN}) {
my $hostname = $worker->{host};
my $token = $worker->{properties}{JOBTOKEN};
my $workerport = $worker->{instance} * 10 + 20002 + 1; # the same as in openqa/script/worker
my $url = "http://$hostname:$workerport/$token";
return $url;
}
}
bmwqemu::diag("get_job_autoinst_url: No worker info for job $target_id available.");
return undef;
}
=head2 get_job_autoinst_vars
my $vars = get_job_autoinst_vars($target_id);
print $vars->{WORKER_ID};
Returns hash reference containing variables of job $target_id or C<undef> on failure. The variables
are taken from vars.json file of the corresponding worker.
=cut
sub get_job_autoinst_vars ($target_id) {
my $url = get_job_autoinst_url($target_id);
return undef unless $url;
# query the os-autoinst webserver of the job specified by $target_id
$url .= '/vars';
my $ua = Mojo::UserAgent->new;
$ua->server->app($app) if $app;
my $tx = $ua->get($url);
return undef if handle_api_error($tx, 'get_job_autoinst_vars', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/vars');
}
=head2 wait_for_children
wait_for_children();
Wait while any running or scheduled children exist.
=cut
sub wait_for_children () {
while (1) {
my $children = get_children() // die 'Failed to wait for children';
my $n = 0;
for my $state (values %$children) {
next if $state eq 'done' or $state eq 'cancelled';
$n++;
}
bmwqemu::diag("Waiting for $n jobs to finish");
last unless $n;
sleep $poll_interval;
}
}
=head2 wait_for_children_to_start
wait_for_children_to_start();
Wait while any scheduled children exist.
=cut
sub wait_for_children_to_start () {
while (1) {
my $children = get_children() // die 'Failed to wait for children to start';
my $n = 0;
for my $state (values %$children) {
next if $state eq 'done' or $state eq 'cancelled' or $state eq 'running';
$n++;
}
bmwqemu::diag("Waiting for $n jobs to start");
last unless $n;
sleep $poll_interval;
}
}
=head2 get_current_job_id
get_current_job_id();
Query openQA's API to retrieve the current job ID
=cut
sub get_current_job_id () {
my $tx = api_call_2(get => 'whoami', undef, $CODES_EXPECTED_BY_MMAPI);
return undef if handle_api_error($tx, 'whoami', $CODES_EXPECTED_BY_MMAPI);
return $tx->res->json('/id');
}
1;