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.



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

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.



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

def executed_commands
  @executed_commands
end

Instance Method Details

#finalizeObject



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

def finalize
  # pass
end

#get_command(*args) ⇒ Object



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

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

#host_inventoryObject



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

def host_inventory
  @backend.host_inventory
end

#receive_file(src, dst = nil) ⇒ Object



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

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



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
87
# File 'lib/itamae/backend.rb', line 47

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



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

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



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

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