Class: Itamae::Backend
- Inherits:
-
Object
- Object
- Itamae::Backend
- Includes:
- Singleton
- Defined in:
- lib/itamae/backend.rb
Constant Summary collapse
- UnknownBackendTypeError =
Class.new(StandardError)
- CommandExecutionError =
Class.new(StandardError)
Instance Method Summary collapse
- #get_command(*args) ⇒ Object
- #run_command(commands, options = {}) ⇒ Object
- #send_directory(*args) ⇒ Object
- #send_file(*args) ⇒ Object
- #set_type(type, options = {}) ⇒ Object
Instance Method Details
#get_command(*args) ⇒ Object
109 110 111 |
# File 'lib/itamae/backend.rb', line 109 def get_command(*args) Specinfra.command.get(*args) end |
#run_command(commands, options = {}) ⇒ Object
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 106 107 |
# File 'lib/itamae/backend.rb', line 45 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 -u #{Shellwords.escape(user)} -- /bin/sh -c #{Shellwords.escape(command)}" end Logger.debug "Executing `#{command}`..." result = Specinfra::Runner.run_command(command) exit_status = result.exit_status Logger.formatter.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(*args) ⇒ Object
117 118 119 |
# File 'lib/itamae/backend.rb', line 117 def send_directory(*args) Specinfra::Runner.send_directory(*args) end |
#send_file(*args) ⇒ Object
113 114 115 |
# File 'lib/itamae/backend.rb', line 113 def send_file(*args) Specinfra::Runner.send_file(*args) end |
#set_type(type, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/itamae/backend.rb', line 29 def set_type(type, = {}) case type when :local Specinfra.configuration.backend = :exec when :ssh Specinfra.configuration.request_pty = true Specinfra.configuration.host = .delete(:host) Specinfra.configuration.disable_sudo = .delete(:disable_sudo) Specinfra.configuration. = Specinfra.configuration.backend = :ssh else raise UnknownBackendTypeError, "'#{type}' backend is unknown." end end |