Class: Nucleon::Action::Network::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/nucleon/action/network/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.describeObject


Info



10
11
12
# File 'lib/nucleon/action/network/config.rb', line 10

def self.describe
  super(:network, :config, 949)
end

Instance Method Details

#argumentsObject




38
39
40
# File 'lib/nucleon/action/network/config.rb', line 38

def arguments
  [ :name, :value ]
end

#configureObject


Settings



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nucleon/action/network/config.rb', line 17

def configure
  super do
    codes :configuration_parse_failed,
          :configuration_save_failed,
          :configuration_delete_failed

    register_str :name
    register_array :value

    register_bool :array
    register_bool :delete
    register_bool :append

    register_translator :input_format
    register_translator :save_format
    register_translator :format, :json
  end
end

#delete_config_property(config_info) ⇒ Object




97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/nucleon/action/network/config.rb', line 97

def delete_config_property(config_info)
  remote_text    = remote_message(settings[:net_remote])
  config_file    = config_info[:file].sub(network.directory + File::SEPARATOR, '')
  render_options = { :config_file => blue(config_file), :remote_text => yellow(remote_text) }
  success        = false

  if config_info[:property]
    name = parse_property_name(config_info[:property])
    render_options.merge({ :name => blue(name) })

    config_info[:config].delete(config_info[:property])

    if File.exists?(config_info[:file])
      if Util::Disk.write(config_info[:file], config_info[:translator].generate(config_info[:config].export))
        success = true
      else
        error('file_save', render_options)
        myself.status = code.configuration_save_failed
      end
    else
      info('no_config_file', render_options)
    end
  else
    if File.exists?(config_info[:file])
      if FileUtils.rm(config_info[:file])
        success = true
      else
        error('file_remove', render_options)
      end
    end
  end

  if success
    if network.save({ :files => config_file, :remote => settings[:net_remote], :message => "Deleting configuration #{name} from #{config_file}", :allow_empty => true })
      success('delete', render_options)
    else
      error('delete', render_options)
      myself.status = code.configuration_save_failed
    end
  end
end

#executeObject


Operations



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nucleon/action/network/config.rb', line 45

def execute
  super do |node|
    ensure_network do
      config_info = parse_config_reference(node, settings[:name])

      unless config_info
        myself.status = code.configuration_parse_failed
      end

      if settings.delete(:delete, false)
        delete_config_property(config_info)

      elsif config_info[:property].nil?
        render_config_properties(config_info)

      elsif ! settings[:value].empty?
        set_config_property(config_info, settings[:value])
      else
        render_config_property(config_info)
      end
    end
  end
end

#parse_config_reference(node, name) ⇒ Object


Utilities



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/nucleon/action/network/config.rb', line 197

