設定Configuration
Laravelはリモートサーバーへ接続し、コマンドを実行するシンプルな方法をサポートしています。これによりリモートサーバーで動作するArtisanタスクを簡単に作成することができます。SSH
ファサードはリモートサーバーへの接続とコマンドの実行を提供します。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.
設定ファイルはapp/config/remote.php
です。すべてのオプションはリモート接続の設定として必要です。connections
配列は、名前をキーとしたサーバーの接続リストです。connections
配列に、ただ接続情報を指定すれば、リモートタスクを実行する準備が整います。SSH
は認証にパスワードかSSHキーが使用できることに注意して下さい。The configuration file is located
at app/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.
**注目:**簡単に、いろいろなタスクをリモートサーバーで走らせたいのですか? なら、Envoyタスクランナーを試してください!Note: Need to easily run a variety of tasks on your remote server? Check out the Envoy task runner[#envoy-task-runner]!
基本的な使用法Basic Usage
デフォルトサーバーでコマンドを実行するRunning Commands On The Default Server
default
リモート接続は、SSH::run
メソッドで使用されます。To run commands on your
default
remote connection, use the
SSH::run
method:
SSH::run(array(
'cd /var/www',
'git pull origin master',
));
指定した接続でコマンドを実行するRunning Commands On A Specific Connection
別の方法として、into
メソッドを使用し、特定の接続でコマンドを実行することができます。Alternatively, you may run
commands on a specific connection using the
into
method:
SSH::into('staging')->run(array(
'cd /var/www',
'git pull origin master',
));
コマンドからの出力をキャッシュするCatching Output From Commands
run
メソッドに渡したクロージャーで、リモートコマンドの出力をキャッシュすることができます。You may catch the
"live" output of your remote commands by
passing a Closure into the run
method:
SSH::run($commands, function($line)
{
echo $line.PHP_EOL;
});
タスクTasks
いつも一緒に実行するコマンドのグループを定義したい場合は、define
メソッドでtask
を定義できます。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
:
SSH::into('staging')->define('deploy', array(
'cd /var/www',
'git pull origin master',
'php artisan migrate',
));
タスクが一度定義されると、task
メソッドを使用し実行できます。Once the task has been defined,
you may use the task
method to run
it:
SSH::into('staging')->task('deploy', function($line)
{
echo $line.PHP_EOL;
});
SFTPダウンロードSFTP Downloads
SSH
クラスは、get
とgetString
メソッドを使用し、ファイルをダウンロードするシンプルな方法を、持っています。The SSH
class
includes a simple way to download files using the
get
and getString
methods:
SSH::into('staging')->get($remotePath, $localPath);
$contents = SSH::into('staging')->getString($remotePath);
SFTPアップロードSFTP Uploads
SSH
クラスはファイルか文字列をサーバーにアップロードする簡単な方法を用意しています。put
、putString
メソッドを使用して下さい。The SSH
class also
includes a simple way to upload files, or even
strings, to the server using the put
and putString
methods:
SSH::into('staging')->put($localFile, $remotePath);
SSH::into('staging')->putString($remotePath, 'Foo');
リモートのログ表示Tailing Remote Logs
Laravelはリモート接続先にある、laravel.log
ファイルの最新部分を表示する便利なコマンドを用意しています。tail
Artisanコマンドを使用し、最新行を表示したいリモート接続の名前を指定して下さい。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:
php artisan tail staging
php artisan tail staging --path=/path/to/log.file
EnvoyタスクランナーEnvoy Task Runner
- インストールInstallation[#envoy-installation]
- タスク実行Running Tasks[#envoy-running-tasks]
- 複数サーバーMultiple Servers[#envoy-multiple-servers]
- 並列実行Parallel Execution[#envoy-parallel-execution]
- タスクマクロTask Macros[#envoy-task-macros]
- 通知Notifications[#envoy-notifications]
- EnvoyのアップデートUpdating Envoy[#envoy-updating-envoy]
Laravel Envoy(使節)は、リモートサーバーで定義済みの共通タスクを実行するための、クリーンで最低限の記法を提供します。Bladeスタイルの記述法を使用し、デプロイやArtisanコマンドなどのタスクを簡単にセットアップできます。Laravel Envoy provides a clean, minimal syntax for defining common tasks you run on your remote servers. Using a Blade[/docs/4.2/templates#blade-templating] style syntax, you can easily setup tasks for deployment, Artisan commands, and more.
注目: Envoyは、PHPバージョン5.4以上で、MacかLinuxのオペレーティングシステム上で動作します。Note: Envoy requires PHP version 5.4 or greater, and only runs on Mac / Linux operating systems.
インストールInstallation
最初に、EnvoyをComposerのglobal
コマンドでインストールします。First,
install Envoy using the
Composer
global
command:
composer global require "laravel/envoy=~1.0"
envoy
コマンドを端末で実行するときには、envoy
の実行ファイルが起動できるように、~/.composer/vendor/bin
ディレクトリーに、実行パスを通しておくのを忘れないでください。Make
sure to place the
~/.composer/vendor/bin
directory in your PATH
so the
envoy
executable is found when
you run the
envoy
command in your
terminal.
次に、Envoy.blade.php
ファイルをプロジェクトのルートに作成します。最初のサンプル設定をどうぞ。Next,
create an
Envoy.blade.php
file in the root of your
project. Here's an
example to get you
started:
@servers(['web' => '192.168.1.1'])
@task('foo', ['on' => 'web'])
ls -la
@endtask
見ての通り、最初に@servers
で、配列を定義しています。これらのサーバーへは、タスク宣言のon
オプションの中で、参照できます。@task
宣言の中に、タスクを実行するサーバーで走らせる、Bashコードを記述します。As
you can see, an array of
@servers
is
defined at the top of
the file. You can
reference these servers
in the on
option of your task
declarations. Within
your @task
declarations you should
place the Bash code that
will be run on your
server when the task is
executed.
init
コマンドを使用し、Envoyファイルのスタブを簡単に作成できます。The
init
command may be used to
easily create a stub
Envoy file:
envoy init user@192.168.1.1
タスク実行Running Tasks
タスクを実行するには、インストールしたEnvoyで、run
コマンドを実行してください。To
run a task, use the
run
command
of your Envoy
installation:
envoy run foo
必要であれば、コマンドラインスイッチを使用し、Envoyファイルへ変数を渡すことができます。If needed, you may pass variables into the Envoy file using command line switches:
envoy run deploy --branch=master
指定したオプションをBlade記法で使用できます。You may use the options via the Blade syntax you are used to:
@servers(['web' => '192.168.1.1'])
@task('deploy', ['on' => 'web'])
cd site
git pull origin {{ $branch }}
php artisan migrate
@endtask
準備コードBootstrapping
@setup
ディレクティブを使えば、Envoyファイルの中で変数宣言をしたり、一般的なPHPコードを動作させたりできます。You
may use the
@setup
directive to declare
variables and do general
PHP work inside the
Envoy file:
@setup
$now = new DateTime();
$environment = isset($env) ? $env : "testing";
@endsetup
また、@include
を使用し、PHPファイルを読み込むことも可能です。You
may also use
@include
to
include any PHP
files:
@include('vendor/autoload.php');
複数サーバーMultiple Servers
複数のサーバーに渡り、簡単にタスクを実行できます。タスク宣言で、サーバーのリストを指定するだけです。You may easily run a task across multiple servers. Simply list the servers in the task declaration:
@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
@task('deploy', ['on' => ['web-1', 'web-2']])
cd site
git pull origin {{ $branch }}
php artisan migrate
@endtask
デフォルトでは、タスクは各サーバーで、順番に実行します。つまり、最初のサーバーで実行が終わったら、次のサーバーの実行へと進みます。By default, the task will be executed on each server serially. Meaning, the task will finish running on the first server before proceeding to execute on the next server.
並列実行Parallel Execution
複数のサーバー間で、同時にタスクを実行したい場合は、ただparallel
オプションをタスク宣言で指定してください。If
you would like to run a
task across multiple
servers in parallel,
simply add the
parallel
option to your task
declaration:
@servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
@task('deploy', ['on' => ['web-1', 'web-2'], 'parallel' => true])
cd site
git pull origin {{ $branch }}
php artisan migrate
@endtask
タスクマクロTask Macros
マクロでは、一つのコマンドで順番に実行する、一連のタスクを定義します。例えば:Macros allow you to define a set of tasks to be run in sequence using a single command. For instance:
@servers(['web' => '192.168.1.1'])
@macro('deploy')
foo
bar
@endmacro
@task('foo')
echo "HELLO"
@endtask
@task('bar')
echo "WORLD"
@endtask
これで、deploy
マクロは、ひとつのシンプルなコマンドにより、実行されます。The
deploy
macro can now be run via
a single, simple
command:
envoy run deploy
通知Notifications
HipChatHipChat
タスクを実行後、あなたのチームのHipChatルームへ、シンプルな@hipchat
ディレクティブを使用し、通知を送ることができます。After
running a task, you may
send a notification to
your team's HipChat room
using the simple
@hipchat
directive:
@servers(['web' => '192.168.1.1'])
@task('foo', ['on' => 'web'])
ls -la
@endtask
@after
@hipchat('token', 'room', 'Envoy')
@endafter
また、カスタムメッセージをHipChatルームに指定することもできます。@setup
で変数を直接宣言できますし、@include
でファイルを読み込むこともできます。そうして宣言した変数をメッセージの中で使用できます。You
can also specify a
custom message to the
hipchat room. Any
variables declared in
@setup
or
included with
@include
will be available for
use in the
message:
@after
@hipchat('token', 'room', 'Envoy', "$task ran on [$environment]")
@endafter
これは、サーバーでタスクが実行されたことをチームへ必ず通知できる、とてもシンプルな方法です。This is an amazingly simple way to keep your team notified of the tasks being run on the server.
SlackSlack
Slackへ通知を送るには、以下の記述法が使用できます。The following syntax may be used to send a notification to Slack[https://slack.com]:
@after
@slack('team', 'token', 'channel')
@endafter
EnvoyのアップデートUpdating Envoy
Envoyを更新するには、ただ、self-update
コマンドを実行してください。To
update Envoy, simply run
the
self-update
command:
envoy self-update
/usr/local/bin
にEnvoyをインストールしている場合、sudo
を使用する必要があるでしょう。If
your Envoy installation
is in
/usr/local/bin
,
you may need to use
sudo
:
composer global update