k-holyのPHPとか諸々メモ

Webで働くk-holyがPHP(スクリプト言語)とか諸々のことをメモしていきます。ソースコードはだいたいWindowsで動かしてます。

Composerでプロジェクトグローバルにインストールした Stagehand_TestRunner + PHPUnit を ver.4 に更新(Windows7 + NYAOS編)

PHPUnit 4 に対応、PEARおよびPHPUnit3.6のサポートを終えた Stagehand_TestRunner V4 をローカル開発環境 (Windows7) に入れたメモです。特に新しいことはしていません。

内容的には Composerでプロジェクトグローバルに Stagehand_TestRunner + PHPUnit をインストール(Windows7 + NYAOS編) の続きになります。

前回時点で Stagehand_TestRunner は PHPUnit4系に未対応だったのですが、その後すぐ対応されてまして、もう2ヶ月経ってしまいました。

そういうわけで今回は PHPUnit 3.7 → 4.1, Stagehand_TestRunner 3.6 → 4.1 という更新を行います。

以下、シェルは NYAOS を使っています。

Windowsの場合 composer global コマンドでインストールしたファイルは %APPDATA%\Composer 以下に配置されます。

(私の環境では C:\Users\k_horii\AppData\Roaming\Composer になりました。)

composer.json ファイルもここにあります。

{
    "require": {
        "piece/stagehand-testrunner": "~4.1@dev",
        "phpunit/phpunit": "4.1.*"
    }
}

GitHubリポジトリを見たところ、Stagehand_TestRunnerの開発版4.1ではまだ安定版が出ていないPHPUnit4.2まですでに対応されているようです。

READMEの導きに従い @devフラグを付けて入れることにします。

@devの意味については kohkimakimoto さんの翻訳記事が参考になります。

なおPHPUnitの方は、安定版の4.1を指定しました。チキンですみません…。

$ composer global update
Changed current directory to C:/Users/k_horii/AppData/Roaming/Composer
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Removing symfony/class-loader (v2.4.5)
  - Installing sebastian/version (1.0.3)
    Downloading: 100%

  - Removing symfony/yaml (v2.4.5)
  - Installing symfony/yaml (v2.5.2)
    Loading from cache

  - Removing symfony/process (v2.4.5)
  - Installing symfony/process (v2.5.2)
    Downloading: 100%

  - Removing symfony/finder (v2.4.5)
  - Installing symfony/finder (v2.5.2)
    Downloading: 100%

  - Removing symfony/dependency-injection (v2.4.5)
  - Installing symfony/dependency-injection (v2.5.2)
    Downloading: 100%

  - Removing symfony/console (v2.4.5)
  - Installing symfony/console (v2.5.2)
    Loading from cache

  - Removing symfony/filesystem (v2.4.5)
  - Installing symfony/filesystem (v2.5.2)
    Loading from cache

  - Removing symfony/config (v2.4.5)
  - Installing symfony/config (v2.5.2)
    Loading from cache

  - Removing piece/stagehand-testrunner (v3.6.2)
  - Installing piece/stagehand-testrunner (dev-master 184496d)
    Cloning 184496d0c39b304a5db907e6d5b704ad0c436267

  - Installing sebastian/exporter (1.0.1)
    Downloading: 100%

  - Installing sebastian/environment (1.0.0)
    Downloading: 100%

  - Installing sebastian/diff (1.1.0)
    Loading from cache

  - Installing sebastian/comparator (1.0.0)
    Downloading: 100%

  - Removing phpunit/phpunit-mock-objects (1.2.3)
  - Installing phpunit/phpunit-mock-objects (2.1.5)
    Downloading: 100%

  - Removing phpunit/php-code-coverage (1.2.17)
  - Installing phpunit/php-code-coverage (2.0.9)
    Downloading: 100%

  - Removing phpunit/phpunit (3.7.37)
  - Installing phpunit/phpunit (4.1.4)
    Downloading: 100%

Writing lock file
Generating autoload files

