Skip to content

Commit

Permalink
Upstart Adapter seems to be working and importing definitions OK, nee…
Browse files Browse the repository at this point in the history
…ds a lot more testing though. Lots of fun doing it!
  • Loading branch information
Alexandre Gomes Gaigalas committed Sep 29, 2010
1 parent a9a6558 commit 85bd37b
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 81 deletions.
6 changes: 2 additions & 4 deletions README.textile
@@ -1,7 +1,6 @@
h2. About

Respect\Daemon is PHP component to create and manage deamons using OS specific
tools.
Respect\Daemon is PHP component to create and manage deamons using OS specific tools.

h3. Sample Usage (not working yet)

Expand All @@ -27,5 +26,4 @@ Manages upstart (http://upstart.ubuntu.com) scripts and cron jobs.

h3. Windows

Manager windows services using the Windows Service Management API (through the
win32service PECL extension).
Manager windows services using the Windows Service Management API (through the win32service PECL extension).
152 changes: 144 additions & 8 deletions library/Respect/Daemon/Adapters/Upstart.php
Expand Up @@ -4,25 +4,31 @@

use Respect\Daemon\Exceptions\DirectoryNotFoundException;
use \UnexpectedValueException;
use \DirectoryIterator;
use Respect\Daemon\Job;
use Respect\Daemon\Meta;
use Respect\Daemon\Trigger;
use Respect\Daemon\EventListener;
use Respect\Daemon\Executable;

class Upstart
{

protected $dir;
protected $dirHandle;

public static function runsOnCurrentEnvironment()
public static function runsOnEnvironment()
{
$uname = php_uname();
if (!stripos($uname, 'linux'))
return false;
return stripos(system('initctl --vesion'), 'upstart');
return false !== stripos(system('initctl --vesion'), 'upstart');
}

public function __construct()
public function __construct($dir='/etc/init')
{
try {
$this->dir = trim(realpath('/etc/init'), DIRECTORY_SEPARATOR);
$this->dir = rtrim(realpath($dir), DIRECTORY_SEPARATOR);
$this->dirHandle = new DirectoryIterator($this->dir);
} catch (UnexpectedValueException $e) {
throw new DirectoryNotFoundException(
Expand All @@ -37,7 +43,7 @@ public function all()
foreach ($this->dirHandle as $jobFile) {
if (!$jobFile->isFile())
continue;
$scripts[] = $jobFile->getFilename();
$scripts[] = $jobFile->getBasename('.conf');
}
return $scripts;
}
Expand All @@ -58,8 +64,9 @@ public function remove($jobName)
public function get($jobName)
{
return $this->getJobFromDefinition(
$jobName,
file_get_contents(
$this->dir . DIRECTORY_SEPARATOR . $job->getName()
$this->dir . DIRECTORY_SEPARATOR . $jobName . '.conf'
)
);
}
Expand All @@ -84,9 +91,138 @@ protected function getDefinition(Job $job)

}

protected function getJobFromDefinition($definition)
public function getJobFromDefinition($name, $definition)
{

$lines = explode(PHP_EOL, $definition);
$job = new Job($name);
$previousStanza = null;
$scriptData = array();
foreach ($lines as $l) {
$stanza = $this->findStanza($l);
if (
(stripos($previousStanza, 'script') !== false
&& $previousStanza !== 'end script'
) || !empty($scriptData)) {
$scriptData[] = $l;
}
switch ($stanza) {
//meta
case 'author':
case 'console':
case 'description':
case 'emits':
case 'env':
case 'expect':
case 'export':
case 'instance':
case 'kill':
case 'normal exit':
case 'oom':
case 'respawn':
case 'task':
case 'umask':
$job->addMeta(
new Meta(
$stanza,
$this->findSingleValue($stanza, $l)
)
);
break;
//script
case 'script':
case 'post-start script':
case 'post-stop script':
case 'pre-start script':
case 'pre-stop script':
$scriptData[] = trim(str_replace('script', '', $stanza)) ? : 'main';
break;
case 'end script':
$job->addTrigger(
new Trigger(
array_shift($scriptData),
new Script(implode(PHP_EOL, $scriptData))
)
);
$scriptData = array();
break;
//events
case 'exec':
case 'post-start exec':
case 'post-stop exec':
case 'pre-start exec':
case 'pre-stop exec':
$job->addTrigger(
new Trigger(
trim(str_replace('exec', '', $stanza)) ? : 'main',
new Executable($this->findSingleValue($stanza, $l))
)
);
break;
case 'start on':
case 'stop on':
$job->addEventListener(
new EventListener(
trim(str_replace('on', '', $stanza)),
$this->findSingleValue($stanza, $l)
)
);
break;
//not implemented
default:
break;
}
$previousStanza = $stanza;
}
return $job;
}

protected function findSingleValue($stanza, $line)
{
if ($this->findStanza($line) !== $stanza)
return;
return trim(str_replace($stanza, '', $line), " \n\t\r\0\x0B\"");
}

protected function findStanza($line)
{
$stanzas = array(
'author',
'chdir',
'chroot',
'console',
'description',
'emits',
'env',
'exec',
'expect',
'export',
'instance',
'kill',
'limit',
'nice',
'normal exit',
'oom',
'post-start exec',
'post-stop exec',
'pre-start exec',
'pre-stop exec',
'post-start script',
'post-stop script',
'pre-start script',
'pre-stop script',
'respawn',
'script',
'session leader',
'start on',
'stop on',
'task',
'umask',
'version'
);
$line = trim($line);
foreach ($stanzas as $s)
if (stripos($line, $s) === 0)
return $s;
}

}
28 changes: 11 additions & 17 deletions library/Respect/Daemon/EventListener.php
Expand Up @@ -2,36 +2,30 @@

namespace Respect\Daemon;

class EventListener
class EventListener extends Meta
{

protected $action;
protected $event;
protected $name;
protected $value;

public function __construct($action='', $event='')
public function getName()
{
$this->action = $action;
$this->event = $event;
return $this->name;
}

public function getAction()
public function setName($name)
{
return $this->action;
$this->name = $name;
}

public function setAction($action)
public function getValue()
{
$this->action = $action;
return $this->value;
}

public function getEvent()
public function setValue($value)
{
return $this->event;
}

public function setEvent($event)
{
$this->event = $event;
$this->value = $value;
}

}
Expand Up @@ -2,6 +2,8 @@

namespace Respect\Daemon\Exceptions;

use \Exception;

class DirectoryNotFoundException extends Exception
{

Expand Down
7 changes: 6 additions & 1 deletion library/Respect/Daemon/Executable.php
Expand Up @@ -2,9 +2,14 @@

namespace Respect\Daemon;

class Executable
class Executable implements Runnable
{

protected $path;

public function __construct($path)
{
$this->path = $path;
}

}
58 changes: 10 additions & 48 deletions library/Respect/Daemon/Job.php
Expand Up @@ -5,71 +5,33 @@
class Job
{

protected $name;
protected $description;
protected $main;
protected $preStart;
protected $postStop;
protected $meta = array();
protected $eventListeners = array();
protected $triggers = array();

public function __construct($name)
{

$this->name = $name;
}

public function getName()
public function addTrigger(Trigger $trigger)
{
return $this->name;
$this->triggers[spl_object_hash($trigger)] = $trigger;
}

public function getDescription()
public function removeTrigger(Trigger $trigger)
{
return $this->description;
unset($this->triggers[spl_object_hash($trigger)]);
}

public function getMain()
public function addMeta(Meta $meta)
{
return $this->main;
$this->meta[spl_object_hash($meta)] = $meta;
}

public function getPreStart()
{
return $this->preStart;
}

public function getPostStop()
{
return $this->postStop;
}

public function setDescription($description)
{
$this->description = $description;
}

public function setMain(Runnable $main)
{
$this->main = $main;
}

public function setPreStart(Script $preStart)
{
$this->preStart = $preStart;
}

public function setPostStop(Script $postStop)
{
$this->postStop = $postStop;
}

public function addEventListener(Event $event)
public function addEventListener(EventListener $event)
{
$this->eventListeners[spl_object_hash($event)] = $event;
}

public function removeEventListener(Event $event)
{
unset($this->eventListeners[spl_object_hash($event)]);
}

}
2 changes: 1 addition & 1 deletion library/Respect/Daemon/Manager.php
Expand Up @@ -30,7 +30,7 @@ public static function getAvailableAdapters()
$runs = call_user_func(
array(
'\Respect\Daemon\Adapters\\' . $adapterName,
'runsOnCurrentEnvironment'
'runsOnEnvironment'
)
);
if ($runs)
Expand Down

0 comments on commit 85bd37b

Please sign in to comment.