LaravelCollective / remote

Remote SSH Access for the Laravel Framework
MIT License
276 stars 106 forks source link

Dynamic configuration #41

Closed OleVik closed 7 years ago

OleVik commented 7 years ago

Running with Laravel 5.4, I set up a table to add connections for use with SSH. $server = \App\Server::find(1)->first()->toArray() returns a full configuration, and I tried to apply it like this (in remote.php):

use App\App;
use App\Http\Controllers;
$server = \App\Server::find(1)->first()->toArray();
return [
    'default' => 'production',
    'connections' => [
        'production' => [
            'host'      => $server['host'],
            'username'  => $server['username'],
            'password'  => $server['password'],
            'key'       => $server['key'],
            'keytext'   => $server['keytext'],
            'keyphrase' => $server['keyphrase'],
            'agent'     => $server['agent'],
            'timeout'   => $server['timeout']
        ],
...

But this returns:

FatalThrowableError in Model.php line 1013:
Call to a member function connection() on null

Is there a way to call dynamic data in remote.php, or pass it when constructing SSH? For example, in a controller I run:

    \SSH::run($commands, function($line)
   {
         echo $line.PHP_EOL;
   });

Could I pass the connection-details into SSH before executing run()?

OleVik commented 7 years ago

I found a solution. In the controller method, set the configuration values on runtime:

public function tryRemote()
{
    $target = [
        'host'      => '127.0.0.1',
        'username'  => 'User',
        'password'  => '',
        'key'       => 'keyfile-absolute-path',
        'keytext'   => '',
        'keyphrase' => 'phrase',
        'agent'     => '',
        'timeout'   => 10,
    ];
    config([
        'remote.connections.runtime.host' => $target['host'],
        'remote.connections.runtime.username' => $target['username'],
        'remote.connections.runtime.keyhost' => $target['key'],
        'remote.connections.runtime.keyphrase' => $target['keyphrase']
    ]);
    $commands = [
        'cd'
    ];
    \SSH::run($commands, function($line)
    {
        echo $line.PHP_EOL;
    });
}