nieuwe versie
This commit is contained in:
@@ -1,181 +1,130 @@
|
||||
<?php
|
||||
include '../../include/include_header.php';
|
||||
include '../../include/include_functions.php';
|
||||
|
||||
$templatePath = __DIR__ . '/template.json.tpl';
|
||||
$deployDir = __DIR__;
|
||||
|
||||
// --- Node-RED omgevingen ophalen ---
|
||||
$envFile = __DIR__ . '/../../environments.json'; // Pas eventueel pad aan
|
||||
$environments = getNodeRedEnvironments($envFile);
|
||||
$selectedEnv = $_POST['environment'] ?? '';
|
||||
include __DIR__ . '/../../include/include_header.php';
|
||||
include __DIR__ . '/../../include/include_functions.php';
|
||||
|
||||
// --- Parameters ophalen of defaults ---
|
||||
$parameters = [
|
||||
'assetId' => $_POST['assetId'] ?? '',
|
||||
'assetName' => $_POST['assetName'] ?? '',
|
||||
'param1' => $_POST['param1'] ?? '',
|
||||
'param2' => $_POST['param2'] ?? ''
|
||||
'assetId' => $_POST['assetId'] ?? '',
|
||||
'assetName' => $_POST['assetName'] ?? '',
|
||||
'param1' => $_POST['param1'] ?? '',
|
||||
'param2' => $_POST['param2'] ?? ''
|
||||
];
|
||||
|
||||
$deployResponse = '';
|
||||
$newFileName = '';
|
||||
$envUrl = '';
|
||||
// Node-RED omgevingen
|
||||
$envFile = __DIR__ . '/../../settings/environments.json';
|
||||
$environments = getNodeRedEnvironments($envFile);
|
||||
$selectedEnv = $_POST['environment'] ?? '';
|
||||
|
||||
// Alert tonen als template versie hoger is dan laatste deploy
|
||||
// Alert template-versie
|
||||
$newVersionAlert = checkNewTemplateVersionAlert($templatePath, $deployDir);
|
||||
|
||||
// Deploy knop verwerken
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy']) && $selectedEnv) {
|
||||
$templateVersion = getVersionFromTemplate($templatePath);
|
||||
|
||||
$placeholders = [
|
||||
'id' => $parameters['assetId'],
|
||||
'name' => $parameters['assetName'],
|
||||
'version' => $templateVersion,
|
||||
'param1' => $parameters['param1'],
|
||||
'param2' => $parameters['param2']
|
||||
];
|
||||
|
||||
$flowJson = renderTemplate($templatePath, $placeholders);
|
||||
$newFileName = generateNextDeployFileName($templateVersion);
|
||||
file_put_contents($deployDir.'/'.$newFileName, $flowJson);
|
||||
|
||||
// Zoek de URL van de geselecteerde omgeving
|
||||
foreach ($environments as $env) {
|
||||
if (($env['ip'] ?? '') === $selectedEnv) {
|
||||
$envUrl = $env['url'] ?? '';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($envUrl) {
|
||||
$deployResponse = deployFlowToUrl($deployDir.'/'.$newFileName, $envUrl);
|
||||
} else {
|
||||
$deployResponse = 'Geselecteerde omgeving niet gevonden of URL ontbreekt!';
|
||||
}
|
||||
}
|
||||
|
||||
// Overzicht van deploys met versies
|
||||
// Overzicht deploys
|
||||
$deploys = getDeploysWithVersions($deployDir);
|
||||
|
||||
/**
|
||||
* Deploy naar specifieke Node-RED URL
|
||||
*/
|
||||
function deployFlowToUrl(string $flowPath, string $url): string {
|
||||
if (!file_exists($flowPath)) return "Flowbestand niet gevonden: $flowPath";
|
||||
$deployResponse = '';
|
||||
$newFileName = '';
|
||||
$envUrl = '';
|
||||
$successMessage = '';
|
||||
|
||||
$flowJson = file_get_contents($flowPath);
|
||||
// Deploy verwerken
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy'])) {
|
||||
$deployResult = deployFlow($parameters, $templatePath, $deployDir, $environments, $selectedEnv);
|
||||
|
||||
$ch = curl_init(rtrim($url, '/') . '/flows'); // Zorg dat er geen dubbele slashes komen
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $flowJson,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $error ?: $response;
|
||||
if ($deployResult['success']) {
|
||||
$deployResponse = $deployResult['response'];
|
||||
$newFileName = $deployResult['file'];
|
||||
$envUrl = $deployResult['envUrl'];
|
||||
$successMessage = renderAlert($deployResult['message'], 'success');
|
||||
} else {
|
||||
$successMessage = renderAlert($deployResult['message'], 'danger');
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
||||
<main class="container py-5">
|
||||
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1 class="h3 text-dark">Pomp flow</h1>
|
||||
</div>
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1 class="h3 text-dark">Pomp flow</h1>
|
||||
</div>
|
||||
|
||||
<!-- Alert nieuwe template-versie -->
|
||||
<?php if ($newVersionAlert): ?>
|
||||
<?= $newVersionAlert ?>
|
||||
<?php endif; ?>
|
||||
<?php if ($newVersionAlert) echo $newVersionAlert; ?>
|
||||
<?php if ($successMessage) echo $successMessage; ?>
|
||||
<?php if ($deployResponse) echo "<pre class='bg-light border p-2 rounded small'>".htmlspecialchars($deployResponse)."</pre>"; ?>
|
||||
|
||||
<!-- Deploy response -->
|
||||
<?php if ($deployResponse): ?>
|
||||
<?= renderAlert("Nieuwe versie <strong>$newFileName</strong> gedeployed naar <strong>$envUrl</strong>!", 'success') ?>
|
||||
<pre class="bg-light border p-2 rounded small"><?= htmlspecialchars($deployResponse) ?></pre>
|
||||
<?php endif; ?>
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Parameters voor deploy</h5>
|
||||
<form method="post">
|
||||
<div class="mb-3">
|
||||
<label for="assetId" class="form-label">Asset ID</label>
|
||||
<input type="text" class="form-control" id="assetId" name="assetId" value="<?= htmlspecialchars($parameters['assetId']) ?>" required>
|
||||
</div>
|
||||
|
||||
<!-- Parameters form + deploy knop -->
|
||||
<div class="card shadow-sm mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Parameters voor deploy</h5>
|
||||
<div class="mb-3">
|
||||
<label for="assetName" class="form-label">Asset Naam</label>
|
||||
<input type="text" class="form-control" id="assetName" name="assetName" value="<?= htmlspecialchars($parameters['assetName']) ?>" required>
|
||||
</div>
|
||||
|
||||
<form method="post">
|
||||
<div class="mb-3">
|
||||
<label for="assetId" class="form-label">Asset ID</label>
|
||||
<input type="text" class="form-control" id="assetId" name="assetId" value="<?= htmlspecialchars($parameters['assetId']) ?>" required>
|
||||
<div class="mb-3">
|
||||
<label for="param1" class="form-label">Extra parameter 1</label>
|
||||
<input type="text" class="form-control" id="param1" name="param1" value="<?= htmlspecialchars($parameters['param1']) ?>">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="param2" class="form-label">Extra parameter 2</label>
|
||||
<input type="text" class="form-control" id="param2" name="param2" value="<?= htmlspecialchars($parameters['param2']) ?>">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="environment" class="form-label">Selecteer Node-RED omgeving</label>
|
||||
<select class="form-select" id="environment" name="environment" required>
|
||||
<option value="">-- Kies een omgeving --</option>
|
||||
<?php foreach ($environments as $env): ?>
|
||||
<option value="<?= htmlspecialchars($env['ip']) ?>" <?= $selectedEnv === $env['ip'] ? 'selected' : '' ?>>
|
||||
<?= htmlspecialchars($env['name'] ?? 'Onbekend') ?> (<?= htmlspecialchars($env['ip']) ?>)
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit" name="deploy" class="btn btn-primary">Nieuwe versie deployen</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="assetName" class="form-label">Asset Naam</label>
|
||||
<input type="text" class="form-control" id="assetName" name="assetName" value="<?= htmlspecialchars($parameters['assetName']) ?>" required>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="param1" class="form-label">Extra parameter 1</label>
|
||||
<input type="text" class="form-control" id="param1" name="param1" value="<?= htmlspecialchars($parameters['param1']) ?>">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="param2" class="form-label">Extra parameter 2</label>
|
||||
<input type="text" class="form-control" id="param2" name="param2" value="<?= htmlspecialchars($parameters['param2']) ?>">
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label for="environment" class="form-label">Selecteer Node-RED omgeving</label>
|
||||
<select class="form-select" id="environment" name="environment" required>
|
||||
<option value="">-- Kies een omgeving --</option>
|
||||
<?php if (!empty($environments)): ?>
|
||||
<?php foreach($environments as $env): ?>
|
||||
<option value="<?= htmlspecialchars($env['ip'] ?? '') ?>" <?= $selectedEnv === ($env['ip'] ?? '') ? 'selected' : '' ?>>
|
||||
<?= htmlspecialchars($env['name'] ?? 'Onbekend') ?> (<?= htmlspecialchars($env['ip'] ?? '') ?>)
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Overzicht van deploys</h5>
|
||||
<?php if (!empty($deploys)): ?>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover table-bordered mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Bestand</th>
|
||||
<th>Versie</th>
|
||||
<th>Bekijk</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($deploys as $d): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($d['name']) ?></td>
|
||||
<td><?= htmlspecialchars($d['version']) ?></td>
|
||||
<td>
|
||||
<a href="<?= htmlspecialchars($d['name']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary">Bekijk</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<option value="">Geen omgevingen gevonden</option>
|
||||
<p class="text-muted mb-0">Geen deploys gevonden.</p>
|
||||
<?php endif; ?>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<button type="submit" name="deploy" class="btn btn-primary">Nieuwe versie deployen</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Overzicht deploys -->
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title mb-3">Overzicht van deploys</h5>
|
||||
<?php if (!empty($deploys)): ?>
|
||||
<div class="table-responsive">
|
||||
<table class="table table-hover table-bordered mb-0">
|
||||
<thead class="table-light">
|
||||
<tr>
|
||||
<th>Bestand</th>
|
||||
<th>Versie</th>
|
||||
<th>Bekijk</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($deploys as $d): ?>
|
||||
<tr>
|
||||
<td><?= htmlspecialchars($d['name']) ?></td>
|
||||
<td><?= htmlspecialchars($d['version']) ?></td>
|
||||
<td>
|
||||
<a href="<?= htmlspecialchars($d['name']) ?>" target="_blank" class="btn btn-sm btn-outline-secondary">Bekijk</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<p class="text-muted mb-0">Geen deploys gevonden.</p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
@@ -1,59 +1,60 @@
|
||||
[
|
||||
{
|
||||
"id": "inject1",
|
||||
"type": "inject",
|
||||
"name": "Start – {{name}} v{{version}}",
|
||||
"props": [ { "p": "payload" } ],
|
||||
"repeat": "",
|
||||
"crontab": "",
|
||||
"once": false,
|
||||
"onceDelay": 0.1,
|
||||
"topic": "",
|
||||
"payload": "{{id}}",
|
||||
"payloadType": "str",
|
||||
"wires": [["function1"]],
|
||||
"version": 9
|
||||
},
|
||||
{
|
||||
"id": "function1",
|
||||
"type": "function",
|
||||
"name": "Verwerk asset data v{{version}}",
|
||||
"func": "msg.payload = { \"asset_id\": \"{{id}}\", \"asset_name\": \"{{name}}\", \"version\": {{version}}, \"timestamp\": Date.now() }; return msg;",
|
||||
"outputs": 1,
|
||||
"wires": [["change1","debug1","http1"]]
|
||||
},
|
||||
{
|
||||
"id": "change1",
|
||||
"type": "change",
|
||||
"name": "Voeg status toe v{{version}}",
|
||||
"rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ],
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "http1",
|
||||
"type": "http request",
|
||||
"name": "Stuur naar API v{{version}}",
|
||||
"method": "POST",
|
||||
"ret": "txt",
|
||||
"url": "http://localhost:3000/api/asset",
|
||||
"tls": "",
|
||||
"persist": false,
|
||||
"proxy": "",
|
||||
"authType": "",
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "debug1",
|
||||
"type": "debug",
|
||||
"name": "Output debug v{{version}}",
|
||||
"active": true,
|
||||
"tosidebar": true,
|
||||
"console": false,
|
||||
"tostatus": false,
|
||||
"complete": "payload",
|
||||
"targetType": "msg",
|
||||
"statusVal": "",
|
||||
"statusType": "auto",
|
||||
"wires": []
|
||||
}
|
||||
{
|
||||
"id": "inject1",
|
||||
"type": "inject",
|
||||
"name": "Start – {{name}} v{{version}}",
|
||||
"props": [ { "p": "payload" } ],
|
||||
"repeat": "",
|
||||
"crontab": "",
|
||||
"once": false,
|
||||
"onceDelay": 0.1,
|
||||
"topic": "",
|
||||
"payload": "{{id}}",
|
||||
"payloadType": "str",
|
||||
"wires": [["function1"]],
|
||||
"version": 1,
|
||||
"url": "http://{{ip}}:3000/api/asset"
|
||||
},
|
||||
{
|
||||
"id": "function1",
|
||||
"type": "function",
|
||||
"name": "Verwerk asset data v{{version}}",
|
||||
"func": "msg.payload = { \"asset_id\": \"{{id}}\", \"asset_name\": \"{{name}}\", \"version\": {{version}}, \"timestamp\": Date.now() }; return msg;",
|
||||
"outputs": 1,
|
||||
"wires": [["change1","debug1","http1"]]
|
||||
},
|
||||
{
|
||||
"id": "change1",
|
||||
"type": "change",
|
||||
"name": "Voeg status toe v{{version}}",
|
||||
"rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ],
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "http1",
|
||||
"type": "http request",
|
||||
"name": "Stuur naar API v{{version}}",
|
||||
"method": "POST",
|
||||
"ret": "txt",
|
||||
"url": "http://{{ip}}:3000/api/asset",
|
||||
"tls": "",
|
||||
"persist": false,
|
||||
"proxy": "",
|
||||
"authType": "",
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "debug1",
|
||||
"type": "debug",
|
||||
"name": "Output debug v{{version}}",
|
||||
"active": true,
|
||||
"tosidebar": true,
|
||||
"console": false,
|
||||
"tostatus": false,
|
||||
"complete": "payload",
|
||||
"targetType": "msg",
|
||||
"statusVal": "",
|
||||
"statusType": "auto",
|
||||
"wires": []
|
||||
}
|
||||
]
|
||||
|
||||
Binary file not shown.
@@ -1,59 +0,0 @@
|
||||
[
|
||||
{
|
||||
"id": "inject1",
|
||||
"type": "inject",
|
||||
"name": "Start – 1234 v8",
|
||||
"props": [ { "p": "payload" } ],
|
||||
"repeat": "",
|
||||
"crontab": "",
|
||||
"once": false,
|
||||
"onceDelay": 0.1,
|
||||
"topic": "",
|
||||
"payload": "123",
|
||||
"payloadType": "str",
|
||||
"wires": [["function1"]],
|
||||
"version": 8
|
||||
},
|
||||
{
|
||||
"id": "function1",
|
||||
"type": "function",
|
||||
"name": "Verwerk asset data v8",
|
||||
"func": "msg.payload = { \"asset_id\": \"123\", \"asset_name\": \"1234\", \"version\": 8, \"timestamp\": Date.now() }; return msg;",
|
||||
"outputs": 1,
|
||||
"wires": [["change1","debug1","http1"]]
|
||||
},
|
||||
{
|
||||
"id": "change1",
|
||||
"type": "change",
|
||||
"name": "Voeg status toe v8",
|
||||
"rules": [ { "t": "set", "p": "payload.status", "pt": "msg", "to": "active", "tot": "str" } ],
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "http1",
|
||||
"type": "http request",
|
||||
"name": "Stuur naar API v8",
|
||||
"method": "POST",
|
||||
"ret": "txt",
|
||||
"url": "http://localhost:3000/api/asset",
|
||||
"tls": "",
|
||||
"persist": false,
|
||||
"proxy": "",
|
||||
"authType": "",
|
||||
"wires": [["debug1"]]
|
||||
},
|
||||
{
|
||||
"id": "debug1",
|
||||
"type": "debug",
|
||||
"name": "Output debug v8",
|
||||
"active": true,
|
||||
"tosidebar": true,
|
||||
"console": false,
|
||||
"tostatus": false,
|
||||
"complete": "payload",
|
||||
"targetType": "msg",
|
||||
"statusVal": "",
|
||||
"statusType": "auto",
|
||||
"wires": []
|
||||
}
|
||||
]
|
||||
@@ -1,30 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* FlowManager helper functies
|
||||
* FlowManager Helper Functions
|
||||
*
|
||||
* Bevat functies en standaardinstellingen voor het beheren van Node-RED flows.
|
||||
* Inclusief:
|
||||
* - Deploy van flows
|
||||
* - Versiebeheer
|
||||
* - Template rendering
|
||||
* - Omgevingen ophalen
|
||||
*/
|
||||
|
||||
// Deploy een JSON-flow naar Node-RED
|
||||
function deployFlow(string $flowPath): string {
|
||||
if (!file_exists($flowPath)) return "Flowbestand niet gevonden: $flowPath";
|
||||
// ------------------------------------------------------
|
||||
// *** DEFAULT SETTINGS FLOW ***
|
||||
// ------------------------------------------------------
|
||||
|
||||
$flowJson = file_get_contents($flowPath);
|
||||
$url = "http://localhost:1880/flows";
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $flowJson,
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
|
||||
return $error ?: $response;
|
||||
/**
|
||||
* Verkrijg een overzicht van alle Node-RED omgevingen
|
||||
*
|
||||
* @param string $envFile Pad naar JSON-bestand met omgevingen
|
||||
* @return array
|
||||
*/
|
||||
function getNodeRedEnvironments(string $envFile): array {
|
||||
$json = @file_get_contents($envFile);
|
||||
if (!$json) return [];
|
||||
$data = json_decode($json, true);
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
|
||||
// Haal alle versie-bestanden op in een map
|
||||
/**
|
||||
* Vind alle versioned JSON-bestanden in een directory
|
||||
*
|
||||
* @param string $dir
|
||||
* @return array
|
||||
*/
|
||||
function findVersionedJsonFilesInDir(string $dir): array {
|
||||
$matches = [];
|
||||
foreach (scandir($dir) as $file) {
|
||||
@@ -32,35 +40,56 @@ function findVersionedJsonFilesInDir(string $dir): array {
|
||||
$matches[] = ['name' => $file];
|
||||
}
|
||||
}
|
||||
usort($matches, fn($a,$b) => strcmp($a['name'],$b['name']));
|
||||
usort($matches, fn($a, $b) => strcmp($a['name'], $b['name']));
|
||||
return $matches;
|
||||
}
|
||||
|
||||
// Vind het laatst gedeployde bestand
|
||||
/**
|
||||
* Vind het laatst gedeployde bestand
|
||||
*
|
||||
* @param string $dir
|
||||
* @return string|null
|
||||
*/
|
||||
function findLatestDeployFile(string $dir): ?string {
|
||||
$files = findVersionedJsonFilesInDir($dir);
|
||||
return !empty($files) ? end($files)['name'] : null;
|
||||
}
|
||||
|
||||
// Haal versie uit een JSON-bestand
|
||||
/**
|
||||
* Haal versie uit een JSON-bestand
|
||||
*
|
||||
* @param string $filePath
|
||||
* @return int
|
||||
*/
|
||||
function getVersionFromJson(string $filePath): int {
|
||||
if (!file_exists($filePath)) return 0;
|
||||
$json = json_decode(file_get_contents($filePath), true);
|
||||
return $json[0]['version'] ?? 0;
|
||||
}
|
||||
|
||||
// Haal versie uit een template
|
||||
/**
|
||||
* Haal versie uit een template
|
||||
*
|
||||
* @param string $templatePath
|
||||
* @return int
|
||||
*/
|
||||
function getVersionFromTemplate(string $templatePath): int {
|
||||
if (!file_exists($templatePath)) return 0;
|
||||
preg_match('/"version"\s*:\s*(\d+)/', file_get_contents($templatePath), $m);
|
||||
return $m[1] ?? 0;
|
||||
}
|
||||
|
||||
// Controleer of de template een hogere versie heeft dan de laatste deploy
|
||||
/**
|
||||
* Controleer of de template een hogere versie heeft dan de laatste deploy
|
||||
*
|
||||
* @param string $templatePath
|
||||
* @param string $deployDir
|
||||
* @return string|null
|
||||
*/
|
||||
function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string {
|
||||
$templateVersion = getVersionFromTemplate($templatePath);
|
||||
$templateVersion = getVersionFromTemplate($templatePath);
|
||||
$latestDeployFile = findLatestDeployFile($deployDir);
|
||||
$latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0;
|
||||
$latestVersion = $latestDeployFile ? getVersionFromJson($deployDir.'/'.$latestDeployFile) : 0;
|
||||
|
||||
if ($templateVersion > $latestVersion) {
|
||||
return "<div class='alert alert-warning'>
|
||||
@@ -71,12 +100,23 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir):
|
||||
return null;
|
||||
}
|
||||
|
||||
// Genereer de bestandsnaam voor een nieuwe deploy
|
||||
/**
|
||||
* Genereer de bestandsnaam voor een nieuwe deploy
|
||||
*
|
||||
* @param int $version
|
||||
* @return string
|
||||
*/
|
||||
function generateNextDeployFileName(int $version): string {
|
||||
return "version{$version}_".date('YmdHi').".json";
|
||||
}
|
||||
|
||||
// Template renderen met placeholders
|
||||
/**
|
||||
* Render een template met placeholders
|
||||
*
|
||||
* @param string $templatePath
|
||||
* @param array $placeholders
|
||||
* @return string
|
||||
*/
|
||||
function renderTemplate(string $templatePath, array $placeholders): string {
|
||||
$content = file_get_contents($templatePath);
|
||||
foreach ($placeholders as $key => $value) {
|
||||
@@ -85,26 +125,133 @@ function renderTemplate(string $templatePath, array $placeholders): string {
|
||||
return $content;
|
||||
}
|
||||
|
||||
// Render een standaard alert
|
||||
/**
|
||||
* Render een standaard alert
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
function renderAlert(string $message, string $type = 'info'): string {
|
||||
return "<div class='alert alert-{$type}'>$message</div>";
|
||||
}
|
||||
|
||||
// Verkrijg een overzicht van alle deploys inclusief versies
|
||||
/**
|
||||
* Verkrijg een overzicht van alle deploys inclusief versies
|
||||
*
|
||||
* @param string $dir
|
||||
* @return array
|
||||
*/
|
||||
function getDeploysWithVersions(string $dir): array {
|
||||
$files = findVersionedJsonFilesInDir($dir);
|
||||
return array_map(function($f) use ($dir) {
|
||||
return [
|
||||
'name' => $f['name'],
|
||||
'name' => $f['name'],
|
||||
'version' => getVersionFromJson($dir.'/'.$f['name'])
|
||||
];
|
||||
}, array_reverse($files));
|
||||
}
|
||||
|
||||
function getNodeRedEnvironments(string $envFile = '/environments.json'): array {
|
||||
$json = @file_get_contents($envFile); // @ om warnings te onderdrukken
|
||||
if (!$json) return [];
|
||||
$data = json_decode($json, true);
|
||||
return is_array($data) ? $data : [];
|
||||
// ------------------------------------------------------
|
||||
// *** DEPLOY FUNCTIES ***
|
||||
// ------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Deploy een JSON-flowbestand naar een Node-RED URL via POST
|
||||
*
|
||||
* @param string $flowPath Pad naar flow JSON
|
||||
* @param string $url Node-RED URL
|
||||
* @return string HTTP status code of foutmelding
|
||||
*/
|
||||
function deployFlowToUrl(string $flowPath, string $url): string {
|
||||
if (!file_exists($flowPath)) {
|
||||
return '<div class="alert alert-danger" role="alert">
|
||||
<strong>Fout:</strong> Flowbestand niet gevonden: ' . htmlspecialchars($flowPath) . '
|
||||
</div>';
|
||||
}
|
||||
|
||||
$flowJson = file_get_contents($flowPath);
|
||||
|
||||
$ch = curl_init(rtrim($url, '/') . '/flows');
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_POSTFIELDS => $flowJson,
|
||||
]);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
return $httpCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deploy een flow naar een geselecteerde Node-RED omgeving
|
||||
*
|
||||
* @param array $parameters Asset parameters
|
||||
* @param string $templatePath Pad naar template JSON
|
||||
* @param string $deployDir Deploy directory
|
||||
* @param array $environments Beschikbare Node-RED omgevingen
|
||||
* @param string $selectedEnv Geselecteerde omgeving (IP)
|
||||
* @return array Resultaat van de deploy
|
||||
*/
|
||||
function deployFlow(array $parameters, string $templatePath, string $deployDir, array $environments, string $selectedEnv): array {
|
||||
$result = [
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
'file' => null,
|
||||
'envUrl' => null,
|
||||
'response' => null
|
||||
];
|
||||
|
||||
if (empty($selectedEnv)) {
|
||||
$result['message'] = 'Geen omgeving geselecteerd!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Template-versie ophalen
|
||||
$templateVersion = getVersionFromTemplate($templatePath);
|
||||
|
||||
// URL van geselecteerde omgeving
|
||||
$envUrl = '';
|
||||
foreach ($environments as $env) {
|
||||
if (($env['ip'] ?? '') === $selectedEnv) {
|
||||
$envUrl = $env['ip'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$envUrl) {
|
||||
$result['message'] = 'Omgeving niet gevonden of URL ontbreekt!';
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Placeholders invullen
|
||||
$placeholders = [
|
||||
'id' => $parameters['assetId'] ?? '',
|
||||
'name' => $parameters['assetName'] ?? '',
|
||||
'version' => $templateVersion,
|
||||
'param1' => $parameters['param1'] ?? '',
|
||||
'param2' => $parameters['param2'] ?? '',
|
||||
'ip' => $envUrl
|
||||
];
|
||||
|
||||
// Template renderen en deploy-bestand schrijven
|
||||
$flowJson = renderTemplate($templatePath, $placeholders);
|
||||
$newFileName = generateNextDeployFileName($templateVersion);
|
||||
$deployFile = $deployDir . '/' . $newFileName;
|
||||
file_put_contents($deployFile, $flowJson);
|
||||
|
||||
// Deploy naar Node-RED uitvoeren
|
||||
$deployResponse = deployFlowToUrl($deployFile, $envUrl);
|
||||
|
||||
$result['success'] = true;
|
||||
$result['message'] = "Nieuwe versie <strong>$newFileName</strong> gedeployed naar <strong>$envUrl</strong>!";
|
||||
$result['file'] = $newFileName;
|
||||
$result['envUrl'] = $envUrl;
|
||||
$result['response'] = $deployResponse;
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
$base_url = 'http://localhost:8888/rdlab/flowmanager_app/v1.0';
|
||||
$base_url = 'http://localhost:63342/flowmanager';
|
||||
|
||||
?>
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
<div class="collapse navbar-collapse" id="navbarMenu">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="<?php echo $base_url ?>/settings.php">
|
||||
<a class="nav-link" href="/settings">
|
||||
<i class="bi bi-gear-fill"></i> Instellingen
|
||||
</a>
|
||||
</li>
|
||||
|
||||
32
index.php
32
index.php
@@ -1,25 +1,23 @@
|
||||
<?php
|
||||
include 'include/include_header.php';
|
||||
include 'include/include_header.php';
|
||||
?>
|
||||
|
||||
To-do: zorgen dat je juiste flow naar de juiste omgeving gaat
|
||||
|
||||
<div class="container my-5">
|
||||
<div class="row row-cols-1 row-cols-md-3 g-4">
|
||||
<div class="row row-cols-1 row-cols-md-3 g-4">
|
||||
|
||||
<!-- Kaart 1 -->
|
||||
<div class="col">
|
||||
<div class="card h-100 p-3">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">
|
||||
<i class="bi bi-file-earmark-text me-2" style="font-size: 1.5rem;"></i>
|
||||
Template Pomp
|
||||
</h5>
|
||||
<p class="card-text">Korte beschrijving van Template 1.</p>
|
||||
<a href="flows/pomp" class="btn btn-primary btn-sm">Bekijk</a>
|
||||
<!-- Kaart 1 -->
|
||||
<div class="col">
|
||||
<div class="card h-100 p-3">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">
|
||||
<i class="bi bi-file-earmark-text me-2" style="font-size: 1.5rem;"></i>
|
||||
Template Pomp
|
||||
</h5>
|
||||
<p class="card-text">Korte beschrijving van Template 1.</p>
|
||||
<a href="flows/pomp" class="btn btn-primary btn-sm">Bekijk</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[
|
||||
{
|
||||
"name": "localhost",
|
||||
"ip": "127.0.0.1"
|
||||
"ip": "127.0.0.1:1880"
|
||||
}
|
||||
]
|
||||
@@ -1,5 +1,5 @@
|
||||
<?php
|
||||
include 'include/include_header.php';
|
||||
include '../include/include_header.php';
|
||||
|
||||
// Laad opgeslagen omgevingen (optioneel uit een JSON-bestand)
|
||||
$env_file = 'environments.json';
|
||||
Reference in New Issue
Block a user