概要
definitionの利用により独自定義のresourceを作成する
詳細
definitionの利用により独自定義のresourceを作成します。
tar resourceを定義してみる
definitions/tar.rb
# encoding: utf-8 define :tar, :extract => false, :compress_name => "sample.tar.gz", :targets => [], :output_path => ".", :cwd => "." do return if params[:targets].size == 0 tmp = "" bash "execute tar #{params[:name]}" do codes = [] if params[:extract] params[:targets].each do |target| codes << "mv #{target} #{params[:output_path]}" codes << "tar xvfz #{params[:output_path]}/#{target}" end else codes << "tar cvfz #{params[:compress_name]} #{params[:targets].join(' ')}" end cwd params[:cwd] code codes.join("\n") end end
recipe/default.rb
まずはファイルを作成
/home/vagrant/notification.txt
/home/vagrant/subscribe.txt
/home/vagrant/hoge.txt
/home/vagrant/hoo.txt
/home/vagrant/bar.txt
ファイルを圧縮
["subscribe.txt", "notification.txt"]
を/home/vagrant/work/sub_not.tar.gzに。
["hoge.txt", "hoo.txt", "bar.txt"]
を/home/vagrant/work/hoges.tar.gzに。
ファイルを解凍
/home/vagrant/work/sub_not.tar.gzを解凍
/home/vagrant/work/hoges.tar.gzを解凍
execute "notification_sample" do command "echo notification!!!!! > /home/vagrant/notification.txt" end execute "subscript_sample" do command "echo subscribe!!!!! > /home/vagrant/subscribe.txt" notifies :run, "execute[notification_sample]" end file_names = ["hoge.txt", "hoo.txt", "bar.txt"] file_names.each do |f| file "/home/vagrant/#{f}" do action :create owner "vagrant" group "vagrant" mode "0755" end end tar "compress1" do cwd "/home/vagrant" extract false compress_name "sub_not.tar.gz" targets ["subscribe.txt", "notification.txt"] output_path "/home/vagrant/work" end tar "compress2" do cwd "/home/vagrant" compress_name "hoges.tar.gz" targets file_names output_path "/home/vagrant/work" end tar "compress3" do cwd "/home/vagrant" extract false compress_name "sub_not.tar.gz" targets ["subscribe.txt", "notification.txt"] output_path "/home/vagrant/work/sublime_text_plugins" end tar "extract" do cwd "/home/vagrant" extract true targets ["sub_not.tar.gz", "hoges.tar.gz"] output_path "/home/vagrant/work" end