diff --git a/.gitignore b/.gitignore index 339663e..b5ca1aa 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .vscode/launch.json .vscode/ipch generated/* -lib/generated \ No newline at end of file +lib/generated +webinstall/token.php diff --git a/webinstall/cibuild.php b/webinstall/cibuild.php new file mode 100644 index 0000000..e2fb9c2 --- /dev/null +++ b/webinstall/cibuild.php @@ -0,0 +1,122 @@ +$CI_TOKEN); +} +function getPipeline($pipeline){ + $url=apiBase."/pipeline/$pipeline"; + $token=getTokenHeaders(); + return getJson($url,$token,true); +} +function getWorkflow($pipeline,$workflowName){ + $url=apiBase."/pipeline/$pipeline/workflow"; + $token=getTokenHeaders(); + $pstate=getJson($url,$token,true); + if (! isset($pstate['items'])){ + throw new Exception("no workflows in pipeline"); + } + foreach ($pstate['items'] as $workflow){ + if (isset($workflow['name']) && $workflow['name'] == $workflowName){ + if (!isset($workflow['id'])){ + throw new Exception("no workflow id found"); + } + return $workflow; + } + } + throw new Exception("workflow $workflowName not found"); +} +function getJob($pipeline,$workflow,$jobName){ + $url=apiBase."/workflow/".$workflow."/job"; + $token=getTokenHeaders(); + $wstate=getJson($url,$token,true); + if (! isset($wstate['items'])){ + throw new Exception("no jobs in workflow"); + } + foreach ($wstate['items'] as $job){ + if (isset($job['name']) && $job['name'] == $jobName){ + if (! isset($job['id'])){ + throw new Exception("no job id found"); + } + return $job; + } + } + throw new Exception("job $jobName not found"); +} +function getJobStatus($pipeline,$wf=workflowName,$job=jobName){ + $pstat=getPipeline($pipeline); + if (isset($pstat['error'])){ + throw new Exception($pstat["error"]); + } + if (! isset($pstat['state'])){ + throw new Exception("state not set"); + } + if ($pstat['state'] != 'created'){ + return $pstat; + } + $pstat=getWorkflow($pipeline,$wf); + $pstat=getJob($pipeline,$pstat['id'],$job); + return $pstat; +} + +function getArtifacts($job,$slug){ + $url=apiBase."/project/$slug/$job/artifacts"; + return getJson($url,getTokenHeaders(),true); +} + +if (isset($_REQUEST['api'])){ + $action=$_REQUEST['api']; + header("Content-Type: application/json"); + $par=array(); + if ($action == 'status') { + addVars( + $par, + ['pipeline', 'workflow', 'job'], + array('workflow' => workflowName, 'job' => jobName) + ); + try { + $pstat = getJobStatus($par['pipeline'], $par['workflow'], $par['job']); + echo(json_encode($pstat)); + } catch (Exception $e) { + $rt=array('status'=>'error','error'=>$e->getMessage()); + echo(json_encode($rt)); + } + exit(0); + } + if ($action == 'artifacts'){ + addVars( + $par, + ['pipeline', 'workflow', 'job'], + array('workflow' => workflowName, 'job' => jobName) + ); + try{ + $jstat=getJobStatus($par['pipeline'], $par['workflow'], $par['job']); + if (! isset($jstat['project_slug'])){ + throw new Exception("no project_slug in job"); + } + if (! isset($jstat['status'])){ + throw new Exception("no job status"); + } + if ($jstat['status'] != 'success'){ + throw new Exception("invalid job status ".$jstat['status']); + } + $astat=getArtifacts($jstat['job_number'],$jstat['project_slug']); + echo (json_encode($astat)); + }catch (Exception $e){ + echo(json_encode(array('status'=>'error','error'=>$e->getMessage()))); + } + exit(0); + } + die("invalid api $action"); +} +die("no action"); +?> \ No newline at end of file diff --git a/webinstall/config.php b/webinstall/config.php new file mode 100644 index 0000000..afc3741 --- /dev/null +++ b/webinstall/config.php @@ -0,0 +1,26 @@ + array('wellenvogel'), + 'repo'=> array('esp32-nmea2000') +); + + +function fillUserAndRepo($vars=null){ + global $allowed; + if ($vars == null) { + $vars=array(); + } + foreach (array('user','repo') as $n){ + if (! isset($_REQUEST[$n])){ + die("missing parameter $n"); + } + $v=$_REQUEST[$n]; + $av=$allowed[$n]; + if (! in_array($v,$av)){ + die("value $v for $n not allowed"); + } + $vars[$n]=$v; + } + return $vars; +} +?> \ No newline at end of file diff --git a/webinstall/functions.php b/webinstall/functions.php new file mode 100644 index 0000000..1be14d9 --- /dev/null +++ b/webinstall/functions.php @@ -0,0 +1,152 @@ + &$v) { + $str = str_replace("#" . $n . "#", $v, $str); + } + return $str; +} +if (!function_exists('getallheaders')) { + function getallheaders() + { + $headers = []; + foreach ($_SERVER as $name => $value) { + if (substr($name, 0, 5) == 'HTTP_') { + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; + } + } + return $headers; + } +} +function addVars(&$vars,$names,$defaults=null){ + foreach ($names as $n){ + $v=null; + if (! isset($_REQUEST[$n])){ + if ($defaults == null || ! isset($defaults[$n])) die("missing parameter $n"); + $v=$defaults[$n]; + } + else{ + $v=safeName($_REQUEST[$n]); + } + $vars[$n]=$v; + } + return $vars; +} + +function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { + $mr = $maxredirect === null ? 5 : intval($maxredirect); + if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off') && false) { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); + curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); + } else { + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); + if ($mr > 0) { + $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); + $rch = curl_copy_handle($ch); + curl_setopt($rch, CURLOPT_HEADER, true); + curl_setopt($rch, CURLOPT_NOBODY, true); + curl_setopt($rch, CURLOPT_FORBID_REUSE, false); + curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); + do { + curl_setopt($rch, CURLOPT_URL, $newurl); + $header = curl_exec($rch); + if (curl_errno($rch)) { + $code = 0; + } else { + $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); + if ($code == 301 || $code == 302) { + preg_match('/Location:(.*?)\n/', $header, $matches); + $newurl = trim(array_pop($matches)); + } else { + $code = 0; + } + } + } while ($code && --$mr); + curl_close($rch); + if (!$mr) { + if ($maxredirect === null) { + trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); + } else { + $maxredirect = 0; + } + return false; + } + curl_setopt($ch, CURLOPT_URL, $newurl); + } + } + curl_setopt( + $ch, + CURLOPT_HEADERFUNCTION, + function ($curl, $header) { + header($header); + return strlen($header); + } + ); + curl_setopt( + $ch, + CURLOPT_WRITEFUNCTION, + function ($curl, $body) { + echo $body; + return strlen($body); + } + ); + header('Access-Control-Allow-Origin:*'); + return curl_exec($ch); +} + +function setFw($curl,$aheaders=null){ + $headers=getallheaders(); + $FWHDR = ['User-Agent']; + $outHeaders = array(); + foreach ($FWHDR as $k) { + if (isset($headers[$k])) { + array_push($outHeaders, "$k: $headers[$k]"); + } + } + if ($aheaders != null){ + foreach ($aheaders as $hk => $hv){ + array_push($outHeaders,"$hk: $hv"); + } + } + curl_setopt($curl, CURLOPT_HTTPHEADER, $outHeaders); +} +function getJson($url,$headers=null){ + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL,$url); + curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); + setFw($curl,$headers); + $response = curl_exec($curl); + $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE); + #echo("curl exec for $url:$response:$httpcode\n"); + if($e = curl_error($curl)) { + curl_close($curl); + return array('error'=>$e); + } else { + if ($httpcode >= 300){ + curl_close($curl); + return array('error'=>"HTTP code ".$httpcode); + } + curl_close($curl); + return json_decode($response, true); + } +} +function proxy($url) +{ + $ch = curl_init($url); + curl_setopt_array( + $ch, + [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 30, + ] + ); + setFw($ch); + $response = curl_exec_follow($ch); + curl_close($ch); +} + +?> \ No newline at end of file diff --git a/webinstall/install.php b/webinstall/install.php index 344f57a..2cd046b 100644 --- a/webinstall/install.php +++ b/webinstall/install.php @@ -1,162 +1,10 @@ array('wellenvogel'), - 'repo'=> array('esp32-nmea2000') - ); - if (!function_exists('getallheaders')) { - function getallheaders() - { - $headers = []; - foreach ($_SERVER as $name => $value) { - if (substr($name, 0, 5) == 'HTTP_') { - $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; - } - } - return $headers; - } - } - function safeName($name){ - return preg_replace('[^0-9_a-zA-Z.-]','',$name); - } - function replaceVars($str,$vars){ - foreach ($vars as $n => &$v){ - $str=str_replace("#".$n."#",$v,$str); - } - return $str; - } - - function fillUserAndRepo($vars=null){ - global $allowed; - if ($vars == null) { - $vars=array(); - } - foreach (array('user','repo') as $n){ - if (! isset($_REQUEST[$n])){ - die("missing parameter $n"); - } - $v=$_REQUEST[$n]; - $av=$allowed[$n]; - if (! in_array($v,$av)){ - die("value $v for $n not allowed"); - } - $vars[$n]=$v; - } - return $vars; - } - function addVars($vars,$names){ - foreach ($names as $n){ - if (! isset($_REQUEST[$n])){ - die("missing parameter $n"); - } - $safe=safeName($_REQUEST[$n]); - $vars[$n]=$safe; - } - return $vars; - } - - function curl_exec_follow(/*resource*/ $ch, /*int*/ &$maxredirect = null) { - $mr = $maxredirect === null ? 5 : intval($maxredirect); - if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off') && false) { - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $mr > 0); - curl_setopt($ch, CURLOPT_MAXREDIRS, $mr); - } else { - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); - if ($mr > 0) { - $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); - $rch = curl_copy_handle($ch); - curl_setopt($rch, CURLOPT_HEADER, true); - curl_setopt($rch, CURLOPT_NOBODY, true); - curl_setopt($rch, CURLOPT_FORBID_REUSE, false); - curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); - do { - curl_setopt($rch, CURLOPT_URL, $newurl); - $header = curl_exec($rch); - if (curl_errno($rch)) { - $code = 0; - } else { - $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); - if ($code == 301 || $code == 302) { - preg_match('/Location:(.*?)\n/', $header, $matches); - $newurl = trim(array_pop($matches)); - } else { - $code = 0; - } - } - } while ($code && --$mr); - curl_close($rch); - if (!$mr) { - if ($maxredirect === null) { - trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); - } else { - $maxredirect = 0; - } - return false; - } - curl_setopt($ch, CURLOPT_URL, $newurl); - } - } - curl_setopt( - $ch, - CURLOPT_HEADERFUNCTION, - function ($curl, $header) { - header($header); - return strlen($header); - } - ); - curl_setopt( - $ch, - CURLOPT_WRITEFUNCTION, - function ($curl, $body) { - echo $body; - return strlen($body); - } - ); - header('Access-Control-Allow-Origin:*'); - return curl_exec($ch); - } - function setFw($curl){ - $headers=getallheaders(); - $FWHDR = ['User-Agent']; - $outHeaders = array(); - foreach ($FWHDR as $k) { - if (isset($headers[$k])) { - array_push($outHeaders, "$k: $headers[$k]"); - } - } - curl_setopt($curl, CURLOPT_HTTPHEADER, $outHeaders); - } - function getJson($url){ - $curl = curl_init(); - curl_setopt($curl, CURLOPT_URL,$url); - curl_setopt($curl,CURLOPT_RETURNTRANSFER, true); - setFw($curl); - $response = curl_exec($curl); - if($e = curl_error($curl)) { - curl_close($curl); - return array('error'=>$e); - } else { - curl_close($curl); - return json_decode($response, true); - } - } - function proxy($url) - { - $ch = curl_init($url); - curl_setopt_array( - $ch, - [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_CONNECTTIMEOUT => 30, - ] - ); - setFw($ch); - $response = curl_exec_follow($ch); - curl_close($ch); - } - + if (isset($_REQUEST['api'])) { $vars=fillUserAndRepo(); proxy(replaceVars($api,$vars)); diff --git a/webinstall/watch.sh b/webinstall/watch.sh new file mode 100755 index 0000000..99de9ef --- /dev/null +++ b/webinstall/watch.sh @@ -0,0 +1,12 @@ +#! /bin/sh +if [ "$1" = "" ] ; then + echo "usage: $0 targetDir" + exit 1 +fi +while true +do + inotifywait -e modify -e create -e delete -r `dirname $0` + echo sync + rsync -rav --exclude=\*.swp --exclude=\*~ `dirname $0`/ $1 + +done