Class: Itchy::EventHandlers::BaseEventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/itchy/event_handlers/base_event_handler.rb

Overview

Basic handler implementing required methods. Can be used as a dummy for testing purposes.

Constant Summary collapse

TEMPFILE_BASE =
'vmcatcher_event_metadata_archive'
EVENT_FILE_REGEXP =
/^(?<time>\d+)_(?<type>[[:alnum:]]+)_(?<dc_identifier>[[[:alnum:]]-]+)\.json$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vmcatcher_configuration, options) ⇒ BaseEventHandler

Event handler constructor.

Parameters:

  • vmcatcher_configuration (Itchy::VmcatcherConfiguration)

    current vmcatcher configuration

  • options (Settingslogic)

    current itchy configuration



14
15
16
17
18
19
20
21
22
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 14

def initialize(vmcatcher_configuration, options)
  unless vmcatcher_configuration.is_a?(Itchy::VmcatcherConfiguration)
    fail ArgumentError, '\'vmcatcher_configuration\' must be an instance of ' \
                        'Itchy::VmcatcherConfiguration!'
  end

  @vmcatcher_configuration = vmcatcher_configuration
  @options = options || ::Hashie::Mash.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 8

def options
  @options
end

#vmcatcher_configurationObject (readonly)

Returns the value of attribute vmcatcher_configuration.



8
9
10
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 8

def vmcatcher_configuration
  @vmcatcher_configuration
end

Instance Method Details

#archive!(vmcatcher_event) ⇒ Object

Triggers an archiving procedure on the registered event.

Parameters:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 27

def archive!(vmcatcher_event)
  unless vmcatcher_event.is_a?(Itchy::VmcatcherEvent)
    fail ArgumentError, '\'vmcatcher_event\' must be an instance of ' \
                        'Itchy::VmcatcherEvent!'
  end

  Itchy::Log.info "[#{self.class.name}] Saving " \
                         "#{vmcatcher_event.type.inspect} " \
                         "for #{vmcatcher_event.dc_identifier.inspect}"

  temp_file = ::Tempfile.new(TEMPFILE_BASE)
  permanent_file_path = ::File.join(
    options.,
    "#{::Time.now.to_i}_#{vmcatcher_event.type || 'Unknown'}_#{vmcatcher_event.dc_identifier || 'NoID'}.json"
  )

  temp_file.write(vmcatcher_event.to_pretty_json)
  temp_file.flush

  ::FileUtils.cp(temp_file.path, permanent_file_path)
  set_file_permissions(permanent_file_path)
  temp_file.close

  true
end

#handle!(vmcatcher_event, _event_file) ⇒ Object

Handling procedure



54
55
56
57
58
59
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 54

def handle!(vmcatcher_event, _event_file)
  unless vmcatcher_event.is_a?(Itchy::VmcatcherEvent)
    fail ArgumentError, '\'vmcatcher_event\' must be an instance of ' \
            'Itchy::VmcatcherEvent!'
  end
end

#save_descriptor(descriptor, name) ⇒ Object

Save created descriptor to descriptor directory.

Parameters:

  • descriptor (String)

    json form of appliance descriptor

  • name (String)

    name of appliance descriptor (event name)



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 65

def save_descriptor(descriptor, name)
  begin
    File.open("#{@options.descriptor_dir}/#{File.basename(name)}", 'w') { |f| f.write(descriptor) }
  rescue SystemCallError => ex
    Itchy::Log.fatal "[#{self.class.name}] Failed to save a descriptor #{File.basename(name)}. " \
      "Attempt failed with error #{ex.message}"
      fail Itchy::Errors::PrepareEnvError, ex
  end
  # file permissions for descriptor
  set_file_permissions(File.join(@options.descriptor_dir, File.basename(name)))
end

#set_file_permissions(file) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/itchy/event_handlers/base_event_handler.rb', line 77

def set_file_permissions(file)
  Itchy::Log.debug "[#{self.class.name}] Setting file permissions for file #{file} to #{@options.file_permissions}."

  begin
    ::FileUtils.chmod @options.file_permissions.to_i(8), file
  rescue SystemCallError => ex
    Itchy::Log.error 'Failed to set permissions!!!'
    fail Itchy::Errors::PrepareEnvError, ex
  end
end