Class: Lightchef::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/lightchef/resources/base.rb

Direct Known Subclasses

File, Package

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recipe, name, &block) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/lightchef/resources/base.rb', line 8

def initialize(recipe, name, &block)
  @options = {}
  @recipe = recipe
  instance_eval(&block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lightchef/resources/base.rb', line 25

def method_missing(method, *args)
  if args.size == 1
    @options[method] = args.first
    return
  end
  super
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/lightchef/resources/base.rb', line 6

def options
  @options
end

Instance Method Details

#copy_file(src, dst) ⇒ Object



49
50
51
52
# File 'lib/lightchef/resources/base.rb', line 49

def copy_file(src, dst)
  Logger.debug "Copying a file from '#{src}' to '#{dst}'..."
  backend.copy_file(src, dst)
end

#fetch_option(key) ⇒ Object



19
20
21
22
23
# File 'lib/lightchef/resources/base.rb', line 19

def fetch_option(key)
  @options.fetch(key) do |k|
    raise Error, "#{k} is not specified."
  end
end

#nodeObject



54
55
56
# File 'lib/lightchef/resources/base.rb', line 54

def node
  current_runner.node
end

#runObject



14
15
16
17
# File 'lib/lightchef/resources/base.rb', line 14

def run
  action = fetch_option(:action)
  public_send("#{action}_action".to_sym)
end

#run_command(type, *args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lightchef/resources/base.rb', line 33

def run_command(type, *args)
  command = backend.commands.public_send(type, *args)
  result = backend.run_command(command)
  exit_status = result[:exit_status]
  if exit_status == 0
    Logger.debug "Command `#{command}` succeeded"
    Logger.debug "STDOUT> #{(result[:stdout] || "").chomp}"
    Logger.debug "STDERR> #{(result[:stderr] || "").chomp}"
  else
    Logger.error "Command `#{command}` failed. (exit status: #{exit_status})"
    Logger.error "STDOUT> #{(result[:stdout] || "").chomp}"
    Logger.error "STDERR> #{(result[:stderr] || "").chomp}"
    raise CommandExecutionError
  end
end