itamae 1.3.3 → 1.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ad665ed56e3ebcad528cbfd6a6a6b1d3b77f084
4
- data.tar.gz: 989188954e3f2f2062a6a21397b54a4bc81c3afc
3
+ metadata.gz: 3c3734924aed2c2ab9472e781e7c512c31599b46
4
+ data.tar.gz: fe29b7a53a36075a3886be1d89e17355edf528cd
5
5
  SHA512:
6
- metadata.gz: 9a3708718a9b72bc1403bb85c4fbf283dc83f58aa9709ea8ddc60f27d1e1a0dec3a3e0ba1ae1a5890237f7fd8772f59a918030bac7d7406e95d946a25a3f8e54
7
- data.tar.gz: 6780d1cf5dda81bbb1cd2966f06b87ad1c12420e3df4557f123ed8b50a46575841f9bcdbca9aed7bea70be615637e12d7038a258ce36ba317707d5f124a660c5
6
+ metadata.gz: c7a9d8d4400cc1b46276c7e5115e4c5bd653fb9d2cb63c3edcd548d36207b021947b8925aaaab9b693bf8e4a26db4c9324275c0b80a4e2ea9ac5e4cb382f97a2
7
+ data.tar.gz: abd0f092cfa89168efe723b0210a58ba3a4ec82993818be5cda8a7787d5827148744fd6dda2355e2482146b79d7414f084bc7e4d3b568a2d695385abc4721379
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v1.3.4
2
+
3
+ Improvements
4
+
5
+ - [Output stdout/err logs during command execution](https://github.com/itamae-kitchen/itamae/commit/24f140dd9744f30c645422959a6a72b6e31eacc4)
6
+
1
7
  ## v1.3.3
2
8
 
3
9
  Improvements
data/itamae.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ["lib"]
19
19
 
20
20
  spec.add_runtime_dependency "thor"
21
- spec.add_runtime_dependency "specinfra", [">= 2.31.0", "< 3.0.0"]
21
+ spec.add_runtime_dependency "specinfra", [">= 2.37.0", "< 3.0.0"]
22
22
  spec.add_runtime_dependency "hashie"
23
23
  spec.add_runtime_dependency "ansi"
24
24
  spec.add_runtime_dependency "schash", "~> 0.1.0"
@@ -43,61 +43,37 @@ module Itamae
43
43
  def run_command(commands, options = {})
44
44
  options = {error: true}.merge(options)
45
45
 
46
- if commands.is_a?(Array)
47
- command = commands.map do |cmd|
48
- Shellwords.escape(cmd)
49
- end.join(' ')
50
- else
51
- command = commands
52
- end
53
-
54
- cwd = options[:cwd]
55
- if cwd
56
- command = "cd #{Shellwords.escape(cwd)} && #{command}"
57
- end
58
-
59
- user = options[:user]
60
- if user
61
- command = "sudo -H -u #{Shellwords.escape(user)} -- /bin/sh -c #{Shellwords.escape(command)}"
62
- end
63
-
46
+ command = build_command(commands, options)
64
47
  Logger.debug "Executing `#{command}`..."
65
48
 
66
- result = @backend.run_command(command)
67
- exit_status = result.exit_status
49
+ result = nil
68
50
 
69
51
  Logger.formatter.with_indent do
70
- if exit_status == 0 || !options[:error]
52
+ reset_output_handler
53
+ result = @backend.run_command(command)
54
+ flush_output_handler_buffer
55
+
56
+ if result.exit_status == 0 || !options[:error]
71
57
  method = :debug
72
- message = "exited with #{exit_status}"
58
+ message = "exited with #{result.exit_status}"
73
59
  else
74
60
  method = :error
75
- message = "Command `#{command}` failed. (exit status: #{exit_status})"
76
- end
77
-
78
- Logger.public_send(method, message)
79
-
80
- {"stdout" => result.stdout, "stderr" => result.stderr}.each_pair do |name, value|
81
- next unless value && value != ''
61
+ message = "Command `#{command}` failed. (exit status: #{result.exit_status})"
82
62
 
83
- if value.bytesize > 1024 * 1024
84
- Logger.public_send(method, "#{name} is suppressed because it's too large")
85
- next
86
- end
87
-
88
- value.each_line do |line|
89
- # remove control chars
90
- case line.encoding
91
- when Encoding::UTF_8
92
- line = line.tr("\u0000-\u001f\u007f\u2028",'')
63
+ unless Logger.logger.level == ::Logger::DEBUG
64
+ result.stdout.each_line do |l|
65
+ log_output_line("stdout", l, :error)
66
+ end
67
+ result.stderr.each_line do |l|
68
+ log_output_line("stderr", l, :error)
93
69
  end
94
-
95
- Logger.public_send(method, "#{name} | #{line}")
96
70
  end
97
71
  end
72
+
73
+ Logger.public_send(method, message)
98
74
  end
99
75
 
100
- if options[:error] && exit_status != 0
76
+ if options[:error] && result.exit_status != 0
101
77
  raise CommandExecutionError
102
78
  end
103
79
 
@@ -152,6 +128,59 @@ module Itamae
152
128
  def create_specinfra_backend
153
129
  raise NotImplementedError
154
130
  end
131
+
132
+ def reset_output_handler
133
+ @buf = {}
134
+ %w!stdout stderr!.each do |output_name|
135
+ @buf[output_name] = ""
136
+ handler = lambda do |str|
137
+ lines = str.split(/\r?\n/, -1)
138
+ @buf[output_name] += lines.pop
139
+ unless lines.empty?
140
+ lines[0] = @buf[output_name] + lines[0]
141
+ @buf[output_name] = ""
142
+ lines.each do |l|
143
+ log_output_line(output_name, l)
144
+ end
145
+ end
146
+ end
147
+ @backend.public_send("#{output_name}_handler=", handler)
148
+ end
149
+ end
150
+
151
+ def flush_output_handler_buffer
152
+ @buf.each do |output_name, line|
153
+ next if line.empty?
154
+ log_output_line(output_name, line)
155
+ end
156
+ end
157
+
158
+ def log_output_line(output_name, line, severity = :debug)
159
+ line = line.gsub(/[[:cntrl:]]/, '')
160
+ Logger.public_send(severity, "#{output_name} | #{line}")
161
+ end
162
+
163
+ def build_command(commands, options)
164
+ if commands.is_a?(Array)
165
+ command = commands.map do |cmd|
166
+ Shellwords.escape(cmd)
167
+ end.join(' ')
168
+ else
169
+ command = commands
170
+ end
171
+
172
+ cwd = options[:cwd]
173
+ if cwd
174
+ command = "cd #{Shellwords.escape(cwd)} && #{command}"
175
+ end
176
+
177
+ user = options[:user]
178
+ if user
179
+ command = "sudo -H -u #{Shellwords.escape(user)} -- /bin/sh -c #{Shellwords.escape(command)}"
180
+ end
181
+
182
+ command
183
+ end
155
184
  end
156
185
 
157
186
  # TODO: Make Specinfra's backends instanciatable
@@ -1 +1 @@
1
- 1.3.3
1
+ 1.3.4
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itamae
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.3
4
+ version: 1.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryota Arai
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 2.31.0
33
+ version: 2.37.0
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
36
  version: 3.0.0
@@ -40,7 +40,7 @@ dependencies:
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 2.31.0
43
+ version: 2.37.0
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: 3.0.0