Commit b0c5f6e9 authored by Leigh Stoller's avatar Leigh Stoller

Couple of new DB tables and libraries for them:

1. web_tasks: This table is to track background processes that we
   start either from the web interface or from the protogeni XMLRPC
   interface. The goal is to not have a bunch of task specific stuff
   in tables, but a more general way to start a process tied to an
   object (say, like taking an image) and record state of the process
   in the descriptor (say, the image size as it grows). A client can
   poll for info about the process; the server just looks for the task
   descriptor associated with the object and returns the (currently)
   adhoc data in JSON format to the client.

   Work in progress, still determining if this general approach is
   worth it.

2. web_sessions: Playing with real PHP sessions, using the DB to store
   the session data (instead of the default, which is data files in
   /var/run). No idea how I am going to use this one, just want to
   play with sessions.
parent fda1fbb7
#!/usr/bin/perl -wT
#
# Copyright (c) 2013-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file 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 Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
package WebSession;
use strict;
use Carp;
use Exporter;
use SelfLoader ();
use vars qw(@ISA @EXPORT $AUTOLOAD);
@ISA = qw(Exporter);
@EXPORT = qw( );
use emdb;
use JSON;
use Data::Dumper;
use overload ('""' => 'Stringify');
# Configure variables
my $TB = "@prefix@";
#
# Lookup by session id
#
sub Lookup($$)
{
my ($class, $id) = @_;
if ($id !~ /^[-\w]+$/) {
return undef;
}
my $query_result =
DBQueryWarn("select session_id,session_data, ".
" UNIX_TIMESTAMP(session_expires) as session_expires ".
" from web_sessions ".
"where session_id='$id' and session_expires>now()");
return undef
if (!$query_result || !$query_result->numrows);
my $self = {};
$self->{'DBROW'} = $query_result->fetchrow_hashref();
bless($self, $class);
#
# Turn the session data into a perl array so we can mess with it.
#
$self->{'DATA'} = eval { decode_json($self->session_data()); };
if ($@) {
print STDERR "Could not json decode session data\n";
return -1;
}
return $self;
}
AUTOLOAD {
my $self = $_[0];
my $type = ref($self) or croak "$self is not an object";
my $name = $AUTOLOAD;
$name =~ s/.*://; # strip fully-qualified portion
# A DB row proxy method call.
if (exists($self->{'DBROW'}->{$name})) {
return $self->{'DBROW'}->{$name};
}
# Or it is for a session data.
if (scalar(@_) == 2) {
return $self->{'DATA'}->{$name} = $_[1];
}
elsif (exists($self->{'DATA'}->{$name})) {
return $self->{'DATA'}->{$name};
}
carp("No such slot '$name' field in class $type");
return undef;
}
# Break circular reference someplace to avoid exit errors.
sub DESTROY {
my $self = shift;
$self->{'DBROW'} = undef;
$self->{'DATA'} = undef;
}
#
# Write the session back to the DB.
#
sub Store($)
{
my ($self) = @_;
my $id = $self->session_id();
my $data = eval { encode_json($self->{'DATA'}); };
if ($@) {
print STDERR "Could not json encode session data\n";
return -1;
}
$data = emdb::DBQuoteSpecial($data);
DBQueryWarn("update web_sessions set session_data=$data ".
"where session_id='$id'")
or return -1;
return 0;
}
#
# Stringify for output.
#
sub Stringify($)
{
my ($self) = @_;
my $id = $self->session_id();
my $expires = $self->session_expires();
return "[WebSession: $id: $expires]";
}
# Debugging.
sub Dump($)
{
my ($self) = @_;
print STDERR Dumper($self->{'DATA'}) . "\n";
}
# _Always_ make sure that this 1 is at the end of the file...
1;
#!/usr/bin/perl -wT
#
# Copyright (c) 2013-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file 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 Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
package WebTask;
use strict;
use Carp;
use Exporter;
use SelfLoader ();
use vars qw(@ISA @EXPORT $AUTOLOAD);
@ISA = qw(Exporter);
@EXPORT = qw( );
use emdb;
use libtestbed;
use JSON;
use Data::Dumper;
use overload ('""' => 'Stringify');
# Configure variables
my $TB = "@prefix@";
#
# Lookup by id
#
sub Lookup($$)
{
my ($class, $id) = @_;
if ($id !~ /^[-\w]+$/) {
return undef;
}
my $query_result =
DBQueryWarn("select *,UNIX_TIMESTAMP(created) as created ".
" from web_tasks ".
"where task_id='$id'");
return undef
if (!$query_result || !$query_result->numrows);
my $self = {};
$self->{'DBROW'} = $query_result->fetchrow_hashref();
$self->{'AUTOSTORE'} = 0;
#
# Turn the data into a perl array so we can mess with it.
#
$self->{'DATA'} = {};
if (defined($self->{'DBROW'}->{'task_data'}) &&
$self->{'DBROW'}->{'task_data'} ne "") {
$self->{'DATA'} =
eval { decode_json($self->{'DBROW'}->{'task_data'}); };
if ($@) {
print STDERR "Could not json decode task data\n";
return undef
}
}
bless($self, $class);
return $self;
}
AUTOLOAD {
my $self = $_[0];
my $type = ref($self) or croak "$self is not an object";
my $name = $AUTOLOAD;
$name =~ s/.*://; # strip fully-qualified portion
# A DB row proxy method call.
if (exists($self->{'DBROW'}->{$name})) {
return $self->{'DBROW'}->{$name};
}
# Or it is for a task data.
if (scalar(@_) == 2) {
$self->{'DATA'}->{$name} = $_[1];
$self->Store()
if ($self->{'AUTOSTORE'});
return $_[1];
}
elsif (exists($self->{'DATA'}->{$name})) {
return $self->{'DATA'}->{$name};
}
carp("No such slot '$name' field in class $type");
return undef;
}
# Break circular reference someplace to avoid exit errors.
sub DESTROY {
my $self = shift;
$self->{'DBROW'} = undef;
$self->{'DATA'} = undef;
}
# Mark the AUTOSTORE flag.
sub AutoStore($$)
{
my ($self, $flag) = @_;
$self->{'AUTOSTORE'} = $flag;
}
#
# Create a new web task. The task id is optional, it allows the
# web interface to pass in the task ID it wants us to use.
#
sub Create($$;$)
{
my ($class, $object_uuid, $task_id) = @_;
# Kill existing.
my $existing = WebTask->LookupByObject($object_uuid);
$existing->Delete()
if (defined($existing));
$task_id = TBGenSecretKey()
if (!defined($task_id));
DBQueryWarn("insert into web_tasks set task_id='$task_id', ".
" created=now(), object_uuid='$object_uuid'")
or return undef;
return WebTask->Lookup($task_id);
}
sub Delete($)
{
my ($self) = @_;
my $task_id = $self->task_id();
DBQueryWarn("delete from web_tasks where task_id='$task_id'")
or return -1;
return 0;
}
#
# Lookup by object id. We can get into a problem here if there
# is more then one.
#
sub LookupByObject($$)
{
my ($class, $uuid) = @_;
my $query_result =
DBQueryWarn("select task_id from web_tasks where object_uuid='$uuid'");
return undef
if (!$query_result || !$query_result->numrows);
my ($task_id) = $query_result->fetchrow_array();
return WebTask->Lookup($task_id);
}
#
# Write the data back to the DB.
#
sub Store($)
{
my ($self) = @_;
my $id = $self->{'DBROW'}->{'task_id'};
my $data = eval { encode_json($self->{'DATA'}); };
if ($@) {
print STDERR "Could not json encode task data\n";
return -1;
}
$data = emdb::DBQuoteSpecial($data);
DBQueryWarn("update web_tasks set modified=now(),task_data=$data ".
"where task_id='$id'")
or return -1;
return 0;
}
#
# Refresh the object from the database.
#
sub Refresh($)
{
my ($self) = @_;
my $id = $self->task_id();
my $query_result =
DBQueryWarn("select *,UNIX_TIMESTAMP(created) as created ".
" from web_tasks ".
"where task_id='$id'");
return -1
if (!$query_result || !$query_result->numrows);
$self->{'DBROW'} = $query_result->fetchrow_hashref();
#
# Turn the data into a perl array so we can mess with it.
#
$self->{'DATA'} = {};
if (isset($self->task_data()) && $self->task_data()) {
$self->{'DATA'} = eval { decode_json($self->task_data()); };
if ($@) {
print STDERR "Could not json decode task data\n";
return -1;
}
}
return 0;
}
#
# Stringify for output.
#
sub Stringify($)
{
my ($self) = @_;
my $id = $self->task_id();
my $uuid = $self->uuid();
return "[WebTask: $id: $uuid]";
}
# Debugging.
sub Dump($)
{
my ($self) = @_;
print STDERR Dumper($self->{'DATA'}) . "\n";
}
# Set the process ID.
sub SetProcessID($$)
{
my ($self, $pid) = @_;
my $task_id = $self->task_id();
DBQueryWarn("update web_tasks set process_id='$pid' ".
"where task_id='$task_id'")
or return -1;
$self->{'DBROW'}->{'process_id'} = $pid;
return 0;
}
#
# Mark as exited. We want to leave the row around long enough for
# anyone waiting to notice it has finished. We will have to GC these
# on a periodic basis.
#
sub Exited($$)
{
my ($self, $exitcode) = @_;
my $task_id = $self->task_id();
DBQueryWarn("update web_tasks set process_id=0, ".
" exited=now(), exitcode='$exitcode' ".
"where task_id='$task_id'")
or return -1;
$self->{'DBROW'}->{'process_id'} = 0;
$self->{'DBROW'}->{'exitcode'} = $exitcode;
$self->{'DBROW'}->{'exited'} = time();
return 0;
}
sub HasExited($)
{
my ($self) = @_;
return (defined($self->exited()) ? 1 : 0);
}
# _Always_ make sure that this 1 is at the end of the file...
1;
<?php
#
# Copyright (c) 2006-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file 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 Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
# Store PHP sessions in out DB. This came off the php manual page,
# in one of the comments. There is some oddity below for $_SESSION,
# which will go away when we are running php 5.4.
#
# I am not really sure how we will be usng sessions yet, for now
# just experimenting with it.
#
class SessionSaveHandler {
// session-lifetime
var $lifeTime;
public function __construct() {
global $TBAUTHDOMAIN;
# Enforce secure cookies, this domain only.
session_set_cookie_params(600, "/", $TBAUTHDOMAIN, 1);
session_set_save_handler(
array($this, "open"),
array($this, "close"),
array($this, "read"),
array($this, "write"),
array($this, "destroy"),
array($this, "gc")
);
register_shutdown_function('session_write_close');
}
function ValidSessionID($id) {
if (preg_match("/^[-\w]+$/", $id)) {
return TRUE;
}
return FALSE;
}
// Note that we have a permanent connection to the database, so
// we do not need to do open/close on it.
public function open($savePath, $sessionName) {
// get session-lifetime
$this->lifeTime = ini_get("session.gc_maxlifetime");
return true;
}
public function close() {
$this->gc($this->lifeTime);
return true;
}
public function read($id) {
if (! $this->ValidSessionID($id)) {
return "";
}
$query_result =
DBQueryWarn("select session_data from web_sessions ".
"where session_id='$id' and ".
" session_expires>now()");
if (! ($query_result && mysql_num_rows($query_result))) {
return "";
}
$row = mysql_fetch_array($query_result);
$foo = json_decode($row["session_data"]);
foreach ($foo AS $key => $value) {
$_SESSION[$key] = $value;
}
return "";
}
public function write($id, $data) {
if (! $this->ValidSessionID($id)) {
return "";
}
// new session-expire-time
$newExp = time() + $this->lifeTime;
// Convert to JSON, but need to unserialize the data first.
// We want the backend perl code to be able to use it.
$safe_data = addslashes(json_encode($_SESSION));
// is a session with this id in the database?
$query_result =
DBQueryFatal("select * FROM web_sessions ".
"where session_id = '$id'");
if (mysql_num_rows($query_result)) {
$query_result =
DBQueryWarn("update web_sessions set ".
" session_expires=FROM_UNIXTIME($newExp), ".
" session_data='$safe_data' ".
"where session_id='$id'");
if ($query_result && DBAffectedRows()) {
return true;
}
}
else {
// New session.
$query_result =
DBQueryWarn("replace into web_sessions set ".
" session_id='$id', ".
" session_expires=FROM_UNIXTIME($newExp), ".
" session_data='$safe_data'");
if ($query_result && DBAffectedRows()) {
return true;
}
}
// an unknown error occured
return false;
}
public function destroy($id) {
if (! $this->ValidSessionID($id)) {
return false;
}
$query_result =
DBQueryWarn("delete from web_sessions ".
"where session_id='$id'");
// if session was deleted, return true,
if ($query_result && DBAffectedRows()) {
return true;
}
return false;
}
public function gc($maxlifetime) {
// delete old sessions
DBQueryWarn("delete from web_sessions where session_expires<now()");
// return affected rows
return DBAffectedRows();
}
}
# Create once and we are done.
new SessionSaveHandler();
?>
<?php
#
# Copyright (c) 2006-2014 University of Utah and the Flux Group.
#
# {{{EMULAB-LICENSE
#
# This file is part of the Emulab network testbed software.
#
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This file 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 Affero General Public
# License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this file. If not, see <http://www.gnu.org/licenses/>.
#
# }}}
#
#
class WebTask {
var $webtask;
#
# Constructor by lookup on unique ID
#
function WebTask($task_id) {
$safe_id = addslashes($task_id);
$query_result =
DBQueryWarn("select * from web_tasks ".
"where task_id='$safe_id'");
if (!$query_result || !mysql_num_rows($query_result)) {
$this->webtask = NULL;
return;
}
$this->webtask = mysql_fetch_array($query_result);
}
# Hmm, how does one cause an error in a php constructor?
function IsValid() {
return !is_null($this->webtask);
}
# Lookup by imageid
function Lookup($id) {
$foo = new WebTask($id);
if (! $foo->IsValid())
return null;
return $foo;
}
# Lookup by object.
function LookupByObject($uuid) {
$query_result =
DBQueryWarn("select task_id from web_tasks ".
"where object_uuid='$uuid'");
if (!$query_result || !mysql_num_rows($query_result)) {
return null;
}
$row = mysql_fetch_array($query_result);
$idx = $row['task_id'];
return WebTask::Lookup($idx);
}
# accessors
function field($name) {
return (is_null($this->webtask) ? -1 : $this->webtask[$name]);
}
function task_id() { return $this->field("task_id"); }
function created() { return $this->field("created"); }
function modified() { return $this->field("modified"); }
function process_id() { return $this->field("process_id"); }
function object_uuid() { return $this->field("object_uuid"); }
function exitcode() { return $this->field("exitcode"); }
function exited() { return $this->field("exited"); }
function task_data() { return $this->field("task_data"); }
#
# Return the task data as a real object intead of JSON
#
function TaskData() {
if ($this->task_data()) {
return json_decode($this->task_data(), true);
}
else {
return array();
}
}
function ValidTaskID($id) {
if (preg_match("/^[-\w]+$/", $id)) {
return TRUE;
}
return FALSE;
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment