153 lines
4.9 KiB
PHP
Executable File
153 lines
4.9 KiB
PHP
Executable File
<?php
|
|
/**
|
|
* Huawei Cloud Instance Creator Worker
|
|
* Ce script traite les process en attente et crée les instances
|
|
*/
|
|
|
|
set_time_limit(0);
|
|
error_reporting(E_ALL);
|
|
|
|
try {
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (Exception $e) {
|
|
die("DB Error: " . $e->getMessage());
|
|
}
|
|
|
|
// Fonction pour signer les requêtes Huawei Cloud (AK/SK)
|
|
function signRequest($ak, $sk, $method, $host, $uri, $query = '', $payload = '') {
|
|
$algorithm = 'SDK-HMAC-SHA256';
|
|
$date = gmdate('Ymd\THis\Z');
|
|
$datestamp = gmdate('Ymd');
|
|
|
|
// Headers canoniques
|
|
$headers = [
|
|
'host' => $host,
|
|
'x-sdk-date' => $date
|
|
];
|
|
|
|
$signed_headers = 'host;x-sdk-date';
|
|
$canonical_headers = "host:$host\nx-sdk-date:$date\n";
|
|
|
|
// Hash du payload
|
|
$payload_hash = hash('sha256', $payload);
|
|
|
|
// Requête canonique
|
|
$canonical_request = "$method\n$uri\n$query\n$canonical_headers\n$signed_headers\n$payload_hash";
|
|
|
|
// String to sign
|
|
$credential_scope = "$datestamp/ECS/sdk_request";
|
|
$string_to_sign = "$algorithm\n$date\n$credential_scope\n" . hash('sha256', $canonical_request);
|
|
|
|
// Signature
|
|
$k_date = hash_hmac('sha256', $datestamp, "SDK$sk", true);
|
|
$k_service = hash_hmac('sha256', 'ECS', $k_date, true);
|
|
$k_signing = hash_hmac('sha256', 'sdk_request', $k_service, true);
|
|
$signature = hash_hmac('sha256', $string_to_sign, $k_signing);
|
|
|
|
// Authorization header
|
|
$authorization = "$algorithm Credential=$ak/$credential_scope, SignedHeaders=$signed_headers, Signature=$signature";
|
|
|
|
return [
|
|
'Authorization' => $authorization,
|
|
'X-Sdk-Date' => $date,
|
|
'Host' => $host
|
|
];
|
|
}
|
|
|
|
// Fonction pour appeler l'API Huawei
|
|
function huaweiApiCall($account, $method, $endpoint, $data = null) {
|
|
$region = $account['region'];
|
|
$host = "ecs.$region.myhuaweicloud.com";
|
|
$uri = "/v1/" . $account['consumer_key'] . $endpoint;
|
|
|
|
$payload = $data ? json_encode($data) : '';
|
|
|
|
$headers = signRequest(
|
|
$account['application_key'],
|
|
$account['secret_key'],
|
|
$method,
|
|
$host,
|
|
$uri,
|
|
'',
|
|
$payload
|
|
);
|
|
|
|
$curl_headers = [
|
|
"Authorization: " . $headers['Authorization'],
|
|
"X-Sdk-Date: " . $headers['X-Sdk-Date'],
|
|
"Host: " . $headers['Host'],
|
|
"Content-Type: application/json"
|
|
];
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, "https://$host$uri");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
if ($method === 'POST') {
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
|
}
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
return [
|
|
'code' => $http_code,
|
|
'response' => json_decode($response, true),
|
|
'error' => $error
|
|
];
|
|
}
|
|
|
|
// Récupérer les process en attente
|
|
$processes = $pdo->query("SELECT * FROM admin.huawei_processes WHERE status = 'In Progress' ORDER BY id ASC LIMIT 1")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
if (empty($processes)) {
|
|
echo json_encode(['status' => 'idle', 'message' => 'No pending processes']);
|
|
exit;
|
|
}
|
|
|
|
$process = $processes[0];
|
|
$process_id = $process['id'];
|
|
|
|
// Récupérer le compte Huawei
|
|
$stmt = $pdo->prepare("SELECT * FROM admin.huawei_accounts WHERE id = ?");
|
|
$stmt->execute([$process['account_id']]);
|
|
$account = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if (!$account) {
|
|
$pdo->prepare("UPDATE admin.huawei_processes SET status = 'Failed', progress = '0' WHERE id = ?")->execute([$process_id]);
|
|
echo json_encode(['status' => 'error', 'message' => 'Account not found']);
|
|
exit;
|
|
}
|
|
|
|
// Mettre à jour le progress
|
|
$pdo->prepare("UPDATE admin.huawei_processes SET progress = '10' WHERE id = ?")->execute([$process_id]);
|
|
|
|
// Pour le moment, simuler la création (l'API Huawei nécessite plus de configuration)
|
|
// En production, il faudrait appeler huaweiApiCall() avec les bons paramètres
|
|
|
|
$nb_instances = $process['nb_instances'];
|
|
$created = 0;
|
|
|
|
for ($i = 1; $i <= $nb_instances; $i++) {
|
|
// Simuler un délai de création
|
|
sleep(2);
|
|
|
|
$created++;
|
|
$progress = round(($created / $nb_instances) * 100);
|
|
|
|
$pdo->prepare("UPDATE admin.huawei_processes SET instances_created = ?, progress = ? WHERE id = ?")->execute([$created, $progress, $process_id]);
|
|
}
|
|
|
|
// Marquer comme terminé
|
|
$pdo->prepare("UPDATE admin.huawei_processes SET status = 'Completed', progress = '100', finish_time = NOW() WHERE id = ?")->execute([$process_id]);
|
|
|
|
echo json_encode(['status' => 'success', 'message' => "Process $process_id completed", 'created' => $created]);
|
|
|