Module: ItamaeSpec::Task::Base

Defined in:
lib/itamae-spec/task/base.rb

Constant Summary collapse

RoleLoadError =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.get_node_recipes(node_file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/itamae-spec/task/base.rb', line 59

def get_node_recipes(node_file)
  recipes = []
  JSON.parse(File.read(node_file))['run_list'].each do |recipe|
    if /recipe\[(.+)::(.+)\]/.match?(recipe)
      recipes << { recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2') }
    elsif /recipe\[(.+)\]/.match?(recipe)
      recipes << { recipe.gsub(/recipe\[(.+)\]/, '\1') => 'default' }
    elsif /role\[(.+)\]/.match?(recipe)
      recipes << get_role_recipes(recipe.gsub(/role\[(.+)\]/, '\1'))
    end
  end
rescue JSON::ParserError
  raise RoleLoadError, "JSON Parser Faild. - #{node_file}"
rescue Errno::ENOENT
  raise RoleLoadError, "No such node file or directory - #{node_fie}"
else
  recipes
end

.get_role_recipes(role) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/itamae-spec/task/base.rb', line 42

def get_role_recipes(role)
  recipes = []
  JSON.parse(File.read("roles/#{role}.json"))['run_list'].each do |recipe|
    if /recipe\[(.+)::(.+)\]/.match?(recipe)
      recipes << { recipe.gsub(/recipe\[(.+)::(.+)\]/, '\1') => recipe.gsub(/recipe\[(.+)::(.+)\]/, '\2') }
    elsif /recipe\[(.+)\]/.match?(recipe)
      recipes << { recipe.gsub(/recipe\[(.+)\]/, '\1') => 'default' }
    end
  end
rescue JSON::ParserError
  raise RoleLoadError, "JSON Parser Faild. - roles/#{role}.json"
rescue Errno::ENOENT
  raise RoleLoadError, "No such role file or directory - roles/#{role}.json"
else
  recipes
end

.write_tmp_nodes(filename) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/itamae-spec/task/base.rb', line 78

def write_tmp_nodes(filename)
  Itamae.logger.info "Output attributes log file to: tmp-nodes/#{filename}.json"

  File.open "tmp-nodes/#{filename}.json", 'w' do |f|
    f.flock File::LOCK_EX
    yield f
    f.flock File::LOCK_UN
  end
end