From 85d4930e85f2b385f9792925a8e4300981c26291 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 28 Sep 2017 17:25:24 +0200 Subject: [PATCH] Updated deployment (auto-managed by each feature's own implementation) --- feature/rfid-read/deploy | 3 + feature/rfid-read/source/deploy.php | 155 ++++++++++++++++++++++++++++ lib/api/source/deploy.php | 150 ++++++++------------------- 3 files changed, 203 insertions(+), 105 deletions(-) create mode 100644 feature/rfid-read/deploy create mode 100644 feature/rfid-read/source/deploy.php diff --git a/feature/rfid-read/deploy b/feature/rfid-read/deploy new file mode 100644 index 0000000..edc3aee --- /dev/null +++ b/feature/rfid-read/deploy @@ -0,0 +1,3 @@ +#!/bin/sh + +/usr/bin/env php $(realpath $(dirname $0))/source/deploy.php; diff --git a/feature/rfid-read/source/deploy.php b/feature/rfid-read/source/deploy.php new file mode 100644 index 0000000..d610068 --- /dev/null +++ b/feature/rfid-read/source/deploy.php @@ -0,0 +1,155 @@ +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); + + +?> diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php index b973117..1c70a49 100755 --- a/lib/api/source/deploy.php +++ b/lib/api/source/deploy.php @@ -19,122 +19,61 @@ $arr_r = json_decode($file_r, true); /* (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'); return 127; } - /* [2] Get system states (global_state) + + /* [2] Get the list of daemons (features) =========================================================*/ - /* (1) Check data */ - if( !isset($arr_r['states']) || !is_array($arr_r['states']) ){ - slog('No \'states\' found in api.response file', 'api:deploy', 'update'); - return 127; + $feature = []; + $f = new SplFileObject(ETREE_CONF, 'r'); + + 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 */ - if( !isset($arr_r['chips']) || !is_array($arr_r['chips']) ){ - slog('No \'chips\' found in api.response file', 'api:deploy', 'update'); - return 127; + /* (1) For each feature */ + foreach($feature as $feat_name){ + + /* (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 */ - 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 + /* [4] Get etrees list =========================================================*/ /* (1) Check data */ 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; } @@ -151,11 +90,11 @@ - /* [6] Remove features' entries + /* [5] Remove features' entries =========================================================*/ /* (0) We are done 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; } @@ -165,7 +104,7 @@ /* (2) Check history 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; } @@ -178,17 +117,18 @@ /* (5) Manage error */ if( $truncated === false ){ - slog('History cannot be truncated', 'api:deploy', 'update'); + slog('History cannot be truncated', "$FEATURE:deploy", 'update'); return 127; } - slog("History '$feat_name' succesfully truncated", 'api:deploy', 'update'); + slog("History '$feat_name' succesfully truncated", "$FEATURE:deploy", 'update'); } } + return 0; }