[Done] init|sync works[Todo] Fetcher

[Todo] Deployer
This commit is contained in:
xdrm-brackets 2017-02-19 12:31:36 +01:00
parent a3ff6b9cbe
commit 5ff68d5c42
5 changed files with 64 additions and 6 deletions

40
lib/api/source/deploy.php Executable file
View File

@ -0,0 +1,40 @@
<?php
require_once __DIR__.'/../../include/php/const';
function api_deploy(){
/* [1] Fetch api response
=========================================================*/
/* (1) Try to read response */
$file_r = @file_get_contents(TMP_DIR.'/api.response');
/* (2) Check response */
if( $file_r === false )
return 127;
/* (3) Try to parse response */
$arr_r = json_decode($file_r, true);
/* (4) Check parse error */
if( is_null($arr_r) )
return 127;
return 0;
}
/* [1] Check $1 argument
=========================================================*/
if( $argc < 2 || !in_array($argv[1], 'init', 'sync' ){
echo 127;
die();
}
echo api_deploy($argv[1]);
?>

View File

@ -45,13 +45,14 @@
} }
/* (5) Fetch machine unlock code */ /* (5) Fetch machine unlock code */
$unlock_code = @file_get_contents(ID_CONF); $unlock_code = @file_get_contents(UNLOCK_CODE);
if( $unlock_code === false ){ if( $unlock_code === false ){
slog("Cannot find unlock code", 'api:init','update'); slog("Cannot find unlock code", 'api:init','update');
return 127; return 127;
} }
$unlock_code = preg_replace('/\s/', '', $unlock_code);
@ -96,6 +97,8 @@
return 127; return 127;
} }
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Set Auth header digest */ /* (2) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [ curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest $wh_tok" "Authorization: Digest $wh_tok"
@ -136,7 +139,7 @@
file_put_contents(TMP_DIR.'/api.response', $response); file_put_contents(TMP_DIR.'/api.response', $response);
/* (2) Manage response deployement */ /* (2) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); $deploy = syscall(SOURCE_DIR.'/lib/api/deploy init');
/* (3) Return state */ /* (3) Return state */
return ($deploy === true) ? 0 : 127; return ($deploy === true) ? 0 : 127;

View File

@ -85,6 +85,8 @@
return 127; return 127;
} }
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Set Auth header digest */ /* (2) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [ curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest $wh_tok" "Authorization: Digest $wh_tok"
@ -121,13 +123,24 @@
/* [6] Response management /* [6] Response management
=========================================================*/ =========================================================*/
/* (1) Writes request to temporary pipe */ /* (1) Try to json_decode response */
$response_arr = json_decode($response, true);
// if cannot, abort
if( is_null($response_arr) )
return 127;
/* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 )
return 127;
/* (3) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response); file_put_contents(TMP_DIR.'/api.response', $response);
/* (2) Manage response deployement */ /* (4) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy'); $deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync');
/* (3) Return state */ /* (5) Return state */
return ($deploy === true) ? 0 : 127; return ($deploy === true) ? 0 : 127;
} }

View File

@ -20,6 +20,7 @@ export ID_CONF="$CONF_DIR/machine.id";
export URL_CONF="$CONF_DIR/api.url"; export URL_CONF="$CONF_DIR/api.url";
export AUTH_CONF="$CONF_DIR/auth.list"; export AUTH_CONF="$CONF_DIR/auth.list";
export WAREHOUSE_TOK="$CONF_DIR/warehouse.token"; export WAREHOUSE_TOK="$CONF_DIR/warehouse.token";
export UNLOCK_CODE="$CONF_DIR/machine.unlock";
# LOG FILES # LOG FILES
export BOOT_LOG="$LOG_DIR/boot.log"; export BOOT_LOG="$LOG_DIR/boot.log";

View File

@ -20,6 +20,7 @@
define('URL_CONF', CONF_DIR.'/api.url'); define('URL_CONF', CONF_DIR.'/api.url');
define('AUTH_CONF', CONF_DIR.'/auth.list'); define('AUTH_CONF', CONF_DIR.'/auth.list');
define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token'); define('WAREHOUSE_TOK', CONF_DIR.'/warehouse.token');
define('UNLOCK_CODE', CONF_DIR.'/machine.unlock');
# LOG FILES # LOG FILES
define('BOOT_LOG', LOG_DIR.'/boot.log'); define('BOOT_LOG', LOG_DIR.'/boot.log');