[update] renamed `init` to `sync` and `sync` to `update`

This commit is contained in:
xdrm-brackets 2017-05-10 14:01:37 +02:00
parent 2bbb2974d8
commit 05eebd3930
6 changed files with 86 additions and 86 deletions

View File

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

View File

@ -2,7 +2,7 @@
require_once __DIR__.'/../../include/php/const';
function api_deploy($init){
function api_deploy($sync){
/* [1] Fetch api response
=========================================================*/
@ -133,9 +133,9 @@
/* [6] Remove history entries
=========================================================*/
/* (0) We are done if @init */
if( $init ){
slog('Post-init deployment -> no history truncate', 'api:deploy', 'update');
/* (0) We are done if @sync */
if( $sync ){
slog('Post-sync deployment -> no history truncate', 'api:deploy', 'update');
return 0;
}
@ -170,12 +170,12 @@
}
/* Manage init vs. sync */
$is_init = false;
if( $argc > 1 && $argv[1] == 'init' )
$is_init = true;
/* Manage sync vs. sync */
$is_sync = false;
if( $argc > 1 && $argv[1] == 'sync' )
$is_sync = true;
$exec = api_deploy($is_init);
$exec = api_deploy($is_sync);
if( $exec == 0 ) slog('Success', 'api:deploy', 'update');
else slog('Failure', 'api:deploy', 'update');

View File

@ -2,7 +2,7 @@
require_once __DIR__.'/../../include/php/const';
function api_request(){
function api_sync(){
/* [1] Fetch & generate useful data
=========================================================*/
@ -18,7 +18,18 @@
$url = preg_replace('/\s/', '', $url).'/sync/';
/* (2) Fetch cyclic hash */
/* (2) Fetch machine id */
$id_machine = @file_get_contents(ID_CONF);
if( $id_machine === false ){
slog("Cannot find machine id", 'api:sync','update');
return 127;
}
$id_machine = (int) preg_replace('/\s/', '', $id_machine);
/* (3) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 ){
@ -26,22 +37,23 @@
return 127;
}
/* (3) Try new hash if available */
$new_hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
/* (4) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new_hash === false ){
if( $new === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:sync','update');
return 127;
}
/* (4) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'), true);
/* (5) Fetch machine unlock code */
$unlock_code = @file_get_contents(UNLOCK_CODE);
if( is_null($data) ){
slog("api:fetch returned unreadable content", 'api:sync','update');
$data = [];
if( $unlock_code === false ){
slog("Cannot find unlock code", 'api:sync','update');
return 127;
}
$unlock_code = preg_replace('/\s/', '', $unlock_code);
@ -62,9 +74,17 @@
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [ 'data' => json_encode($data) ];
$postarray = [
'id_machine' => $id_machine,
'token' => $hash,
'unlock' => $unlock_code
];
/* (2) Set postdata raw to curl */
/* (2) Add new hash if renew */
if( strlen($new) == 128 )
$postarray['token'] = $new;
/* (3) Set postdata raw to curl */
curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray);
@ -80,13 +100,9 @@
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Calculate @new_hash (default value) */
if( strlen($new_hash) != 128 )
$new_hash = $hash;
/* (3) Set Auth header digest */
/* (2) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest {$wh_tok}{$hash}{$new_hash}"
"Authorization: Digest $wh_tok"
]);
@ -105,6 +121,7 @@
return 127;
}
/* [6] Response management
=========================================================*/
/* (1) Try to json_decode response */
@ -122,6 +139,7 @@
return 127;
}
/* (3) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response);
@ -138,10 +156,11 @@
slog('HTTP Response succesfully received', 'api:sync', 'update');
/* [8] Deploy received data
=========================================================*/
/* (1) Manage response deployement */
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy');
$deploy = syscall(SOURCE_DIR.'/lib/api/deploy sync');
/* (2) Return state */
return ($deploy === true) ? 0 : 127;
@ -149,7 +168,8 @@
}
$exec = api_request();
$exec = api_sync();
if( $exec == 0 ) slog('Success', 'api:sync', 'update');
else slog('Failure', 'api:sync', 'update');

View File

@ -2,7 +2,7 @@
require_once __DIR__.'/../../include/php/const';
function api_init(){
function api_request(){
/* [1] Fetch & generate useful data
=========================================================*/
@ -10,50 +10,38 @@
$url = @file_get_contents(URL_CONF);
if( $url === false ){
slog("Cannot find server's api url", 'api:init','update');
slog("Cannot find server's api url", 'api:update','update');
return 127;
}
// remove unwanted spaces or linebreaks
$url = preg_replace('/\s/', '', $url).'/init/';
$url = preg_replace('/\s/', '', $url).'/update/';
/* (2) Fetch machine id */
$id_machine = @file_get_contents(ID_CONF);
if( $id_machine === false ){
slog("Cannot find machine id", 'api:init','update');
return 127;
}
$id_machine = (int) preg_replace('/\s/', '', $id_machine);
/* (3) Fetch cyclic hash */
/* (2) Fetch cyclic hash */
$hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/hash');
if( strlen($hash) != 128 ){
slog("Wrong hash length (".strlen($hash).")", 'api:init','update');
slog("Wrong hash length (".strlen($hash).")", 'api:update','update');
return 127;
}
/* (4) Try new hash if available */
$new = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
/* (3) Try new hash if available */
$new_hash = syscall(SOURCE_DIR.'/lib/cyclic-hash/new');
if( $new === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:init','update');
if( $new_hash === false ){
slog("cyclic-hash:new returned $new EXIT_STATUS", 'api:update','update');
return 127;
}
/* (5) Fetch machine unlock code */
$unlock_code = @file_get_contents(UNLOCK_CODE);
/* (4) Fetch data */
$data = json_decode(syscall(SOURCE_DIR.'/lib/api/fetch'), true);
if( $unlock_code === false ){
slog("Cannot find unlock code", 'api:init','update');
return 127;
if( is_null($data) ){
slog("api:fetch returned unreadable content", 'api:update','update');
$data = [];
}
$unlock_code = preg_replace('/\s/', '', $unlock_code);
@ -74,17 +62,9 @@
/* [3] Manage post data
=========================================================*/
/* (1) Set post data */
$postarray = [
'id_machine' => $id_machine,
'token' => $hash,
'unlock' => $unlock_code
];
$postarray = [ 'data' => json_encode($data) ];
/* (2) Add new hash if renew */
if( strlen($new) == 128 )
$postarray['token'] = $new;
/* (3) Set postdata raw to curl */
/* (2) Set postdata raw to curl */
curl_setopt($curl, CURLOPT_POSTFIELDS, $postarray);
@ -94,15 +74,19 @@
$wh_tok = @file_get_contents(WAREHOUSE_TOK);
if( $wh_tok === false ){
slog("Cannot find warehouse token", 'api:init','update');
slog("Cannot find warehouse token", 'api:update','update');
return 127;
}
$wh_tok = preg_replace('/\s/', '', $wh_tok);
/* (2) Set Auth header digest */
/* (2) Calculate @new_hash (default value) */
if( strlen($new_hash) != 128 )
$new_hash = $hash;
/* (3) Set Auth header digest */
curl_setopt($curl, CURLOPT_HTTPHEADER, [
"Authorization: Digest $wh_tok"
"Authorization: Digest {$wh_tok}{$hash}{$new_hash}"
]);
@ -117,11 +101,10 @@
/* (3) Return response as result */
if( $response === false ){
slog("Request error", 'api:init','update');
slog("Request error", 'api:update','update');
return 127;
}
/* [6] Response management
=========================================================*/
/* (1) Try to json_decode response */
@ -129,17 +112,16 @@
// if cannot, abort
if( is_null($response_arr) ){
slog('Cannot parse HTTP response', 'api:init', 'update');
slog('Cannot parse HTTP response', 'api:update', 'update');
return 127;
}
/* (2) Check response error */
if( !isset($response_arr['error']) || $response_arr['error'] != 0 ){
slog('API error not on \'Success\'', 'api:init', 'update');
slog('API error not on \'Success\'', 'api:update', 'update');
return 127;
}
/* (3) Writes request to temporary pipe */
file_put_contents(TMP_DIR.'/api.response', $response);
@ -150,17 +132,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:update','update');
return 127;
}
slog('HTTP Response succesfully received', 'api:init', 'update');
slog('HTTP Response succesfully received', 'api:update', '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;
@ -168,11 +149,10 @@
}
$exec = api_request();
$exec = api_init();
if( $exec == 0 ) slog('Success', 'api:init', 'update');
else slog('Failure', 'api:init', 'update');
if( $exec == 0 ) slog('Success', 'api:update', 'update');
else slog('Failure', 'api:update', 'update');
echo $exec;
die($exec);

3
lib/api/update Executable file
View File

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

4
update
View File

@ -8,12 +8,12 @@ source $__DIR__/lib/include/bash/func;
# [1] Send data + fetch configuration
if [ ! -e /target/sync ]; then
test "$($SOURCE_DIR/lib/api/init)" = "127" && slog "init failed" - "update" && exit 127;
test "$($SOURCE_DIR/lib/api/sync)" = "127" && slog "sync failed" - "update" && exit 127;
touch /target/sync;
else
test "$($SOURCE_DIR/lib/api/sync)" = "127" && slog "sync failed" - "update" && exit 127;
test "$($SOURCE_DIR/lib/api/update)" = "127" && slog "update failed" - "update" && exit 127;
fi;