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 =
/^[.[:alnum:]_-]+$/.freeze

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.



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

def initialize(storage_config, chef_config: nil)
  @name = nil
  @errors = []
  @run_list = []
  @named_run_lists = {}
  @included_policies = []
  @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.



47
48
49
# File 'lib/chef-dk/policyfile/dsl.rb', line 47

def chef_config
  @chef_config
end

#cookbook_location_specsObject (readonly)

Returns the value of attribute cookbook_location_specs.



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

def cookbook_location_specs
  @cookbook_location_specs
end

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



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

def default_source(source_type = nil, source_argument = nil, &block)
  return @default_source if source_type.nil?

  case source_type
  when :community, :supermarket
    set_default_community_source(source_argument, &block)
  when :delivery_supermarket
    set_default_delivery_supermarket_source(source_argument, &block)
  when :chef_server
    set_default_chef_server_source(source_argument, &block)
  when :chef_repo
    set_default_chef_repo_source(source_argument, &block)
  when :artifactory
    set_default_artifactory_source(source_argument, &block)
  else
    @errors << "Invalid default_source type '#{source_type.inspect}'"
  end
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#included_policiesObject (readonly)

Returns the value of attribute included_policies.



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

def included_policies
  @included_policies
end

#name(name = nil) ⇒ Object



62
63
64
65
66
67
# File 'lib/chef-dk/policyfile/dsl.rb', line 62

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

#named_run_listsObject (readonly)

Returns the value of attribute named_run_lists.



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

def named_run_lists
  @named_run_lists
end

#node_attributesObject (readonly)

Returns the value of attribute node_attributes.



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

def node_attributes
  @node_attributes
end

#run_list(*run_list_items) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/chef-dk/policyfile/dsl.rb', line 69

def run_list(*run_list_items)
  run_list_items = run_list_items.flatten
  unless run_list_items.empty?
    validate_run_list_items(run_list_items)
    @run_list = run_list_items
  end
  @run_list
end

#storage_configObject (readonly)

Returns the value of attribute storage_config.



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

def storage_config
  @storage_config
end

Instance Method Details

#cookbook(name, *version_and_source_opts) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/chef-dk/policyfile/dsl.rb', line 106

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



141
142
143
# File 'lib/chef-dk/policyfile/dsl.rb', line 141

def default
  @node_attributes.default
end

#eval_policyfile(policyfile_string) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/chef-dk/policyfile/dsl.rb', line 149

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

#include_policy(name, source_options = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/chef-dk/policyfile/dsl.rb', line 128

def include_policy(name, source_options = {})
  if ( existing = included_policies.find { |p| p.name == name } )
    err = "Included policy '#{name}' assigned conflicting locations or was already specified\n\n"
    err << "Previous source: #{existing.source_options.inspect}\n"
    err << "Conflicts with: #{source_options.inspect}\n"
    @errors << err
  else
    spec = PolicyfileLocationSpecification.new(name, source_options, storage_config, chef_config)
    included_policies << spec
    @errors += spec.errors
  end
end

#named_run_list(name, *run_list_items) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/chef-dk/policyfile/dsl.rb', line 78

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



145
146
147
# File 'lib/chef-dk/policyfile/dsl.rb', line 145

def override
  @node_attributes.override
end