| 1 | package WWW::VastAI::API::Instances; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 10 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Instance lifecycle and helper methods for Vast.ai | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use WWW::VastAI::Instance; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | has client => ( | |||||||||||||||||||||||||||||||||||||||||
| 12 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||
| 13 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 14 | weak_ref => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | sub _wrap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_wrap: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | my ($self, $data) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | return WWW::VastAI::Instance->new( | |||||||||||||||||||||||||||||||||||||||||
| 20 | client => $self->client, | ||||||||||||||||||||||||||||||||||||||||||
| 21 | data => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 8 | ▶ | sub _extract_instance { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_instance: called CC=6 SCAR=18.2
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | my ($self, $result) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 8 | ▶ | return $result unless ref $result eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 8 | ▶ | return $result->{instance} if ref $result->{instance} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 8 | ▶ | return $result->{instances} if ref $result->{instances} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 4 | ▶ | return $result->{instances}[0] if ref $result->{instances} eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 4 | ▶ | return $result->{new_contract} if ref $result->{new_contract} eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | ▶ | sub list { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | my $result = $self->client->request_op('listInstances'); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my $instances = ref $result eq 'HASH' ? ($result->{instances} || $result->{results} || []) : ($result || []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition28%2/7
$result->{"instances"} or $result->{"results"} or []
$result->{"instances"} or $result->{"results"}
$result or []
MC/DC0%0/2
$result->{"instances"} or $result->{"results"} or []
$result->{"instances"}
$result->{"results"}
MC/DC0%0/1
$result or []
$result
Truth table33%1/3
A or B
A= $result->{"instances"}B= $result->{"results"}
Truth table0%0/2
A
A= $result
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | return [ map { $self->_wrap($_) } @{$instances} ]; | |||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 4 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 4 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | my $result = $self->client->request_op('getInstance', path => { id => $id }); | |||||||||||||||||||||||||||||||||||||||||
| 49 | 4 | ▶ | my $instance = $self->_extract_instance($result) || $result; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->_extract_instance($result) || $result
MC/DC0%0/2
$self->_extract_instance($result) || $result
$self->_extract_instance($result)
$result
Truth table33%1/3
A || B
A= $self->_extract_instance($result)B= $result
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | return $self->_wrap($instance); | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | sub create { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
create: called CC=8 SCAR=21.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | my ($self, $offer_id, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | croak "offer_id required" unless defined $offer_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | croak "image or template_hash_id required" | ||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | unless $params{image} || $params{template_hash_id}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$params{"image"} or $params{"template_hash_id"}
MC/DC50%1/2
$params{"image"} or $params{"template_hash_id"}
$params{"image"}
$params{"template_hash_id"}
Truth table66%2/3
A or B
A= $params{"image"}B= $params{"template_hash_id"}
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||
| 59 | 2 | my $result = $self->client->request_op( | |||||||||||||||||||||||||||||||||||||||||
| 60 | 'createInstance', | ||||||||||||||||||||||||||||||||||||||||||
| 61 | path => { id => $offer_id }, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | body => \%params, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my $instance = $self->_extract_instance($result); | |||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | ▶ | return $self->_wrap($instance) if $instance; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | my $new_id = ref $result eq 'HASH' ? $result->{new_contract} : $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | return $self->get($new_id) if defined $new_id && !ref $new_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $new_id and not ref $new_id
MC/DC0%0/2
defined $new_id and not ref $new_id
defined $new_id
not ref $new_id
Truth table33%1/3
A and not(B)
A= defined $new_idB= not ref $new_id
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | croak 'create instance did not return an instance payload or contract id'; | |||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 2 | ▶ | sub update { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 2 | my ($self, $id, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 2 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | my $result = $self->client->request_op( | |||||||||||||||||||||||||||||||||||||||||
| 79 | 'manageInstance', | ||||||||||||||||||||||||||||||||||||||||||
| 80 | path => { id => $id }, | ||||||||||||||||||||||||||||||||||||||||||
| 81 | body => \%params, | ||||||||||||||||||||||||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 2 | my $instance = $self->_extract_instance($result); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | return $self->_wrap($instance) if $instance; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | return $self->get($id); | |||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return $self->update($id, state => 'running'); | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub stop { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stop: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | return $self->update($id, state => 'stopped'); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | sub label { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
label: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 1 | my ($self, $id, $label) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | return $self->update($id, label => $label); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
delete: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | return $self->client->request_op('deleteInstance', path => { id => $id }); | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub logs { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logs: called CC=8 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 1 | my ($self, $id, %params) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 1 | my %body = ( | |||||||||||||||||||||||||||||||||||||||||
| 116 | tail => 100, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | timestamps => 0, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | %params, | ||||||||||||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my $result = $self->client->request_op( | |||||||||||||||||||||||||||||||||||||||||
| 122 | 'requestInstanceLogs', | ||||||||||||||||||||||||||||||||||||||||||
| 123 | path => { id => $id }, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | body => \%body, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | if (ref $result eq 'HASH' && $result->{result_url}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $result eq "HASH" and $result->{"result_url"}
MC/DC0%0/2
ref $result eq "HASH" and $result->{"result_url"}
ref $result eq "HASH"
$result->{"result_url"}
Truth table33%1/3
A and B
A= ref $result eq "HASH"B= $result->{"result_url"}
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | my $ua = LWP::UserAgent->new( | |||||||||||||||||||||||||||||||||||||||||
| 129 | agent => 'WWW-VastAI', | ||||||||||||||||||||||||||||||||||||||||||
| 130 | timeout => 30, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $response = $ua->get($result->{result_url}); | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | croak 'Failed to fetch instance logs: ' . $response->status_line | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | unless $response->is_success; | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return $response->decoded_content; | |||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | return ref $result eq 'HASH' ? ($result->{logs} || $result->{result} || $result) : $result; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$result->{"logs"} or $result->{"result"} or $result
$result->{"logs"} or $result->{"result"}
MC/DC0%0/3
$result->{"logs"} or $result->{"result"} or $result
$result->{"logs"}
$result->{"result"}
$result
Truth table25%1/4
A or B or C
A= $result->{"logs"}B= $result->{"result"}C= $result
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | ▶ | sub ssh_keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ssh_keys: called CC=6 SCAR=22.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | my ($self, $id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 1 | my $result = $self->client->request_op('listInstanceSSHKeys', path => { id => $id }); | |||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | return ref $result eq 'HASH' ? ($result->{ssh_keys} || $result->{results} || []) : ($result || []); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition28%2/7
$result->{"ssh_keys"} or $result->{"results"} or []
$result->{"ssh_keys"} or $result->{"results"}
$result or []
MC/DC0%0/2
$result->{"ssh_keys"} or $result->{"results"} or []
$result->{"ssh_keys"}
$result->{"results"}
MC/DC0%0/1
$result or []
$result
Truth table33%1/3
A or B
A= $result->{"ssh_keys"}B= $result->{"results"}
Truth table0%0/2
A
A= $result
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | 1 | ▶ | sub attach_ssh_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
attach_ssh_key: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | 1 | my ($self, $id, $ssh_key) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 1 | ▶ | croak "ssh key required" unless defined $ssh_key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | return $self->client->request_op( | |||||||||||||||||||||||||||||||||||||||||
| 154 | 'attachInstanceSSHKey', | ||||||||||||||||||||||||||||||||||||||||||
| 155 | path => { id => $id }, | ||||||||||||||||||||||||||||||||||||||||||
| 156 | body => { ssh_key => $ssh_key }, | ||||||||||||||||||||||||||||||||||||||||||
| 157 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | sub detach_ssh_key { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
detach_ssh_key: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 1 | my ($self, $id, $ssh_key_id) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | ▶ | croak "instance id required" unless defined $id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | 1 | ▶ | croak "ssh key id required" unless defined $ssh_key_id; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 1 | return $self->client->request_op( | |||||||||||||||||||||||||||||||||||||||||
| 166 | 'detachInstanceSSHKey', | ||||||||||||||||||||||||||||||||||||||||||
| 167 | path => { | ||||||||||||||||||||||||||||||||||||||||||
| 168 | id => $id, | ||||||||||||||||||||||||||||||||||||||||||
| 169 | ssh_key_id => $ssh_key_id, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | __END__ | ||||||||||||||||||||||||||||||||||||||||||