PHPUnitはライブラリの一部が sebastian/*** に外出しされたみたいです。Stagehand_TestRunnerは最新のdev-masterが入りました。ついでにSymfonyコンポーネントも2.5系に更新されてます。

testrunner コマンドを実行してみると…

$ testrunner
PHP Warning:  require_once(Stagehand/TestRunner/Core/Bootstrap.php): failed to open stream: No such file or directory in C:\php\testrunner on line 86

Warning: require_once(Stagehand/TestRunner/Core/Bootstrap.php): failed to open stream: No such file or directory in C:\php\testrunner on line 86
PHP Fatal error:  require_once(): Failed opening required 'Stagehand/TestRunner/Core/Bootstrap.php' (include_path='.;c:\php\includes') in C:\php\testrunner on line 86

Fatal error: require_once(): Failed opening required 'Stagehand/TestRunner/Core/Bootstrap.php' (include_path='.;c:\php\includes') in C:\php\testrunner on line 86

以前にPEARでインストールした際の C:\php にtestrunnerコマンドが残っていて、PATH環境変数のせいでそちらが呼ばれているようです。

そういえば、NAYOSのalias設定にまかせてGlobal Composerのコマンドにパスを通してなかったような。

C:\php 以下のファイルは削除、NYAOSの設定ファイルを編集してPATH環境変数にパスを追加、前回aliasに定義したコマンドも修正します。

alias testrunner-phpunit "testrunner phpunit -c testrunner.yml | cat"

set PATH=%PATH%;%APPDATA%\Composer\vendor\bin

PEARサポート切り、Composerオートロード対応ということで、これだけでいけるようになったはず…。

$ source ~/_nya
$ testrunner-phpunit
Please run the following command before running the phpunit command:

  testrunner compile

あっ、また…(汗)

インストール後 phpunit コマンドの前に testrunner compile が必要なのでした。

$ testrunner compile
$ testrunner-phpunit
PHPUnit 4.1.4 by Sebastian Bergmann.

Configuration read from C:\Users\k_horii\Documents\Projects-priv\Volcanus\Volcanus_TemplateRenderer\phpunit.xml

The Xdebug extension is not loaded. No code coverage will be generated.

....................................

(中略)

Time: 1.85 seconds, Memory: 10.00Mb

OK (36 tests, 39 assertions)

やったー。

SparkleShareをWindows7に入れてみたメモ

SparkleShareWindows7に入れて使ってみたメモです。

SparkleShareが何なのかは、ちょっと古いですがこちらの記事が分かりやすかったです。

SparkleShareは、バージョン管理システムGitを利用したリアルタイムファイル共有システムです。 誤解を恐れず書くと オープンソース版オレオレDropboxが作れるツール というとわかりやすいでしょう。

SparkleShareを利用するにはクライアントソフトのSparkleShareのほかに、ファイルを中継して共有するための共有Gitリポジトリが必要になります。 そう書くと、とても面倒なように聞こえますがGitを利用したWebサービスgithubやGitorious、Bitbucketなどに対応しており、アカウントがあればすぐにSparkleShareを利用できます。

また、SparkleShare自体は 特定のディレクトリに置いたファイルを自動的にコミット・プッシュするGitクライアント ですので、Gitに抵抗のある人向けのGitクライアントとしても利用できます。

ちょっと、何か社内でGitを普及促進するのに良さそうじゃないですか?(今更とか言わないで…)というわけで、試してみました。

インストールと初期状態

クライアントのパッケージダウンロードは公式サイト http://sparkleshare.org から sparkleshare-windows-1.4.msi を入れました。

GitHubhbons/SparkleShare にあるものは古いので要注意…。)

インストーラに従ってユーザー名とメールアドレスを入力すると、以下のファイルが生成されます。

%HOMEPATH%\SparkleShare\ユーザー名's link code.txt

中身はOpenSSHの公開鍵なんですが、SSHとか知らなくてもこの「リンクコード」をGitサーバの管理者に送って設置してもらえばいいというわけですね。

実際に利用されると思われる秘密鍵と公開鍵、アプリケーションの設定らしきファイルやログファイルは、以下の場所に生成されていました。

%HOMEPATH%\AppData\Roaming\sparkleshare\2014-06-05_10h08.key
%HOMEPATH%\AppData\Roaming\sparkleshare\2014-06-05_10h08.key.pub
%HOMEPATH%\AppData\Roaming\sparkleshare\config.xml
%HOMEPATH%\AppData\Roaming\sparkleshare\logs\debug_log_2014-06-05.1.txt

SparkleShare.exe を起動するとタスクバーに入ります。

まずは公開鍵をGitサーバ側に登録しておく必要があるので、とりあえずGitHub上にshare-testプロジェクトを作成して、SparkleShare用の公開鍵を追加します。

タスクバーのアイコンをクリック → SparkleShare → Client ID → Copy to Clipboard でインストール時に作成されたOpenSSH形式の公開鍵をクリップボードにコピーします。

GitHubのプロジェクト k-holy/share-test から Settings → Deploy keys → Add deploy key でフォームが開くので、適当な名前付けてペーストして「Add key」。

タスクバーのアイコンをクリック → SparkleShare → Add hosted project... でGitリポジトリを追加します。

うまくいけば「Your shared project is ready!」というメッセージが表示されるので「Show files」をクリックすると、プロジェクトルート %HOMEPATH%\SparkleShare\share-test が開きます。

初期状態ではこんな構成

%HOMEPATH%\SparkleShare\share-test\.git
%HOMEPATH%\SparkleShare\share-test\.sparkleshare
%HOMEPATH%\SparkleShare\share-test\SparkleShare.txt

SparkleShare.txtを覗いてみると…

Congratulations, you've successfully created a SparkleShare repository!

Any files you add or change in this folder will be automatically synced to 
ssh://git@github.com/k-holy/share-test and everyone connected to it.

SparkleShare is an Open Source software program that helps people collaborate and 
share files. If you like what we do, consider buying us a beer: http://www.sparkleshare.org/

Have fun! :)

おっと思いつつGitHubリポジトリを確認してみると、すでに上記のファイルが commit & push されている状態でした。

.sparkleshare には何かのハッシュ値らしきものが書かれていました。

コミットの内容はこんな感じ。

コミットログには、変更のあったファイル名を列挙した内容が自動的にセットされるようです。なるほど。

SparkleShareは自動でリポジトリと同期してくれるGitクライアント

今度はローカルで新規フォルダ test を作成してみます。Gitでは空のフォルダは無視されるはずですが、どうでしょうか…。

作成したフォルダの直下に .empty というファイルが自動的に作成された状態で commit & push されています。なるほど。

(「I'm a folder!」とか別に要らないよぉって感じですが…)

もちろん、これらの変更をクライアント側で見ることもできます。

タスクバーのアイコンをクリック → SparkleShare → Recent changes...

ユーザー名@プロジェクト名 のリンクをクリックすると、プロジェクトルートのフォルダをエクスプローラで開きます。

一覧に表示されているファイル名のリンクをクリックすると、そのファイルが拡張子に関連付けられたアプリケーションで開きます。

なるほど。これは「Dropboxクローン」というよりも、管理対象フォルダ以下の変更を検知して自動でリポジトリと同期してくれるGitクライアントですね。

日本語ファイル名は一応使えるけど一部で化ける?

新規フォルダ てすと を作成して、日本語のファイル名が使えるかどうかも確認します。

なんか微妙に文字化けしてますね…それに、フォルダ命名してる間にコミットされてしまったようです。

盛んに紹介されていた2012年頃の記事には、SparkleShareに同梱されているmsysgitの問題で日本語ファイルが使えないという情報がありますが、2014年6月現在のバージョン SparkleShare 1.4 + Git 1.8.0 では大丈夫でした。

次はファイルの中身も日本語にしてみます。

ファイルを作成…

ファイル名を変更…

UTF-8で中身を記述。diffは文字化けしてません。

中身をShift_JISに変更して保存したところ、diffが文字化け。

クライアント側のRecent Changesはこんな感じ。

ちょっと惜しい…クライアント側にとってコミットログはそれほど重要な情報ではないと思いますが、文字化けしたファイルへのリンクが切れちゃってるのが残念。

なお、複数ユーザーで触った場合のRecent Changesはこんな感じでした。

クライアント間の更新通知は notifications.sparkleshare.org を仲介して行われる

複数クライアント間でリアルタイムに同期を取るには、別のクライアントで更新されたことを通知する必要があると思いますが、クライアント間の更新通知については、公式リポジトリWikiにそれらしい情報がありました。

SparkleShare uses a small script that handles update notifications between clients.

By default it uses the one running on notifications.sparkleshare.org.

The only information sent to this service is a hash of a folder identifier and a hash of the current revision.

The service then tells the other connected clients that are subscribed to a folder that they can pull new changes from wherever your repository is hosted.

This allows SparkleShare clients to sync new changes instantly, instead of polling with potentially long delays (up to 10 minutes).

英語はよく分かりませんが、notifications.sparkleshare.org を介して、フォルダID(設定ファイルの <identifier>544d01eae5adc8ea5f8765f84f3d6ae5fdd9846e</identifier> の値)とリビジョンIDをやり取りすることで、クライアント間の同期を取る仕組みのようです?

ただ、前述のログファイル %HOMEPATH%\AppData\Roaming\sparkleshare\logs\debug_log_2014-06-05.1.txt を見たところ、1分間隔で notifications.sparkleshare.org へ接続を試みているものの、失敗しているようでした。

23:43:43 | Listener | Trying to reconnect to tcp://notifications.sparkleshare.org:443/
23:43:45 | Listener | Disconnected from tcp://notifications.sparkleshare.org:443/: 対象のコンピューターによって拒否されたため、接続できませんでした。 204.62.14.135:443
23:43:45 | share-test | Falling back to regular polling

対象のコンピューターによって拒否されたため、接続できませんでした。 というメッセージは相手から拒否されたように読めますが、これはクライアント側の問題でしょうか。

該当メッセージでGoogle検索してみると、様々なWindowsアプリケーションでTCP通信時にこのエラーが発生することがあるようですが、ちょっと自分ではすぐに解決できなさそうなので保留…。

リポジトリへの反映は即時、リポジトリからの取り込みは定期的に行われる

前述の通り、管理対象フォルダへの変更は即時に検知され、リポジトリに反映されます。

これもログファイルを見ると、何となく動きが分かりました。

23:41:08 | Cmd | share-test | git status --porcelain
23:41:08 | Local | share-test | Activity detected, waiting for it to settle...
23:41:10 | Local | share-test | Activity has settled
23:41:10 | Cmd | share-test | git status --porcelain
23:41:10 | SyncUp | share-test | Initiated
23:41:10 | Cmd | share-test | git add --all
23:41:10 | Cmd | share-test | git status --porcelain
23:41:10 | Cmd | share-test | git config user.name "k-holy"
23:41:10 | Cmd | share-test | git config user.email "k.holy74@gmail.com"

リポジトリへのコミットで使用される user.name と user.email が、前述の設定ファイル %HOMEPATH%\AppData\Roaming\sparkleshare\config.xml の内容になっています。

<?xml version="1.0" encoding="UTF-8"?>
<sparkleshare>
  <user>
    <name>k-holy</name>
    <email>k.holy74@gmail.com</email>
  </user>
  <notifications>False</notifications>
  <folder>
    <name>share-test</name>
    <identifier>544d01eae5adc8ea5f8765f84f3d6ae5fdd9846e</identifier>
    <url>ssh://git@github.com/k-holy/share-test</url>
    <backend>Git</backend>
  </folder>
</sparkleshare>

git config の後 git commit コマンドを発行しており、コミットログの文字化けはこの時点で発生しているようです。

自動生成されるコミットメッセージへのファイル名の受け渡しでマルチバイトが考慮されていないのでしょうけど、どうすればいいのやら…。

23:41:10 | Cmd | share-test | git commit --all --message="+ ‘test/譁ー縺励>繝・く繧ケ繝・繝峨く繝・繝。繝ウ繝・txt’
" --author="k-holy <k.holy74@gmail.com>"
23:41:10 | Cmd | share-test | git push --progress "ssh://git@github.com/k-holy/share-test" master
23:41:13 | Git | share-test | Counting objects: 6, done.
23:41:13 | Git | share-test | Delta compression using up to 4 threads.
23:41:13 | Git | share-test | Total 4 (delta 1), reused 0 (delta 0)
23:41:14 | Git | share-test | To ssh://git@github.com/k-holy/share-test
23:41:14 | Git | share-test |    41b5b0b..da151e2  master -> master
23:41:14 | SyncUp | share-test | Done
23:41:14 | Cmd | share-test | git log --since=1.month --raw --find-renames --date=iso --format=medium --no-color -m --first-parent
23:41:14 | Cmd | share-test | git rev-parse HEAD
23:41:14 | Listener | Can't send message to tcp://notifications.sparkleshare.org:443/. Queuing message

最後に Listener が Can't send message to tcp://notifications.sparkleshare.org:443/. Queuing message と言ってるのは、更新通知を送ろうとして接続に失敗しているようです。

(表には一切何も表示されませんが、裏側では結構エラーが出てたりするようで、この辺は要注意ですね…。)

また、クライアント間の更新通知とは別に、リモートのGitリポジトリへの更新チェックが5分間隔で行われるようです。

23:45:47 | Git | share-test | Checking for remote changes...
23:45:47 | Cmd | share-test | git rev-parse HEAD
23:45:47 | Cmd | share-test | git ls-remote --heads --exit-code "ssh://git@github.com/k-holy/share-test" master
23:45:51 | Git | share-test | No remote changes, local+remote: 9ecb3784403aa484bd08b618dee765927ab04bda

こちらは問題なく完了していますし、そこまでリアルタイム同期にこだわらないのであれば、これでも充分という気がします。

SparkleShareはテキストコンテンツ管理に向いてるかも

似たような趣旨で紹介される ownCloud が完全なWeb + DBアプリケーションであるのに対して、中身は全く異なることが分かりました。

SparkleShareはGitとおんぶだっこ。だがそれがいい

ディレクトリの状態が変更されるたびに自動コミットされてコミットの粒度が最小になってしまうため、通常のソフトウェア開発には不向きでしょう。

いわゆる静的サイトジェネレータのような、リポジトリを一種のデータストアとして扱う仕組みと相性が良いんじゃないでしょうか。

クライアント側のUIにはSSHやGitコマンドはもちろんリビジョンIDすら登場せず、バージョン管理の概念すら知らなくてもファイルの共有と同期、そして履歴管理の恩恵を受けられるというのは、なかなか凄いことだと思います。

応用例として、RedmineやGitoliteを組み合わせて、フラットODFという単一のXMLファイルに文書とメタ情報とBase64エンコードされた画像などを詰め込んだ文書の管理システムを構築したという方も。

クライアント側ではLibreOffice WriterでフラットODFファイルを出力、SparkleShareでリポジトリに反映、Gitoliteを使うことでユーザーの権限制御を実現しつつ、Redmineを管理者によるタスク管理とリポジトリビューアとして利用する仕組みのようです。

Gitに乗っかることで、こういう組み合わせの可能性が広がるのもいいですね。

Composerでプロジェクトグローバルに Stagehand_TestRunner + PHPUnit をインストール(Windows7 + NYAOS編)

今度はローカル開発環境の Windows7 + NYAOS で composer global install してみました。

以下、シェルは NYAOS を使っています。

Composerをインストール

当然すでにcomposerは導入済みなのですが、Linuxの場合と同じ手順を踏んでやってみます。

PHPのインストールディレクトリ C:\php に composer.phar をインストールします。

$ cd C:\php
$ curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: C:\php\composer.phar
Use it: php composer.phar

ここからがWindows限定の作業ですが、以下のようなバッチファイルを作成して、composer.phar を代行させます。

中身はStagehand_TestRunnerの testrunner.bat を参考にしました。

(その testrunner.bat では symfony.bat を参考にされてるみたいです)

@echo off

REM This script will do the following:
REM - check for PHP_COMMAND env, if found, use it.
REM   - if not found detect php, if found use it, otherwise err and terminate

IF "%OS%"=="Windows_NT" @SETLOCAL

REM %~dp0 is expanded pathname of the current script under NT
SET SCRIPT_DIR=%~dp0

GOTO INIT

:INIT

IF "%PHP_COMMAND%" == "" GOTO NO_PHPCOMMAND

IF EXIST ".\composer" (
  "%PHP_COMMAND%" ".\composer.phar" %1 %2 %3 %4 %5 %6 %7 %8 %9
) ELSE (
  "%PHP_COMMAND%" "%SCRIPT_DIR%composer.phar" %1 %2 %3 %4 %5 %6 %7 %8 %9
)
GOTO CLEANUP

:NO_PHPCOMMAND
REM ECHO ------------------------------------------------------------------------
REM ECHO WARNING: Set environment var PHP_COMMAND to the location of your php.exe
REM ECHO          executable (e.g. C:\PHP\php.exe).  (assuming php.exe on PATH)
REM ECHO ------------------------------------------------------------------------
SET PHP_COMMAND=php.exe
GOTO INIT

:CLEANUP
IF "%OS%"=="Windows_NT" @ENDLOCAL
REM PAUSE

REM Local Variables:
REM mode: conf
REM coding: iso-8859-1
REM indent-tabs-mode: nil
REM End:

composerコマンド打ってみる。

$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 64ac32fca9e64eb38e50abfadc6eb6f2d0470039 2014-05-24 20:57:50

OKです。

ComposerでStagehand_TestRunnerをインストール

再びこちらを参考

$ composer global require piece/stagehand-testrunner:3.6.*
Changed current directory to C:/Users/k_horii/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/yaml (v2.4.5)
    Downloading: 100%

  - Installing symfony/process (v2.4.5)
    Downloading: 100%

  - Installing symfony/finder (v2.4.5)
    Downloading: 100%

  - Installing symfony/dependency-injection (v2.4.5)
    Downloading: 100%

  - Installing symfony/console (v2.4.5)
    Downloading: 100%

  - Installing symfony/filesystem (v2.4.5)
    Downloading: 100%

  - Installing symfony/config (v2.4.5)
    Downloading: 100%

  - Installing symfony/class-loader (v2.4.5)
    Downloading: 100%

  - Installing piece/stagehand-componentfactory (v1.0.1)
    Loading from cache

  - Installing piece/stagehand-alterationmonitor (2.0.0)
    Loading from cache

  - Installing piece/stagehand-testrunner (v3.6.2)
    Downloading: 100%

symfony/dependency-injection suggests installing symfony/proxy-manager-bridge (Generate service proxies to lazy load them)
symfony/console suggests installing symfony/event-dispatcher ()
piece/stagehand-testrunner suggests installing phpunit/phpunit (>=3.6.0)
Writing lock file
Generating autoload files

Windowsの場合、composer global でインストールしたファイルは %APPDATA%\Composer 以下に配置されるようです。

私の環境では具体的には C:\Users\k_horii\AppData\Roaming\Composer になりました。

composerでインストールしたライブラリの実行ファイルは C:\Users\k_horii\AppData\Roaming\Composer\vendor\bin 以下に配置されました。

testrunner コマンドを実行してみると…

$ source ~/_nya
$ %APPDATA%\Composer\vendor\bin\testrunner
Warning: require_once(Stagehand/TestRunner/Core/Bootstrap.php): failed to open stream: No such file or directory in C:\Users\k_horii\AppData\Roaming\Composer\vendor\piece\stagehand-testrunner\bin\testrunner on line 52

案の定、前回と同じ警告で詰まりましたので、同じように -p オプションでComposerのオートロードスクリプトを読ませてみます。

$ %APPDATA%\Composer\vendor\bin\testrunner -p %APPDATA%\Composer\vendor\autoload.php
Stagehand_TestRunner version @package_version@

Copyright (c) 2005-2013 KUBO Atsuhiro and contributors,
All rights reserved.

Usage:
  [options] command [arguments]

Options:
  --help           -h Prints help and exit.
  --version        -V Prints version information and exit.
  --ansi              Enables ANSI output.
  --no-ansi           Disables ANSI output.

Testing Framework Commands:
  cakephp               Runs tests with CakePHP.
  ciunit                Runs tests with CIUnit.
  phpspec               Runs tests with PHPSpec.
  phpunit               Runs tests with PHPUnit.
  simpletest            Runs tests with SimpleTest.
Other Commands:
  compile               Compiles the DIC for the production environment.
  help                  Prints the help for a command.
  list                  Lists commands.
  phpunit:passthrough   Runs the phpunit command via the testrunner command.

無事にコマンドを実行できました。

ComposerでPHPUnit(3.7系)をインストール

Windowsの方でも3.7系を入れることにします。

$ composer global require phpunit/phpunit:3.7.*
Changed current directory to C:/Users/k_horii/AppData/Roaming/Composer
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing phpunit/php-text-template (1.2.0)
    Loading from cache

  - Installing phpunit/phpunit-mock-objects (1.2.3)
    Loading from cache

  - Installing phpunit/php-timer (1.0.5)
    Loading from cache

  - Installing phpunit/php-token-stream (1.2.2)
    Downloading: 100%

  - Installing phpunit/php-file-iterator (1.3.4)
    Loading from cache

  - Installing phpunit/php-code-coverage (1.2.17)
    Downloading: 100%

  - Installing phpunit/phpunit (3.7.37)
    Downloading: 100%

phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/php-code-coverage suggests installing ext-xdebug (>=2.0.5)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files

3.7.37が入りました。

$ ls -l %APPDATA%\Composer\vendor\bin
-a--rw-      151 May 27 17:05               phpunit
-a--rw-       96 May 27 17:05               phpunit.bat
-a--rw-      156 May 27 16:31 testru~1      testrunner
-a--rwx      101 May 27 16:31 testru~1.bat  testrunner.bat

こんな感じで、testrunner.batに続いてphpunit.batが配置されてます。

プロジェクトルートでテスト実行

プロジェクトルートに移動してtestrunnerコマンドでPHPUnitのテストを実行してみます。

-c オプションでStagehand_TestRunnerの設定ファイルを読み込ませます。

$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -c testrunner.yml
Please run the following command before running the phpunit command:

  testrunner compile

phpunit command の前に testrunner compile しろって言われたので、その通りにします。

$ %APPDATA%\Composer\vendor\bin\testrunner compile -p %APPDATA%\Composer\vendor\autoload.php
$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml

テストを実行できましたが、例のごとくエスケープシーケンスによる色付けが効かないので、msysGitのcatにテスト結果を渡します。

$ %APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml | cat

色付けもできました。

こちらではGrowl経由の通知もされています。

NYAOSにコマンドのエイリアスを定義

最後に、NYAOSでは初期設定ファイル _nya にコマンドのエイリアスを定義できますので、以下を追加します。

alias testrunner-phpunit "%APPDATA%\Composer\vendor\bin\testrunner phpunit -p %APPDATA%\Composer\vendor\autoload.php -c testrunner.yml | cat"
$ source ~/_nya
$ testrunner-phpunit

これで、testrunner-phpunitコマンドでテストが可能になりました。

これまで歴史的な経緯により(?) PEARは C:\xampp\php に入れて動かしていたのですが、今ではローカル開発時はPHPは C:\php でビルトインWebサーバーを動かしており、XAMPPの機能は使っていなかったため、この機会にPEARと同時にXAMPP離れも断行しました。

噂のPhalcon FrameworkをWindowsで動かしてみた

"The fastest PHP Framework" を標榜する Phalcon フレームワーク、実はWindows版もあるんです。

Phalcon for Windows を入れる

extension として実装されてるわけですが、Windowsの場合は DLL ファイルを置くだけなのである意味Linux版よりもインストールは楽ちんです。

環境に合うものをダウンロードして展開、php_phalcon.dll を php.ini で設定された extension_dir に配置します。

(C:\phpPHPをインストールした場合、多分 C:\php\ext になるはず)

php.iniへの設定追加も忘れずに。

php.ini

extension=php_phalcon.dll

Phalcon DevTools を入れて phalcon コマンドの準備

Phalcon の流儀に従って一からファイルを作成してもいいんですが…

Phalcon DevTools に付属の phalcon コマンドを使えば、プロジェクトのひな形を作成してくれるので、最初はこれに頼った方がいいかなぁと。

Composer でのインストールにも対応していますが、プロジェクト単位に配置する物ではないので、普通に git clone して、クライアントアプリケーションと考えて適当な場所に入れた方がいいでしょう。

私の場合は C:\Applications\phalcon-devtools に配置しました。

Windows用に用意されている phalcon.bat の中身はこんな感じになってます。

phalcon.bat

@echo off

set PTOOLSPATH=%~dp0\
php %PTOOLSPATH%\phalcon.php %*

ここにパスを通すなり、シンボリックリンクやジャンクションを作成するなりします。

私の場合はシェルにNYAOSを使ってるので _nya 設定ファイルにこんな感じで alias の設定を追加しました。

%HOME%_nya

alias phalcon C:\Applications\phalcon-devtools\phalcon.bat

なお公式ドキュメントでは c:\phalcon-tools に入れて、このパスをPath環境変数に追加することを推奨しています。

phalcon コマンドでプロジェクトの雛形を作成

コマンドラインで phalcon project と入力すると、ヘルプが表示されます。

$ phalcon project

Phalcon DevTools (1.2.4)

Help:
  Creates a project

Usage:
  project [name] [type] [directory] [enable-webtools]

Arguments:
  help  Shows this help text

Example
  phalcon project store simple

Options:
 --name               Name of the new project
 --enable-webtools    Determines if webtools should be enabled [optional]
 --directory=s        Base path on which project will be created [optional]
 --type=s             Type of the application to be generated (micro, simple, modules)
 --template-path      Specify a template path [optional]
 --use-config-ini     Use a ini file as configuration file [optional]
 --trace              Shows the trace of the framework in case of exception. [optional]
 --help               Shows this help

これによると "project [name] [type] [directory] [enable-webtools]" とあります。

type オプションには micro, simple, modules の三種類あって、プロジェクトのスタイルによっていずれかを選択すればいいようです。

スタイルの違いはこちらが参考になります。

「simple」の場合は controllers ディレクトリにコントローラクラスを配置するのに対して、「micro」の場合は app.php にルーティング設定とハンドラを無名関数で定義するスタイルのようです。

ここは、Silex好きな私には馴染み良さそうな感じの micro で。

$ phalcon project test micro

Phalcon DevTools (1.2.4)


  Success: Project 'test' was successfully created.

testディレクトリが作成され、中には .htaccess, app.php, index.html ファイルと、.phalcon, config, modules, public, views ディレクトリが作成されています。

流れを追ってみます。

.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule  ^$ public/    [L]
    RewriteRule  (.*) public/$1 [L]
</IfModule>

リクエストされたパスの前に public/ を付けて…

public/.htaccess

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

public/ で受けたパスを index.php に _url パラメータで渡して…

public/index.php

<?php

error_reporting(E_ALL);

try {

    /**
    * Read the configuration
    */
    $config = include __DIR__ . '/../config/config.php';

    /**
    * Include Services
    */
    include __DIR__ . '/../config/services.php';

    /**
    * Include Autoloader
    */
    include __DIR__ . '/../config/loader.php';

    /**
    * Starting the application
   */
    $app = new \Phalcon\Mvc\Micro();

    /**
    * Assign service locator to the application
    */
    $app->setDi($di);

    /**
    * Incude Application
    */
    include __DIR__ . '/../app.php';

    /**
    * Handle the request
    */
    $app->handle();

} catch (Phalcon\Exception $e) {
    echo $e->getMessage();
} catch (PDOException $e){
    echo $e->getMessage();
}

