ptut-vhost/build/api/module/releaseController.php

90 lines
2.1 KiB
PHP

<?php
namespace api\module;
use \error\core\Error;
use \error\core\Err;
class releaseController{
/* Releases a project
*
* @project<String> Project's name
* @step<id> [OPT] Step to run
*
*/
public function get($args){
$project = "";
$step = null;
extract($args);
/* (1) Load projects' configuration
---------------------------------------------------------*/
/* (1) Fetch file */
$fc = file_get_contents(__CONFIG__.'/projects.json');
if( !$fc )
return [ 'error' => new Error(Err::UnreachableResource) ];
/* (2) Checks json format */
$js = json_decode( $fc, true );
if( !$js )
return [ 'error' => new Error(Err::ParsingFailed) ];
/* (2) Checks @project argument and files
---------------------------------------------------------*/
/* (1) Checks @project */
if( !isset($js[$project]) )
return [ 'error' => new Error(Err::UnreachableResource) ];
/* (2) Checks @project's folder */
if( !is_dir($js[$project]['dir']) )
return [ 'error' => new Error(Err::UnreachableResource) ];
/* (3) Checks @git directory */
if( !is_dir($js[$project]['dir']) )
return [ 'error' => new Error(Err::UnreachableResource) ];
/* (3) Launch script
---------------------------------------------------------*/
/* (1) 'cd' to project dir */
chdir($js[$project]['dir']);
/* (2) If given step, only do this one */
if( !is_null($step) ){
/* (2.1) If step does not exist */
if( !isset($js[$project]['cmd'][$step]) )
return [ 'error' => new Error(Err::ModuleError, 'step out of bounds') ];
/* (2.2) Execute step */
exec($js[$project]['cmd'][$step], $stdout);
/* (2.3) Dispatch output */
return [ 'stdout' => [ $js[$project]['cmd'][$step] => $stdout ] ];
}
/* (3) Execute each step one by one */
$stdout = [];
foreach($js[$project]['cmd'] as $step=>$cmd){
/* (3.1) Pre-create stdout */
$stdout[$cmd] = '';
/* (3.2) Execute step */
exec($cmd, $stdout[$cmd]);
}
/* (4) Dispatch stdout */
return [ 'stdout' => $stdout ];
}
}