2017-02-19 11:31:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__.'/../../include/php/const';
|
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
function api_deploy($sync){
|
2017-02-19 11:31:36 +00:00
|
|
|
|
|
|
|
/* [1] Fetch api response
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Try to read response */
|
|
|
|
$file_r = @file_get_contents(TMP_DIR.'/api.response');
|
|
|
|
|
|
|
|
/* (2) Check response */
|
2017-05-09 09:47:16 +00:00
|
|
|
if( $file_r === false ){
|
|
|
|
slog('Cannot find api.response file', 'api:deploy', 'update');
|
2017-02-19 11:31:36 +00:00
|
|
|
return 127;
|
2017-05-09 09:47:16 +00:00
|
|
|
}
|
2017-02-19 11:31:36 +00:00
|
|
|
|
|
|
|
/* (3) Try to parse response */
|
|
|
|
$arr_r = json_decode($file_r, true);
|
|
|
|
|
|
|
|
/* (4) Check parse error */
|
2017-09-28 15:25:24 +00:00
|
|
|
if( is_null($arr_r) || !isset($arr_r['feature']) ){
|
2017-05-09 09:47:16 +00:00
|
|
|
slog('Cannot parse api.response file', 'api:deploy', 'update');
|
2017-02-19 11:31:36 +00:00
|
|
|
return 127;
|
2017-05-09 09:47:16 +00:00
|
|
|
}
|
2017-02-19 11:31:36 +00:00
|
|
|
|
2017-05-08 20:07:20 +00:00
|
|
|
|
2017-09-28 15:42:16 +00:00
|
|
|
/* [2] Get etrees list
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Check data */
|
|
|
|
if( !isset($arr_r['etrees']) || !is_array($arr_r['etrees']) ){
|
|
|
|
slog('No \'etree\' found in response file', "api:deploy", 'update');
|
|
|
|
return 127;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Reset file */
|
|
|
|
file_put_contents(ETREE_CONF, '');
|
|
|
|
|
|
|
|
/* (3) Replace content */
|
|
|
|
$f = new SplFileObject(ETREE_CONF, 'w');
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-09-28 15:42:16 +00:00
|
|
|
foreach($arr_r['etrees'] as $etree)
|
|
|
|
$f->fwrite( $etree.PHP_EOL );
|
|
|
|
|
|
|
|
$f = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] Get the list of daemons (features)
|
2017-02-20 20:22:42 +00:00
|
|
|
=========================================================*/
|
2017-09-28 15:25:24 +00:00
|
|
|
$feature = [];
|
|
|
|
$f = new SplFileObject(ETREE_CONF, 'r');
|
2017-05-08 20:07:20 +00:00
|
|
|
|
2017-09-28 15:25:24 +00:00
|
|
|
while( !$f->eof() ){
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-09-28 15:25:24 +00:00
|
|
|
/* (1) Try to parse each line */
|
|
|
|
$etree = trim($f->fgets(), "\n");
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-09-28 15:25:24 +00:00
|
|
|
/* (2) Manage error */
|
|
|
|
if( is_null($etree) || strlen($etree) == 0 )
|
|
|
|
continue;
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-09-28 15:25:24 +00:00
|
|
|
/* (3) Add entry to log */
|
|
|
|
$feature[] = $etree;
|
2017-02-19 11:31:36 +00:00
|
|
|
|
2017-05-09 09:47:16 +00:00
|
|
|
}
|
2017-09-26 08:26:02 +00:00
|
|
|
$f = null;
|
|
|
|
|
|
|
|
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-28 15:42:16 +00:00
|
|
|
/* [4] Remove features' entries
|
2017-07-17 17:10:50 +00:00
|
|
|
=========================================================*/
|
|
|
|
/* (0) We are done if @sync */
|
|
|
|
if( $sync ){
|
2017-09-28 16:53:30 +00:00
|
|
|
slog('Post-sync deployment -> no history truncate', "api:deploy", 'update');
|
2017-07-17 17:10:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
if( isset($arr_r['saved']) && is_array($arr_r['saved']) ){
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
foreach($arr_r['saved'] as $feat_name=>$count){
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
/* (2) Check history count */
|
|
|
|
if( !is_numeric($count) ){
|
2017-09-28 16:53:30 +00:00
|
|
|
slog("No \'history\' found for '$feat_name' in response file", 'api:deploy', 'update');
|
|
|
|
continue;
|
2017-09-26 08:26:02 +00:00
|
|
|
}
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
/* (3) Fetch number of entries */
|
|
|
|
$saved = intval($count);
|
|
|
|
$saved = $saved < 0 ? 0 : $saved; // prevent negative
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
/* (4) Truncate the log file */
|
|
|
|
$truncated = syscall(SOURCE_DIR."/lib/file/truncate $feat_name {$saved}");
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
/* (5) Manage error */
|
|
|
|
if( $truncated === false ){
|
2017-09-28 16:53:30 +00:00
|
|
|
slog('History cannot be truncated', "$feat_name:deploy", 'update');
|
|
|
|
continue;
|
2017-09-26 08:26:02 +00:00
|
|
|
}
|
2017-02-23 17:13:11 +00:00
|
|
|
|
2017-09-26 08:26:02 +00:00
|
|
|
|
2017-09-28 16:53:30 +00:00
|
|
|
slog("History succesfully truncated", "$feat_name:deploy", 'update');
|
2017-09-26 08:26:02 +00:00
|
|
|
|
|
|
|
}
|
2017-09-26 14:35:26 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-09-28 15:25:24 +00:00
|
|
|
|
2017-09-28 15:42:16 +00:00
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
/* [4] Deploy default data
|
|
|
|
=========================================================*/ {
|
2017-09-28 16:55:25 +00:00
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
/* (1) Get system states (global_state)
|
|
|
|
---------------------------------------------------------*/ {
|
2017-09-28 15:42:16 +00:00
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
/* (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;
|
2017-09-28 15:42:16 +00:00
|
|
|
|
2017-09-28 16:55:25 +00:00
|
|
|
}
|
2017-09-28 15:42:16 +00:00
|
|
|
|
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
/* (2) 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 );
|
2017-09-28 16:58:19 +00:00
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
$f = null;
|
|
|
|
|
|
|
|
}
|
2017-09-28 15:42:16 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-28 17:07:30 +00:00
|
|
|
/* [5] Launch deploy script for each feature
|
|
|
|
=========================================================*/ {
|
|
|
|
|
|
|
|
slog('Deploying each feature', "api:deploy", 'update');
|
|
|
|
|
|
|
|
/* (1) For each feature */
|
|
|
|
foreach($feature as $feat_name){
|
|
|
|
|
|
|
|
/* (2) Check if specific data in response */
|
|
|
|
if( !isset($arr_r['feature'][$feat_name]) ){
|
|
|
|
slog("No data found for $feat_name", "api:deploy", 'update');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Check for feature deploy script */
|
|
|
|
if( !file_exists(SOURCE_DIR."/feature/$feat_name/deploy") ){
|
|
|
|
slog("No deploy script found for $feat_name", "api:deploy", 'update');
|
|
|
|
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' : '';
|
|
|
|
|
|
|
|
if( syscall(SOURCE_DIR."/feature/$feat_name/deploy$sync") !== false )
|
|
|
|
slog("Successfully deployed $feat_name", 'api:deploy', 'update');
|
|
|
|
else
|
|
|
|
slog("Failed deploying $feat_name", 'api:deploy', 'update');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
return 0;
|
2017-02-19 11:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
/* Manage sync vs. sync */
|
|
|
|
$is_sync = false;
|
|
|
|
if( $argc > 1 && $argv[1] == 'sync' )
|
|
|
|
$is_sync = true;
|
2017-05-08 20:07:20 +00:00
|
|
|
|
2017-05-10 12:01:37 +00:00
|
|
|
$exec = api_deploy($is_sync);
|
2017-02-20 20:22:42 +00:00
|
|
|
|
2017-05-09 10:27:09 +00:00
|
|
|
if( $exec == 0 ) slog('Success', 'api:deploy', 'update');
|
|
|
|
else slog('Failure', 'api:deploy', 'update');
|
2017-05-09 09:47:16 +00:00
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
echo $exec;
|
|
|
|
die($exec);
|
2017-05-08 20:07:20 +00:00
|
|
|
|
2017-02-19 11:31:36 +00:00
|
|
|
|
|
|
|
?>
|