設定ファイル(.phpファイル!)を読み込んで、DIコンテナにサービスを登録して、オートローダーを読み込んで、アプリケーションオブジェクト(Phalcon\Mvc\Micro)を生成して実行しているようです。

例外処理とかすっごい雑ですが、あくまで雛形なので、好きに変えちゃっていい…のでしょうか。

ルーティング設定は、途中で読み込まれる app.php で行われています。

app.php

<?php

/**
 * Add your routes here
 */
$app->get('/', function () use ($app) {
    echo $app['view']->getRender(null, 'index');
});

/**
 * Not found handler
 */
$app->notFound(function () use ($app) {
    $app->response->setStatusCode(404, "Not Found")->sendHeaders();
    echo $app['view']->getRender(null, '404');
});

こういう突然変数が現れる書き方も好きじゃないんですが、なんとなく流れは分かりました。

ビルトインWebサーバ用のルーティングスクリプトを作成

せっかくなので、ビルトインWebサーバで実行できるようにします。

プロジェクト内の public ディレクトリ直下にルーティングスクリプトを作成します。

public/.router.php

<?php
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

if (file_exists(realpath(__DIR__ . $path))) {
    return false;
}
$_GET['_url'] = $_SERVER['REQUEST_URI'];
require __DIR__ . DIRECTORY_SEPARATOR . 'index.php';

