Repository navigation
Expand file tree
/
Copy pathruntime-install-smoke.php
More file actions
194 lines (171 loc) · 5.8 KB
/
Copy pathruntime-install-smoke.php
File metadata and controls
194 lines (171 loc) · 5.8 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
<?php
declare(strict_types=1);
$schema = 'InstallSmoke_' . getmypid();
$semanticDnsStatePath = getenv('KING_SEMANTIC_DNS_STATE_PATH');
function install_smoke_allocate_udp_port(): int
{
$errno = 0;
$errstr = '';
$probe = @stream_socket_server(
'udp://127.0.0.1:0',
$errno,
$errstr,
STREAM_SERVER_BIND
);
if ($probe === false) {
throw new RuntimeException("Failed to allocate UDP smoke port: {$errstr} ({$errno})");
}
$name = stream_socket_get_name($probe, false);
fclose($probe);
if (!is_string($name) || $name === '') {
throw new RuntimeException('Failed to read allocated UDP smoke port.');
}
$separator = strrpos($name, ':');
if ($separator === false) {
throw new RuntimeException("Unexpected UDP socket name: {$name}");
}
$port = (int) substr($name, $separator + 1);
if ($port < 1 || $port > 65535) {
throw new RuntimeException("Invalid allocated UDP smoke port: {$name}");
}
return $port;
}
function install_smoke_default_semantic_dns_state_path(): string
{
return sys_get_temp_dir()
. '/king_install_smoke_semantic_dns_' . getmypid()
. '/semantic_dns_state.bin';
}
function install_smoke_semantic_dns_config(int $dnsPort, ?string $statePath): array
{
return [
'enabled' => true,
'bind_address' => '127.0.0.1',
'dns_port' => $dnsPort,
'default_record_ttl_sec' => 120,
'service_discovery_max_ips_per_response' => 5,
'semantic_mode_enable' => true,
'state_path' => is_string($statePath) && $statePath !== ''
? $statePath
: install_smoke_default_semantic_dns_state_path(),
'mothernode_uri' => 'mother://install-smoke',
'routing_policies' => ['mode' => 'local'],
];
}
function install_smoke_start_semantic_dns(?string $statePath): int
{
$lastError = null;
for ($attempt = 0; $attempt < 8; $attempt++) {
$dnsPort = install_smoke_allocate_udp_port();
if (!king_semantic_dns_init(install_smoke_semantic_dns_config($dnsPort, $statePath))) {
fwrite(STDERR, "Semantic-DNS init smoke failed.\n");
exit(1);
}
try {
if (king_semantic_dns_start_server()) {
return $dnsPort;
}
$lastError = 'king_semantic_dns_start_server returned false';
} catch (Throwable $error) {
$lastError = $error->getMessage();
if (!str_contains($lastError, 'could not bind the UDP DNS listener')) {
throw $error;
}
}
}
fwrite(STDERR, "Semantic-DNS start smoke failed after retries: {$lastError}\n");
exit(1);
}
if (!function_exists('king_connect')) {
fwrite(STDERR, "King extension functions are unavailable.\n");
exit(1);
}
if (!king_proto_define_schema($schema, [
'name' => ['tag' => 3, 'type' => 'string'],
'id' => ['tag' => 1, 'type' => 'int32', 'required' => true],
'enabled' => ['tag' => 2, 'type' => 'bool'],
])) {
fwrite(STDERR, "Failed to define proto smoke schema.\n");
exit(1);
}
$encoded = king_proto_encode($schema, [
'name' => 'install-smoke',
'id' => 7,
'enabled' => true,
]);
$decoded = king_proto_decode($schema, $encoded);
if (!is_array($decoded) || ($decoded['id'] ?? null) !== 7) {
fwrite(STDERR, "Proto smoke roundtrip failed.\n");
exit(1);
}
$root = sys_get_temp_dir() . '/king_install_smoke_' . getmypid();
if (!is_dir($root) && !mkdir($root, 0777, true) && !is_dir($root)) {
fwrite(STDERR, "Failed to create object-store smoke directory.\n");
exit(1);
}
try {
king_object_store_init([
'storage_root_path' => $root,
'max_storage_size_bytes' => 1048576,
'cdn_config' => [
'enabled' => true,
'default_ttl_seconds' => 60,
],
]);
king_object_store_put('install-smoke', 'payload');
if (king_object_store_get('install-smoke') !== 'payload') {
fwrite(STDERR, "Object-store smoke roundtrip failed.\n");
exit(1);
}
if (!king_cdn_cache_object('install-smoke') || !king_object_store_delete('install-smoke')) {
fwrite(STDERR, "Object-store smoke cache/delete failed.\n");
exit(1);
}
} finally {
foreach (scandir($root) ?: [] as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
@unlink($root . DIRECTORY_SEPARATOR . $entry);
}
@rmdir($root);
}
install_smoke_start_semantic_dns(is_string($semanticDnsStatePath) ? $semanticDnsStatePath : null);
if (!king_semantic_dns_register_service([
'service_id' => 'install-smoke',
'service_name' => 'install-smoke',
'service_type' => 'pipeline_orchestrator',
'status' => 'healthy',
'hostname' => 'install.internal',
'port' => 8443,
])) {
fwrite(STDERR, "Semantic-DNS register smoke failed.\n");
exit(1);
}
$discovery = king_semantic_dns_discover_service('pipeline_orchestrator');
$route = king_semantic_dns_get_optimal_route('install-smoke');
$topology = king_semantic_dns_get_service_topology();
if (($discovery['service_count'] ?? 0) < 1
|| ($route['service_id'] ?? null) !== 'install-smoke'
|| ($topology['statistics']['healthy_services'] ?? 0) < 1) {
fwrite(STDERR, "Semantic-DNS smoke verification failed.\n");
exit(1);
}
$session = king_connect('127.0.0.1', 443, [
'sni' => 'install.example',
'alpn' => ['h3'],
]);
if (!is_resource($session)) {
fwrite(STDERR, "Session smoke failed to create a resource.\n");
exit(1);
}
$stats = king_get_stats($session);
if (!is_array($stats) || ($stats['state'] ?? null) !== 'open') {
fwrite(STDERR, "Session smoke failed to read stats.\n");
exit(1);
}
if (!king_poll($session, 0) || !king_close($session)) {
fwrite(STDERR, "Session smoke poll/close failed.\n");
exit(1);
}
echo "install smoke ok\n";