Skip to content

Commit

Permalink
Web|Builder: Basic setup of the build database
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Feb 27, 2017
1 parent f72f347 commit c0f6504
Show file tree
Hide file tree
Showing 4 changed files with 288 additions and 33 deletions.
1 change: 1 addition & 0 deletions webapi/1/admin/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deny from all
208 changes: 208 additions & 0 deletions webapi/1/admin/bdb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

/*
* Doomsday Web API: Build Database Administration
* Copyright (c) 2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* License: GPL v2+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses/gpl.html
*/

require_once(__DIR__.'/../database.inc.php');

function add_build($json_args)
{
$args = json_decode($json_args);
if ($args == NULL) return; // JSON parse error.

$build = (int) $args->build;
$type = ($args->type == 'stable'? BT_STABLE :
$args->type == 'candidate'? BT_CANDIDATE : BT_UNSTABLE);
$version = $db->real_escape_string($args->version);
$major = (int) $args->major;
$minor = (int) $args->minor;
$patch = (int) $args->patch;
$label = $db->real_escape_string($args->label);

$db = db_open();
db_query($db, "INSERT INTO ".DB_TABLE_BUILDS
. " (build, type, version, major, minor, patch, label) VALUES ("
. "$build, $type, '$version', $major, $minor, $patch, '$label')");
$db->close();
}

function get_platform_id($db, $platform)
{
$result = db_query($db, "SELECT id FROM ".DB_TABLE_PLATFORMS
." WHERE platform='$platform'");
while ($row = $result->fetch_assoc()) {
return $row['id'];
}
return 0;
}

function add_file($json_args)
{
$args = json_decode($json_args);
if ($args == NULL) return; // JSON parse error.

$db = db_open();

$build = (int) $args->build;
$plat_id = get_platform_id($db, $args->platform);
$type = ($args->type == 'binary'? FT_BINARY :
$args->type == 'log'? FT_LOG :
$args->type == 'changes'? FT_CHANGES : FT_NONE);
$name = $db->real_escape_string($args->name);

$header = 'build, plat_id, type, name';
$values = "$build, $plat_id, $type, '$name'";

if (in_array('md5', $args)) {
$md5 = $db->real_escape_string($args->md5);
$header .= ', md5';
$values .= ", '$md5'";
}
if (in_array('signature', $args)) {
$sigature = $db->real_escape_string($args->signature);
$header .= ', signature';
$values .= ", '$signature'";
}

db_query($db, "INSERT INTO ".DB_TABLE_FILES." ($header) VALUES ($values)");
$db->close();
}

function remove_file($db, $file_id)
{
if ($path = db_file_path($db, $file_id)) {
echo("Deleting $path...\n");
unlink($path);
}
db_query($db, "DELETE FROM ".DB_TABLE_FILES." WHERE id=$file_id");
}

function purge_old_builds()
{
$expire_ts = time() - 100 * 24 * 60 * 60;

// Find non-stable builds that have become obsolete.
$db = db_open();
$result = db_query($db, "SELECT build FROM ".DB_TABLE_BUILDS
." WHERE type != ".BT_STABLE." AND UNIX_TIMESTAMP(timestamp) < $expire_ts");
$builds_sql = "";
$file_paths = array();
while ($row = $result->fetch_assoc()) {
$build = $row['build'];
echo("Purging build $build...");
if ($builds_sql) $builds_sql .= " OR ";
$builds_sql .= "build=$build";
foreach (db_list_build_files($db, $build) as $file) {
$file_paths[] = db_file_path($db, file);
}
}
if ($builds_sql) {
db_query($db, "DELETE FROM ".DB_TABLE_BUILDS." WHERE $builds_sql");
db_query($db, "DELETE FROM ".DB_TABLE_FILES ." WHERE $builds_sql");
}
$db->close();

// Remove the files, too.
foreach ($file_paths as $path) {
echo("Deleting: $path");
unlink($path);
}
}

//---------------------------------------------------------------------------------------

// Check arguments.
$op = $argv[1];

if ($op == 'drop_tables')
{
echo("Dropping database tables...\n");
$db = db_open();
db_query($db, "DROP TABLE IF EXISTS ".DB_TABLE_BUILDS);
db_query($db, "DROP TABLE IF EXISTS ".DB_TABLE_FILES);
db_query($db, "DROP TABLE IF EXISTS ".DB_TABLE_PLATFORMS);
$db->close();
}
else if ($op == 'init')
{
echo("Initializing database tables...\n");
$db = db_open();

$table = DB_TABLE_PLATFORMS;
$sql = "CREATE TABLE $table ("
. "id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, "
. "platform VARCHAR(50) NOT NULL, "
. "name VARCHAR(100) NOT NULL, "
. "os VARCHAR(20) NOT NULL, "
. "cpu_arch VARCHAR(20) NOT NULL, "
. "cpu_bits INT NOT NULL"
. ") CHARACTER SET utf8";
db_query($db, $sql);

// Set up the known platforms.
db_query($db, "INSERT INTO $table (platform, name, os, cpu_arch, cpu_bits) VALUES "
. "('win-x86', 'Windows 7 (32-bit)', 'windows', 'x86', 32), "
. "('win-x64', 'Windows 7 (64-bit)', 'windows', 'x64', 64), "
. "('mac10_8-x86_64', 'macOS 10.8', 'macx', 'x86_64', 64), "
. "('ubuntu-x86_64', 'Ubuntu 16.04 LTS', 'linux', 'amd64', 64), "
. "('fedora-x86_64', 'Fedora 23', 'linux', 'x86_64', 64), "
. "('source', 'Source', 'any', 'any', 0);");

// File archive.
$table = DB_TABLE_FILES;
$sql = "CREATE TABLE $table ("
. "id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, "
. "build INT UNSIGNED NOT NULL, "
. "plat_id INT UNSIGNED NOT NULL, "
. "type TINYINT NOT NULL, "
. "name VARCHAR(200) NOT NULL, "
. "md5 CHAR(32), "
. "signature TEXT, "
. "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
. ") CHARACTER SET utf8";
db_query($db, $sql);

// Builds.
$table = DB_TABLE_BUILDS;
$sql = "CREATE TABLE $table ("
. "build INT UNSIGNED NOT NULL PRIMARY KEY, "
. "type TINYINT NOT NULL, "
. "version VARCHAR(50) NOT NULL, "
. "major INT NOT NULL, "
. "minor INT NOT NULL, "
. "patch INT NOT NULL, "
. "label INT NOT NULL, "
. "blurb TEXT, "
. "timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP"
. ") CHARACTER SET utf8";
db_query($db, $sql);
$db->close();
}
else if ($op == 'add_build')
{
add_build(file_get_contents("php://input"));
}
else if ($op == 'add_file')
{
add_file(file_get_contents("php://input"));
}
else if ($op == 'purge')
{
purge_old_builds();
}