Windowsなので、ビルトインWebサーバ起動用のショートカットを作成するのが楽だと思います。

リンク先を C:\php\php.exe -S 127.0.0.1:8080 .router.php として、作業フォルダーにドキュメントルートを指定します。

ビルトインWebサーバを起動して、ブラウザで 127.0.0.1:8080 を見てみます。

ヤッター ∩( ・ω・)∩ とりあえず動くところまで確認できました。

Vagrant + シェルスクリプトでPHP開発環境をプロビジョニングしてみたメモ

私はシェルスクリプト力が皆無なので、タイトルはほぼ釣りです。(でも他に表現のしようがないし…)

AnsibleやFabricといった自動化ツールとの連携を検討していたんですが、ひとまずVagrant単体でできるプロビジョニングを試してみたメモです。

新原さんのスライド もう XAMPP / MAMP はいらない!?Vagrant で作る PHP 開発環境 24ページ以降の内容を手がかりに、Vagrant Documentation も参照しつつ進めます。

Windows7にVirtualBoxとVagrantをインストールしたメモ に引き続き、実行した環境はWindowsです。

プロジェクトの準備

まずはプロジェクト用ディレクトリを作成して、gitで管理します。

以下、Windows8 + NYAOSのシェルにて。 (コマンドは MinGW/MSYSNT版UNIX-like tools へのエイリアスです…自分でも把握できてない)

