coral_core 0.2.19 → 0.2.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- metadata +4 -38
- data/.document +0 -5
- data/.gitmodules +0 -12
- data/Gemfile +0 -14
- data/Gemfile.lock +0 -39
- data/Rakefile +0 -77
- data/VERSION +0 -1
- data/coral_core.gemspec +0 -102
- data/lib/coral_core/command.rb +0 -244
- data/lib/coral_core/config.rb +0 -347
- data/lib/coral_core/core.rb +0 -212
- data/lib/coral_core/event/regexp_event.rb +0 -55
- data/lib/coral_core/event.rb +0 -170
- data/lib/coral_core/interface.rb +0 -180
- data/lib/coral_core/memory.rb +0 -226
- data/lib/coral_core/repository.rb +0 -164
- data/lib/coral_core/resource.rb +0 -243
- data/lib/coral_core/template/environment.rb +0 -72
- data/lib/coral_core/template/json.rb +0 -13
- data/lib/coral_core/template/wrapper.rb +0 -13
- data/lib/coral_core/template/yaml.rb +0 -13
- data/lib/coral_core/template.rb +0 -92
- data/lib/coral_core/util/data.rb +0 -219
- data/lib/coral_core/util/disk.rb +0 -92
- data/lib/coral_core/util/git/base.rb +0 -58
- data/lib/coral_core/util/git/lib.rb +0 -82
- data/lib/coral_core/util/git/remote.rb +0 -12
- data/lib/coral_core/util/git.rb +0 -15
- data/lib/coral_core/util/shell.rb +0 -183
- data/lib/coral_core.rb +0 -260
- data/lib/hiera_backend.rb +0 -63
- data/spec/coral_core/interface_spec.rb +0 -489
- data/spec/coral_mock_input.rb +0 -29
- data/spec/coral_test_kernel.rb +0 -22
- data/spec/spec_helper.rb +0 -15
data/lib/coral_core/util/data.rb
DELETED
|
@@ -1,219 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
module Coral
|
|
3
|
-
module Util
|
|
4
|
-
class Data
|
|
5
|
-
|
|
6
|
-
#-----------------------------------------------------------------------------
|
|
7
|
-
# Type checking
|
|
8
|
-
|
|
9
|
-
def self.undef?(value)
|
|
10
|
-
if value.nil? ||
|
|
11
|
-
(value.is_a?(Symbol) && value == :undef || value == :undefined) ||
|
|
12
|
-
(value.is_a?(String) && value.match(/^\s*(undef|UNDEF|Undef|nil|NIL|Nil)\s*$/))
|
|
13
|
-
return true
|
|
14
|
-
end
|
|
15
|
-
return false
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
#---
|
|
19
|
-
|
|
20
|
-
def self.true?(value)
|
|
21
|
-
if value == true ||
|
|
22
|
-
(value.is_a?(String) && value.match(/^\s*(true|TRUE|True)\s*$/))
|
|
23
|
-
return true
|
|
24
|
-
end
|
|
25
|
-
return false
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
#---
|
|
29
|
-
|
|
30
|
-
def self.false?(value)
|
|
31
|
-
if value == false ||
|
|
32
|
-
(value.is_a?(String) && value.match(/^\s*(false|FALSE|False)\s*$/))
|
|
33
|
-
return true
|
|
34
|
-
end
|
|
35
|
-
return false
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
#---
|
|
39
|
-
|
|
40
|
-
def self.empty?(value)
|
|
41
|
-
if undef?(value) || false?(value) || (value.respond_to?('empty?') && value.empty?)
|
|
42
|
-
return true
|
|
43
|
-
end
|
|
44
|
-
return false
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
#-----------------------------------------------------------------------------
|
|
48
|
-
# Translation
|
|
49
|
-
|
|
50
|
-
def self.to_json(data)
|
|
51
|
-
output = ''
|
|
52
|
-
begin
|
|
53
|
-
output = data.to_json
|
|
54
|
-
|
|
55
|
-
rescue Exception
|
|
56
|
-
end
|
|
57
|
-
return output
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
#---
|
|
61
|
-
|
|
62
|
-
def self.to_yaml(data)
|
|
63
|
-
output = ''
|
|
64
|
-
begin
|
|
65
|
-
require 'yaml'
|
|
66
|
-
output = YAML.dump(data)
|
|
67
|
-
|
|
68
|
-
rescue Exception
|
|
69
|
-
end
|
|
70
|
-
return output
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
#---
|
|
74
|
-
|
|
75
|
-
def self.value(value)
|
|
76
|
-
case value
|
|
77
|
-
when String
|
|
78
|
-
if undef?(value)
|
|
79
|
-
value = nil
|
|
80
|
-
elsif true?(value)
|
|
81
|
-
value = true
|
|
82
|
-
elsif false?(value)
|
|
83
|
-
value = false
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
when Array
|
|
87
|
-
value.each_with_index do |item, index|
|
|
88
|
-
value[index] = value(item)
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
when Hash
|
|
92
|
-
value.each do |key, data|
|
|
93
|
-
value[key] = value(data)
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
return value
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
#-----------------------------------------------------------------------------
|
|
100
|
-
# Operations
|
|
101
|
-
|
|
102
|
-
def self.merge(data, force = true)
|
|
103
|
-
value = data
|
|
104
|
-
|
|
105
|
-
# Special case because this method is called from within Config.new so we
|
|
106
|
-
# can not use Config.ensure, as that would cause an infinite loop.
|
|
107
|
-
force = force.is_a?(Coral::Config) ? force.get(:force, true) : force
|
|
108
|
-
|
|
109
|
-
if data.is_a?(Array)
|
|
110
|
-
value = data.shift.clone
|
|
111
|
-
|
|
112
|
-
data.each do |item|
|
|
113
|
-
item = item.clone
|
|
114
|
-
|
|
115
|
-
case value
|
|
116
|
-
when Hash
|
|
117
|
-
begin
|
|
118
|
-
require 'deep_merge'
|
|
119
|
-
value = force ? value.deep_merge!(item) : value.deep_merge(item)
|
|
120
|
-
|
|
121
|
-
rescue LoadError
|
|
122
|
-
if item.is_a?(Hash) # Non recursive top level by default.
|
|
123
|
-
value = value.merge(item)
|
|
124
|
-
elsif force
|
|
125
|
-
value = item
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
when Array
|
|
129
|
-
if item.is_a?(Array)
|
|
130
|
-
value = value.concat(item).uniq
|
|
131
|
-
elsif force
|
|
132
|
-
value = item
|
|
133
|
-
end
|
|
134
|
-
|
|
135
|
-
when String, Symbol
|
|
136
|
-
value = item if item.is_a?(String) || item.is_a?(Symbol) || force
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
return value
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
#---
|
|
145
|
-
|
|
146
|
-
def self.interpolate(value, scope, options = {})
|
|
147
|
-
|
|
148
|
-
pattern = ( options.has_key?(:pattern) ? options[:pattern] : '\$(\{)?([a-zA-Z0-9\_\-]+)(\})?' )
|
|
149
|
-
group = ( options.has_key?(:var_group) ? options[:var_group] : 2 )
|
|
150
|
-
flags = ( options.has_key?(:flags) ? options[:flags] : '' )
|
|
151
|
-
|
|
152
|
-
if scope.is_a?(Hash)
|
|
153
|
-
regexp = Regexp.new(pattern, flags.split(''))
|
|
154
|
-
|
|
155
|
-
replace = lambda do |item|
|
|
156
|
-
matches = item.match(regexp)
|
|
157
|
-
result = nil
|
|
158
|
-
|
|
159
|
-
#dbg(item, 'item')
|
|
160
|
-
#dbg(matches, 'matches')
|
|
161
|
-
|
|
162
|
-
unless matches.nil?
|
|
163
|
-
replacement = scope.search(matches[group], options)
|
|
164
|
-
result = item.gsub(matches[0], replacement) unless replacement.nil?
|
|
165
|
-
end
|
|
166
|
-
return result
|
|
167
|
-
end
|
|
168
|
-
|
|
169
|
-
case value
|
|
170
|
-
when String
|
|
171
|
-
#dbg(value, 'interpolate (string) -> init')
|
|
172
|
-
while (temp = replace.call(value))
|
|
173
|
-
#dbg(temp, 'interpolate (string) -> replacement')
|
|
174
|
-
value = temp
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
when Hash
|
|
178
|
-
#dbg(value, 'interpolate (hash) -> init')
|
|
179
|
-
value.each do |key, data|
|
|
180
|
-
#dbg(data, "interpolate (#{key}) -> data")
|
|
181
|
-
value[key] = interpolate(data, scope, options)
|
|
182
|
-
end
|
|
183
|
-
end
|
|
184
|
-
end
|
|
185
|
-
#dbg(value, 'interpolate -> result')
|
|
186
|
-
return value
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
#-----------------------------------------------------------------------------
|
|
190
|
-
# Utilities
|
|
191
|
-
|
|
192
|
-
def self.prefix(prefix, data)
|
|
193
|
-
result = nil
|
|
194
|
-
|
|
195
|
-
unless prefix.is_a?(String) && ! empty?(prefix)
|
|
196
|
-
prefix = ''
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
case data
|
|
200
|
-
when String, Symbol
|
|
201
|
-
result = ( prefix.empty? ? data.to_s : prefix + '_' + data.to_s )
|
|
202
|
-
|
|
203
|
-
when Array
|
|
204
|
-
result = []
|
|
205
|
-
data.each do |value|
|
|
206
|
-
result << prefix(prefix, value)
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
when Hash
|
|
210
|
-
result = {}
|
|
211
|
-
data.each do |key, value|
|
|
212
|
-
result[prefix(prefix, key)] = value
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
return result
|
|
216
|
-
end
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
end
|
data/lib/coral_core/util/disk.rb
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
module Coral
|
|
3
|
-
module Util
|
|
4
|
-
class Disk
|
|
5
|
-
|
|
6
|
-
#-----------------------------------------------------------------------------
|
|
7
|
-
# Properties
|
|
8
|
-
|
|
9
|
-
@@files = {}
|
|
10
|
-
|
|
11
|
-
@@separator = false
|
|
12
|
-
@@description = ''
|
|
13
|
-
|
|
14
|
-
#-----------------------------------------------------------------------------
|
|
15
|
-
# Utilities
|
|
16
|
-
|
|
17
|
-
def self.open(file_name, options = {}, reset = false)
|
|
18
|
-
mode = options[:mode].to_s
|
|
19
|
-
|
|
20
|
-
@@separator = ( options[:separator] ? options[:separator] : false )
|
|
21
|
-
@@description = ( options[:description] ? options[:description] : '' )
|
|
22
|
-
|
|
23
|
-
if @@files.has_key?(file_name) && ! reset
|
|
24
|
-
reset = true if ! mode.empty? && mode != @@files[file_name][:mode]
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
if ! @@files.has_key?(file_name) || ! @@files[file_name][:file] || reset
|
|
28
|
-
@@files[file_name][:file].close if @@files[file_name] && @@files[file_name][:file]
|
|
29
|
-
unless mode.empty? || ( mode == 'r' && ! File.exists?(file_name) )
|
|
30
|
-
@@files[file_name] = {
|
|
31
|
-
:file => File.open(file_name, mode),
|
|
32
|
-
:mode => mode,
|
|
33
|
-
}
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
return nil unless @@files[file_name]
|
|
37
|
-
return @@files[file_name][:file]
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
#---
|
|
41
|
-
|
|
42
|
-
def self.read(file_name, options = {})
|
|
43
|
-
options[:mode] = ( options[:mode] ? options[:mode] : 'r' )
|
|
44
|
-
file = open(file_name, options)
|
|
45
|
-
|
|
46
|
-
if file
|
|
47
|
-
return file.read
|
|
48
|
-
end
|
|
49
|
-
return nil
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
#---
|
|
53
|
-
|
|
54
|
-
def self.write(file_name, data, options = {})
|
|
55
|
-
options[:mode] = ( options[:mode] ? options[:mode] : 'w' )
|
|
56
|
-
file = open(file_name, options)
|
|
57
|
-
|
|
58
|
-
if file
|
|
59
|
-
return file.write(data)
|
|
60
|
-
end
|
|
61
|
-
return nil
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
#---
|
|
65
|
-
|
|
66
|
-
def self.log(data, options = {})
|
|
67
|
-
reset = ( options[:file_name] || options[:mode] )
|
|
68
|
-
file = open(( options[:file_name] ? options[:file_name] : 'log.txt' ), options, reset)
|
|
69
|
-
if file
|
|
70
|
-
file.write("--------------------------------------\n") if @@separator
|
|
71
|
-
file.write("#{@@description}\n") if @@description
|
|
72
|
-
file.write("#{data}\n")
|
|
73
|
-
end
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
#---
|
|
77
|
-
|
|
78
|
-
def self.close(file_names = [])
|
|
79
|
-
file_names = @@files.keys unless file_names && ! file_names.empty?
|
|
80
|
-
|
|
81
|
-
unless file_names.is_a?(Array)
|
|
82
|
-
file_names = [ file_names ]
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
file_names.each do |file_name|
|
|
86
|
-
@@files[file_name][:file].close if @@files[file_name] && @@files[file_name][:file]
|
|
87
|
-
@@files.delete(file_name)
|
|
88
|
-
end
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
module Git
|
|
3
|
-
|
|
4
|
-
#*******************************************************************************
|
|
5
|
-
# Errors
|
|
6
|
-
|
|
7
|
-
class GitDirectoryError < StandardError
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
#*******************************************************************************
|
|
11
|
-
# Base Git definition
|
|
12
|
-
|
|
13
|
-
class Base
|
|
14
|
-
|
|
15
|
-
#-----------------------------------------------------------------------------
|
|
16
|
-
# Constructor / Destructor
|
|
17
|
-
|
|
18
|
-
def initialize(options = {})
|
|
19
|
-
if working_dir = options[:working_directory]
|
|
20
|
-
options[:repository] ||= File.join(working_dir, '.git')
|
|
21
|
-
|
|
22
|
-
if File.file?(options[:repository])
|
|
23
|
-
File.read(options[:repository]).each_line do |line|
|
|
24
|
-
matches = line.match(/^\s*gitdir:\s*(.+)\s*/)
|
|
25
|
-
if matches.length && matches[1]
|
|
26
|
-
options[:repository] = matches[1]
|
|
27
|
-
break
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
if File.directory?(options[:repository])
|
|
33
|
-
options[:index] ||= File.join(options[:repository], 'index')
|
|
34
|
-
else
|
|
35
|
-
raise GitDirectoryError.new("Git repository directory #{options[:repository]} not found for #{working_dir}")
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
if options[:log]
|
|
40
|
-
@logger = options[:log]
|
|
41
|
-
@logger.info("Starting Git")
|
|
42
|
-
else
|
|
43
|
-
@logger = nil
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
@working_directory = options[:working_directory] ? Git::WorkingDirectory.new(options[:working_directory]) : nil
|
|
47
|
-
@repository = options[:repository] ? Git::Repository.new(options[:repository]) : nil
|
|
48
|
-
@index = options[:index] ? Git::Index.new(options[:index], false) : nil
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
#-----------------------------------------------------------------------------
|
|
52
|
-
# Commit extensions
|
|
53
|
-
|
|
54
|
-
def add(path = '.', opts = {})
|
|
55
|
-
self.lib.add(path, opts)
|
|
56
|
-
end
|
|
57
|
-
end
|
|
58
|
-
end
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
module Git
|
|
3
|
-
class Lib
|
|
4
|
-
|
|
5
|
-
#-----------------------------------------------------------------------------
|
|
6
|
-
# Commit extensions
|
|
7
|
-
|
|
8
|
-
def add(path = '.', opts = {})
|
|
9
|
-
arr_opts = []
|
|
10
|
-
arr_opts << '-u' if opts[:update]
|
|
11
|
-
if path.is_a?(Array)
|
|
12
|
-
arr_opts += path
|
|
13
|
-
else
|
|
14
|
-
arr_opts << path
|
|
15
|
-
end
|
|
16
|
-
command('add', arr_opts)
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
#---
|
|
20
|
-
|
|
21
|
-
def commit(message, opts = {})
|
|
22
|
-
arr_opts = ['-m', message]
|
|
23
|
-
arr_opts << "--author=\'#{opts[:author]}\'" unless opts[:author] && opts[:author].empty?
|
|
24
|
-
arr_opts << '-a' if opts[:add_all]
|
|
25
|
-
arr_opts << '--allow-empty' if opts[:allow_empty]
|
|
26
|
-
command('commit', arr_opts)
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
#-----------------------------------------------------------------------------
|
|
30
|
-
# Remote extensions
|
|
31
|
-
|
|
32
|
-
def remote_add(name, url, opts = {})
|
|
33
|
-
arr_opts = ['add']
|
|
34
|
-
arr_opts << '-f' if opts[:with_fetch]
|
|
35
|
-
arr_opts << name
|
|
36
|
-
arr_opts << url
|
|
37
|
-
|
|
38
|
-
command('remote', arr_opts)
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
#---
|
|
42
|
-
|
|
43
|
-
def remote_set_url(name, url, opts = {})
|
|
44
|
-
arr_opts = ['set-url']
|
|
45
|
-
|
|
46
|
-
if opts[:add]
|
|
47
|
-
arr_opts << '--add' if opts[:add]
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
if opts[:delete]
|
|
51
|
-
arr_opts << '--delete' if opts[:delete]
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
if opts[:push]
|
|
55
|
-
arr_opts << '--push' if opts[:push]
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
arr_opts << name
|
|
59
|
-
arr_opts << url
|
|
60
|
-
|
|
61
|
-
command('remote', arr_opts)
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
#---
|
|
65
|
-
|
|
66
|
-
def remote_remove(name)
|
|
67
|
-
command('remote', ['rm', name])
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
#-----------------------------------------------------------------------------
|
|
71
|
-
# Utilities
|
|
72
|
-
|
|
73
|
-
def escape(s)
|
|
74
|
-
escaped = s.to_s.gsub('"', '\'')
|
|
75
|
-
if escaped =~ /^\-+/
|
|
76
|
-
escaped
|
|
77
|
-
else
|
|
78
|
-
%Q{"#{escaped}"}
|
|
79
|
-
end
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
end
|
data/lib/coral_core/util/git.rb
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
require 'git'
|
|
3
|
-
|
|
4
|
-
module Git
|
|
5
|
-
|
|
6
|
-
#-----------------------------------------------------------------------------
|
|
7
|
-
# Utilities
|
|
8
|
-
|
|
9
|
-
def self.url(host, repo, options = {})
|
|
10
|
-
options[:user] = ( options[:user] ? options[:user] : 'git' )
|
|
11
|
-
options[:auth] = ( options[:auth] ? options[:auth] : true )
|
|
12
|
-
|
|
13
|
-
return options[:user] + ( options[:auth] ? '@' : '://' ) + host + ( options[:auth] ? ':' : '/' ) + repo
|
|
14
|
-
end
|
|
15
|
-
end
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
module Coral
|
|
3
|
-
module Util
|
|
4
|
-
class Shell < Core
|
|
5
|
-
|
|
6
|
-
#-----------------------------------------------------------------------------
|
|
7
|
-
# Utilities
|
|
8
|
-
|
|
9
|
-
def self.exec!(command, options = {})
|
|
10
|
-
config = Config.ensure(options)
|
|
11
|
-
|
|
12
|
-
min = config.get(:min, 1).to_i
|
|
13
|
-
tries = config.get(:tries, min).to_i
|
|
14
|
-
tries = ( min > tries ? min : tries )
|
|
15
|
-
|
|
16
|
-
info_prefix = config.get(:info_prefix, '')
|
|
17
|
-
info_suffix = config.get(:info_suffix, '')
|
|
18
|
-
error_prefix = config.get(:error_prefix, '')
|
|
19
|
-
error_suffix = config.get(:error_suffix, '')
|
|
20
|
-
|
|
21
|
-
ui = config.get(:ui, Coral.ui)
|
|
22
|
-
|
|
23
|
-
conditions = Coral::Event.instance(config.get(:exit, {}), true)
|
|
24
|
-
|
|
25
|
-
$stdout.sync = true
|
|
26
|
-
$stderr.sync = true
|
|
27
|
-
|
|
28
|
-
for i in tries.downto(1)
|
|
29
|
-
ui.info(">> running: #{command}")
|
|
30
|
-
|
|
31
|
-
begin
|
|
32
|
-
t1, output_new, output_orig, output_reader = pipe_exec_stream!($stdout, conditions, {
|
|
33
|
-
:prefix => info_prefix,
|
|
34
|
-
:suffix => info_suffix,
|
|
35
|
-
}, 'output') do |line|
|
|
36
|
-
block_given? ? yield(line) : true
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
t2, error_new, error_orig, error_reader = pipe_exec_stream!($stderr, conditions, {
|
|
40
|
-
:prefix => error_prefix,
|
|
41
|
-
:suffix => error_suffix,
|
|
42
|
-
}, 'error') do |line|
|
|
43
|
-
block_given? ? yield(line) : true
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
system_success = system(command)
|
|
47
|
-
|
|
48
|
-
ensure
|
|
49
|
-
output_success = close_exec_pipe(t1, $stdout, output_orig, output_new, 'output')
|
|
50
|
-
error_success = close_exec_pipe(t2, $stderr, error_orig, error_new, 'error')
|
|
51
|
-
end
|
|
52
|
-
ui.info('')
|
|
53
|
-
|
|
54
|
-
success = ( system_success && output_success && error_success )
|
|
55
|
-
|
|
56
|
-
min -= 1
|
|
57
|
-
break if success && min <= 0 && conditions.empty?
|
|
58
|
-
end
|
|
59
|
-
unless conditions.empty?
|
|
60
|
-
success = false
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
return success
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
#---
|
|
67
|
-
|
|
68
|
-
def self.exec(command, options = {})
|
|
69
|
-
return exec!(command, options)
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
#---
|
|
73
|
-
|
|
74
|
-
def self.pipe_exec_stream!(output, conditions, options, label)
|
|
75
|
-
original = output.dup
|
|
76
|
-
read, write = IO.pipe
|
|
77
|
-
|
|
78
|
-
match_prefix = ( options[:match_prefix] ? options[:match_prefix] : 'EXIT' )
|
|
79
|
-
|
|
80
|
-
thread = process_stream!(read, original, options, label) do |line|
|
|
81
|
-
check_conditions!(line, conditions, match_prefix) do
|
|
82
|
-
block_given? ? yield(line) : true
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
thread.abort_on_exception = false
|
|
87
|
-
|
|
88
|
-
output.reopen(write)
|
|
89
|
-
return thread, write, original, read
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
#---
|
|
93
|
-
|
|
94
|
-
def self.close_exec_pipe(thread, output, original, write, label)
|
|
95
|
-
output.reopen(original)
|
|
96
|
-
|
|
97
|
-
write.close
|
|
98
|
-
success = thread.value
|
|
99
|
-
|
|
100
|
-
original.close
|
|
101
|
-
return success
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
#---
|
|
105
|
-
|
|
106
|
-
def self.check_conditions!(line, conditions, match_prefix = '')
|
|
107
|
-
prefix = ''
|
|
108
|
-
|
|
109
|
-
unless ! conditions || conditions.empty?
|
|
110
|
-
conditions.each do |key, event|
|
|
111
|
-
if event.check(line)
|
|
112
|
-
prefix = match_prefix
|
|
113
|
-
conditions.delete(key)
|
|
114
|
-
end
|
|
115
|
-
end
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
result = true
|
|
119
|
-
if block_given?
|
|
120
|
-
result = yield
|
|
121
|
-
|
|
122
|
-
unless prefix.empty?
|
|
123
|
-
case result
|
|
124
|
-
when Hash
|
|
125
|
-
result[:prefix] = prefix
|
|
126
|
-
else
|
|
127
|
-
result = { :success => result, :prefix => prefix }
|
|
128
|
-
end
|
|
129
|
-
end
|
|
130
|
-
end
|
|
131
|
-
return result
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
#---
|
|
135
|
-
|
|
136
|
-
def self.process_stream!(input, output, options, label)
|
|
137
|
-
return Thread.new do
|
|
138
|
-
success = true
|
|
139
|
-
default_prefix = ( options[:prefix] ? options[:prefix] : '' )
|
|
140
|
-
default_suffix = ( options[:suffix] ? options[:suffix] : '' )
|
|
141
|
-
|
|
142
|
-
begin
|
|
143
|
-
while ( data = input.readpartial(1024) )
|
|
144
|
-
message = data.strip
|
|
145
|
-
newline = ( data[-1,1].match(/\n/) ? true : false )
|
|
146
|
-
|
|
147
|
-
unless message.empty?
|
|
148
|
-
lines = message.split(/\n/)
|
|
149
|
-
lines.each_with_index do |line, index|
|
|
150
|
-
prefix = default_prefix
|
|
151
|
-
suffix = default_suffix
|
|
152
|
-
|
|
153
|
-
unless line.empty?
|
|
154
|
-
if block_given?
|
|
155
|
-
result = yield(line)
|
|
156
|
-
|
|
157
|
-
if result && result.is_a?(Hash)
|
|
158
|
-
prefix = result[:prefix]
|
|
159
|
-
suffix = result[:suffix]
|
|
160
|
-
result = result[:success]
|
|
161
|
-
end
|
|
162
|
-
success = result if success
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
prefix = ( prefix && ! prefix.empty? ? "#{prefix}: " : '' )
|
|
166
|
-
suffix = ( suffix && ! suffix.empty? ? suffix : '' )
|
|
167
|
-
eol = ( index < lines.length - 1 || newline ? "\n" : ' ' )
|
|
168
|
-
|
|
169
|
-
output.write(prefix.lstrip + line + suffix.rstrip + eol)
|
|
170
|
-
end
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
rescue EOFError
|
|
175
|
-
end
|
|
176
|
-
|
|
177
|
-
input.close()
|
|
178
|
-
success
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
end
|
|
183
|
-
end
|