-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathvastAiUtils.php
More file actions
executable file
·254 lines (210 loc) · 6.94 KB
/
vastAiUtils.php
File metadata and controls
executable file
·254 lines (210 loc) · 6.94 KB
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
<?php
use DBA\Factory;
// exists to make it easier to reference the json objects
// from within the template
class VastAiGPUArrayClass {
private $internalArray = array();
public function __construct($array) {
$this->internalArray = $array;
}
public function getUptime() {
if (! $this->getVal('start_date')) {
return null;
}
$start = (int) $this->getVal('start_date');
$start = round($start, 0);
$current = time();
$delta = $current - $start;
$days = floor($delta / (60 * 60 * 24));
$delta %= (60 * 60 * 24);
$hours = floor($delta / (60 * 60));
$delta %= (60 * 60);
$minutes = floor($delta / 60);
$seconds = $delta % 60;
$duration = '';
if ($days > 0) {
$duration .= $days . ' day' . ($days > 1 ? 's' : '') . ' ';
}
$duration .= sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds);
return $duration;
}
public function getFloatValOld($key, $precision) {
return round(floatval($this->internalArray[$key]), $precision);
}
public function getStartingVoucher() {
return $this->getVal('extra_env')[1][1];
}
public function getFloatval($key, $precision) {
$number = $this->getVal($key);
$retNumber = (float) number_format($number,0,'.','');
for ($n = 0; $n <= 8; $n++) {
if ($retNumber === 0.0){
$retNumber = (double) number_format($number,$n,'.','');
} else {
return number_format($number,$n+(max($precision-2,0)),'.','');
}
}
return "<0.00000000";
}
public function getFloatVal100($key, $precision) {
return round(floatval($this->internalArray[$key])*100, $precision);
}
public function getVal($key) {
if (array_key_exists($key, $this->internalArray)) {
return $this->internalArray[$key];
} else {
return null;
}
}
}
function vastAiDestroyInstance($apiKey, $id) {
$url = 'https://console.vast.ai/api/v0/instances/' . $id . '/?api_key=' . $apiKey;
$headers = array('Accept: application/json');
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => "DELETE"
);
$ch = curl_init($url);
$options[CURLOPT_HTTPHEADER] = $headers;
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
if($response === false) {
return 'vastaiAgentSearch curl error: ' . curl_error($ch);
}
return json_decode($response, true);
}
function makeVastaiVoucher($id){
$sub = substr(md5(rand()),0,10);
$voucher = $randomSubStr = 'vast' . $sub . $id;
AgentUtils::createVoucher($voucher);
return $voucher;
}
// retrieves the instances that the account is currently renting
function getVastAiAgents($apiKey) {
$url = 'https://console.vast.ai/api/v0/instances?api_key=' . $apiKey;
$headers = array('Accept: application/json');
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
);
$ch = curl_init($url);
$options[CURLOPT_HTTPHEADER] = $headers;
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
if($response === false) {
return'vastaiAgentSearch curl error: ' . curl_error($ch);
}
return json_decode($response, true);
}
// search current GPUs, this does not require an api key
// an example query input value would be
// $query = '{"verified": {"eq": true}, "external": {"eq": false}, "rentable": {"eq": true}, "compute_cap": {"gt": "600"}, "disk_space": {"gt": "10000"}, "order": [["score", "desc"]], "type": "on-demand"}';
function searchGpus($query) {
$url = 'https://console.vast.ai/api/v0/bundles?q=' . urlencode($query);
$headers = array('Accept: application/json');
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
);
$ch = curl_init($url);
$options[CURLOPT_HTTPHEADER] = $headers;
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);
if($response === false) {
return'vastaiAgentSearch curl error: ' . curl_error($ch);
}
return json_decode($response, true);
}
function rentMachine($apiKey, $id, $imageUrl, $price, $disk, $voucher, $baseUrl, $imageLogin) {
// API endpoint
$url = 'https://console.vast.ai/api/v0/asks/' . $id . '/?api_key=' . $apiKey;
if ( $imageLogin === ''){
$imageLogin = null;
}
// Data to be sent
$data = array(
"client_id" => "me",
"image" => $imageUrl,
"env" => array(
"TZ" => "UTC",
"VOUCHER" => $voucher,
"HCATURL" => $baseUrl
),
"disk" => (int) $disk,
"label" => "instance-" . $id,
"extra" => null,
"image_login" => $imageLogin,
"onstart" => "/root/hcat/run.sh",
"runtype" => "ssh",
"python_utf8" => false,
"lang_utf8" => false,
"use_jupyter_lab" => false,
"jupyter_dir" => null
);
// Initialize cURL session
$ch = curl_init();
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
// Execute the cURL request
$response = curl_exec($ch);
// REMOVE THIS LINE, for debugging
curl_close($ch);
// Check for any errors
if ($response === false) {
return 'cURL error: ' . curl_error($ch);
}
// Close cURL session
return json_decode($response, true);
}
function doesVoucherExist($voucher){
$vouchers = Factory::getRegVoucherFactory()->filter([]);
foreach ($vouchers as $voucher){
$voucherValue = $voucher->getVoucher();
if (str_contains($voucherValue, "vast.ai") == false){
continue;
}
$index = strpos($voucherValue, "-");
$voucherInstanceId = substr($voucherValue, $index);
if ($voucherInstanceId === $intsanceId){
return $voucherValue;
}
}
return null;
}
// given an instanceId it goes through all vouchers
// containing the substr vast.ai and returns the
// voucher that contains the instanceId
function getVoucherForInstance($instanceId){
$vouchers = Factory::getRegVoucherFactory()->filter([]);
foreach ($vouchers as $voucher){
$voucherValue = $voucher->getVoucher();
if (str_contains($voucherValue, "vast.ai") == false){
continue;
}
$index = strpos($voucherValue, "-");
$voucherInstanceId = substr($voucherValue, $index);
if ($voucherInstanceId === $intsanceId){
return $voucherValue;
}
}
return null;
}
function get_or($arr,$key,$default){
if (!$arr[$key]){
return $default;
}
return $arr[$key];
}
?>