|
| 1 | +# SSH |
| 2 | + |
| 3 | +[](https://travis-ci.org/tshafer/remote) |
| 4 | +[](https://packagist.org/packages/laravelcollective/html) |
| 5 | +[](https://packagist.org/packages/laravelcollective/html) |
| 6 | +[](https://packagist.org/packages/laravelcollective/html) |
| 7 | +[](https://packagist.org/packages/laravelcollective/html) |
| 8 | + |
| 9 | +- [Installation](#installation) |
| 10 | +- [Configuration](#configuration) |
| 11 | +- [Basic Usage](#basic-usage) |
| 12 | +- [Tasks](#tasks) |
| 13 | +- [SFTP Downloads](#sftp-downloads) |
| 14 | +- [SFTP Uploads](#sftp-uploads) |
| 15 | +- [Tailing Remote Logs](#tailing-remote-logs) |
| 16 | + |
| 17 | +<a name="installation"></a> |
| 18 | +## Installation |
| 19 | + |
| 20 | +> If you have changed the top-level namespace to something like 'MyCompany', then you would use the new namespace instead of 'App'. |
| 21 | +
|
| 22 | +Begin by installing this package through Composer. Edit your project's `composer.json` file to require `laravelcollective/remote`. |
| 23 | + |
| 24 | + "require": { |
| 25 | + "laravelcollective/remote": "~5.0" |
| 26 | + } |
| 27 | + |
| 28 | +Next, update Composer from the Terminal: |
| 29 | + |
| 30 | + composer update |
| 31 | + |
| 32 | +Next, add your new provider to the `providers` array of `config/app.php`: |
| 33 | + |
| 34 | +```php |
| 35 | + 'providers' => [ |
| 36 | + // ... |
| 37 | + 'Collective\Remote\RemoteServiceProvider', |
| 38 | + // ... |
| 39 | + ], |
| 40 | +``` |
| 41 | + |
| 42 | +Finally, add two class aliases to the `aliases` array of `config/app.php`: |
| 43 | + |
| 44 | +```php |
| 45 | + 'aliases' => [ |
| 46 | + // ... |
| 47 | + 'SSH' => 'Collective\Remote\RemoteFacade', |
| 48 | + // ... |
| 49 | + ], |
| 50 | +``` |
| 51 | +<a name="configuration"></a> |
| 52 | +## Configuration |
| 53 | + |
| 54 | +Laravel includes a simple way to SSH into remote servers and run commands, allowing you to easily build Artisan tasks that work on remote servers. The `SSH` facade provides the access point to connecting to your remote servers and running commands. |
| 55 | + |
| 56 | +The configuration file is located at `config/remote.php`, and contains all of the options you need to configure your remote connections. The `connections` array contains a list of your servers keyed by name. Simply populate the credentials in the `connections` array and you will be ready to start running remote tasks. Note that the `SSH` can authenticate using either a password or an SSH key. |
| 57 | + |
| 58 | +> **Note:** Need to easily run a variety of tasks on your remote server? Check out the [Envoy task runner](http://laravel.com/docs/5.0/envoy)! |
| 59 | +
|
| 60 | +<a name="basic-usage"></a> |
| 61 | +## Basic Usage |
| 62 | + |
| 63 | +#### Running Commands On The Default Server |
| 64 | + |
| 65 | +To run commands on your `default` remote connection, use the `SSH::run` method: |
| 66 | + |
| 67 | + SSH::run([ |
| 68 | + 'cd /var/www', |
| 69 | + 'git pull origin master', |
| 70 | + ]); |
| 71 | + |
| 72 | +#### Running Commands On A Specific Connection |
| 73 | + |
| 74 | +Alternatively, you may run commands on a specific connection using the `into` method: |
| 75 | + |
| 76 | + SSH::into('staging')->run([ |
| 77 | + 'cd /var/www', |
| 78 | + 'git pull origin master', |
| 79 | + ]); |
| 80 | + |
| 81 | +#### Catching Output From Commands |
| 82 | + |
| 83 | +You may catch the "live" output of your remote commands by passing a Closure into the `run` method: |
| 84 | + |
| 85 | + SSH::run($commands, function($line) |
| 86 | + { |
| 87 | + echo $line.PHP_EOL; |
| 88 | + }); |
| 89 | + |
| 90 | +## Tasks |
| 91 | +<a name="tasks"></a> |
| 92 | + |
| 93 | +If you need to define a group of commands that should always be run together, you may use the `define` method to define a `task`: |
| 94 | + |
| 95 | + SSH::into('staging')->define('deploy', [ |
| 96 | + 'cd /var/www', |
| 97 | + 'git pull origin master', |
| 98 | + 'php artisan migrate', |
| 99 | + ]); |
| 100 | + |
| 101 | +Once the task has been defined, you may use the `task` method to run it: |
| 102 | + |
| 103 | + SSH::into('staging')->task('deploy', function($line) |
| 104 | + { |
| 105 | + echo $line.PHP_EOL; |
| 106 | + }); |
| 107 | + |
| 108 | +<a name="sftp-downloads"></a> |
| 109 | +## SFTP Downloads |
| 110 | + |
| 111 | +The `SSH` class includes a simple way to download files using the `get` and `getString` methods: |
| 112 | + |
| 113 | + SSH::into('staging')->get($remotePath, $localPath); |
| 114 | + |
| 115 | + $contents = SSH::into('staging')->getString($remotePath); |
| 116 | + |
| 117 | +<a name="sftp-uploads"></a> |
| 118 | +## SFTP Uploads |
| 119 | + |
| 120 | +The `SSH` class also includes a simple way to upload files, or even strings, to the server using the `put` and `putString` methods: |
| 121 | + |
| 122 | + SSH::into('staging')->put($localFile, $remotePath); |
| 123 | + |
| 124 | + SSH::into('staging')->putString($remotePath, 'Foo'); |
| 125 | + |
| 126 | +<a name="tailing-remote-logs"></a> |
| 127 | +## Tailing Remote Logs |
| 128 | + |
| 129 | +Laravel includes a helpful command for tailing the `laravel.log` files on any of your remote connections. Simply use the `tail` Artisan command and specify the name of the remote connection you would like to tail: |
| 130 | + |
| 131 | + php artisan tail staging |
| 132 | + |
| 133 | + php artisan tail staging --path=/path/to/log.file |
| 134 | + |
0 commit comments