def parse_config_reference(node, name)
  # @TODO: Break this method up, URGENTLY, before it gets too hideous
  # Oh wait, it's already too hideous

  info        = {}
  data        = {}
  config      = CORL::Config.new({}, {}, true, false)
  translators = CORL.loaded_plugins(:nucleon, :translator).keys

  # common@php::apache::memory_limit
  # identity/test@users::user[admin][shell]
  # servers/development/dev.loc@facts[server_environment]

  config_elements  = name.split('@')

  property         = config_elements.size > 1 ? config_elements.pop : nil
  config_elements  = config_elements[0].split('/') if config_elements.size > 0
  config_file_name = config_elements.pop
  config_path      = File.join(network.config_directory, *config_elements)
  config_dir       = config_file_name ? File.join(config_path, config_file_name) : config_path
  config_file      = nil
  config_files     = nil
  translator       = []

  if config_file_name
    property = property.gsub(/\]$/, '').split(/\]?\[/) if property

    translators.each do |translator_name|
      config_file = File.join(config_path, "#{config_file_name}." + translator_name.to_s)

      if File.exists?(config_file)
        unless data = Util::Disk.read(config_file)
          error('file_read', { :config_file => config_file })
          return nil
        end
        unless load_translator = CORL.translator({}, translator_name)
          error('translator_load', { :translator => translator_name })
          return nil
        end
        config.import(load_translator.parse(data))
        translator << load_translator
      end
    end
  end

  file_exists = translator.empty? ? false : true

  if settings[:save_format]
    translator = CORL.translator({}, settings[:save_format])
  else
    if translator.empty?
      translator = CORL.translator({}, CORL.type_default(:nucleon, :translator))
    else
      translator = translator.size > 1 ? translator.shift : translator[0]
    end
  end

  config_file = File.join(config_path, "#{config_file_name}." + translator.plugin_name.to_s)

  unless file_exists
    hiera_search_path = node.hiera_configuration[:hierarchy]
    config_files      = []

    if File.directory?(config_dir)
      config_files = Dir.glob("#{config_dir}/**/*").select do |file|
        is_config = false

        translators.each do |translator_name|
          is_config = true if file.match(/\.#{translator_name}/)
        end
        is_config
      end

      config_files.collect! do |file|
        file.sub(/#{network.config_directory + File::SEPARATOR}/, '')
      end
    end

    ordered_config_files  = []
    rendered_config_files = []

    hiera_search_path.each do |search_path|
      search_components = search_path.split(File::SEPARATOR)

      rendered_config_files << "SEARCH: #{search_path}"

      config_files.each do |file|
        file_ext                 = File.extname(file)
        file_components          = file.sub(/\..*$/, '').split(File::SEPARATOR)
        rendered_file_components = []
        file_match               = true

        search_components.each_with_index do |search_item, index|
          if search_item.strip =~ /^%{:?:?([^}]+)}$/ && index < file_components.size
            rendered_file_components << cyan(file_components[index])
          elsif search_item != file_components[index]
            file_match = false
          else
            rendered_file_components << yellow(search_item)
          end
        end

        rendered_file = "        #{rendered_file_components.join(File::SEPARATOR)} [ #{blue(file_ext.sub('.', ''))} ]"

        if file_match && ! ordered_config_files.include?(file)
          ordered_config_files << file
          rendered_config_files << rendered_file
        end
      end
    end
  end

  {
    :translator     => translator,
    :file           => config_file,
    :files          => ordered_config_files,
    :rendered_files => rendered_config_files,
    :property       => property,
    :config         => config,
    :value          => property ? Util::Data.value(config.get(property)) : nil
  }
end

#render_config_properties(config_info) ⇒ Object


Sub operations



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nucleon/action/network/config.rb', line 72

def render_config_properties(config_info)
  if file_labels = config_info[:rendered_files]
    info('subconfigurations', { :prefix => false })
    info("\n", { :i18n => false })
    file_labels.each do |label|
      prefixed_message(:info, '  ', label, { :i18n => false, :prefix => false })
    end
    info("\n", { :i18n => false })
  else
    format        = settings[:format]
    myself.result = config_info[:config].export
    render result, :format => format
  end
end

#render_config_property(config_info) ⇒ Object




89
90
91
92
93
# File 'lib/nucleon/action/network/config.rb', line 89

def render_config_property(config_info)
  format        = settings[:format]
  myself.result = config_info[:value]
  render result, :format => format
end

#set_config_property(config_info, values) ⇒ Object




141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/nucleon/action/network/config.rb', line 141

def set_config_property(config_info, values)
  name        = parse_property_name(config_info[:property])
  remote_text = remote_message(settings[:net_remote])
  config_file = config_info[:file].sub(network.directory + File::SEPARATOR, '')

  render_options = { :config_file => blue(config_file), :name => blue(name), :remote_text => yellow(remote_text) }

  config_file  = config_info[:file].sub(network.directory + File::SEPARATOR, '')
  input_format = settings[:input_format]

  values.each_with_index do |value, index|
    if input_format
      translator    = CORL.translator({}, input_format)
      values[index] = translator.parse(value)
    end
    values[index] = Util::Data.value(values[index])
  end

  if settings[:append]
    if prev_value = config_info[:value]
      prev_value = array(prev_value)

      values.each do |value|
        prev_value.push(value)
      end
      values = prev_value
    end
  else
    if settings[:array]
      values = array(values)
    elsif values.size == 1
      values = values[0]
    end
  end

  myself.result = values
  config_info[:config].set(config_info[:property], result)

  FileUtils.mkdir_p(File.dirname(config_info[:file]))

  if Util::Disk.write(config_info[:file], config_info[:translator].generate(config_info[:config].export))
    if network.save({ :files => config_file, :remote => settings[:net_remote], :message => "Updating configuration #{name} in #{config_file}", :allow_empty => true })
      success('update', render_options)
    else
      error('update', render_options)
      myself.status = code.configuration_save_failed
    end
  else
    error('file_save', render_options)
    myself.status = code.configuration_save_failed
  end
end