Class: ChefDK::Policyfile::DSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StorageConfigDelegation

#cache_path, #policyfile_expanded_path, #policyfile_filename, #policyfile_lock_expanded_path, #relative_paths_root

Constructor Details

#initialize(storage_config) ⇒ DSL

Returns a new instance of DSL.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/chef-dk/policyfile/dsl.rb', line 42

def initialize(storage_config)
  @name = nil
  @errors = []
  @run_list = []
  @named_run_lists = {}
  @default_source = [ NullCookbookSource.new ]
  @cookbook_location_specs = {}
  @storage_config = storage_config

  @node_attributes = Chef::Node::Attribute.new({}, {}, {}, {})
end

Instance Attribute Details

#cookbook_location_specsObject (readonly)

Returns the value of attribute cookbook_location_specs.



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

def cookbook_location_specs
  @cookbook_location_specs
end

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

Returns the value of attribute default_source.



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

def default_source
  @default_source
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#name(name = nil) ⇒ Object



54
55
56
57
58
59
# File 'lib/chef-dk/policyfile/dsl.rb', line 54

def name(name = nil)
  unless name.nil?
    @name = name
  end
  @name
end

#named_run_listsObject (readonly)

Returns the value of attribute named_run_lists.



37
38
39
# File 'lib/chef-dk/policyfile/dsl.rb', line 37

def named_run_lists
  @named_run_lists
end

#node_attributesObject (readonly)

Returns the value of attribute node_attributes.



38
39
40
# File 'lib/chef-dk/policyfile/dsl.rb', line 38

def node_attributes
  @node_attributes
end

#run_list(*run_list_items) ⇒ Object (readonly)

Returns the value of attribute run_list.



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

def run_list
  @run_list
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



40
41
42
# File 'lib/chef-dk/policyfile/dsl.rb', line 40

def storage_config
  @storage_config
end

Instance Method Details

#cookbook(name, *version_and_source_opts) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/chef-dk/policyfile/dsl.rb', line 85

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 = CookbookLocationSpecification.new(name, constraint, source_options, storage_config)


  if existing_source = @cookbook_location_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
    @cookbook_location_specs[name] = spec
    @errors += spec.errors
  end
end

#defaultObject



108
109
110
# File 'lib/chef-dk/policyfile/dsl.rb', line 108

def default
  @node_attributes.default
end

#eval_policyfile(policyfile_string) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/chef-dk/policyfile/dsl.rb', line 116

def eval_policyfile(policyfile_string)
  @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"
    # TODO: need a way to disable filtering
    error_message << filtered_bt(policyfile_filename, e).inject("") { |formatted_trace, line| formatted_trace << "    #{line}\n" }
  end
  @errors << error_message
end

#named_run_list(name, *run_list_items) ⇒ Object



67
68
69
# File 'lib/chef-dk/policyfile/dsl.rb', line 67

def named_run_list(name, *run_list_items)
  @named_run_lists[name] = run_list_items.flatten
end

#overrideObject



112
113
114
# File 'lib/chef-dk/policyfile/dsl.rb', line 112

def override
  @node_attributes.override
end