Class: Itamae::Backend::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/itamae/backend.rb

Direct Known Subclasses

Docker, Local, Ssh

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



38
39
40
41
# File 'lib/itamae/backend.rb', line 38

def initialize(options)
  @options = options
  @backend = create_specinfra_backend
end

Instance Method Details

#finalizeObject



122
123
124
# File 'lib/itamae/backend.rb', line 122

def finalize
  # pass
end

#get_command(*args) ⇒ Object



83
84
85
# File 'lib/itamae/backend.rb', line 83

def get_command(*args)
  @backend.command.get(*args)
end

#host_inventoryObject



118
119
120
# File 'lib/itamae/backend.rb', line 118

def host_inventory
  @backend.host_inventory
end

#receive_file(src, dst = nil) ⇒ Object



87
88
89
90
91
92
93
94
# File 'lib/itamae/backend.rb', line 87

def receive_file(src, dst = nil)
  if dst
    Itamae.logger.debug "Receiving a file from '#{src}' to '#{dst}'..."
  else
    Itamae.logger.debug "Receiving a file from '#{src}'..."
  end
  @backend.receive_file(src, dst)
end

#run_command(commands, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/itamae/backend.rb', line 43

def run_command(commands, options = {})
  options = {error: true}.merge(options)

  command = build_command(commands, options)
  Itamae.logger.debug "Executing `#{command}`..."

  result = nil

  Itamae.logger.with_indent do
    reset_output_handler
    result = @backend.run_command(command)
    flush_output_handler_buffer

    if result.exit_status == 0 || !options[:error]
      method = :debug
      message = "exited with #{result.exit_status}"
    else
      method = :error
      message = "Command `#{command}` failed. (exit status: #{result.exit_status})"

      unless Itamae.logger.level == ::Logger::DEBUG
        result.stdout.each_line do |l|
          log_output_line("stdout", l, :error)
        end
        result.stderr.each_line do |l|
          log_output_line("stderr", l, :error)
        end
      end
    end

    Itamae.logger.public_send(method, message)
  end

  if options[:error] && result.exit_status != 0
    raise CommandExecutionError
  end

  result
end

#send_directory(src, dst) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/itamae/backend.rb', line 107

def send_directory(src, dst)
  Itamae.logger.debug "Sending a directory from '#{src}' to '#{dst}'..."
  unless ::File.exist?(src)
    raise SourceNotExistError, "The directory '#{src}' doesn't exist."
  end
  unless ::File.directory?(src)
    raise SourceNotExistError, "'#{src}' is not a directory."
  end
  @backend.send_directory(src, dst)
end

#send_file(src, dst) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/itamae/backend.rb', line 96

def send_file(src, dst)
  Itamae.logger.debug "Sending a file from '#{src}' to '#{dst}'..."
  unless ::File.exist?(src)
    raise SourceNotExistError, "The file '#{src}' doesn't exist."
  end
  unless ::File.file?(src)
    raise SourceNotExistError, "'#{src}' is not a file."
  end
  @backend.send_file(src, dst)
end