elecksee 1.0.22 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,63 +1,29 @@
1
- require 'shellwords'
1
+ require 'elecksee'
2
2
 
3
3
  class Lxc
4
- class CommandFailed < StandardError
5
- attr_accessor :original, :result
6
- def initialize(orig, result=nil)
7
- @original = orig
8
- @result = result
9
- super(orig.to_s)
10
- end
11
- end
12
-
13
- class Timeout < CommandFailed
14
- end
15
-
16
- class CommandResult
17
- attr_reader :original, :stdout, :stderr
18
- def initialize(result)
19
- @original = result
20
- if(result.class.ancestors.map(&:to_s).include?('ChildProcess::AbstractProcess'))
21
- extract_childprocess
22
- elsif(result.class.to_s == 'Mixlib::ShellOut')
23
- extract_shellout
24
- else
25
- raise TypeError.new("Unknown process result type received: #{result.class}")
26
- end
27
- end
28
-
29
- def extract_childprocess
30
- original.io.stdout.rewind
31
- original.io.stderr.rewind
32
- @stdout = original.io.stdout.read
33
- @stderr = original.io.stderr.read
34
- original.io.stdout.delete
35
- original.io.stderr.delete
36
- end
37
-
38
- def extract_shellout
39
- @stdout = original.stdout
40
- @stderr = original.stderr
41
- end
42
- end
43
-
4
+ # Helper modules
44
5
  module Helpers
45
6
 
7
+ autoload :Copies, 'elecksee/helpers/copies'
8
+ autoload :Options, 'elecksee/helpers/options'
9
+
10
+ # @return [String] sudo command string
46
11
  def sudo
47
12
  Lxc.sudo
48
13
  end
49
14
 
50
- # Simple helper to shell out
15
+ # Shellout wrapper
16
+ #
17
+ # @param cmd [String]
18
+ # @param args [Hash]
19
+ # @option args [Integer] :allow_failure_retry number of retries
20
+ # @option args [Numeric] :timeout max execution time
21
+ # @option args [TrueClass, FalseClass] :sudo use sudo
22
+ # @option args [TrueClass, FalseClass] :allow_failure don't raise on error
23
+ # @return [CommandResult]
51
24
  def run_command(cmd, args={})
52
25
  result = nil
53
- cmd_type = Lxc.shellout_helper
54
- unless(cmd_type)
55
- if(defined?(ChildProcess))
56
- cmd_type = :childprocess
57
- else
58
- cmd_type = :mixlib_shellout
59
- end
60
- end
26
+ cmd_type = :childprocess
61
27
  com_block = nil
62
28
  case cmd_type
63
29
  when :childprocess
@@ -73,6 +39,15 @@ class Lxc
73
39
  result == false ? false : CommandResult.new(result)
74
40
  end
75
41
 
42
+ # Shellout using childprocess
43
+ #
44
+ # @param cmd [String]
45
+ # @param args [Hash]
46
+ # @option args [Integer] :allow_failure_retry number of retries
47
+ # @option args [Numeric] :timeout max execution time
48
+ # @option args [TrueClass, FalseClass] :sudo use sudo
49
+ # @option args [TrueClass, FalseClass] :allow_failure don't raise on error
50
+ # @return [ChildProcess::AbstractProcess]
76
51
  def child_process_command(cmd, args)
77
52
  retries = args[:allow_failure_retry].to_i
78
53
  cmd = [sudo, cmd].join(' ') if args[:sudo]
@@ -109,6 +84,15 @@ class Lxc
109
84
  end
110
85
  end
111
86
 
87
+ # Shellout using mixlib shellout
88
+ #
89
+ # @param cmd [String]
90
+ # @param args [Hash]
91
+ # @option args [Integer] :allow_failure_retry number of retries
92
+ # @option args [Numeric] :timeout max execution time
93
+ # @option args [TrueClass, FalseClass] :sudo use sudo
94
+ # @option args [TrueClass, FalseClass] :allow_failure don't raise on error
95
+ # @return [Mixlib::ShellOut]
112
96
  def mixlib_shellout_command(cmd, args)