$ mkdir test-vagrant
$ cd test-vagrant
$ git init
Reinitialized existing Git repository in c:/Users/k_horii/Projects/test-vagrant/.git/

ソース格納用ディレクトリ(source)と、Vagrant設定用ディレクトリ(vagrant)を作成し、 すでに登録済の VagrantBox "centos64_64" を使ってVMの作成準備を行います。

$ mkdir source
$ mkdir vagrant
$ cd vagrant
$ vagrant init centos64_64
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.

カレントディレクトリに Vagrantfile が作成されました。

Vagrantfileで共有ディレクトリを設定する

config.vm.synced_folder はドキュメント (Synced Folders / Basic Usage) にもあまり詳しくは書かれていませんが、以下のようなサンプルと解説があります。

Vagrant.configure("2") do |config|
  # other config here

  config.vm.synced_folder "src/", "/srv/website"
end

The first parameter is a path to a directory on the host machine. If the path is relative, it is relative to the project root.

第1引数はホストPCのパスで、相対パスで記述した場合はプロジェクトルート(Vagrantfileを作成したディレクトリ)からのパスになるようです。

The second parameter must be an absolute path of where to share the folder within the guest machine. This folder will be created (recursively, if it must) if it doesn't exist.

第2引数はVMのどのパスと共有させるかを絶対パスで指定、存在しない場合は再帰的に作成してくれるようです。

スライドの30ページの内容を参考に、以下のような設定としました。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/home/vagrant/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'

end

プロビジョニングは後回しにして、まずはこの設定でVMを作成、起動してみます。事前にPageantを停止しておくことを忘れずに。

※ちなみに config.vm.synced_folder の第2引数に "/home/vagrant" などと書くと、容赦なくホームディレクトリがすっからかんにされるので注意してください。(やってしまいました)

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /home/vagrant

PuTTYで vagrant@127.0.0.1:2222 に接続して、共有ディレクトリが作成されているか確認してみます。

[vagrant@localhost ~]$ ls -lsap
total 24
4 drwx------  4 vagrant vagrant 4096 Sep  2 10:02 ./
4 drwxr-xr-x. 4 root    root    4096 Apr 27 10:05 ../
4 -rw-r--r--  1 vagrant vagrant   18 Feb 21  2013 .bash_logout
4 -rw-r--r--  1 vagrant vagrant  176 Feb 21  2013 .bash_profile
4 -rw-r--r--  1 vagrant vagrant  124 Feb 21  2013 .bashrc
0 drwxrwxrwx  1 vagrant vagrant    0 Sep  2 10:07 source/
4 drwx------  2 vagrant root    4096 Apr 27 10:05 .ssh/

確かにできてます。

ホストPC側でサブディレクトリを作成してみます。

$ mkdir public

VM側への反映を確認してみます。

[vagrant@localhost ~]$ ls -lsap source
total 4
0 drwxrwxrwx 1 vagrant vagrant    0 Sep  2 10:14 ./
4 drwx------ 4 vagrant vagrant 4096 Sep  2 10:02 ../
0 drwxrwxrwx 1 vagrant vagrant    0 Sep  2 10:07 public/

publicディレクトリが vagrant:vagrant, 0777で作成されています。

Vagrantfileで config.vm.synced_folder "../source", "/home/vagrant/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666' と指定した内容が反映されているようです。

ここで一旦終了、VMを破棄します。

インラインスクリプトでプロビジョニング

スライドの32ページの内容を手がかりに、ドキュメントを読んでみます。

$script = <<SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT

Vagrant.configure("2") do |config|
  config.vm.provision :shell, :inline => $script
end

I understand if you're not familiar with Ruby, the above may seem very advanced or foreign. But don't fear, what it is doing is quite simple: the script is assigned to a global variable $script.

ご配慮ありがとうございます!

Ruby全然分からないんですが、これなら何とかやれそうです。

まずはこのインラインスクリプトによるプロビジョニングを、以下の設定で試してみます。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/var/www/test/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'
  $script = <<SCRIPT
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
service iptables save
service iptables restart
mkdir -p /var/www/test/source/public
chown vagrant: /var/www/test/source/public
yum -y install httpd
touch /etc/httpd/conf.d/vhosts.conf
chown vagrant: /etc/httpd/conf.d/vhosts.conf
echo "NameVirtualHost *:80" >> /etc/httpd/conf.d/vhosts.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf.d/vhosts.conf
echo "  DocumentRoot /var/www/test/source/public" >> /etc/httpd/conf.d/vhosts.conf
echo "</VirtualHost>" >> /etc/httpd/conf.d/vhosts.conf
yum -y install install php \
php-cli \
php-common \
php-devel \
php-gd \
php-intl \
php-mbstring \
php-pdo \
php-pear.noarch \
php-xml \
php-mcrypt
echo "date.timezone = Asia/Tokyo" >> /etc/php.ini
echo "<?php phpinfo(); ?>" >> /var/www/test/source/public/index.php
chkconfig httpd on
service httpd start
SCRIPT
  config.vm.provision :shell, :inline => $script

end

VM側の同期ディレクトリはホーム以下だと何かと不都合なので、CentOSApacheで使われる /var/www 以下に /var/www/test/source を設定しました。

iptablesはとりあえず全てのポリシーをACCEPTで初期化、Apacheのドキュメントルートを /var/www/test/source/public とします。

同期させるホストPC側のディレクトリには、まだ何も入ってません。

この状態で一旦 git commit しておき、VMを初期化します。以下、長くなりますが…

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
0K
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /var/www/test/source
[default] Running provisioner: shell...
[default] Running: inline script
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
iptables: Flushing firewall rules: [  OK  ]
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Unloading modules: [  OK  ]
iptables: Applying firewall rules: [  OK  ]
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed
---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch        Version                      Repository    Size
================================================================================
Installing:
 httpd              x86_64      2.2.15-29.el6.centos         updates      821 k
Installing for dependencies:
 apr                x86_64      1.3.9-5.el6_2                base         123 k
 apr-util           x86_64      1.3.9-3.el6_0.1              base          87 k
 apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k
 httpd-tools        x86_64      2.2.15-29.el6.centos         updates       73 k
 mailcap            noarch      2.1.31-2.el6                 base          27 k

Transaction Summary
================================================================================
Install       6 Package(s)

Total download size: 1.1 M
Installed size: 3.6 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           611 kB/s | 1.1 MB     00:01
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6
  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6
  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6
  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6
  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6
  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6
  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6
  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6
  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6
  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6
  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6
  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6

Installed:
  httpd.x86_64 0:2.2.15-29.el6.centos

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2
  apr-util.x86_64 0:1.3.9-3.el6_0.1
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  httpd-tools.x86_64 0:2.2.15-29.el6.centos
  mailcap.noarch 0:2.1.31-2.el6

Complete!
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Setting up Install Process
No package install available.
No package php-mcrypt available.
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-cli.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-common.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-devel.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: automake for package: php-devel-5.3.3-23.el6_4.x86_64
--> Processing Dependency: autoconf for package: php-devel-5.3.3-23.el6_4.x86_64
---> Package php-gd.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libpng12.so.0(PNG12_0)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libpng12.so.0()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libfreetype.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
---> Package php-intl.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libicuuc.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicuio.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicui18n.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicudata.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
---> Package php-mbstring.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pdo.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
---> Package php-xml.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package freetype.x86_64 0:2.3.11-14.el6_3.1 will be installed
---> Package libX11.x86_64 0:1.5.0-4.el6 will be installed
--> Processing Dependency: libX11-common = 1.5.0-4.el6 for package: libX11-1.5.0-4.el6.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.5.0-4.el6.x86_64
---> Package libXpm.x86_64 0:3.5.10-2.el6 will be installed
---> Package libicu.x86_64 0:4.2.1-9.1.el6_2 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.1-1.el6 will be installed
---> Package libpng.x86_64 2:1.2.49-1.el6_2 will be installed
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.5.0-4.el6 will be installed
---> Package libxcb.x86_64 0:1.8.1-1.el6 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.8.1-1.el6.x86_64
--> Running transaction check
---> Package libXau.x86_64 0:1.0.6-4.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch         Version                   Repository     Size
================================================================================
Installing:
 php                 x86_64       5.3.3-23.el6_4            updates       1.1 M
 php-cli             x86_64       5.3.3-23.el6_4            updates       2.2 M
 php-common          x86_64       5.3.3-23.el6_4            updates       524 k
 php-devel           x86_64       5.3.3-23.el6_4            updates       507 k
 php-gd              x86_64       5.3.3-23.el6_4            updates       106 k
 php-intl            x86_64       5.3.3-23.el6_4            updates        70 k
 php-mbstring        x86_64       5.3.3-23.el6_4            updates       455 k
 php-pdo             x86_64       5.3.3-23.el6_4            updates        75 k
 php-pear            noarch       1:1.9.4-4.el6             base          393 k
 php-xml             x86_64       5.3.3-23.el6_4            updates       103 k
