class Nucleon::Action::Network::Config

Public Class Methods

describe() click to toggle source
Calls superclass method
   # File lib/nucleon/action/network/config.rb
10 def self.describe
11   super(:network, :config, 949)
12 end

Public Instance Methods

arguments() click to toggle source
   # File lib/nucleon/action/network/config.rb
38 def arguments
39   [ :name, :value ]
40 end
configure() click to toggle source
Calls superclass method
   # File lib/nucleon/action/network/config.rb
17 def configure
18   super do
19     codes :configuration_parse_failed,
20           :configuration_save_failed,
21           :configuration_delete_failed
22 
23     register_str :name
24     register_array :value
25 
26     register_bool :array
27     register_bool :delete
28     register_bool :append
29 
30     register_translator :input_format
31     register_translator :save_format
32     register_translator :format, :json
33   end
34 end
delete_config_property(config_info) click to toggle source
    # File lib/nucleon/action/network/config.rb
 97 def delete_config_property(config_info)
 98   remote_text    = remote_message(settings[:net_remote])
 99   config_file    = config_info[:file].sub(network.directory + File::SEPARATOR, '')
100   render_options = { :config_file => blue(config_file), :remote_text => yellow(remote_text) }
101   success        = false
102 
103   if config_info[:property]
104     name = parse_property_name(config_info[:property])
105     render_options.merge({ :name => blue(name) })
106 
107     config_info[:config].delete(config_info[:property])
108 
109     if File.exists?(config_info[:file])
110       if Util::Disk.write(config_info[:file], config_info[:translator].generate(config_info[:config].export))
111         success = true
112       else
113         error('file_save', render_options)
114         myself.status = code.configuration_save_failed
115       end
116     else
117       info('no_config_file', render_options)
118     end
119   else
120     if File.exists?(config_info[:file])
121       if FileUtils.rm(config_info[:file])
122         success = true
123       else
124         error('file_remove', render_options)
125       end
126     end
127   end
128 
129   if success
130     if network.save({ :files => config_file, :remote => settings[:net_remote], :message => "Deleting configuration #{name} from #{config_file}", :allow_empty => true })
131       success('delete', render_options)
132     else
133       error('delete', render_options)
134       myself.status = code.configuration_save_failed
135     end
136   end
137 end
execute() click to toggle source
Calls superclass method
   # File lib/nucleon/action/network/config.rb
45 def execute
46   super do |node|
47     ensure_network do
48       config_info = parse_config_reference(node, settings[:name])
49 
50       unless config_info
51         myself.status = code.configuration_parse_failed
52       end
53 
54       if settings.delete(:delete, false)
55         delete_config_property(config_info)
56 
57       elsif config_info[:property].nil?
58         render_config_properties(config_info)
59 
60       elsif ! settings[:value].empty?
61         set_config_property(config_info, settings[:value])
62       else
63         render_config_property(config_info)
64       end
65     end
66   end
67 end
parse_config_reference(node, name) click to toggle source
    # File lib/nucleon/action/network/config.rb
