date: 2014-02-27 18:00 tags: [] categories: []
- Vagrantの挙動を拡張する仕組み
- Vagrant自身のコアもプラグインでつくられている
$ vagrant plugin install NAME
$ vagrant plugin uninstall NAME
$ vagrant plugin list
- 新しいvagrant操作コマンドを追加
- 既存のvagrant操作コマンドに処理を追加
- 新しい設定オプションを追加
- ゲスト固有の機能を追加(フォルダのマウント、ネットワーク設定)
- ホスト固有の機能を追加(NFS)
- プロバイダを追加
- プロビジョナを追加
RubyGemとして開発
利用するコンポーネントを定義(ここではコマンドを追加)
class MyPlugin < Vagrant.plugin("2")
name "My Plugin"
command "run-my-plugin" do
require_relative "command"
Command
end
end
コンポーネントを継承した実装クラスを作成
class Command < Vagrant.plugin("2", "command")
def execute
puts "Hello!"
return 0
end
end
コンポーネント内部からは@env
などでアクセスできる
Vagrant::Environmentを使って処理を組み立てる。
- @env.machine
- @env.ui
- @env.config_global
- upとhaltをhookしてVagrantの起動時間を記録
- showコマンドとhalt時に起動時間累計を表示
- uptimeプラグイン用の設定
- クラウドのプロバイダを使うときにちょっとだけ便利かもしれない。かもしれない。
$ vagrant halt
[default] Attempting graceful shutdown of VM...
[default] Vagrant uptime: 1h60m (3610s)
[default] Vagrant cost: $0.0015