Laravel CollectiveのremoteによるCSVデータ送信
はじめに
こんにちは、インディバル・デベロップメント室のエンジニアです。今回は「Laravel Collective」のremoteについて紹介しようと思います。
これに触れたきっかけは、いま担当しているサービスのサーバから他のサーバへcsvデータを送信したいとの要望がきっかけです。Laravelで実現できる方法について調べていたところ、「Laravel Collective」について知ることが出来ました。
パッケージのインストール
まずは、 composer.jsonに「laravelcollective/remote」を追加します。
1 2 3 4 5 |
"require": { "php": ">=7.1.0", "laravel/framework": "5.5.*", ...略 "laravelcollective/remote": "5.5.*" |
ご覧のとおり、私が担当しているサービスではLaravel5.5とphp7.1を使用しています。
そのためLaravelcollective/remoteのバージョンも5.5にしています。
他にバージョンがあるので、laravelcollective/remoteのgithubでチェックして適切なバージョンを使用するようにしましょう。
その後は「composer update」でパッケージをインストールします。インストールが完了したらvendor配下を確認しましょう。
1 2 3 |
vendor ㄴlaravelcollective ㄴremote |
このようなディレクトリ構成になっていれば成功です(´・ω・`)
セッテイング
「config/app.php」に下記のように追加します。
まずは「providers」配列。
1 2 3 4 5 |
'providers' => [ // ... Collective\Remote\RemoteServiceProvider::class, // ... ], |
次に、「aliases」にも以下を追加します。
1 2 3 4 5 |
'aliases' => [ // ... 'SSH' => Collective\Remote\RemoteFacade::class, // ... ], |
最後に、以下のaritsanを実行し、configファイルを公開しましょう。
1 |
php artisan vendor:publish --provider="Collective\Remote\RemoteServiceProvider" |
上記を実行するとconfig/remote.phpファイルが生成されます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
return [ /* |-------------------------------------------------------------------------- | Default Remote Connection Name |-------------------------------------------------------------------------- | | Here you may specify the default connection that will be used for SSH | operations. This name should correspond to a connection name below | in the server list. Each connection will be manually accessible. | */ 'default' => 'production', /* |-------------------------------------------------------------------------- | Remote Server Connections |-------------------------------------------------------------------------- | | These are the servers that will be accessible via the SSH task runner | facilities of Laravel. This feature radically simplifies executing | tasks on your servers, such as deploying out these applications. | */ 'connections' => [ 'production' => [ 'host' => '', 'username' => '', 'password' => '', 'key' => '', 'keytext' => '', 'keyphrase' => '', 'agent' => '', 'timeout' => 10, ], ], /* |-------------------------------------------------------------------------- | Remote Server Groups |-------------------------------------------------------------------------- | | Here you may list connections under a single group name, which allows | you to easily access all of the servers at once using a short name | that is extremely easy to remember, such as "web" or "database". | */ 'groups' => [ 'web' => ['production'], ], ]; |
この「connections」のところに各環境や送信先のサーバにログインするための情報を入れて置きます。
データを送ってみよう
まずは、「$localFile」に送信元のファイルパス、「$remoteFile」に送信先ファイルパスを指定します。
1 2 |
$localFile = '/var/〇〇/data.csv'; $remoteFile = '/var/△△/data.csv'; |
そして、
1 2 3 4 5 |
try{ SSH::into(env('ENV'))->put($localFile, $remoteFile); }catch (\Exception $e){ Logger::error($e); } |
のように書いて実行するとデータを送信することができます!
おわり