Installing for dependencies:
 autoconf            noarch       2.63-5.1.el6              base          781 k
 automake            noarch       1.11.1-4.el6              base          550 k
 freetype            x86_64       2.3.11-14.el6_3.1         updates       359 k
 libX11              x86_64       1.5.0-4.el6               base          584 k
 libX11-common       noarch       1.5.0-4.el6               base          192 k
 libXau              x86_64       1.0.6-4.el6               base           24 k
 libXpm              x86_64       3.5.10-2.el6              base           51 k
 libicu              x86_64       4.2.1-9.1.el6_2           base          4.9 M
 libjpeg-turbo       x86_64       1.2.1-1.el6               base          174 k
 libpng              x86_64       2:1.2.49-1.el6_2          base          182 k
 libxcb              x86_64       1.8.1-1.el6               base          110 k
 libxslt             x86_64       1.1.26-2.el6_3.1          base          452 k

Transaction Summary
================================================================================
Install      22 Package(s)

Total download size: 14 M
Installed size: 51 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           522 kB/s |  14 MB     00:27
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-common-5.3.3-23.el6_4.x86_64                            1/22
  Installing : php-cli-5.3.3-23.el6_4.x86_64                               2/22
  Installing : autoconf-2.63-5.1.el6.noarch                                3/22
  Installing : automake-1.11.1-4.el6.noarch                                4/22
  Installing : php-5.3.3-23.el6_4.x86_64                                   5/22
  Installing : libxslt-1.1.26-2.el6_3.1.x86_64                             6/22
  Installing : libicu-4.2.1-9.1.el6_2.x86_64                               7/22
  Installing : libXau-1.0.6-4.el6.x86_64                                   8/22
  Installing : libxcb-1.8.1-1.el6.x86_64                                   9/22
  Installing : 2:libpng-1.2.49-1.el6_2.x86_64                             10/22
  Installing : libX11-common-1.5.0-4.el6.noarch                           11/22
  Installing : libX11-1.5.0-4.el6.x86_64                                  12/22
  Installing : libXpm-3.5.10-2.el6.x86_64                                 13/22
  Installing : freetype-2.3.11-14.el6_3.1.x86_64                          14/22
  Installing : libjpeg-turbo-1.2.1-1.el6.x86_64                           15/22
  Installing : php-gd-5.3.3-23.el6_4.x86_64                               16/22
  Installing : php-intl-5.3.3-23.el6_4.x86_64                             17/22
  Installing : php-xml-5.3.3-23.el6_4.x86_64                              18/22
  Installing : php-devel-5.3.3-23.el6_4.x86_64                            19/22
  Installing : 1:php-pear-1.9.4-4.el6.noarch                              20/22
  Installing : php-mbstring-5.3.3-23.el6_4.x86_64                         21/22
  Installing : php-pdo-5.3.3-23.el6_4.x86_64                              22/22
  Verifying  : libjpeg-turbo-1.2.1-1.el6.x86_64                            1/22
  Verifying  : php-mbstring-5.3.3-23.el6_4.x86_64                          2/22
  Verifying  : libXpm-3.5.10-2.el6.x86_64                                  3/22
  Verifying  : php-5.3.3-23.el6_4.x86_64                                   4/22
  Verifying  : php-pdo-5.3.3-23.el6_4.x86_64                               5/22
  Verifying  : php-devel-5.3.3-23.el6_4.x86_64                             6/22
  Verifying  : automake-1.11.1-4.el6.noarch                                7/22
  Verifying  : 1:php-pear-1.9.4-4.el6.noarch                               8/22
  Verifying  : php-xml-5.3.3-23.el6_4.x86_64                               9/22
  Verifying  : php-common-5.3.3-23.el6_4.x86_64                           10/22
  Verifying  : freetype-2.3.11-14.el6_3.1.x86_64                          11/22
  Verifying  : autoconf-2.63-5.1.el6.noarch                               12/22
  Verifying  : php-gd-5.3.3-23.el6_4.x86_64                               13/22
  Verifying  : php-cli-5.3.3-23.el6_4.x86_64                              14/22
  Verifying  : libX11-common-1.5.0-4.el6.noarch                           15/22
  Verifying  : libxcb-1.8.1-1.el6.x86_64                                  16/22
  Verifying  : 2:libpng-1.2.49-1.el6_2.x86_64                             17/22
  Verifying  : libXau-1.0.6-4.el6.x86_64                                  18/22
  Verifying  : libicu-4.2.1-9.1.el6_2.x86_64                              19/22
  Verifying  : libX11-1.5.0-4.el6.x86_64                                  20/22
  Verifying  : libxslt-1.1.26-2.el6_3.1.x86_64                            21/22
  Verifying  : php-intl-5.3.3-23.el6_4.x86_64                             22/22

Installed:
  php.x86_64 0:5.3.3-23.el6_4              php-cli.x86_64 0:5.3.3-23.el6_4
  php-common.x86_64 0:5.3.3-23.el6_4       php-devel.x86_64 0:5.3.3-23.el6_4
  php-gd.x86_64 0:5.3.3-23.el6_4           php-intl.x86_64 0:5.3.3-23.el6_4
  php-mbstring.x86_64 0:5.3.3-23.el6_4     php-pdo.x86_64 0:5.3.3-23.el6_4
  php-pear.noarch 1:1.9.4-4.el6            php-xml.x86_64 0:5.3.3-23.el6_4

Dependency Installed:
  autoconf.noarch 0:2.63-5.1.el6          automake.noarch 0:1.11.1-4.el6
  freetype.x86_64 0:2.3.11-14.el6_3.1     libX11.x86_64 0:1.5.0-4.el6
  libX11-common.noarch 0:1.5.0-4.el6      libXau.x86_64 0:1.0.6-4.el6
  libXpm.x86_64 0:3.5.10-2.el6            libicu.x86_64 0:4.2.1-9.1.el6_2
  libjpeg-turbo.x86_64 0:1.2.1-1.el6      libpng.x86_64 2:1.2.49-1.el6_2
  libxcb.x86_64 0:1.8.1-1.el6             libxslt.x86_64 0:1.1.26-2.el6_3.1

Complete!
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[  OK  ]

無事に起動したでしょうか? httpdの警告は httpd.conf で ServerName を定義していないためでしょう。

とりあえずブラウザで 192.168.33.10 を開いたところ、phpinfoが表示されました。

ホストPC側の同期ディレクトリの状態を確認してみます。

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       ../source/public/
nothing added to commit but untracked files present (use "git add" to track)

$ ls -lsa ../source/public
   0 d---rwx        0 Sep  4 07:30 .             .
   0 d---rwx        0 Sep  4 07:29 ..            ..
   1 -a--rw-       20 Sep  4 07:30               index.php

ホストPCの方にも source/public ディレクトリと index.php が作成されています。

外部スクリプトでプロビジョニング

ドキュメントには、ホストPCで記述したシェルスクリプトを読み込んで実行する方法も書かれています。

The shell provisioner can also take an option specifying a path to a shell script on the host machine. Vagrant will then upload this script into the guest and execute it. An example:

Vagrant.configure("2") do |config|
  config.vm.provision :shell, :path => "script.sh"
end

Relative paths, such as above, are expanded relative to the location of the root Vagrantfile for your project. Absolute paths can also be used, as well as shortcuts such as ~ (home directory) and .. (parent directory).

なお例のごとく、相対パスで記述した場合はプロジェクトルート(Vagrantfileを作成したディレクトリ)からのパスになるようです。

とりあえず一旦VMを破棄して、今度は同じ内容で外部ファイルに移してVMを作成してみます。

Vagrantfile

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "centos64_64"
  config.vm.network :private_network, ip: "192.168.33.10"
  config.vm.synced_folder "../source", "/var/www/test/source", :create => true, :owner => 'vagrant', :group => 'vagrant', :extra => 'dmode=777,fmode=666'
  config.vm.provision :shell, :path => "provision.sh"

end

provision.sh

#!/bin/sh

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F
service iptables save
service iptables restart
mkdir -p /var/www/test/source/public
chown vagrant: /var/www/test/source/public
yum -y install httpd
touch /etc/httpd/conf.d/vhosts.conf
chown vagrant: /etc/httpd/conf.d/vhosts.conf
echo "NameVirtualHost *:80" >> /etc/httpd/conf.d/vhosts.conf
echo "<VirtualHost *:80>" >> /etc/httpd/conf.d/vhosts.conf
echo "  DocumentRoot /var/www/test/source/public" >> /etc/httpd/conf.d/vhosts.conf
echo "</VirtualHost>" >> /etc/httpd/conf.d/vhosts.conf
yum -y install install php \
php-cli \
php-common \
php-devel \
php-gd \
php-intl \
php-mbstring \
php-pdo \
php-pear.noarch \
php-xml \
php-mcrypt
echo "date.timezone = Asia/Tokyo" >> /etc/php.ini
echo "<?php phpinfo(); ?>" >> /var/www/test/source/public/index.php
chkconfig httpd on
service httpd start

VMを作り直します。再作成の前に、ディレクトリ同期でホストPC側に作成されたディレクトリとファイルの削除も忘れずに。

$ vagrant destroy --force
[default] Forcing shutdown of VM...
[default] Destroying VM and associated drives...
$ rm -rf ../source/public
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Importing base box 'centos64_64'...
0K
[default] Matching MAC address for NAT networking...
[default] Setting the name of the VM...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Booting VM...
[default] Waiting for VM to boot. This can take a few minutes.
[default] VM booted and ready for use!
[default] Configuring and enabling network interfaces...
[default] Mounting shared folders...
[default] -- /vagrant
[default] -- /var/www/test/source
[default] Running provisioner: shell...
[default] Running: C:/Users/k_horii/AppData/Local/Temp/vagrant-shell20130904-540-hsxmzv
iptables: Saving firewall rules to /etc/sysconfig/iptables: [  OK  ]
iptables: Flushing firewall rules: [  OK  ]
iptables: Setting chains to policy ACCEPT: filter [  OK  ]
iptables: Unloading modules: [  OK  ]
iptables: Applying firewall rules: [  OK  ]
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: ftp.nara.wide.ad.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: centosx4.centos.org
http://centosx4.centos.org/centos/6.4/updates/x86_64/repodata/2131be604d1cf4779786f3f56e061bd1617a3aa16d3157be5b7511cff232b66a-primary.sqlite.bz2: [Errno 12] Timeout on http://centosx4.centos.org/centos/6.4/updates/x86_64/repodata/2131be604d1cf4779786f3f56e061bd1617a3aa16d3157be5b7511cff232b66a-primary.sqlite.bz2: (28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds')
Trying other mirror.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.2.15-29.el6.centos will be installed
--> Processing Dependency: httpd-tools = 2.2.15-29.el6.centos for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: apr-util-ldap for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.2.15-29.el6.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.3.9-5.el6_2 will be installed
---> Package apr-util.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1 will be installed
---> Package httpd-tools.x86_64 0:2.2.15-29.el6.centos will be installed
---> Package mailcap.noarch 0:2.1.31-2.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch        Version                      Repository    Size
================================================================================
Installing:
 httpd              x86_64      2.2.15-29.el6.centos         updates      821 k
Installing for dependencies:
 apr                x86_64      1.3.9-5.el6_2                base         123 k
 apr-util           x86_64      1.3.9-3.el6_0.1              base          87 k
 apr-util-ldap      x86_64      1.3.9-3.el6_0.1              base          15 k
 httpd-tools        x86_64      2.2.15-29.el6.centos         updates       73 k
 mailcap            noarch      2.1.31-2.el6                 base          27 k

Transaction Summary
================================================================================
Install       6 Package(s)

Total download size: 1.1 M
Installed size: 3.6 M
Downloading Packages:
--------------------------------------------------------------------------------
Total                                           131 kB/s | 1.1 MB     00:08
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : apr-1.3.9-5.el6_2.x86_64                                     1/6
  Installing : apr-util-1.3.9-3.el6_0.1.x86_64                              2/6
  Installing : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         3/6
  Installing : httpd-tools-2.2.15-29.el6.centos.x86_64                      4/6
  Installing : mailcap-2.1.31-2.el6.noarch                                  5/6
  Installing : httpd-2.2.15-29.el6.centos.x86_64                            6/6
  Verifying  : httpd-2.2.15-29.el6.centos.x86_64                            1/6
  Verifying  : apr-util-ldap-1.3.9-3.el6_0.1.x86_64                         2/6
  Verifying  : httpd-tools-2.2.15-29.el6.centos.x86_64                      3/6
  Verifying  : apr-1.3.9-5.el6_2.x86_64                                     4/6
  Verifying  : mailcap-2.1.31-2.el6.noarch                                  5/6
  Verifying  : apr-util-1.3.9-3.el6_0.1.x86_64                              6/6

Installed:
  httpd.x86_64 0:2.2.15-29.el6.centos

Dependency Installed:
  apr.x86_64 0:1.3.9-5.el6_2
  apr-util.x86_64 0:1.3.9-3.el6_0.1
  apr-util-ldap.x86_64 0:1.3.9-3.el6_0.1
  httpd-tools.x86_64 0:2.2.15-29.el6.centos
  mailcap.noarch 0:2.1.31-2.el6

Complete!
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.nara.wide.ad.jp
 * extras: ftp.nara.wide.ad.jp
 * updates: centosx4.centos.org
Setting up Install Process
No package install available.
No package php-mcrypt available.
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-cli.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-common.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-devel.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: automake for package: php-devel-5.3.3-23.el6_4.x86_64
--> Processing Dependency: autoconf for package: php-devel-5.3.3-23.el6_4.x86_64
---> Package php-gd.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libpng12.so.0(PNG12_0)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libpng12.so.0()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libfreetype.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: php-gd-5.3.3-23.el6_4.x86_64
---> Package php-intl.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libicuuc.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicuio.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicui18n.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libicudata.so.42()(64bit) for package: php-intl-5.3.3-23.el6_4.x86_64
---> Package php-mbstring.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pdo.x86_64 0:5.3.3-23.el6_4 will be installed
---> Package php-pear.noarch 1:1.9.4-4.el6 will be installed
---> Package php-xml.x86_64 0:5.3.3-23.el6_4 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.24)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.22)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.13)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: php-xml-5.3.3-23.el6_4.x86_64
--> Running transaction check
---> Package autoconf.noarch 0:2.63-5.1.el6 will be installed
---> Package automake.noarch 0:1.11.1-4.el6 will be installed
---> Package freetype.x86_64 0:2.3.11-14.el6_3.1 will be installed
---> Package libX11.x86_64 0:1.5.0-4.el6 will be installed
--> Processing Dependency: libX11-common = 1.5.0-4.el6 for package: libX11-1.5.0-4.el6.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.5.0-4.el6.x86_64
---> Package libXpm.x86_64 0:3.5.10-2.el6 will be installed
---> Package libicu.x86_64 0:4.2.1-9.1.el6_2 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.1-1.el6 will be installed
---> Package libpng.x86_64 2:1.2.49-1.el6_2 will be installed
---> Package libxslt.x86_64 0:1.1.26-2.el6_3.1 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.5.0-4.el6 will be installed
---> Package libxcb.x86_64 0:1.8.1-1.el6 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.8.1-1.el6.x86_64
--> Running transaction check
---> Package libXau.x86_64 0:1.0.6-4.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch         Version                   Repository     Size
================================================================================
Installing:
 php                 x86_64       5.3.3-23.el6_4            updates       1.1 M
 php-cli             x86_64       5.3.3-23.el6_4            updates       2.2 M
 php-common          x86_64       5.3.3-23.el6_4            updates       524 k
 php-devel           x86_64       5.3.3-23.el6_4            updates       507 k
 php-gd              x86_64       5.3.3-23.el6_4            updates       106 k
 php-intl            x86_64       5.3.3-23.el6_4            updates        70 k
 php-mbstring        x86_64       5.3.3-23.el6_4            updates       455 k
 php-pdo             x86_64       5.3.3-23.el6_4            updates        75 k
 php-pear            noarch       1:1.9.4-4.el6             base          393 k
 php-xml             x86_64       5.3.3-23.el6_4            updates       103 k
Installing for dependencies:
 autoconf            noarch       2.63-5.1.el6              base          781 k
 automake            noarch       1.11.1-4.el6              base          550 k
 freetype            x86_64       2.3.11-14.el6_3.1         updates       359 k
 libX11              x86_64       1.5.0-4.el6               base          584 k
 libX11-common       noarch       1.5.0-4.el6               base          192 k
 libXau              x86_64       1.0.6-4.el6               base           24 k
 libXpm              x86_64       3.5.10-2.el6              base           51 k
 libicu              x86_64       4.2.1-9.1.el6_2           base          4.9 M
 libjpeg-turbo       x86_64       1.2.1-1.el6               base          174 k
 libpng              x86_64       2:1.2.49-1.el6_2          base          182 k
 libxcb              x86_64       1.8.1-1.el6               base          110 k
 libxslt             x86_64       1.1.26-2.el6_3.1          base          452 k

Transaction Summary
================================================================================
Install      22 Package(s)

Total download size: 14 M
Installed size: 51 M
Downloading Packages:
http://centosx4.centos.org/centos/6.4/updates/x86_64/Packages/php-5.3.3-23.el6_4.x86_64.rpm: [Errno 12] Timeout on http://centosx4.centos.org/centos/6.4/updates/x86_64/Packages/php-5.3.3-23.el6_4.x86_64.rpm: (28, 'Operation too slow. Less than 1 bytes/sec transfered the last 30 seconds')
Trying other mirror.
--------------------------------------------------------------------------------
Total                                            53 kB/s |  14 MB     04:26
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : php-common-5.3.3-23.el6_4.x86_64                            1/22
  Installing : php-cli-5.3.3-23.el6_4.x86_64                               2/22
  Installing : autoconf-2.63-5.1.el6.noarch                                3/22
  Installing : automake-1.11.1-4.el6.noarch                                4/22
  Installing : php-5.3.3-23.el6_4.x86_64                                   5/22
  Installing : libxslt-1.1.26-2.el6_3.1.x86_64                             6/22
  Installing : libicu-4.2.1-9.1.el6_2.x86_64                               7/22
  Installing : libXau-1.0.6-4.el6.x86_64                                   8/22
  Installing : libxcb-1.8.1-1.el6.x86_64                                   9/22
  Installing : 2:libpng-1.2.49-1.el6_2.x86_64                             10/22
  Installing : libX11-common-1.5.0-4.el6.noarch                           11/22
  Installing : libX11-1.5.0-4.el6.x86_64                                  12/22
  Installing : libXpm-3.5.10-2.el6.x86_64                                 13/22
  Installing : freetype-2.3.11-14.el6_3.1.x86_64                          14/22
  Installing : libjpeg-turbo-1.2.1-1.el6.x86_64                           15/22
  Installing : php-gd-5.3.3-23.el6_4.x86_64                               16/22
  Installing : php-intl-5.3.3-23.el6_4.x86_64                             17/22
  Installing : php-xml-5.3.3-23.el6_4.x86_64                              18/22
  Installing : php-devel-5.3.3-23.el6_4.x86_64                            19/22
  Installing : 1:php-pear-1.9.4-4.el6.noarch                              20/22
  Installing : php-mbstring-5.3.3-23.el6_4.x86_64                         21/22
  Installing : php-pdo-5.3.3-23.el6_4.x86_64                              22/22
  Verifying  : libjpeg-turbo-1.2.1-1.el6.x86_64                            1/22
  Verifying  : php-mbstring-5.3.3-23.el6_4.x86_64                          2/22
  Verifying  : libXpm-3.5.10-2.el6.x86_64                                  3/22
  Verifying  : php-5.3.3-23.el6_4.x86_64                                   4/22
  Verifying  : php-pdo-5.3.3-23.el6_4.x86_64                               5/22
  Verifying  : php-devel-5.3.3-23.el6_4.x86_64                             6/22
  Verifying  : automake-1.11.1-4.el6.noarch                                7/22
  Verifying  : 1:php-pear-1.9.4-4.el6.noarch                               8/22
  Verifying  : php-xml-5.3.3-23.el6_4.x86_64                               9/22
  Verifying  : php-common-5.3.3-23.el6_4.x86_64                           10/22
  Verifying  : freetype-2.3.11-14.el6_3.1.x86_64                          11/22
  Verifying  : autoconf-2.63-5.1.el6.noarch                               12/22
  Verifying  : php-gd-5.3.3-23.el6_4.x86_64                               13/22
  Verifying  : php-cli-5.3.3-23.el6_4.x86_64                              14/22
  Verifying  : libX11-common-1.5.0-4.el6.noarch                           15/22
  Verifying  : libxcb-1.8.1-1.el6.x86_64                                  16/22
  Verifying  : 2:libpng-1.2.49-1.el6_2.x86_64                             17/22
  Verifying  : libXau-1.0.6-4.el6.x86_64                                  18/22
  Verifying  : libicu-4.2.1-9.1.el6_2.x86_64                              19/22
  Verifying  : libX11-1.5.0-4.el6.x86_64                                  20/22
  Verifying  : libxslt-1.1.26-2.el6_3.1.x86_64                            21/22
  Verifying  : php-intl-5.3.3-23.el6_4.x86_64                             22/22

Installed:
  php.x86_64 0:5.3.3-23.el6_4              php-cli.x86_64 0:5.3.3-23.el6_4
  php-common.x86_64 0:5.3.3-23.el6_4       php-devel.x86_64 0:5.3.3-23.el6_4
  php-gd.x86_64 0:5.3.3-23.el6_4           php-intl.x86_64 0:5.3.3-23.el6_4
  php-mbstring.x86_64 0:5.3.3-23.el6_4     php-pdo.x86_64 0:5.3.3-23.el6_4
  php-pear.noarch 1:1.9.4-4.el6            php-xml.x86_64 0:5.3.3-23.el6_4

Dependency Installed:
  autoconf.noarch 0:2.63-5.1.el6          automake.noarch 0:1.11.1-4.el6
  freetype.x86_64 0:2.3.11-14.el6_3.1     libX11.x86_64 0:1.5.0-4.el6
  libX11-common.noarch 0:1.5.0-4.el6      libXau.x86_64 0:1.0.6-4.el6
  libXpm.x86_64 0:3.5.10-2.el6            libicu.x86_64 0:4.2.1-9.1.el6_2
  libjpeg-turbo.x86_64 0:1.2.1-1.el6      libpng.x86_64 2:1.2.49-1.el6_2
  libxcb.x86_64 0:1.8.1-1.el6             libxslt.x86_64 0:1.1.26-2.el6_3.1

Complete!
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
[  OK  ]

途中でyumが centosx4.centos.org を見に行ってタイムアウトしちゃってますが、なんとか完了しました。

ブラウザで 192.168.33.10 を開くとphpinfoが表示されます。

この調子でComposerのインストールまでやってみようとしたのですが、このVMにはgitが入ってないことが分かりました。

Composerに登録されているパッケージの中には、gitがないとインストールできないものが多いので、 それらもプロビジョニングでインストールしておく必要がありますね。

いずれにせよ PHPer的には 5.3.3 とかあり得ないし(composerにも怒られてしまいます…)、 yumリポジトリの追加も必要でしょうから、今回はここまでにしておきます。

Vagrantのプロビジョニングはいつ呼ばれるのか

プロビジョニングがどのタイミングで呼ばれるのかは、ドキュメント (Synced Folders / Basic Usage) に記載があります。

Provisioners are run in three cases: vagrant up, vagrant reload, and vagrant provision.

ここで指定したスクリプトは、vagrant up vagrant reload vagrant provision 実行時に呼ばれるようです。

ということは、このままだと vagrant up するたびにスクリプトが実行されて、 すでに存在するディレクトリを作成しようとしたり、ファイルに同じ内容が追記されたり、 色々と不都合が起きてしまいますね…。

A --no-provision flag can be passed to up and reload if you don't want to run provisioners.

プロビジョンを実行したくない場合は、vagrant up --no-provisionvagrant reload --no-provision とすればいいようですが、いわゆる冪等性に配慮したプロビジョニング手段を講じた方が良いのかな。

スクリプト内でtestコマンド([コマンド)を使って、ファイルが重複して生成されるのを避けたり、 ある処理を実行したことの目印になるファイルを出力して、2回目以降は処理させないという方法も考えられそうです。

コマンドやパッケージのインストールやユーザの作成前に存在確認など、その辺の手法はFabricにも通じるものがありますね。

今後の参考

一応、多少は利用経験のあるFabricとの連携を検討します。

VM作成時にプロビジョニングを実行するのではなくて、作成後にホストPCからFabricでVMに接続してやる方法。

vagrant ssh-config コマンドの戻り値をキャプチャして加工するんですね、なるほど…。

こちらはFabricスクリプトをVagrant側から呼ぶプラグインかな?

それぞれの使用感も参考になります。