nieuwe versie 0.1
This commit is contained in:
@@ -14,15 +14,60 @@
|
||||
// *** DEFAULT SETTINGS FLOW ***
|
||||
// ------------------------------------------------------
|
||||
|
||||
// --- Parameters ophalen of defaults ---
|
||||
$parameters = [
|
||||
'assetId' => $_POST['assetId'] ?? '',
|
||||
'assetName' => $_POST['assetName'] ?? '',
|
||||
'param1' => $_POST['param1'] ?? '',
|
||||
'param2' => $_POST['param2'] ?? ''
|
||||
];
|
||||
|
||||
// Node-RED omgevingen
|
||||
$envFile = __DIR__ . '/../settings/environments.json';
|
||||
$environments = getNodeRedEnvironments($envFile);
|
||||
$selectedEnv = $_POST['environment'] ?? '';
|
||||
|
||||
// Alert template-versie
|
||||
$newVersionAlert = checkNewTemplateVersionAlert($templatePath, $deployDir);
|
||||
|
||||
// Overzicht deploys
|
||||
$deploys = getDeploysWithVersions($deployDir);
|
||||
|
||||
// Init variabelen
|
||||
$deployResponse = '';
|
||||
$newFileName = '';
|
||||
$envUrl = '';
|
||||
$successMessage = '';
|
||||
|
||||
// Deploy verwerken
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['deploy'])) {
|
||||
$deployResult = deployFlow($parameters, $templatePath, $deployDir, $environments, $selectedEnv);
|
||||
|
||||
if ($deployResult['success']) {
|
||||
$deployResponse = $deployResult['response'];
|
||||
$newFileName = $deployResult['file'];
|
||||
$envUrl = $deployResult['envUrl'];
|
||||
$successMessage = renderAlert($deployResult['message'], 'success');
|
||||
} else {
|
||||
$successMessage = renderAlert($deployResult['message'], 'danger');
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------
|
||||
// *** HELPERS ***
|
||||
// ------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Verkrijg een overzicht van alle Node-RED omgevingen
|
||||
*
|
||||
* @param string $envFile Pad naar JSON-bestand met omgevingen
|
||||
* @return array
|
||||
*/
|
||||
function getNodeRedEnvironments(string $envFile): array {
|
||||
function getNodeRedEnvironments(string $envFile): array
|
||||
{
|
||||
$json = @file_get_contents($envFile);
|
||||
if (!$json) return [];
|
||||
|
||||
$data = json_decode($json, true);
|
||||
return is_array($data) ? $data : [];
|
||||
}
|
||||
@@ -33,13 +78,15 @@ function getNodeRedEnvironments(string $envFile): array {
|
||||
* @param string $dir
|
||||
* @return array
|
||||
*/
|
||||
function findVersionedJsonFilesInDir(string $dir): array {
|
||||
function findVersionedJsonFilesInDir(string $dir): array
|
||||
{
|
||||
$matches = [];
|
||||
foreach (scandir($dir) as $file) {
|
||||
if (preg_match('/^version\d+_\d{12}\.json$/', $file)) {
|
||||
$matches[] = ['name' => $file];
|
||||
}
|
||||
}
|
||||
|
||||
usort($matches, fn($a, $b) => strcmp($a['name'], $b['name']));
|
||||
return $matches;
|
||||
}
|
||||
@@ -50,7 +97,8 @@ function findVersionedJsonFilesInDir(string $dir): array {
|
||||
* @param string $dir
|
||||
* @return string|null
|
||||
*/
|
||||
function findLatestDeployFile(string $dir): ?string {
|
||||
function findLatestDeployFile(string $dir): ?string
|
||||
{
|
||||
$files = findVersionedJsonFilesInDir($dir);
|
||||
return !empty($files) ? end($files)['name'] : null;
|
||||
}
|
||||
@@ -61,8 +109,10 @@ function findLatestDeployFile(string $dir): ?string {
|
||||
* @param string $filePath
|
||||
* @return int
|
||||
*/
|
||||
function getVersionFromJson(string $filePath): 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;
|
||||
}
|
||||
@@ -73,8 +123,10 @@ function getVersionFromJson(string $filePath): int {
|
||||
* @param string $templatePath
|
||||
* @return int
|
||||
*/
|
||||
function getVersionFromTemplate(string $templatePath): 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;
|
||||
}
|
||||
@@ -86,10 +138,11 @@ function getVersionFromTemplate(string $templatePath): int {
|
||||
* @param string $deployDir
|
||||
* @return string|null
|
||||
*/
|
||||
function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string {
|
||||
function checkNewTemplateVersionAlert(string $templatePath, string $deployDir): ?string
|
||||
{
|
||||
$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'>
|
||||
@@ -97,6 +150,7 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir):
|
||||
(Template v$templateVersion > Laatste deploy v$latestVersion)
|
||||
</div>";
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -106,8 +160,9 @@ function checkNewTemplateVersionAlert(string $templatePath, string $deployDir):
|
||||
* @param int $version
|
||||
* @return string
|
||||
*/
|
||||
function generateNextDeployFileName(int $version): string {
|
||||
return "version{$version}_".date('YmdHi').".json";
|
||||
function generateNextDeployFileName(int $version): string
|
||||
{
|
||||
return "version{$version}_" . date('YmdHi') . ".json";
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -117,11 +172,13 @@ function generateNextDeployFileName(int $version): string {
|
||||
* @param array $placeholders
|
||||
* @return string
|
||||
*/
|
||||
function renderTemplate(string $templatePath, array $placeholders): string {
|
||||
function renderTemplate(string $templatePath, array $placeholders): string
|
||||
{
|
||||
$content = file_get_contents($templatePath);
|
||||
foreach ($placeholders as $key => $value) {
|
||||
$content = str_replace("{{{$key}}}", $value, $content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
@@ -132,7 +189,8 @@ function renderTemplate(string $templatePath, array $placeholders): string {
|
||||
* @param string $type
|
||||
* @return string
|
||||
*/
|
||||
function renderAlert(string $message, string $type = 'info'): string {
|
||||
function renderAlert(string $message, string $type = 'info'): string
|
||||
{
|
||||
return "<div class='alert alert-{$type}'>$message</div>";
|
||||
}
|
||||
|
||||
@@ -142,12 +200,14 @@ function renderAlert(string $message, string $type = 'info'): string {
|
||||
* @param string $dir
|
||||
* @return array
|
||||
*/
|
||||
function getDeploysWithVersions(string $dir): array {
|
||||
function getDeploysWithVersions(string $dir): array
|
||||
{
|
||||
$files = findVersionedJsonFilesInDir($dir);
|
||||
return array_map(function($f) use ($dir) {
|
||||
|
||||
return array_map(function ($f) use ($dir) {
|
||||
return [
|
||||
'name' => $f['name'],
|
||||
'version' => getVersionFromJson($dir.'/'.$f['name'])
|
||||
'version' => getVersionFromJson($dir . '/' . $f['name'])
|
||||
];
|
||||
}, array_reverse($files));
|
||||
}
|
||||
@@ -163,7 +223,8 @@ function getDeploysWithVersions(string $dir): array {
|
||||
* @param string $url Node-RED URL
|
||||
* @return string HTTP status code of foutmelding
|
||||
*/
|
||||
function deployFlowToUrl(string $flowPath, string $url): string {
|
||||
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) . '
|
||||
@@ -197,7 +258,8 @@ function deployFlowToUrl(string $flowPath, string $url): string {
|
||||
* @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 {
|
||||
function deployFlow(array $parameters, string $templatePath, string $deployDir, array $environments, string $selectedEnv): array
|
||||
{
|
||||
$result = [
|
||||
'success' => false,
|
||||
'message' => '',
|
||||
|
||||
Reference in New Issue
Block a user