Skip to content
Snippets Groups Projects
Commit 95939aa7 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

#1385 - first basic code for an API file

parent 0b479ff5
Branches
No related tags found
No related merge requests found
<?php
/**
* version data handler for the deployment tool
*
......@@ -15,11 +14,15 @@ class versions {
public $sData = false;
/**
* filename of
* filename of sqlite database file
* @var type
*/
private $_dbfile = false;
/**
* create statement for the database
* @var type
*/
private $_sCreate = '
CREATE TABLE "versions" (
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL UNIQUE ,
......@@ -38,7 +41,9 @@ class versions {
// CONSTRUCTOR
// ----------------------------------------------------------------------
/**
* constructor ... no params
*/
public function __construct(){
// cache dir is hardcoded to versions directory :-/
......@@ -46,9 +51,6 @@ class versions {
if (!file_exists($this->_dbfile)) {
$this->_createDb();
if (!file_exists($this->_dbfile)) {
die("ERROR: unable to create sqlite database " . $this->_dbfile);
}
}
}
......@@ -60,8 +62,16 @@ class versions {
* create sqlite database - called in constructor if the file does not exist
*/
private function _createDb() {
echo "try to create file $this->_dbfile ...<br>\n";
return $this->_makeQuery($this->_sCreate);
if (file_exists($this->_dbfile)) {
echo "removing existing file $this->_dbfile ...<br>\n";
unlink($this->_dbfile);
}
echo "create database as file $this->_dbfile ...<br>\n";
$this->_makeQuery($this->_sCreate);
if (!file_exists($this->_dbfile)) {
die("ERROR: unable to create sqlite database " . $this->_dbfile);
}
return true;
}
/**
......@@ -71,39 +81,123 @@ class versions {
*/
private function _makeQuery($sSql) {
// $this->_log(__FUNCTION__."($sSql)");
echo "DEBUG: executing SQL<pre>$sSql</pre>";
$db = new PDO("sqlite:" . $this->_dbfile);
$result = $db->query($sSql);
$db = NULL;
// echo "DEBUG: executing SQL<pre>$sSql</pre>";
$oDb = new PDO("sqlite:" . $this->_dbfile);
$result = $oDb->query($sSql);
return $result;
}
/**
* execute a sql statement
* @param string $sSql sql statement
* @return database object
*/
private function _makeSelectQuery($sSql, $aKey=false) {
// $this->_log(__FUNCTION__."($sSql)");
// echo "DEBUG: executing select SQL<pre>$sSql</pre>";
$oDb = new PDO("sqlite:" . $this->_dbfile);
$oStatement = $oDb->prepare($sSql);
$oStatement->execute();
$aReturn=array();
while ($row = $oStatement->fetch(PDO::FETCH_ASSOC)) {
if ($aKey && array_key_exists($aKey, $row)){
$aReturn[] = $row[$aKey];
} else {
$aReturn[] = $row;
}
}
return $aReturn;
}
// ----------------------------------------------------------------------
// PUBLIC GETTER
// ----------------------------------------------------------------------
/**
* get list of current projects
* @return type
*/
public function getProjects(){
$sSql="select distinct(project) from `versions`";
return $this->_makeSelectQuery($sSql, 'project');
}
/**
* get phases of the current project; a project must be set be set before
* @return type
*/
public function getPhases(){
if (!$this->sProject){
die("ERROR: you need to set a project first. Use the setProject() method to do so.");
}
$sSql="select distinct(phase) from `versions`
WHERE
project='" . $this->sProject . "'
"
;
return $this->_makeSelectQuery($sSql,'phase');
}
/**
* get places of the current project; a project and must be set be set before
* @return type
*/
public function getPlaces(){
if (!$this->sProject || !$this->sPhase){
die("ERROR: you need to set a project, and phase first. Use the setProject() method to do so.");
}
$sSql="select distinct(place) from `versions`
WHERE
project='" . $this->sProject . "'
AND phase='" . $this->sPhase . "'
";
return $this->_makeSelectQuery($sSql, 'place');
}
/**
* get hosts that have installed a project
* @return type
*/
public function getHosts(){
if (!$this->sProject || !$this->sPhase){
die("ERROR: you need to set a project, and phase first. Use the setProject() method to do so.");
}
$sSql="select distinct(host) from `versions`
WHERE
project='" . $this->sProject . "'
AND phase='" . $this->sPhase . "'
AND place='installed'
";
return $this->_makeSelectQuery($sSql, 'host');
}
/**
* get versions of the current place (project, phase and place must be
* set before)
* @see setProject()
* @return type
*/
public function getVersion(){
if (!$this->sProject || !$this->sPhase || !$this->sPlace ){
die("ERROR: you need to set a project, phase and place. use the setProject() method to do so.");
die("ERROR: you need to set a project, phase and place first. Use the setProject() method to do so.");
}
$aReturn=array();
$sSql="select * from `versions`
$sSql="select data from `versions`
WHERE
project='" . $this->sProject . "'
AND phase='" . $this->sPhase . "'
AND place='" . $this->sPlace . "'
AND host='" . $this->sHost . "'
AND variable='version'
";
foreach ($this->_makeQuery($sSql) as $row) {
for ($i = 0; $i <= count($row); $i++) {
unset($row[$i]);
}
$aReturn[] = $row;
}
return $aReturn;
return $this->_makeSelectQuery($sSql, 'data');
}
/**
* return currebntly set project, phase, place and host
* @return type
*/
public function whereiam(){
return array(
'project'=>$this->sProject,
......@@ -112,6 +206,14 @@ class versions {
'host'=>$this->sHost,
);
}
/**
* return currebntly set project, phase, place and host
* @return type
*/
public function dumpdb(){
$sSql="select * from `versions`";
return $this->_makeSelectQuery($sSql);
}
// ----------------------------------------------------------------------
......@@ -166,7 +268,7 @@ class versions {
* update a version
* @return boolean
*/
public function updateVersion($sVersioninfos){
public function updateVar($sVarname,$sValue){
if (!$this->sProject || !$this->sPhase || !$this->sPlace ){
die("ERROR: you need to set a project, phase and place. use the setProject() method to do so.");
}
......@@ -180,11 +282,18 @@ class versions {
'" . $this->sPhase . "',
'" . $this->sPlace . "',
'" . $this->sHost . "',
'version',
'" . $sVersioninfos . "'
'" . $sVarname . "',
'" . $sValue . "'
);
";
return $this->_makeQuery($sSql);
}
/**
* update a version
* @return boolean
*/
public function updateVersion($sVersioninfos){
return $this->updateVar('version', $sVersioninfos);
}
}
<?php
echo '<pre>'.print_r($_GET, 1).'</pre>';
require './classes/versions.class.php';
/**
*
* VERSION HANDLER API
*
* Syntax des Aufrufs (als GET Schreibweise; ein POST soll (später) auch gehen):
* http://dev.ci.iml.unibe.ch:8002/versions/?action=update&project=ci&phase=preview&host=ci.preview.iml.unibe.ch&variable=version&value=007
*
* Parameter:
*
* action=update (get-Funktionen muss ich später noch bauen)
* project=[Projekt-ID (=Name des Package, das installiert wird)]
* phase=(preview|stage|live)
* host=[FQDN]
* variable=version (andere noch nicht unterstützt/ vorgesehen)
* value=[Versionsdaten]
*
* 2017-04-07 hahn first lines
*/
$oVersion=new versions();
$sProject='ci';
$sPhase='preview';
// ----------------------------------------------------------------------
// functions
// ----------------------------------------------------------------------
/**
* show an error message and quit with http status code 400 (Bad request)
* @param string $sMessage message to show
* @return boolean
*/
function quit($sMessage) {
$sProtocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header("$sProtocol 400: Bad request");
die("<h1>Bad request</h1>" . $sMessage);
return false;
}
/**
* get a request param from GET and POST scope (POST has priority) and
* verify it with execution of a cleanup array
* @param string $sKey key to search for in GET or POST
* @param string $sRegex4Cleanup regex for filtering
* @return type
*/
function getParam($sKey, $sRegex4Cleanup = false) {
$sValue=false;
if (array_key_exists($sKey, $_GET)) {
$sValue=$_GET[$sKey];
}
if (array_key_exists($sKey, $_POST)) {
$sValue=$_POST[$sKey];
}
if(!$sValue){
return false;
}
$sReturn = $sRegex4Cleanup ? preg_replace($sRegex4Cleanup, '', $sValue) : $sValue;
if ($sReturn !== $sValue) {
quit("ERROR: the parameter value in [$sKey = ...] has a wrong format.");
}
return $sReturn;
}
// ----------------------------------------------------------------------
// check required params
// ----------------------------------------------------------------------
if (!$_GET || !count($_GET)) {
quit("no parameter was found.");
}
foreach (array("action", "project") as $sKey) {
if (!array_key_exists($sKey, $_GET)) {
quit("value required: $sKey=");
}
}
// ----------------------------------------------------------------------
// get vars
// ----------------------------------------------------------------------
$sAction = getParam('action', '/[^a-z]/');
if ($sAction !== "get" && $sAction !== "update") {
quit("action is unknown or not implemented yet.");
}
$sProject = getParam('project', '/[^a-z\-0-9]/');
$sPhase = getParam('phase', '/[^a-z]/');
$sPlace = getParam('place', '/[^a-z]/');
$sHost = getParam('host', '/[^a-z\.\-0-9]/');
if ($sHost && !$sPlace) {
$sPlace = 'installed';
$sHost='preview02.ci';
}
$sVarname = getParam('variable', '/[^a-z]/');
$sValue = getParam('value', '');
// ----------------------------------------------------------------------
// init class
// ----------------------------------------------------------------------
require './classes/versions.class.php';
$oVersion = new versions();
$oVersion->setProject($sProject, $sPhase, $sPlace, $sHost);
print_r($oVersion->whereiam());
/*
if ($oVersion->updateVersion('{ "revision": "000" }')){
switch ($sAction) {
case 'get':
print_r($oVersion->getVersion());
} else {
echo "insert failed. :-(";
break;
case 'update':
if (!$sVarname) {
quit("ERROR: the update action requires a variable and a value.");
}
*/
switch ($sVarname) {
// case 'other-varname':
case 'version':
$oVersion->updateVar($sVarname, $sValue);
echo "OK: $sVarname was updated.";
break;
print_r($oVersion->getVersion());
default:
quit("ERROR: update of variable [$sVarname] is not supported.");
break;
}
break;
// $oVersion->
\ No newline at end of file
default:
quit("ERROR: the action is not supported.");
break;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment