Search code examples
phplinuxlaravelsshenvoyproxy

How to run SSH command using Laravel Envoy


I want to run SSH command to start/stop Server with Laravel Envoy on Linux. But the am getting error for syntax of SSH::into(). I don't know what to do completely stuck. Need way to solve this and make this works. Envoy setup and works fine with "php artisan" command:

// Envoy.blade.php
@setup
     $connection = ['web' => $user.'@'.$host.' -p '. '22'];
@endsetup
@servers($connection)
@task('foo', ['on' => 'web'])
     php artisan
@endtask

Command: envoy run foo --user=root --host=94.130.97.242

And this is what am doing for SSH::into() to start/stop server.

// Envoy.blade.php
@include('vendor/autoload.php')
     $connection = ['web' => $user.'@'.$host.' -p '. '22'];
     $target = [
          'host' => $host,
          'username' => $user,
          'password' => $password,
          'timeout' => 10,
     ];

     Config::set('remote.connections.runtime.host', $target['host']);
     Config::set('remote.connections.runtime.port', '22');
     Config::set('remote.connections.runtime.username', $target['username']);
     Config::set('remote.connections.runtime.password', $target['password']);

@endsetup
@servers($connection)
@task('foo', ['on' => 'web'])
     SSH::into('runtime')->run(array(
          'docker stop ' . $server
     ));
@endtask

Command: envoy run foo --user=root --host=94.130.97.242 --password=password --server=22 After running it giive an error syntaxt error unexpected token'runtime'. I need your help. Thank you!


Solution

  • Here is a way with a Symfony process (natively available within Laravel)

    use Symfony\Component\Process\Process;
    
    function start(string $user, string $host)
    {
        $sshSetting = sprintf('ssh://%s@%s', $user, $host);
    
        $process = new Process([
            'docker', '-H', $sshSetting, 'start'
        ]);
    
        $process->run(); //synchronous
        $process->mustRun(); //synchronous, throw an exception whenever fail
        $process->start(); //asynchronous
    }
    

    Here is the official documentation https://symfony.com/doc/current/components/process.html#running-processes-asynchronously