| 1 | package App::Netdisco::Web::API::Queue; | ||
| 2 | |||
| 3 | use Dancer ':syntax'; | ||
| 4 | use Dancer::Plugin::DBIC; | ||
| 5 | use Dancer::Plugin::Swagger; | ||
| 6 | use App::Netdisco::Util::JobAction 'action_is_refused'; | ||
| 7 | use Dancer::Plugin::Auth::Extensible; | ||
| 8 | |||
| 9 | use App::Netdisco::JobQueue 'jq_insert'; | ||
| 10 | use App::Netdisco::Backend::Job; | ||
| 11 | use App::Netdisco::Util::DNS 'ipv4_from_hostname'; | ||
| 12 | use App::Netdisco::Util::Port qw/device_acl_by_role_check/; | ||
| 13 | use NetAddr::IP::Lite; | ||
| 14 | use Try::Tiny; | ||
| 15 | |||
| 16 | swagger_path { | ||
| 17 | tags => ['Queue'], | ||
| 18 | path => (setting('api_base') || '').'/queue/status', | ||
| 19 | description => 'Return counts of jobs by status. queued/running reflect current state; done/failed are optionally scoped by since.', | ||
| 20 | parameters => [ | ||
| 21 | since => { | ||
| 22 | in => 'query', | ||
| 23 | description => 'Limit done/failed counts to jobs finished within this duration. Examples: 1h, 30m, 2d. Default: all time.', | ||
| 24 | required => 0, | ||
| 25 | }, | ||
| 26 | ], | ||
| 27 | responses => { default => {} }, | ||
| 28 | }, get '/api/v1/queue/status' => require_role api_admin => sub { | ||
| 29 | my $since = params->{since} || ''; | ||
| 30 | |||
| 31 | my $since_epoch = undef; | ||
| 32 | if ($since =~ /^(\d+)(m|h|d)$/) { | ||
| 33 | my ($n, $unit) = ($1, $2); | ||
| 34 | my %mul = (m => 60, h => 3600, d => 86400); | ||
| 35 | $since_epoch = time - ($n * $mul{$unit}); | ||
| 36 | } | ||
| 37 | elsif ($since) { | ||
| 38 | send_error('Invalid since format. Use e.g. 30m, 2h, 7d', 400); | ||
| 39 | } | ||
| 40 | |||
| 41 | my $rs = schema(vars->{'tenant'})->resultset('Admin'); | ||
| 42 | |||
| 43 | my $since_filter = $since_epoch | ||
| 44 | ? { finished => { '>=' => \["to_timestamp(?)", $since_epoch] } } | ||
| 45 | : {}; | ||
| 46 | |||
| 47 | my $queued = try { $rs->search({ status => 'queued' })->count } catch { 0 }; | ||
| 48 | my $running = try { $rs->search({ status => 'running' })->count } catch { 0 }; | ||
| 49 | my $done = try { $rs->search({ status => 'done', %$since_filter })->count } catch { 0 }; | ||
| 50 | my $failed = try { $rs->search({ status => 'error', %$since_filter })->count } catch { 0 }; | ||
| 51 | |||
| 52 | return to_json { | ||
| 53 | queued => $queued, | ||
| 54 | running => $running, | ||
| 55 | done => $done, | ||
| 56 | failed => $failed, | ||
| 57 | since => ($since || 'all time'), | ||
| 58 | }; | ||
| 59 | }; | ||
| 60 | |||
| 61 | swagger_path { | ||
| 62 | tags => ['Queue'], | ||
| 63 | path => (setting('api_base') || '').'/queue/backends', | ||
| 64 | description => 'Return list of currently active backend names (usually FQDN)', | ||
| 65 | responses => { default => {} }, | ||
| 66 | }, get '/api/v1/queue/backends' => require_role api_admin => sub { | ||
| 67 | # from 1d988bbf7 this always returns an entry | ||
| 68 | my @names = schema(vars->{'tenant'})->resultset('DeviceSkip') | ||
| 69 | ->get_distinct_col('backend'); | ||
| 70 | |||
| 71 | return to_json \@names; | ||
| 72 | }; | ||
| 73 | |||
| 74 | swagger_path { | ||
| 75 | tags => ['Queue'], | ||
| 76 | path => (setting('api_base') || '').'/queue/jobs', | ||
| 77 | description => 'Return jobs in the queue, optionally filtered by fields', | ||
| 78 | parameters => [ | ||
| 79 | limit => { | ||
| 80 | description => 'Maximum number of Jobs to return', | ||
| 81 | type => 'integer', | ||
| 82 | default => (setting('jobs_qdepth') || 50), | ||
| 83 | }, | ||
| 84 | device => { | ||
| 85 | description => 'IP address field of the Job', | ||
| 86 | }, | ||
| 87 | port => { | ||
| 88 | description => 'Port field of the Job', | ||
| 89 | }, | ||
| 90 | action => { | ||
| 91 | description => 'Action field of the Job', | ||
| 92 | }, | ||
| 93 | status => { | ||
| 94 | description => 'Status field of the Job', | ||
| 95 | }, | ||
| 96 | username => { | ||
| 97 | description => 'Username of the Job submitter', | ||
| 98 | }, | ||
| 99 | userip => { | ||
| 100 | description => 'IP address of the Job submitter', | ||
| 101 | }, | ||
| 102 | backend => { | ||
| 103 | description => 'Backend instance assigned the Job', | ||
| 104 | }, | ||
| 105 | ], | ||
| 106 | responses => { default => {} }, | ||
| 107 | }, get '/api/v1/queue/jobs' => require_role api_admin => sub { | ||
| 108 | my @set = schema(vars->{'tenant'})->resultset('Admin')->search({ | ||
| 109 | ( param('device') ? ( device => param('device') ) : () ), | ||
| 110 | ( param('port') ? ( port => param('port') ) : () ), | ||
| 111 | ( param('action') ? ( action => param('action') ) : () ), | ||
| 112 | ( param('status') ? ( status => param('status') ) : () ), | ||
| 113 | ( param('username') ? ( username => param('username') ) : () ), | ||
| 114 | ( param('userip') ? ( userip => param('userip') ) : () ), | ||
| 115 | ( param('backend') ? ( backend => param('backend') ) : () ), | ||
| 116 | -or => [ | ||
| 117 | { 'log' => undef }, | ||
| 118 | { 'log' => { '-not_like' => 'duplicate of %' } }, | ||
| 119 | ], | ||
| 120 | }, { | ||
| 121 | order_by => { -desc => [qw/entered device action/] }, | ||
| 122 | rows => (param('limit') || setting('jobs_qdepth') || 50), | ||
| 123 | })->with_times->hri->all; | ||
| 124 | |||
| 125 | return to_json \@set; | ||
| 126 | }; | ||
| 127 | |||
| 128 | swagger_path { | ||
| 129 | tags => ['Queue'], | ||
| 130 | path => (setting('api_base') || '').'/queue/jobs', | ||
| 131 | description => 'Delete jobs and skiplist entries, optionally filtered by fields', | ||
| 132 | parameters => [ | ||
| 133 | device => { | ||
| 134 | description => 'IP address field of the Job', | ||
| 135 | }, | ||
| 136 | port => { | ||
| 137 | description => 'Port field of the Job', | ||
| 138 | }, | ||
| 139 | action => { | ||
| 140 | description => 'Action field of the Job', | ||
| 141 | }, | ||
| 142 | status => { | ||
| 143 | description => 'Status field of the Job', | ||
| 144 | }, | ||
| 145 | username => { | ||
| 146 | description => 'Username of the Job submitter', | ||
| 147 | }, | ||
| 148 | userip => { | ||
| 149 | description => 'IP address of the Job submitter', | ||
| 150 | }, | ||
| 151 | backend => { | ||
| 152 | description => 'Backend instance assigned the Job', | ||
| 153 | }, | ||
| 154 | ], | ||
| 155 | responses => { default => {} }, | ||
| 156 | }, del '/api/v1/queue/jobs' => require_role api_admin => sub { | ||
| 157 | my $gone = schema(vars->{'tenant'})->resultset('Admin')->search({ | ||
| 158 | ( param('device') ? ( device => param('device') ) : () ), | ||
| 159 | ( param('port') ? ( port => param('port') ) : () ), | ||
| 160 | ( param('action') ? ( action => param('action') ) : () ), | ||
| 161 | ( param('status') ? ( status => param('status') ) : () ), | ||
| 162 | ( param('username') ? ( username => param('username') ) : () ), | ||
| 163 | ( param('userip') ? ( userip => param('userip') ) : () ), | ||
| 164 | ( param('backend') ? ( backend => param('backend') ) : () ), | ||
| 165 | })->delete; | ||
| 166 | |||
| 167 | schema(vars->{'tenant'})->resultset('DeviceSkip')->search({ | ||
| 168 | ( param('device') ? ( device => param('device') ) : () ), | ||
| 169 | ( param('action') ? ( actionset => { '&&' => \[ 'ARRAY[?]', param('action') ] } ) : () ), | ||
| 170 | ( param('backend') ? ( backend => param('backend') ) : () ), | ||
| 171 | })->delete; | ||
| 172 | |||
| 173 | # DBI reports "no rows" as the true string "0E0", which slips past the || | ||
| 174 | # and reached clients as {"deleted":"0E0"}; numify so it is always a number | ||
| 175 | return to_json { deleted => 0 + ($gone || 0) }; | ||
| 176 | }; | ||
| 177 | |||
| 178 | swagger_path { | ||
| 179 | tags => ['Queue'], | ||
| 180 | path => (setting('api_base') || '').'/queue/jobs', | ||
| 181 | description => 'Submit jobs to the queue', | ||
| 182 | parameters => [ | ||
| 183 | jobs => { | ||
| 184 | description => 'List of job specifications (action, device?, port?, extra?).', | ||
| 185 | default => '[]', | ||
| 186 | schema => { | ||
| 187 | type => 'array', | ||
| 188 | items => { | ||
| 189 | type => 'object', | ||
| 190 | properties => { | ||
| 191 | action => { | ||
| 192 | type => 'string', | ||
| 193 | required => 1, | ||
| 194 | description => 'Job action. Unknown actions are accepted but will fail silently in the backend.', | ||
| 195 | }, | ||
| 196 | device => { | ||
| 197 | type => 'string', | ||
| 198 | required => 0, | ||
| 199 | }, | ||
| 200 | port => { | ||
| 201 | type => 'string', | ||
| 202 | required => 0, | ||
| 203 | }, | ||
| 204 | extra => { | ||
| 205 | type => 'string', | ||
| 206 | required => 0, | ||
| 207 | description => 'Optional job parameter.', | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | }, | ||
| 212 | in => 'body', | ||
| 213 | }, | ||
| 214 | ], | ||
| 215 | responses => { default => {} }, | ||
| 216 | }, post '/api/v1/queue/jobs' => require_any_role [qw(api_admin port_control)] => sub { | ||
| 217 | my $data = request->body || ''; | ||
| 218 | my $jobs = (length $data ? try { from_json($data) } : []); | ||
| 219 | |||
| 220 | send_error('Malformed body', 400) if ref $jobs ne ref []; | ||
| 221 | |||
| 222 | # a custom field job is written to the device as it is queued, with no | ||
| 223 | # worker check phase, so a port_control user is scoped to their role here | ||
| 224 | # as /ajax/portcontrol scopes the same edit from the web | ||
| 225 | my $scoped = not user_has_role('api_admin'); | ||
| 226 | |||
| 227 | foreach my $job (@$jobs) { | ||
| 228 | send_error('Malformed job', 400) if ref $job ne ref {}; | ||
| 229 | send_error('Malformed job', 400) if !defined $job->{action}; | ||
| 230 | send_error('Not Authorized', 403) | ||
| 231 | if ($job->{action} =~ m/^cf_/ and not user_has_role('port_control')) | ||
| 232 | or ($job->{action} !~ m/^cf_/ and not user_has_role('api_admin')); | ||
| 233 | send_error('Action not permitted', 403) | ||
| 234 | if action_is_refused($job->{action}); | ||
| 235 | send_error('Configuration overrides in a port or extra need the' | ||
| 236 | .' api_admin role. Send the port name and extra on their own.', 403) | ||
| 237 | if not user_has_role('api_admin') | ||
| 238 | and App::Netdisco::Backend::Job->carries_config_overrides($job); | ||
| 239 | |||
| 240 | if ($job->{device} and not NetAddr::IP::Lite->new($job->{device})) { | ||
| 241 | my $ip = ipv4_from_hostname($job->{device}) | ||
| 242 | or return send_error("Cannot resolve hostname: $job->{device}", 400); | ||
| 243 | $job->{device} = $ip; | ||
| 244 | } | ||
| 245 | |||
| 246 | # checked on the resolved address, which is what the job will write to | ||
| 247 | if ($scoped) { | ||
| 248 | send_error('Device is outside your port control role', 403) | ||
| 249 | unless device_acl_by_role_check($job->{device}, logged_in_user); | ||
| 250 | } | ||
| 251 | |||
| 252 | $job->{username} = session('logged_in_user'); | ||
| 253 | $job->{userip} = request->remote_address; | ||
| 254 | } | ||
| 255 | |||
| 256 | my $happy = jq_insert($jobs); | ||
| 257 | |||
| 258 | return send_error('Failed to insert jobs - check backend logs', 500) unless $happy; | ||
| 259 | return to_json { success => $happy }; | ||
| 260 | }; | ||
| 261 | |||
| 262 | true; |