Class: Itamae::Backend::Base
- Inherits:
-
Object
- Object
- Itamae::Backend::Base
- Defined in:
- lib/itamae/backend.rb
Instance Method Summary collapse
- #finalize ⇒ Object
- #get_command(*args) ⇒ Object
- #host_inventory ⇒ Object
-
#initialize(options) ⇒ Base
constructor
A new instance of Base.
- #receive_file(src, dst = nil) ⇒ Object
- #run_command(commands, options = {}) ⇒ Object
- #send_directory(src, dst) ⇒ Object
- #send_file(src, dst) ⇒ Object
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 = @backend = create_specinfra_backend end |
Instance Method Details
#finalize ⇒ Object
146 147 148 |
# File 'lib/itamae/backend.rb', line 146 def finalize # pass end |
#get_command(*args) ⇒ Object
107 108 109 |
# File 'lib/itamae/backend.rb', line 107 def get_command(*args) @backend.command.get(*args) end |
#host_inventory ⇒ Object
142 143 144 |
# File 'lib/itamae/backend.rb', line 142 def host_inventory @backend.host_inventory end |
#receive_file(src, dst = nil) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/itamae/backend.rb', line 111 def receive_file(src, dst = nil) if dst Logger.debug "Receiving a file from '#{src}' to '#{dst}'..." else 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/itamae/backend.rb', line 43 def run_command(commands, = {}) = {error: true}.merge() if commands.is_a?(Array) command = commands.map do |cmd| Shellwords.escape(cmd) end.join(' ') else command = commands end cwd = [:cwd] if cwd command = "cd #{Shellwords.escape(cwd)} && #{command}" end user = [:user] if user command = "sudo -H -u #{Shellwords.escape(user)} -- /bin/sh -c #{Shellwords.escape(command)}" end Logger.debug "Executing `#{command}`..." result = @backend.run_command(command) exit_status = result.exit_status Logger.formatter.with_indent do if exit_status == 0 || ![:error] method = :debug = "exited with #{exit_status}" else method = :error = "Command `#{command}` failed. (exit status: #{exit_status})" end Logger.public_send(method, ) {"stdout" => result.stdout, "stderr" => result.stderr}.each_pair do |name, value| next unless value && value != '' if value.bytesize > 1024 * 1024 Logger.public_send(method, "#{name} is suppressed because it's too large") next end value.each_line do |line| # remove control chars case line.encoding when Encoding::UTF_8 line = line.tr("\u0000-\u001f\u007f\u2028",'') end Logger.public_send(method, "#{name} | #{line}") end end end if [:error] && exit_status != 0 raise CommandExecutionError end result end |
#send_directory(src, dst) ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/itamae/backend.rb', line 131 def send_directory(src, dst) 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
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/itamae/backend.rb', line 120 def send_file(src, dst) 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 |