echo("Admin done.\n");
71 changes: 71 additions & 0 deletions webapi/1/database.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/*
* Doomsday Web API: Database Access
* Copyright (c) 2016-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* License: GPL v2+
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses/gpl.html
*/

require_once('api_config.inc.php'); // database config

// Opens the database connection.
// @return MySQLi object.
function db_open()
{
return new mysqli(DENG_DB_HOST, DENG_DB_USER, DENG_DB_PASSWORD, DENG_DB_NAME);
}

function db_query(&$db, $sql)
{
$result = $db->query($sql);
if (!$result) {
echo "Database error: " . $db->error;
echo "Query was: " . $sql;
exit;
}
return $result;
}

//---------------------------------------------------------------------------------------

define('DB_TABLE_BUILDS', 'bdb_builds');
define('DB_TABLE_FILES', 'bdb_files');
define('DB_TABLE_PLATFORMS', 'bdb_platforms');

define('FT_NONE', 0);
define('FT_BINARY', 1);
define('FT_LOG', 2);
define('FT_CHANGES', 3);

define('BT_UNSTABLE', 0);
define('BT_CANDIDATE', 1);
define('BT_STABLE', 2);

function db_list_build_files($db, $build)
{
$result = db_query($db, "SELECT id FROM ".DB_TABLE_FILES." WHERE build=$build");
$files = array();
while ($row = $result->fetch_assoc()) {
$files[] = $row['id'];
}
return $result;
}

function db_file_path($db, $file_id)
{
$result = db_query($db, "SELECT name FROM ".DB_TABLE_FILES." WHERE id=$file_id");
if ($row = $result->fetch_assoc()) {
return DENG_FILE_ARCHIVE_PATH.'/'.$row['name'];
}
return '';
}
41 changes: 8 additions & 33 deletions webapi/1/master_server.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Doomsday Web API: Master Server
* Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
* Copyright (c) 2016-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* License: GPL v2+
*
Expand All @@ -26,31 +26,12 @@
* - Remove expired entries from the database.
*/

require_once('api_config.inc.php'); // database config
require_once('database.inc.php');

define('DB_TABLE', 'servers');
define('EXPIRE_SECONDS', 900);
define('DEFAULT_PORT', 13209);

// Opens the database connection.
// @return MySQLi object.
function db_open()
{
return new mysqli(DENG_DB_HOST, DENG_DB_USER, DENG_DB_PASSWORD, DENG_DB_NAME);
}

function db_query(&$db, $sql)
{
$result = $db->query($sql);
if (!$result)
{
echo "Database error: " . $db->error;
echo "Query was: " . $sql;
exit;
}
return $result;
}

// Initializes the Servers database table.
function db_init()
{
Expand Down Expand Up @@ -98,8 +79,7 @@ function parse_announcement($json_data)

$address = ip2long($_SERVER['REMOTE_ADDR']);

if (!is_valid_host($address))
{
if (!is_valid_host($address)) {
echo 'Remote host has an invalid address';
exit;
}
Expand Down Expand Up @@ -143,8 +123,7 @@ function fetch_servers()

// Get all the remaining servers.
$result = db_query($db, "SELECT * FROM $table");
while ($row = $result->fetch_assoc())
{
while ($row = $result->fetch_assoc()) {
$sv = array(
"__obj__" => "Record",
"host" => long2ip($row['address']),
Expand Down Expand Up @@ -173,23 +152,19 @@ function fetch_servers()

//---------------------------------------------------------------------------------------

if ($_SERVER['REQUEST_METHOD'] == 'GET')
{
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// GET requests are for querying information about servers.
$op = $_GET['op'];
if ($op == 'list')
{
if ($op == 'list') {
$servers = fetch_servers();
echo json_encode($servers);
}
else if (DENG_SETUP_ENABLED && $op == 'setup')
{
else if (DENG_SETUP_ENABLED && $op == 'setup') {
echo "Initializing database...";
db_init();
}
}
else if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// POST requests are for submitting info about running servers.
parse_announcement(file_get_contents("php://input"));
}

0 comments on commit c0f6504

Please sign in to comment.