Updated deployment (auto-managed by each feature's own implementation)

This commit is contained in:
xdrm-brackets 2017-09-28 17:25:24 +02:00
parent de64b1eb6d
commit 85d4930e85
3 changed files with 203 additions and 105 deletions

3
feature/rfid-read/deploy Normal file
View File

@ -0,0 +1,3 @@
#!/bin/sh
/usr/bin/env php $(realpath $(dirname $0))/source/deploy.php;

View File

@ -0,0 +1,155 @@
<?php
require_once __DIR__.'/../../../include/php/const';
$FEATURE = basename(dirname(__DIR__));
function rfid_read_deploy($sync){
global $FEATURE;
/* [1] Fetch api response data
=========================================================*/
/* (1) Try to read response */
$file_r = @file_get_contents(TMP_DIR."/$FEATURE");
/* (2) Check response */
if( $file_r === false ){
slog("Cannot find $FEATURE's response data file", "$FEATURE:deploy", 'update');
return 127;
}
/* (3) Try to parse response */
$arr_r = json_decode($file_r, true);
/* (4) Check parse error */
if( is_null($arr_r) || !is_array($arr_r) ){
slog("Cannot parse $FEATURE's response data file", "$FEATURE:deploy", 'update');
return 127;
}
/* [2] Get system states (global_state)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['states']) || !is_array($arr_r['states']) ){
slog('No \'states\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(STATES_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(STATES_CONF, 'w');
foreach($arr_r['states'] as $state)
$f->fwrite( json_encode([
$state['global_state'],
$state['chips']
]).PHP_EOL );
$f = null;
/* [3] Get chip list (chips)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) ){
slog('No \'chips\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(CHIPS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(CHIPS_CONF, 'w');
foreach($arr_r['chips'] as $chip)
$f->fwrite( json_encode([
(int) $chip['position'],
$chip['pins'],
$chip['states']
]).PHP_EOL );
$f = null;
/* [4] Get auth list (permissions)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['permissions']) || !is_array($arr_r['permissions']) ){
slog('No \'permissions\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(AUTH_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(AUTH_CONF, 'w');
foreach($arr_r['permissions'] as $code=>$perm)
$f->fwrite( json_encode([
$code,
(int) $perm['id_user'],
$perm['actions']
]).PHP_EOL );
$f = null;
/* [5] Get action list (with timeout)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['actions']) || !is_array($arr_r['actions']) ){
slog('No \'actions\' found in response file', "$FEATURE:deploy", 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(ACTIONS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(ACTIONS_CONF, 'w');
foreach($arr_r['actions'] as $timeout=>$actions)
foreach($actions as $action)
$f->fwrite( json_encode([
(int) $timeout,
(int) $action['id_action'],
$action['required'],
$action['action']
]).PHP_EOL );
$f = null;
return 0;
}
/* Manage sync vs. sync */
$is_sync = false;
if( $argc > 1 && $argv[1] == 'sync' )
$is_sync = true;
$exec = rfid_read_deploy($is_sync);
if( $exec == 0 ) slog('Success', "$FEATURE:deploy", 'update');
else slog('Failure', "$FEATURE:deploy", 'update');
echo $exec;
die($exec);
?>

View File

@ -19,122 +19,61 @@
$arr_r = json_decode($file_r, true); $arr_r = json_decode($file_r, true);
/* (4) Check parse error */ /* (4) Check parse error */
if( is_null($arr_r) ){ if( is_null($arr_r) || !isset($arr_r['feature']) ){
slog('Cannot parse api.response file', 'api:deploy', 'update'); slog('Cannot parse api.response file', 'api:deploy', 'update');
return 127; return 127;
} }
/* [2] Get system states (global_state)
/* [2] Get the list of daemons (features)
=========================================================*/ =========================================================*/
/* (1) Check data */ $feature = [];
if( !isset($arr_r['states']) || !is_array($arr_r['states']) ){ $f = new SplFileObject(ETREE_CONF, 'r');
slog('No \'states\' found in api.response file', 'api:deploy', 'update');
return 127; while( !$f->eof() ){
/* (1) Try to parse each line */
$etree = trim($f->fgets(), "\n");
/* (2) Manage error */
if( is_null($etree) || strlen($etree) == 0 )
continue;
/* (3) Add entry to log */
$feature[] = $etree;
} }
/* (2) Reset file */
file_put_contents(STATES_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(STATES_CONF, 'w');
foreach($arr_r['states'] as $state)
$f->fwrite( json_encode([
$state['global_state'],
$state['chips']
]).PHP_EOL );
$f = null;
/* [3] Get chip list (chips) /* [3] Launch deploy script for each feature
=========================================================*/ =========================================================*/
/* (1) Check data */ /* (1) For each feature */
if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) ){ foreach($feature as $feat_name){
slog('No \'chips\' found in api.response file', 'api:deploy', 'update');
return 127; /* (2) Check if specific data in response */
if( !isset($arr_r['feature'][$feat_name]) )
continue;
/* (3) Check for feature deploy script */
if( !file_exists(SOURCE_DIR."/feature/$feat_name/deploy") )
continue;
/* (4) Write useful exclusive data to tmp file */
file_put_contents(TMP_DIR."/$feat_name", json_encode($arr_r['feature'][$feat_name]));
/* (4) Execute deploy script */
$sync_suffix = $sync ? ' sync' : '';
syscall(SOURCE_DIR."/feature/$feat_name/deploy$sync");
} }
/* (2) Reset file */ /* [4] Get etrees list
file_put_contents(CHIPS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(CHIPS_CONF, 'w');
foreach($arr_r['chips'] as $chip)
$f->fwrite( json_encode([
(int) $chip['position'],
$chip['pins'],
$chip['states']
]).PHP_EOL );
$f = null;
/* [4] Get auth list (permissions)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['permissions']) || !is_array($arr_r['permissions']) ){
slog('No \'permissions\' found in api.response file', 'api:deploy', 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(AUTH_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(AUTH_CONF, 'w');
foreach($arr_r['permissions'] as $code=>$perm)
$f->fwrite( json_encode([
$code,
(int) $perm['id_user'],
$perm['actions']
]).PHP_EOL );
$f = null;
/* [5] Get action list (with timeout)
=========================================================*/
/* (1) Check data */
if( !isset($arr_r['actions']) || !is_array($arr_r['actions']) ){
slog('No \'actions\' found in api.response file', 'api:deploy', 'update');
return 127;
}
/* (2) Reset file */
file_put_contents(ACTIONS_CONF, '');
/* (3) Replace content */
$f = new SplFileObject(ACTIONS_CONF, 'w');
foreach($arr_r['actions'] as $timeout=>$actions)
foreach($actions as $action)
$f->fwrite( json_encode([
(int) $timeout,
(int) $action['id_action'],
$action['required'],
$action['action']
]).PHP_EOL );
$f = null;
/* [6] Get etrees list
=========================================================*/ =========================================================*/
/* (1) Check data */ /* (1) Check data */
if( !isset($arr_r['etrees']) || !is_array($arr_r['etrees']) ){ if( !isset($arr_r['etrees']) || !is_array($arr_r['etrees']) ){
slog('No \'etree\' found in api.response file', 'api:deploy', 'update'); slog('No \'etree\' found in response file', "$FEATURE:deploy", 'update');
return 127; return 127;
} }
@ -151,11 +90,11 @@
/* [6] Remove features' entries /* [5] Remove features' entries
=========================================================*/ =========================================================*/
/* (0) We are done if @sync */ /* (0) We are done if @sync */
if( $sync ){ if( $sync ){
slog('Post-sync deployment -> no history truncate', 'api:deploy', 'update'); slog('Post-sync deployment -> no history truncate', "$FEATURE:deploy", 'update');
return 0; return 0;
} }
@ -165,7 +104,7 @@
/* (2) Check history count */ /* (2) Check history count */
if( !is_numeric($count) ){ if( !is_numeric($count) ){
slog("No \'history\' found for '$feat_name' in api.response file", 'api:deploy', 'update'); slog("No \'history\' found for '$feat_name' in response file", "$FEATURE:deploy", 'update');
return 127; return 127;
} }
@ -178,17 +117,18 @@
/* (5) Manage error */ /* (5) Manage error */
if( $truncated === false ){ if( $truncated === false ){
slog('History cannot be truncated', 'api:deploy', 'update'); slog('History cannot be truncated', "$FEATURE:deploy", 'update');
return 127; return 127;
} }
slog("History '$feat_name' succesfully truncated", 'api:deploy', 'update'); slog("History '$feat_name' succesfully truncated", "$FEATURE:deploy", 'update');
} }
} }
return 0; return 0;
} }