113
97
  retries = args[:allow_failure_retry].to_i
114
98
  cmd = [sudo, cmd].join(' ') if args[:sudo]
@@ -137,11 +121,9 @@ class Lxc
137
121
  end
138
122
  end
139
123
  end
124
+ alias_method :command, :run_command
140
125
 
141
- def command(*args)
142
- run_command(*args)
143
- end
144
-
126
+ # @return [Logger] logger instance
145
127
  def log
146
128
  if(defined?(Chef))
147
129
  Chef::Log
@@ -154,8 +136,11 @@ class Lxc
154
136
  end
155
137
  end
156
138
 
157
- # Detect HOME environment variable. If not an acceptable
158
- # value, set to /root or /tmp
139
+ # Detect HOME if environment variable is not set
140
+ #
141
+ # @param set_if_missing [TrueClass, FalseClass] set environment variable if missing
142
+ # @return [String] value detected
143
+ # @note if detection fails, first writeable path is used from /root or /tmp
159
144
  def detect_home(set_if_missing=false)
160
145
  if(ENV['HOME'] && Pathname.new(ENV['HOME']).absolute?)
161
146
  ENV['HOME']
@@ -167,5 +152,79 @@ class Lxc
167
152
  home
168
153
  end
169
154
  end
155
+
156
+ end
157
+
158
+ # Command failure class
159
+ class CommandFailed < StandardError
160
+
161
+ # @return [StandardError] original exception
162
+ attr_accessor :original
163
+ # @return [Object] command result
164
+ attr_accessor :result
165
+
166
+ # Create new instance
167
+ #
168
+ # @param orig [StandardError] original exception
169
+ # @param result [Object] command result
170
+ def initialize(orig, result=nil)
171
+ @original = orig
172
+ @result = result
173
+ super(orig.to_s)
174
+ end
175
+ end
176
+
177
+ # Command exceeded timeout
178
+ class Timeout < CommandFailed
179
+ end
180
+
181
+ # Result of command
182
+ class CommandResult
183
+
184
+ # @return [Object] original result
185
+ attr_reader :original
186
+ # @return [IO] stdout of command
187
+ attr_reader :stdout
188
+ # @return [IO] stderr of command
189
+ attr_reader :stderr
190
+
191
+ # Create new instance
192
+ #
193
+ # @param result [Object] result of command
194
+ def initialize(result)
195
+ @original = result
196
+ if(result.class.ancestors.map(&:to_s).include?('ChildProcess::AbstractProcess'))
197
+ extract_childprocess
198
+ elsif(result.class.to_s == 'Mixlib::ShellOut')
199
+ extract_shellout
200
+ elsif(result.class.to_s == 'Rye::Err' || result.class.to_s == 'Rye::Rap')
201
+ extract_rye
202
+ else
203
+ raise TypeError.new("Unknown process result type received: #{result.class}")
204
+ end
205
+ end
206
+
207
+ # Extract information from childprocess result
208
+ def extract_childprocess
209
+ original.io.stdout.rewind
210
+ original.io.stderr.rewind
211
+ @stdout = original.io.stdout.read
212
+ @stderr = original.io.stderr.read
213
+ original.io.stdout.delete
214
+ original.io.stderr.delete
215
+ end
216
+
217
+ # Extract information from mixlib shellout result
218
+ def extract_shellout
219
+ @stdout = original.stdout
220
+ @stderr = original.stderr
221
+ end
222
+
223
+ # Extract information from rye result
224
+ def extract_rye
225
+ @stdout = original.stdout.map(&:to_s).join("\n")
226
+ @stderr = original.stderr.map(&:to_s).join("\n")
227
+ end
228
+
170
229
  end
171
230
  end