197 def parse_config_reference(node, name)
198   # @TODO: Break this method up, URGENTLY, before it gets too hideous
199   # Oh wait, it's already too hideous
200 
201   info        = {}
202   data        = {}
203   config      = CORL::Config.new({}, {}, true, false)
204   translators = CORL.loaded_plugins(:nucleon, :translator).keys
205 
206   # common@php::apache::memory_limit
207   # identity/test@users::user[admin][shell]
208   # servers/development/dev.loc@facts[server_environment]
209 
210   config_elements  = name.split('@')
211 
212   property         = config_elements.size > 1 ? config_elements.pop : nil
213   config_elements  = config_elements[0].split('/') if config_elements.size > 0
214   config_file_name = config_elements.pop
215   config_path      = File.join(network.config_directory, *config_elements)
216   config_dir       = config_file_name ? File.join(config_path, config_file_name) : config_path
217   config_file      = nil
218   config_files     = nil
219   translator       = []
220 
221   if config_file_name
222     property = property.gsub(/\]$/, '').split(/\]?\[/) if property
223 
224     translators.each do |translator_name|
225       config_file = File.join(config_path, "#{config_file_name}." + translator_name.to_s)
226 
227       if File.exists?(config_file)
228         unless data = Util::Disk.read(config_file)
229           error('file_read', { :config_file => config_file })
230           return nil
231         end
232         unless load_translator = CORL.translator({}, translator_name)
233           error('translator_load', { :translator => translator_name })
234           return nil
235         end
236         config.import(load_translator.parse(data))
237         translator << load_translator
238       end
239     end
240   end
241 
242   file_exists = translator.empty? ? false : true
243 
244   if settings[:save_format]
245     translator = CORL.translator({}, settings[:save_format])
246   else
247     if translator.empty?
248       translator = CORL.translator({}, CORL.type_default(:nucleon, :translator))
249     else
250       translator = translator.size > 1 ? translator.shift : translator[0]
251     end
252   end
253 
254   config_file = File.join(config_path, "#{config_file_name}." + translator.plugin_name.to_s)
255 
256   unless file_exists
257     hiera_search_path = node.hiera_configuration[:hierarchy]
258     config_files      = []
259 
260     if File.directory?(config_dir)
261       config_files = Dir.glob("#{config_dir}/**/*").select do |file|
262         is_config = false
263 
264         translators.each do |translator_name|
265           is_config = true if file.match(/\.#{translator_name}/)
266         end
267         is_config
268       end
269 
270       config_files.collect! do |file|
271         file.sub(/#{network.config_directory + File::SEPARATOR}/, '')
272       end
273     end
274 
275     ordered_config_files  = []
276     rendered_config_files = []
277 
278     hiera_search_path.each do |search_path|
279       search_components = search_path.split(File::SEPARATOR)
280 
281       rendered_config_files << "SEARCH: #{search_path}"
282 
283       config_files.each do |file|
284         file_ext                 = File.extname(file)
285         file_components          = file.sub(/\..*$/, '').split(File::SEPARATOR)
286         rendered_file_components = []
287         file_match               = true
288 
289         search_components.each_with_index do |search_item, index|
290           if search_item.strip =~ /^%{:?:?([^}]+)}$/ && index < file_components.size
291             rendered_file_components << cyan(file_components[index])
292           elsif search_item != file_components[index]
293             file_match = false
294           else
295             rendered_file_components << yellow(search_item)
296           end
297         end
298 
299         rendered_file = "        #{rendered_file_components.join(File::SEPARATOR)} [ #{blue(file_ext.sub('.', ''))} ]"
300 
301         if file_match && ! ordered_config_files.include?(file)
302           ordered_config_files << file
303           rendered_config_files << rendered_file
304         end
305       end
306     end
307   end
308 
309   {
310     :translator     => translator,
311     :file           => config_file,
312     :files          => ordered_config_files,
313     :rendered_files => rendered_config_files,
314     :property       => property,
315     :config         => config,
316     :value          => property ? Util::Data.value(config.get(property)) : nil
317   }
318 end
render_config_properties(config_info) click to toggle source
   # File lib/nucleon/action/network/config.rb
72 def render_config_properties(config_info)
73   if file_labels = config_info[:rendered_files]
74     info('subconfigurations', { :prefix => false })
75     info("\n", { :i18n => false })
76     file_labels.each do |label|
77       prefixed_message(:info, '  ', label, { :i18n => false, :prefix => false })
78     end
79     info("\n", { :i18n => false })
80   else
81     format        = settings[:format]
82     myself.result = config_info[:config].export
83     render result, :format => format
84   end
85 end
render_config_property(config_info) click to toggle source
   # File lib/nucleon/action/network/config.rb
89 def render_config_property(config_info)
90   format        = settings[:format]
91   myself.result = config_info[:value]
92   render result, :format => format
93 end
set_config_property(config_info, values) click to toggle source
    # File lib/nucleon/action/network/config.rb
141 def set_config_property(config_info, values)
142   name        = parse_property_name(config_info[:property])
143   remote_text = remote_message(settings[:net_remote])
144   config_file = config_info[:file].sub(network.directory + File::SEPARATOR, '')
145 
146   render_options = { :config_file => blue(config_file), :name => blue(name), :remote_text => yellow(remote_text) }
147 
148   config_file  = config_info[:file].sub(network.directory + File::SEPARATOR, '')
149   input_format = settings[:input_format]
150 
151   values.each_with_index do |value, index|
152     if input_format
153       translator    = CORL.translator({}, input_format)
154       values[index] = translator.parse(value)
155     end
156     values[index] = Util::Data.value(values[index])
157   end
158 
159   if settings[:append]
160     if prev_value = config_info[:value]
161       prev_value = array(prev_value)
162 
163       values.each do |value|
164         prev_value.push(value)
165       end
166       values = prev_value
167     end
168   else
169     if settings[:array]
170       values = array(values)
171     elsif values.size == 1
172       values = values[0]
173     end
174   end
175 
176   myself.result = values
177   config_info[:config].set(config_info[:property], result)
178 
179   FileUtils.mkdir_p(File.dirname(config_info[:file]))
180 
181   if Util::Disk.write(config_info[:file], config_info[:translator].generate(config_info[:config].export))
182     if network.save({ :files => config_file, :remote => settings[:net_remote], :message => "Updating configuration #{name} in #{config_file}", :allow_empty => true })
183       success('update', render_options)
184     else
185       error('update', render_options)
186       myself.status = code.configuration_save_failed
187     end
188   else
189     error('file_save', render_options)
190     myself.status = code.configuration_save_failed
191   end
192 end