Class: ChefDK::Policyfile::DSL

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

Constant Summary collapse

RUN_LIST_ITEM_COMPONENT =
%r{^[.[:alnum:]_-]+$}

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, chef_config: nil) ⇒ DSL

Returns a new instance of DSL.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/chef-dk/policyfile/dsl.rb', line 46

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

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

Instance Attribute Details

#chef_configObject (readonly)

Returns the value of attribute chef_config.



45
46
47
# File 'lib/chef-dk/policyfile/dsl.rb', line 45

def chef_config
  @chef_config
end

#cookbook_location_specsObject (readonly)

Returns the value of attribute cookbook_location_specs.



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

def cookbook_location_specs
  @cookbook_location_specs
end

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

Returns the value of attribute default_source.



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

def default_source
  @default_source
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#name(name = nil) ⇒ Object



59
60
61
62
63
64
# File 'lib/chef-dk/policyfile/dsl.rb', line 59

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

#named_run_listsObject (readonly)

Returns the value of attribute named_run_lists.



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

def named_run_lists
  @named_run_lists
end

#node_attributesObject (readonly)

Returns the value of attribute node_attributes.



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

def node_attributes
  @node_attributes
end

#run_list(*run_list_items) ⇒ Object (readonly)

Returns the value of attribute run_list.



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

def run_list
  @run_list
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



43
44
45
# File 'lib/chef-dk/policyfile/dsl.rb', line 43

def storage_config
  @storage_config
end

Instance Method Details

#cookbook(name, *version_and_source_opts) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/chef-dk/policyfile/dsl.rb', line 100

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



122
123
124
# File 'lib/chef-dk/policyfile/dsl.rb', line 122

def default
  @node_attributes.default
end

#eval_policyfile(policyfile_string) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef-dk/policyfile/dsl.rb', line 130

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}\"\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



75
76
77
78
79
80
81
82
# File 'lib/chef-dk/policyfile/dsl.rb', line 75

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

#overrideObject



126
127
128
# File 'lib/chef-dk/policyfile/dsl.rb', line 126

def override
  @node_attributes.override
end