Files
wevads-platform/scripts/huawei-instances-create.php
2026-02-26 04:53:11 +01:00

380 lines
20 KiB
PHP
Executable File

<?php
session_start();
$base_url = 'http://89.167.40.150:5821';
try {
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
die("Database connection failed: " . $e->getMessage());
}
// Récupérer les comptes Huawei actifs
$huawei_accounts = $pdo->query("SELECT id, name, region, application_key, secret_key, consumer_key FROM admin.huawei_accounts WHERE status = 'Activated' ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
// Récupérer les comptes DNS (Namecheap, GoDaddy, Cloudflare)
$dns_accounts = [];
try {
$stmt = $pdo->query("SELECT id, name, 'namecheap' as type FROM admin.namecheap_accounts WHERE status = 'Activated'");
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $dns_accounts[] = $row; }
} catch (Exception $e) {}
try {
$stmt = $pdo->query("SELECT id, name, 'godaddy' as type FROM admin.godaddy_accounts WHERE status = 'Activated'");
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $dns_accounts[] = $row; }
} catch (Exception $e) {}
try {
$stmt = $pdo->query("SELECT id, name, 'cloudflare' as type FROM admin.cloudflare_accounts WHERE status = 'Activated'");
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) { $dns_accounts[] = $row; }
} catch (Exception $e) {}
// Régions Huawei
$regions = [
'af-south-1' => 'Africa - Johannesburg',
'ap-southeast-1' => 'Asia Pacific - Hong Kong',
'ap-southeast-2' => 'Asia Pacific - Bangkok',
'ap-southeast-3' => 'Asia Pacific - Singapore',
'eu-west-0' => 'Europe - Paris',
'la-north-2' => 'Latin America - Mexico City',
'sa-brazil-1' => 'South America - Sao Paulo'
];
// Types d'instances
$instance_types = [
't6.small.1' => 't6.small.1 (1 vCPU, 1GB RAM)',
't6.medium.2' => 't6.medium.2 (1 vCPU, 2GB RAM)',
't6.large.2' => 't6.large.2 (2 vCPU, 4GB RAM)',
's6.small.1' => 's6.small.1 (1 vCPU, 2GB RAM)',
's6.medium.2' => 's6.medium.2 (1 vCPU, 4GB RAM)',
's6.large.2' => 's6.large.2 (2 vCPU, 8GB RAM)'
];
// Systèmes d'exploitation
$os_list = [
'Ubuntu 20.04 server 64bit' => 'Ubuntu 20.04 LTS',
'Ubuntu 22.04 server 64bit' => 'Ubuntu 22.04 LTS',
'CentOS 7.9 64bit' => 'CentOS 7.9',
'Debian 11.0.0 64bit' => 'Debian 11'
];
// Traitement AJAX
if (isset($_GET['action'])) {
header('Content-Type: application/json');
if ($_GET['action'] === 'get_account_info') {
$account_id = $_POST['account_id'] ?? '';
$stmt = $pdo->prepare("SELECT * FROM admin.huawei_accounts WHERE id = ?");
$stmt->execute([$account_id]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if ($account) {
echo json_encode(['status' => 'success', 'data' => $account]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Account not found']);
}
exit;
}
if ($_GET['action'] === 'create_instances') {
$account_id = $_POST['account_id'] ?? '';
$nb_instances = intval($_POST['nb_instances'] ?? 1);
$region = $_POST['region'] ?? '';
$instance_type = $_POST['instance_type'] ?? '';
$os = $_POST['os'] ?? '';
$domains = $_POST['domains'] ?? '';
// Validation
if (empty($account_id) || empty($region) || empty($instance_type) || empty($os)) {
echo json_encode(['status' => 'error', 'message' => 'Missing required fields']);
exit;
}
// Récupérer les credentials du compte
$stmt = $pdo->prepare("SELECT * FROM admin.huawei_accounts WHERE id = ?");
$stmt->execute([$account_id]);
$account = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$account) {
echo json_encode(['status' => 'error', 'message' => 'Account not found']);
exit;
}
// Créer un process de création
$process_id = 'HW_' . time() . '_' . rand(1000, 9999);
try {
$stmt = $pdo->prepare("INSERT INTO admin.huawei_processes (process_id, status, account_id, account_name, region, nb_instances, instances_created, instances_installed, progress, start_time, created_by, created_date) VALUES (?, 'In Progress', ?, ?, ?, ?, 0, 0, 0, NOW(), 'admin', CURRENT_DATE)");
$stmt->execute([$process_id, $account_id, $account['name'], $region, $nb_instances]);
echo json_encode([
'status' => 'success',
'message' => "Process started! Creating $nb_instances instance(s) in $region",
'process_id' => $process_id
]);
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => 'Database error: ' . $e->getMessage()]);
}
exit;
}
if ($_GET['action'] === 'get_processes') {
$processes = $pdo->query("SELECT * FROM admin.huawei_processes ORDER BY id DESC LIMIT 20")->fetchAll(PDO::FETCH_ASSOC);
echo json_encode(['status' => 'success', 'data' => $processes]);
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>WEVAL APP | Create Huawei Instances</title>
<meta content="width=device-width, initial-scale=1" name="viewport" />
<script src="https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js"></script>
<script>WebFont.load({google: {"families":["Poppins:300,400,500,600,700"]}});</script>
<link href="<?php echo $base_url; ?>/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<link href="<?php echo $base_url; ?>/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
<link rel="shortcut icon" href="<?php echo $base_url; ?>/images/logos/favicon.ico" />
<style>
body { background: #f1f5f9 !important; font-family: 'Poppins', sans-serif; }
.page-container { max-width: 1400px; margin: 0 auto; padding: 20px; }
.back-link { color: #e53935; text-decoration: none; display: inline-flex; align-items: center; margin-bottom: 20px; }
.back-link:hover { color: #c62828; }
.back-link i { margin-right: 8px; }
.page-header { background: #ffffff; border-radius: 8px; padding: 25px; margin-bottom: 25px; border: 1px solid #e2e8f0; }
.page-header h1 { color: #e53935; margin: 0; font-size: 24px; }
.page-header p { color: #64748b; margin: 5px 0 0 0; }
.card-light { background: #ffffff; border-radius: 8px; border: 1px solid #e2e8f0; margin-bottom: 20px; }
.card-light .card-header { padding: 15px 20px; border-bottom: 1px solid #e2e8f0; background: #f8fafc; }
.card-light .card-header h4 { margin: 0; color: #1e293b; font-size: 16px; }
.card-light .card-header h4 i { color: #e53935; margin-right: 10px; }
.card-light .card-body { padding: 20px; }
.form-row { display: flex; flex-wrap: wrap; margin: 0 -10px; }
.form-col { padding: 0 10px; margin-bottom: 15px; }
.form-col-2 { width: 16.66%; }
.form-col-3 { width: 25%; }
.form-col-4 { width: 33.33%; }
.form-group label { color: #1e293b; font-weight: 500; margin-bottom: 8px; display: block; font-size: 12px; }
.form-control { border: 1px solid #e2e8f0; border-radius: 6px; padding: 8px 12px; width: 100%; font-size: 13px; }
.form-control:focus { border-color: #e53935; box-shadow: 0 0 0 3px rgba(229,57,53,0.1); outline: none; }
select.form-control { height: 38px; }
.btn-create { background: linear-gradient(135deg, #e53935 0%, #c62828 100%); color: #fff; border: none; padding: 10px 25px; border-radius: 6px; font-weight: 600; cursor: pointer; }
.btn-create:hover { background: linear-gradient(135deg, #c62828 0%, #b71c1c 100%); color: #fff; }
.btn-create:disabled { background: #ccc; cursor: not-allowed; }
.account-info { background: #f0fdf4; border: 1px solid #10b981; border-radius: 6px; padding: 10px 15px; margin-bottom: 15px; display: none; }
.account-info.show { display: block; }
.account-info span { margin-right: 20px; font-size: 12px; color: #065f46; }
.account-info strong { color: #1e293b; }
table { width: 100%; border-collapse: collapse; }
th { background: #f8fafc; color: #1e293b; font-weight: 600; padding: 10px; text-align: left; border-bottom: 2px solid #e2e8f0; font-size: 12px; }
td { padding: 10px; border-bottom: 1px solid #e2e8f0; color: #475569; font-size: 12px; }
.badge { padding: 4px 8px; border-radius: 4px; font-size: 11px; font-weight: 600; }
.badge-progress { background: #fef3c7; color: #92400e; }
.badge-completed { background: #d1fae5; color: #065f46; }
.badge-failed { background: #fee2e2; color: #991b1b; }
.empty-state { text-align: center; padding: 40px; color: #64748b; }
.alert-box { padding: 12px 15px; border-radius: 6px; margin-bottom: 15px; }
.alert-success { background: #d1fae5; border: 1px solid #10b981; color: #065f46; }
.alert-error { background: #fee2e2; border: 1px solid #ef4444; color: #991b1b; }
.alert-warning { background: #fef3c7; border: 1px solid #f59e0b; color: #92400e; }
</style>
</head>
<body>
<div class="page-container">
<a href="<?php echo $base_url; ?>/huawei-accounts.php" class="back-link"><i class="fa fa-arrow-left"></i> Back to Huawei Accounts</a>
<div class="page-header">
<h1><i class="fa fa-server"></i> Create Huawei Cloud Instances</h1>
<p>Deploy new ECS instances on Huawei Cloud</p>
</div>
<?php if (count($huawei_accounts) == 0): ?>
<div class="alert-box alert-warning">
<i class="fa fa-exclamation-triangle"></i> <strong>No Huawei accounts configured!</strong>
<a href="huawei-accounts-add.php">Add a Huawei account first</a>
</div>
<?php endif; ?>
<div class="card-light">
<div class="card-header"><h4><i class="fa fa-plus-circle"></i> Instance Configuration</h4></div>
<div class="card-body">
<div id="account-info" class="account-info">
<span><i class="fa fa-user"></i> Account: <strong id="info-name">-</strong></span>
<span><i class="fa fa-globe"></i> Region: <strong id="info-region">-</strong></span>
<span><i class="fa fa-key"></i> AK: <strong id="info-ak">-</strong></span>
</div>
<form id="create-form">
<div class="form-row">
<div class="form-col form-col-3">
<div class="form-group">
<label><i class="fa fa-cloud"></i> Huawei Account</label>
<select id="account_id" name="account_id" class="form-control" required>
<option value="">-- Select Account --</option>
<?php foreach ($huawei_accounts as $acc): ?>
<option value="<?php echo $acc['id']; ?>" data-region="<?php echo $acc['region']; ?>"><?php echo htmlspecialchars($acc['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-col form-col-2">
<div class="form-group">
<label><i class="fa fa-sort-numeric-asc"></i> Nb of Instances</label>
<input type="number" id="nb_instances" name="nb_instances" class="form-control" value="1" min="1" max="10">
</div>
</div>
<div class="form-col form-col-3">
<div class="form-group">
<label><i class="fa fa-globe"></i> Region</label>
<select id="region" name="region" class="form-control" required>
<option value="">-- Select Region --</option>
<?php foreach ($regions as $code => $name): ?>
<option value="<?php echo $code; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-col form-col-2">
<div class="form-group">
<label><i class="fa fa-microchip"></i> Instance Type</label>
<select id="instance_type" name="instance_type" class="form-control" required>
<?php foreach ($instance_types as $code => $name): ?>
<option value="<?php echo $code; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-col form-col-2">
<div class="form-group">
<label><i class="fa fa-linux"></i> Operating System</label>
<select id="os" name="os" class="form-control" required>
<?php foreach ($os_list as $code => $name): ?>
<option value="<?php echo $code; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
</div>
<div class="form-row">
<div class="form-col form-col-3">
<div class="form-group">
<label><i class="fa fa-dns"></i> DNS Account (Optional)</label>
<select id="dns_account" name="dns_account" class="form-control">
<option value="">-- None --</option>
<?php foreach ($dns_accounts as $dns): ?>
<option value="<?php echo $dns['type'] . '|' . $dns['id']; ?>"><?php echo ucfirst($dns['type']) . ': ' . htmlspecialchars($dns['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-col form-col-4">
<div class="form-group">
<label><i class="fa fa-globe"></i> Domains (comma separated)</label>
<input type="text" id="domains" name="domains" class="form-control" placeholder="domain1.com, domain2.com">
</div>
</div>
<div class="form-col form-col-2" style="display: flex; align-items: flex-end;">
<button type="submit" id="btn-create" class="btn-create" <?php echo count($huawei_accounts) == 0 ? 'disabled' : ''; ?>>
<i class="fa fa-rocket"></i> Create Instance(s)
</button>
</div>
</div>
</form>
<div id="create-result"></div>
</div>
</div>
<div class="card-light">
<div class="card-header"><h4><i class="fa fa-list"></i> Creation Processes</h4></div>
<div class="card-body">
<div id="processes-list">
<div class="empty-state"><i class="fa fa-spinner fa-spin"></i> Loading processes...</div>
</div>
</div>
</div>
</div>
<script src="<?php echo $base_url; ?>/plugins/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Charger les processus
loadProcesses();
setInterval(loadProcesses, 10000);
// Quand on sélectionne un compte
$('#account_id').on('change', function() {
var accountId = $(this).val();
var region = $(this).find(':selected').data('region');
if (accountId) {
$.post('?action=get_account_info', {account_id: accountId}, function(r) {
if (r.status === 'success') {
$('#info-name').text(r.data.name);
$('#info-region').text(r.data.region);
$('#info-ak').text(r.data.application_key.substring(0, 12) + '...');
$('#account-info').addClass('show');
$('#region').val(r.data.region);
}
}, 'json');
} else {
$('#account-info').removeClass('show');
}
});
// Soumettre le formulaire
$('#create-form').on('submit', function(e) {
e.preventDefault();
var btn = $('#btn-create');
btn.html('<i class="fa fa-spinner fa-spin"></i> Creating...').prop('disabled', true);
$('#create-result').html('<div class="alert-box alert-warning"><i class="fa fa-spinner fa-spin"></i> Initiating instance creation...</div>');
$.post('?action=create_instances', $(this).serialize(), function(r) {
if (r.status === 'success') {
$('#create-result').html('<div class="alert-box alert-success"><i class="fa fa-check-circle"></i> ' + r.message + '</div>');
loadProcesses();
} else {
$('#create-result').html('<div class="alert-box alert-error"><i class="fa fa-times-circle"></i> ' + r.message + '</div>');
}
}, 'json').fail(function() {
$('#create-result').html('<div class="alert-box alert-error"><i class="fa fa-times-circle"></i> Connection error</div>');
}).always(function() {
btn.html('<i class="fa fa-rocket"></i> Create Instance(s)').prop('disabled', false);
});
});
function loadProcesses() {
$.get('?action=get_processes', function(r) {
if (r.status === 'success' && r.data.length > 0) {
var html = '<table><thead><tr><th>ID</th><th>Process ID</th><th>Account</th><th>Region</th><th>Status</th><th>Progress</th><th>Created</th><th>Installed</th><th>Start Time</th><th>Actions</th></tr></thead><tbody>';
r.data.forEach(function(p) {
var badge = 'badge-progress';
if (p.status === 'Completed') badge = 'badge-completed';
if (p.status === 'Failed') badge = 'badge-failed';
html += '<tr>';
html += '<td>' + p.id + '</td>';
html += '<td><code>' + p.process_id + '</code></td>';
html += '<td>' + (p.account_name || '-') + '</td>';
html += '<td>' + (p.region || '-') + '</td>';
html += '<td><span class="badge ' + badge + '">' + p.status + '</span></td>';
html += '<td>' + (p.progress || 0) + '%</td>';
html += '<td>' + (p.instances_created || 0) + '/' + (p.nb_instances || 0) + '</td>';
html += '<td>' + (p.instances_installed || 0) + '</td>';
html += '<td>' + (p.start_time || '-') + '</td>';
html += '<td><a href="#" onclick="deleteProcess(' + p.id + '); return false;" class="btn-action btn-delete" style="background:#fee2e2;color:#991b1b;padding:4px 8px;border-radius:4px;text-decoration:none;"><i class="fa fa-trash"></i></a></td>';
html += '</tr>';
});
html += '</tbody></table>';
$('#processes-list').html(html);
} else {
$('#processes-list').html('<div class="empty-state"><i class="fa fa-inbox"></i> No processes yet</div>');
}
}, 'json');
}
});
</script>
<?php include("includes/chatbot-widget.php"); ?>
</body>
</html>
<!-- Fonction suppression ajoutée via script -->