Class: ChefDK::Policyfile::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/chef-dk/policyfile/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSL

Returns a new instance of DSL.



32
33
34
35
36
37
38
# File 'lib/chef-dk/policyfile/dsl.rb', line 32

def initialize
  @errors = []
  @run_list = []
  @default_source = NullCookbookSource.new
  @policyfile_cookbook_specs = {}
  @policyfile_filename = nil
end

Instance Attribute Details

#default_source(source_type = nil, source_uri = nil) ⇒ Object (readonly)

Returns the value of attribute default_source.



27
28
29
# File 'lib/chef-dk/policyfile/dsl.rb', line 27

def default_source
  @default_source
end

#errorsObject (readonly)

Returns the value of attribute errors.



25
26
27
# File 'lib/chef-dk/policyfile/dsl.rb', line 25

def errors
  @errors
end

#policyfile_cookbook_specsObject (readonly)

Returns the value of attribute policyfile_cookbook_specs.



28
29
30
# File 'lib/chef-dk/policyfile/dsl.rb', line 28

def policyfile_cookbook_specs
  @policyfile_cookbook_specs
end

#policyfile_filenameObject

Returns the value of attribute policyfile_filename.



30
31
32
# File 'lib/chef-dk/policyfile/dsl.rb', line 30

def policyfile_filename
  @policyfile_filename
end

#run_list(*run_list_items) ⇒ Object (readonly)

Returns the value of attribute run_list.



26
27
28
# File 'lib/chef-dk/policyfile/dsl.rb', line 26

def run_list
  @run_list
end

Instance Method Details

#cookbook(name, *version_and_source_opts) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef-dk/policyfile/dsl.rb', line 58

def cookbook(name, *version_and_source_opts)
  source_options =
    if version_and_source_opts.last.is_a?(Hash)
      version_and_source_opts.pop
    else
      {}
    end

  constraint = version_and_source_opts.first || ">= 0.0.0"
  spec = CookbookSpec.new(name, constraint, source_options, policyfile_filename)


  if existing_source = @policyfile_cookbook_specs[name]
    err = "Cookbook '#{name}' assigned to conflicting sources\n\n"
    err << "Previous source: #{existing_source.source_options.inspect}\n"
    err << "Conflicts with: #{source_options.inspect}\n"
    @errors << err
  else
    @policyfile_cookbook_specs[name] = spec
  end
end

#eval_policyfile(policyfile_string, policyfile_filename) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/chef-dk/policyfile/dsl.rb', line 80

def eval_policyfile(policyfile_string, policyfile_filename)
  @policyfile_filename = policyfile_filename
  instance_eval(policyfile_string, policyfile_filename)
  validate!
  self
rescue SyntaxError => e
  @errors << "Invalid ruby syntax in policyfile '#{policyfile_filename}':\n\n#{e.message}"
rescue SignalException, SystemExit
  # allow signal from kill, ctrl-C, etc. to bubble up:
  raise
rescue Exception => e
  error_message = "Evaluation of policyfile '#{policyfile_filename}' raised an exception\n"
  error_message << "  Exception: #{e.class.name} \"#{e.to_s}\"\n\n"
  trace = filtered_bt(policyfile_filename, e)
  error_message << "  Relevant Code:\n"
  error_message << "    #{error_context(policyfile_string, policyfile_filename, e)}\n\n"
  unless trace.empty?
    error_message << "  Backtrace:\n"
    error_message << filtered_bt(policyfile_filename, e).inject("") { |formatted_trace, line| formatted_trace << "    #{line}" }
    error_message << "\n"
  end
  @errors << error_message
end