From ee177181f87c4ce601e05512e31140f2cfd30162 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 20 Feb 2017 21:22:42 +0100 Subject: [PATCH] [Done] Deploy management + updated configuration files (fetched by init/sync) --- lib/api/source/deploy.php | 109 +++++++++++++++++++++++++++++++++--- lib/api/source/init.php | 17 ++++-- lib/api/source/sync.php | 18 ++++-- lib/include/bash/const | 12 +++- lib/include/php/const | 8 ++- lib/mfrc522/source/loop.php | 3 + 6 files changed, 143 insertions(+), 24 deletions(-) diff --git a/lib/api/source/deploy.php b/lib/api/source/deploy.php index f2f4a6e..eb12000 100755 --- a/lib/api/source/deploy.php +++ b/lib/api/source/deploy.php @@ -21,20 +21,111 @@ return 127; + /* [2] Get system states (global_state) + =========================================================*/ + /* (1) Check data */ + if( !isset($arr_r['states']) || !is_array($arr_r['states']) ) + 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']) ) + 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']) ) + 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']) ) + 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['previous'], + $action['action'] + + ]).PHP_EOL ); + + $f = null; + + return 0; } - /* [1] Check $1 argument - =========================================================*/ - if( $argc < 2 || !in_array($argv[1], 'init', 'sync' ){ - echo 127; - die(); - } + $exec = api_deploy(); - - echo api_deploy($argv[1]); - + echo $exec; + die($exec); ?> diff --git a/lib/api/source/init.php b/lib/api/source/init.php index b3b6e35..17116f6 100755 --- a/lib/api/source/init.php +++ b/lib/api/source/init.php @@ -128,12 +128,16 @@ $response_arr = json_decode($response, true); // if cannot, abort - if( is_null($response_arr) ) + if( is_null($response_arr) ){ + slog('Cannot parse HTTP response', 'api:init', 'update'); return 127; + } /* (2) Check response error */ - if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) + if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){ + slog('API error not on \'Success\'', 'api:init', 'update'); return 127; + } /* (3) Writes request to temporary pipe */ @@ -150,13 +154,13 @@ return 127; } - return 0; + slog('HTTP Response succesfully received', 'api:init', 'update'); /* [8] Deploy received data =========================================================*/ /* (1) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); /* (2) Return state */ return ($deploy === true) ? 0 : 127; @@ -164,7 +168,10 @@ } - die(api_init()); + + $exec = api_init(); + echo $exec; + die($exec); diff --git a/lib/api/source/sync.php b/lib/api/source/sync.php index 70b5ac3..ba7ab32 100755 --- a/lib/api/source/sync.php +++ b/lib/api/source/sync.php @@ -114,12 +114,16 @@ $response_arr = json_decode($response, true); // if cannot, abort - if( is_null($response_arr) ) + if( is_null($response_arr) ){ + slog('Cannot parse HTTP response', 'api:sync', 'update'); return 127; + } /* (2) Check response error */ - if( !isset($response_arr['error']) || $response_arr['error'] != 0 ) + if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){ + slog('API error not on \'Success\'', 'api:sync', 'update'); return 127; + } /* (3) Writes request to temporary pipe */ file_put_contents(TMP_DIR.'/api.response', $response); @@ -131,16 +135,16 @@ $decr = syscall(SOURCE_DIR.'/lib/cyclic-hash/decr'); if( $decr === false ){ - slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:init','update'); + slog("cyclic-hash:decr returned $decr EXIT_STATUS", 'api:sync','update'); return 127; } - return 0; + slog('HTTP Response succesfully received', 'api:sync', 'update'); /* [8] Deploy received data =========================================================*/ /* (1) Manage response deployement */ - $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync'); + $deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); /* (2) Return state */ return ($deploy === true) ? 0 : 127; @@ -148,7 +152,9 @@ } - die(api_request()); + $exec = api_request(); + echo $exec; + die($exec); diff --git a/lib/include/bash/const b/lib/include/bash/const index 42d7fc6..ae4caa5 100755 --- a/lib/include/bash/const +++ b/lib/include/bash/const @@ -12,24 +12,32 @@ export CONF_DIR="$ROOT_DIR/conf"; export SOURCE_DIR="/home/xdrm-brackets/Desktop/git.xdr/home/xdrm-brackets/Desktop/git.xdrm.io/logauth-sats"; export TMP_DIR="$ROOT_DIR/tmp"; + # CONFIGURATION FILES export SECRET_CONF="$CONF_DIR/machine.secret"; export STATE_CONF="$CONF_DIR/machine.state"; export BRANCH_CONF="$CONF_DIR/machine.branch"; export ID_CONF="$CONF_DIR/machine.id"; export URL_CONF="$CONF_DIR/api.url"; -export AUTH_CONF="$CONF_DIR/auth.list"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; export UNLOCK_CODE="$CONF_DIR/machine.unlock"; +export AUTH_CONF="$CONF_DIR/auth.list"; +export STATES_CONF="$CONF_DIR/states.list"; +export CHIPS_CONF="$CONF_DIR/chips.list"; +export ACTIONS_CONF="$CONF_DIR/actions.list"; + + # LOG FILES export BOOT_LOG="$LOG_DIR/boot.log"; export DAEMON_LOG="$LOG_DIR/daemon.log"; export INSTALL_LOG="$LOG_DIR/install-source.log"; export UPDATE_LOG="$LOG_DIR/update.log"; + # DATA FILES -export ACCESS_DATA="$DATA_DIR/access.data"; +export DEFAULT_DATA="$DATA_DIR/mfrc522"; + # SETTINGS export SECRET_SIZE=250; diff --git a/lib/include/php/const b/lib/include/php/const index 189f15d..ee9f8f7 100755 --- a/lib/include/php/const +++ b/lib/include/php/const @@ -18,10 +18,14 @@ define('BRANCH_CONF', CONF_DIR.'/machine.branch'); define('ID_CONF', CONF_DIR.'/machine.id'); define('URL_CONF', CONF_DIR.'/api.url'); - define('AUTH_CONF', CONF_DIR.'/auth.list'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); define('UNLOCK_CODE', CONF_DIR.'/machine.unlock'); + define('AUTH_CONF', CONF_DIR.'/auth.list'); + define('STATES_CONF', CONF_DIR.'/states.list'); + define('CHIPS_CONF', CONF_DIR.'/chips.list'); + define('ACTIONS_CONF', CONF_DIR.'/actions.list'); + # LOG FILES define('BOOT_LOG', LOG_DIR.'/boot.log'); define('DAEMON_LOG', LOG_DIR.'/daemon.log'); @@ -29,7 +33,7 @@ define('UPDATE_LOG', LOG_DIR.'/update.log'); # DATA FILES - define('ACCESS_DATA', DATA_DIR.'/access.data'); + define('DEFAULT_DATA', DATA_DIR.'/mfrc522'); # SETTINGS define('SECRET_SIZE', 250); diff --git a/lib/mfrc522/source/loop.php b/lib/mfrc522/source/loop.php index 3252947..74315ed 100755 --- a/lib/mfrc522/source/loop.php +++ b/lib/mfrc522/source/loop.php @@ -50,6 +50,9 @@ =========================================================*/ + /* [3] Manage action (states) + =========================================================*/ + /* [n] Wait for 0.5 s before reading again =========================================================*/