Skip to content

Commit

Permalink
fixed multi role support. closes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
tamagokun committed Aug 6, 2014
1 parent 6d6e0dd commit 7185713
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
51 changes: 34 additions & 17 deletions lib/Pomander/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,31 @@ public function next_role($key)
public function multi_role_support($role,$app)
{
$this->role($role);
$tasks = $app->get_tasks();
foreach ($app->top_level_tasks as $task_name) {
if(!in_array($task_name, $app->get_task_list())) continue;
if( in_array($role,$app->resolve($task_name)->dependencies()) )
return $this->inject_multi_role_after($role,$task_name);
else {
foreach ($app->resolve($task_name)->dependencies() as $dependency) {
if( in_array($role,$app->resolve($dependency)->dependencies()) )
return $this->inject_multi_role_after($role,$dependency);
try {
$task = $app->get_task($task_name);
if (!$task->has_dependencies()) continue;
$deps = $task->get_dependencies();
if ($this->dependency_needs_multi_role($role, $deps)) {
return $this->inject_multi_role_after($role, $task_name);
} else {
foreach ($deps as $dep_name=>$dep) {
if (!$dep->has_dependencies()) continue;
if ($this->dependency_needs_multi_role($role, $dep->get_dependencies())) {
return $this->inject_multi_role_after($role, $dep_name);
}
}
}
} catch (\Exception $e) {
continue;
}
}
}

public function exec($cmd)
{
if(!$this->target) return run_local($cmd);
if (!$this->target) return run_local($cmd);
if (!$this->shell) {
$keypass = $this->key_password;
$auth = is_null($this->password)? $this->key_path : $this->password;
Expand All @@ -130,7 +139,7 @@ public function exec($cmd)

public function put($what,$where)
{
if($this->target)
if ($this->target)
$cmd = "{$this->rsync_cmd} -e \"ssh -i {$this->key_path}\" {$this->rsync_flags} $what {$this->user}@{$this->target}:$where";
else
$cmd = "cp -r $what $where";
Expand All @@ -140,7 +149,7 @@ public function put($what,$where)

public function get($what,$where)
{
if($this->target)
if ($this->target)
$cmd = "{$this->rsync_cmd} -e \"ssh -i {$this->key_path}\" {$this->rsync_flags} {$this->user}@{$this->target}:$what $where";
else
$cmd = "cp -r $what $where";
Expand Down Expand Up @@ -183,9 +192,9 @@ private function defaults()

private function update_target($target)
{
if( !$target ) return false;
if( $this->target == $target ) return true;
if( $this->shell ) $this->shell = null;
if (!$target) return false;
if ($this->target == $target) return true;
if ($this->shell) $this->shell = null;
$this->target = $target;
info("target",$this->target);

Expand All @@ -198,15 +207,23 @@ private function init_scm_adapter()
if( !$this->scm = new $scm($this->repository) )
abort("scm","There is no recipe for {$this->config["scm"]}, perhaps create your own?");
$adapter = "\\Pomander\\Db\\".ucwords(strtolower($this->config["adapter"]));
if( !$this->adapter = new $adapter($this->database) )
if (!$this->adapter = new $adapter($this->database))
abort("db","There is no recipe for {$this->config["adapter"]}, perhaps create your own?");
}

private function dependency_needs_multi_role($role, $deps)
{
if (in_array($role, array_keys($deps))) {
return true;
}

return false;
}

private function inject_multi_role_after($role,$task_name)
{
info("injecting after $task_name", '');
after($task_name,function ($app) use ($task_name,$role) {
if ( $app->env->next_role($role) ) {
after($task_name, function ($app) use ($task_name,$role) {
if ($app->env->next_role($role)) {
$app->reset();
$app->invoke($task_name);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function testDetectEnvironments()

$app = builder()->get_application();
$task_names = array_keys($app->get_tasks());
$this->assertSame(array('development', 'test', 'norelease'), $task_names);
$this->assertSame(array('development', 'multirole', 'test', 'norelease'), $task_names);
}

public function testCanFirstRun()
Expand Down
14 changes: 14 additions & 0 deletions tests/DeployTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,20 @@ public function testRollback()
$this->assertFalse(readlink($app->env->current_dir) == $app->env->release_dir);
}

public function testMultiRole()
{
ob_start();
$this->clean();

$app = $this->app(array("multirole", "check"));

$app->invoke("multirole");
$app->invoke("check");

ob_end_clean();
$this->assertTrue(3 === $app->env->role_count);
}

// private
protected function app($tasks = array(), $args = array())
{
Expand Down

0 comments on commit 7185713

Please sign in to comment.