This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 512
/
index.php
125 lines (107 loc) · 3.76 KB
/
index.php
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
<?php
declare(strict_types=1);
// useful when script is being executed by cron user
$pathPrefix = ''; // e.g. /usr/share/nginx/oci-arm-host-capacity/
require "{$pathPrefix}vendor/autoload.php";
use Dotenv\Dotenv;
use Hitrov\Exception\ApiCallException;
use Hitrov\FileCache;
use Hitrov\OciApi;
use Hitrov\OciConfig;
use Hitrov\TooManyRequestsWaiter;
$envFilename = empty($argv[1]) ? '.env' : $argv[1];
$dotenv = Dotenv::createUnsafeImmutable(__DIR__, $envFilename);
$dotenv->safeLoad();
/*
* No need to modify any value in this file anymore!
* Copy .env.example to .env and adjust there instead.
*
* README.md now has all the information.
*/
$config = new OciConfig(
getenv('OCI_REGION'),
getenv('OCI_USER_ID'),
getenv('OCI_TENANCY_ID'),
getenv('OCI_KEY_FINGERPRINT'),
getenv('OCI_PRIVATE_KEY_FILENAME'),
getenv('OCI_AVAILABILITY_DOMAIN') ?: null, // null or '' or 'jYtI:PHX-AD-1' or ['jYtI:PHX-AD-1','jYtI:PHX-AD-2']
getenv('OCI_SUBNET_ID'),
getenv('OCI_IMAGE_ID'),
(int) getenv('OCI_OCPUS'),
(int) getenv('OCI_MEMORY_IN_GBS')
);
$bootVolumeSizeInGBs = (string) getenv('OCI_BOOT_VOLUME_SIZE_IN_GBS');
$bootVolumeId = (string) getenv('OCI_BOOT_VOLUME_ID');
if ($bootVolumeSizeInGBs) {
$config->setBootVolumeSizeInGBs($bootVolumeSizeInGBs);
} elseif ($bootVolumeId) {
$config->setBootVolumeId($bootVolumeId);
}
$api = new OciApi();
if (getenv('CACHE_AVAILABILITY_DOMAINS')) {
$api->setCache(new FileCache($config));
}
if (getenv('TOO_MANY_REQUESTS_TIME_WAIT')) {
$api->setWaiter(new TooManyRequestsWaiter((int) getenv('TOO_MANY_REQUESTS_TIME_WAIT')));
}
$notifier = (function (): \Hitrov\Interfaces\NotifierInterface {
/*
* if you have own https://core.telegram.org/bots
* and set TELEGRAM_BOT_API_KEY and your TELEGRAM_USER_ID in .env
*
* then you can get notified when script will succeed.
* otherwise - don't mind OR develop you own NotifierInterface
* to e.g. send SMS or email.
*/
return new \Hitrov\Notification\Telegram();
})();
$shape = getenv('OCI_SHAPE');
$maxRunningInstancesOfThatShape = 1;
if (getenv('OCI_MAX_INSTANCES') !== false) {
$maxRunningInstancesOfThatShape = (int) getenv('OCI_MAX_INSTANCES');
}
$instances = $api->getInstances($config);
$existingInstances = $api->checkExistingInstances($config, $instances, $shape, $maxRunningInstancesOfThatShape);
if ($existingInstances) {
echo "$existingInstances\n";
return;
}
if (!empty($config->availabilityDomains)) {
if (is_array($config->availabilityDomains)) {
$availabilityDomains = $config->availabilityDomains;
} else {
$availabilityDomains = [ $config->availabilityDomains ];
}
} else {
$availabilityDomains = $api->getAvailabilityDomains($config);
}
foreach ($availabilityDomains as $availabilityDomainEntity) {
$availabilityDomain = is_array($availabilityDomainEntity) ? $availabilityDomainEntity['name'] : $availabilityDomainEntity;
try {
$instanceDetails = $api->createInstance($config, $shape, getenv('OCI_SSH_PUBLIC_KEY'), $availabilityDomain);
} catch(ApiCallException $e) {
$message = $e->getMessage();
echo "$message\n";
// if ($notifier->isSupported()) {
// $notifier->notify($message);
// }
if (
$e->getCode() === 500 &&
strpos($message, 'InternalError') !== false &&
strpos($message, 'Out of host capacity') !== false
) {
// trying next availability domain
sleep(16);
continue;
}
// current config is broken
return;
}
// success
$message = json_encode($instanceDetails, JSON_PRETTY_PRINT);
echo "$message\n";
if ($notifier->isSupported()) {
$notifier->notify($message);
}
return;
}