Class: Itamae::Backend::Base

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

Direct Known Subclasses

Docker, Jexec, Local, Ssh

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.



40
41
42
43
44
# File 'lib/itamae/backend.rb', line 40

def initialize(options)
  @options = options
  @backend = create_specinfra_backend
  @executed_commands = []
end

Instance Attribute Details

#executed_commandsObject (readonly)

Returns the value of attribute executed_commands.



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

def executed_commands
  @executed_commands
end

Instance Method Details

#finalizeObject



135
136
137
# File 'lib/itamae/backend.rb', line 135

def finalize
  # pass
end

#get_command(*args) ⇒ Object



88
89
90
# File 'lib/itamae/backend.rb', line 88

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

#host_inventoryObject



131
132
133
# File 'lib/itamae/backend.rb', line 131

def host_inventory
  @backend.host_inventory
end

#receive_file(src, dst = nil) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/itamae/backend.rb', line 92

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



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
82
83
84
85
86
# File 'lib/itamae/backend.rb', line 46

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 = run_command_with_profiling(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



120
121
122
123
124
125
126
127
128
129
# File 'lib/itamae/backend.rb', line 120

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, user: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/itamae/backend.rb', line 101

def send_file(src, dst, user: nil)
  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

  if self.instance_of?(Backend::Local)
    read_command = build_command("cat #{src.shellescape}", {})
    write_command = build_command("cat > #{dst.shellescape}", user: user)
    command = [read_command, write_command].join(' | ')
    run_command(command)
  else
    @backend.send_file(src, dst)
  end
end