Skip to content

Instantly share code, notes, and snippets.

@sue445
Last active April 22, 2020 10:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sue445/39ff664b462ab7fb230b to your computer and use it in GitHub Desktop.
Save sue445/39ff664b462ab7fb230b to your computer and use it in GitHub Desktop.
俺のitamaeの実行方法

roleとnodeを設定しておけばいい感じにrake taskを生成します

前提

下記のようなディレクトリ構成を想定しています

  • roles/
    • <role名>.rb
  • nodes/
    • <node名>.yml
  • spec/
    • <role名>/*_spec.rb
  • Rakefile
  • hosts.yml

Usage

rake recipe:production                       # Run itamae recipes to [production] all
rake recipe:production:myapp-01              # Run itamae recipes to [production] myapp-01 (xxx.xxx.xxx.xxx)
rake recipe:production:myapp-02              # Run itamae recipes to [production] myapp-02 (xxx.xxx.xxx.xxx)
rake recipe:staging                          # Run itamae recipes to [staging] all
rake recipe:staging:myapp-staging-01  # Run itamae recipes to [staging] myapp-staging-01 (xxx.xxx.xxx.xxx)
rake spec:production                         # Run serverspec tests to [production] all
rake spec:production:myapp-01                # Run serverspec tests to [production] myapp-01 (xxx.xxx.xxx.xxx)
rake spec:production:myapp-02                # Run serverspec tests to [production] myapp-02 (xxx.xxx.xxx.xxx)
rake spec:staging                            # Run serverspec tests to [staging] all
rake spec:staging:myapp-staging-01           # Run serverspec tests to [staging] myapp-staging-03 (xxx.xxx.xxx.xxx)
staging:
- hostname: myapp-staging-01
ip_address: xxx.xxx.xxx.xxx
role_suite: staging
production:
- hostname: myapp-01
ip_address: xxx.xxx.xxx.xxx
role_suite: production_master
- hostname: myapp-02
ip_address: xxx.xxx.xxx.xxx
role_suite: production_slave
require 'rake'
require 'rspec/core/rake_task'
require "yaml"
require "active_support/core_ext/hash/indifferent_access"
def hosts(stage_name)
@host_config ||= YAML.load_file("#{__dir__}/hosts.yml").with_indifferent_access
raise "NotFound: #{stage_name}" unless @host_config[stage_name]
@host_config[stage_name]
end
def stages
@stages ||= YAML.load_file("#{__dir__}/hosts.yml").keys
end
namespace :spec do
stages.each do |stage_name|
task_names = []
hosts(stage_name).each do |host|
task_name = "#{stage_name}:#{host[:hostname]}"
task_names << task_name
# ホスト単位でspec実行するtask
desc "Run serverspec tests to [#{stage_name}] #{host[:hostname]} (#{host[:ip_address]})"
RSpec::Core::RakeTask.new(task_name) do |t|
ENV["TARGET_HOST"] = host[:ip_address]
ENV["NODE"] = stage_name
t.pattern = "spec/#{stage_name}/*_spec.rb"
end
end
# ホスト全台にspec実行するtask
desc "Run serverspec tests to [#{stage_name}] all"
task stage_name => task_names do
end
end
end
namespace :recipe do
stages.each do |stage_name|
task_names = []
hosts(stage_name).each do |host|
task_name = "#{stage_name}:#{host[:hostname]}"
task_names << task_name
# ホスト単位でitamae実行するtask
desc "Run itamae recipes to [#{stage_name}] #{host[:hostname]} (#{host[:ip_address]})"
task task_name do
log_level = ENV["LOG_LEVEL"] || "info"
sh "bundle exec itamae ssh -h #{host[:ip_address]} -p 22 --node-yaml nodes/#{stage_name}.yml --log-level=#{log_level} roles/#{host[:role_suite]}.rb"
end
end
# ホスト全台にitamae実行するtask
desc "Run itamae recipes to [#{stage_name}] all"
task stage_name => task_names do
end
end
end
# 一番下に追加
ENV["NODE"] ||= "staging"
def node
@node ||= YAML.load_file("#{__dir__}/../nodes/#{ENV["NODE"]}.yml").with_indifferent_access
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment