altax icon indicating copy to clipboard operation
altax copied to clipboard

Feature Req: Bind additional parameters (key=>val) to each role

Open ptheofan opened this issue 9 years ago • 1 comments

Hi, would be very convenient if we could allocate some configuration resource (as simple as key=>val), and bind it to all nodes with 'role' => xyz.

would give the chance of the task runner to just read the assigned config and take decisions accordingly.

Here's a simple usecase

so, say I have 3 web nodes, 2 solr nodes and 1 graylog node. For starters, my paths would be totally different. Some instances have public path some don't, etc.

Server::node('www1.example.com', ['port' => 22, 'host' => '127.0.0.1']. ['web', 'masterweb']);
Server::node('www2.example.com', ['port' => 22, 'host' => '127.0.0.2']. ['web']);
Server::node('www3.example.com', ['port' => 22, 'host' => '127.0.0.3']. ['web']);
Server::node('solr1.example.com', ['port' => 22, 'host' => '127.0.0.10']. ['search', 'mastersearch']);
Server::node('solr2.example.com', ['port' => 22, 'host' => '127.0.0.11']. ['search']);
Server::node('gray1.example.com', ['port' => 22, 'host' => '127.0.0.20']. ['log']);

// Role specific configurations
Role::config('web', [
     'basePath' => '/var/www',
     'publicPath' => '/var/www/public',
]);

Role::config('masterweb', [
      'basePath' => '/var/master-app/www',
]);

Task::register('deploy', function($task) {
    // for role web would print var/www
   // for role web, masterweb - (merging left to right) var/master-app/www
    var_dump($task->basePath));

    // basePath => '.....',
    // publicPath => '....',
    var_dump($task->getConfig());
});

ptheofan avatar Feb 22 '16 19:02 ptheofan

I can hardly implement your suggestion, because I can not have enough time. I have my daily work now.

Workaround: The following code. getConfig function reads role's configuration by using a node.

Server::node("web1.example.com", ["web", "masterweb"]);
Server::node("web2.example.com", ["web"]);

Env::set("roleConfig", [
    "web" => [
     'basePath' => '/var/www',
     'publicPath' => '/var/www/public',
    ],
    "masterweb" => [
      'basePath' => '/var/master-app/www',
    ],
]);

function getConfig($node) {
    $roleConfig = Env::get("roleConfig");
    $roles = $node->getReferenceRoles();

    $config = null;
    foreach($roles as $role) {
        if (array_key_exists($role, $roleConfig)) {
            $config = $roleConfig[$role];
        }
    };

    return $config;
}

Task::register('sample', function($task) {

  $task->exec(function($process) {
       $node = $process->getNode();
       $config = getConfig($process->getNode());
       echo $node->getName() .": " .$config["basePath"]."\n";
       // web1.example.com: /var/master-app/www
       // web2.example.com: /var/www
  }, ["web"]);

});

kohkimakimoto avatar Feb 25 '16 01:02 kohkimakimoto