Skip to content

Instantly share code, notes, and snippets.

@monochromegane
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monochromegane/9246857 to your computer and use it in GitHub Desktop.
Save monochromegane/9246857 to your computer and use it in GitHub Desktop.
Vagrant勉強会資料 - Vagrantプラグインの概要

title: Vagrant勉強会資料 - Vagrantプラグインの概要

date: 2014-02-27 18:00 tags: [] categories: []


Vagrantプラグインの概要

プラグイン

  • 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

かんたんにつくれた

vagrant-uptime

  • 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

参考

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment