今からお前んちこいよ

品川にて細々とお勉強。

itamaeのresourceプラグインを作る

日本語 | English

概要

わりと罠が多かったのでメモ。

以下、環境

  • git 1.8.3.2
  • ruby 2.0.0
  • gem 2.0.3
  • bundler 1.12
  • rake 10.0
  • rspec 3.0
  • itamae v1.6.3

gemテンプレート生成

「itamae-plugin-resource-」がプレフィックス。
そのうしろはスネークケースでリソース名となる。
こんな感じで
$ bundle gem itamae-plugin-resource-tesutotesuto
1回目はいろいろ聞かれる。

$ bundle gem itamae-plugin-resource-tesutotesuto
Creating gem 'itamae-plugin-resource-tesutotesuto'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): rspec
Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at http://choosealicense.com/licenses/mit. y/(n): y
MIT License enabled in config
Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email address is specified as a contact in the generated code of conduct so that people know who to contact in case of a violation. For suggestions about how to enforce codes of conduct, see http://bit.ly/coc-enforcement. y/(n): n
      create  itamae-plugin-resource-tesutotesuto/Gemfile
      create  itamae-plugin-resource-tesutotesuto/.gitignore
      create  itamae-plugin-resource-tesutotesuto/lib/itamae/plugin/resource/tesutotesuto.rb
      create  itamae-plugin-resource-tesutotesuto/lib/itamae/plugin/resource/tesutotesuto/version.rb
      create  itamae-plugin-resource-tesutotesuto/itamae-plugin-resource-tesutotesuto.gemspec
      create  itamae-plugin-resource-tesutotesuto/Rakefile
      create  itamae-plugin-resource-tesutotesuto/README.md
      create  itamae-plugin-resource-tesutotesuto/bin/console
      create  itamae-plugin-resource-tesutotesuto/bin/setup
      create  itamae-plugin-resource-tesutotesuto/.travis.yml
      create  itamae-plugin-resource-tesutotesuto/.rspec
      create  itamae-plugin-resource-tesutotesuto/spec/spec_helper.rb
      create  itamae-plugin-resource-tesutotesuto/spec/itamae/plugin/resource/tesutotesuto_spec.rb
      create  itamae-plugin-resource-tesutotesuto/LICENSE.txt
Initializing git repo in /path/to/itamae-plugin-resource-tesutotesuto

プラグインを書く

参考)Resource Plugins · itamae-kitchen/itamae Wiki · GitHub
参考)itamae/git.rb at master · itamae-kitchen/itamae · GitHub

./lib/itamae/plugin/resource/tesutotesuto.rb を編集

ベースの形はこんな感じ。
テンプレートで出力されているものを少し直す。

require "itamae" の追加
module Tesutotesutoclass Tesutotesuto < Itamae::Resource::Base に修正

require "itamae/plugin/resource/tesutotesuto/version"
require "itamae"

module Itamae
  module Plugin
    module Resource
      class Tesutotesuto < Itamae::Resource::Base
        define_attribute :action, default: :run
        def action_run(options)
          p 'ok'
        end
      end
    end
  end
end

 

次に、./lib/itamae/plugin/resource/tesutotesuto/version.rb を編集
require "itamae" の追加
module Tesutotesutoclass Tesutotesuto < Itamae::Resource::Base に修正

require "itamae"
module Itamae
  module Plugin
    module Resource
      class Tesutotesuto < Itamae::Resource::Base
        VERSION = "0.1.0"
      end
    end
  end
end

 
あと、テストをrspecにしていた場合、*.gemspec
「TODO」という文字列が記述されているためこの後ビルドで

FIXME" or "TODO" is not a description

とか言われてこけるので適宜直す。

ビルド

$ cd itamae-plugin-resource-tesutotesuto
$ rake build
itamae-plugin-resource-tesutotesuto 0.1.0 built to pkg/itamae-plugin-resource-tesutotesuto-0.1.0.gem.

٩( ᐛ )و

作ったリソースのインストール

$ gem install pkg/itamae-plugin-resource-tesutotesuto-0.1.0.gem

これで

tesutotesuto "testotesto exec" do
    action :run
end

みたいな感じで動く。 ちなみに、dry-run だとaction の処理は飛ばされるので、ここで例に挙げたやつを dry-run で実行しても動いてないように見えちゃうので注意。

終わりに

flexibleなitamaeにresourceとかのプラグインを独自で書いてゆくことでより色のあるプロビジョニングファイルリポジトリにして行きたいぞ٩( ᐛ )و