easy_io 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +201 -0
- data/lib/easy_io.rb +36 -0
- data/lib/easy_io/config.rb +15 -0
- data/lib/easy_io/disk.rb +84 -0
- data/lib/easy_io/logger.rb +54 -0
- data/lib/easy_io/registry.rb +16 -0
- data/lib/easy_io/run.rb +163 -0
- data/lib/easy_io/terminal.rb +37 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f4995afc12ae9df92f39bab54a14a07745c319b1
|
4
|
+
data.tar.gz: ee3b92492666e222aa9895eaace81c96c238acb1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 52189de578634351266752e2b67fa3c70ab71ff0713afa9ee9d4385fadc0542444e7d317b9ba2562e9ba583b5d0e64a32ffd4f13a6ec530e18df99a0c6789d4f
|
7
|
+
data.tar.gz: 5d5087dfbbc6c7e64019c38cce980d9f09011ac8fd99fbee01866c995d2b0be0626d37ecd89fbc866274209495894f65599eb41bd1cc84c0499fb704e14566b1
|
data/LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
data/lib/easy_io.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#
|
2
|
+
# Author:: Alex Munoz (<amunoz951@gmail.com>)
|
3
|
+
# Copyright:: Copyright (c) 2020 Alex Munoz
|
4
|
+
# License:: Apache License, Version 2.0
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
|
18
|
+
require 'easy_format'
|
19
|
+
require 'easy_json_config'
|
20
|
+
require 'logger'
|
21
|
+
require 'open3'
|
22
|
+
require 'fileutils'
|
23
|
+
require 'tmpdir'
|
24
|
+
require 'os'
|
25
|
+
require 'digest'
|
26
|
+
if OS.windows?
|
27
|
+
require 'win32/registry'
|
28
|
+
require 'win32api'
|
29
|
+
end
|
30
|
+
|
31
|
+
require_relative 'easy_io/config'
|
32
|
+
require_relative 'easy_io/disk'
|
33
|
+
require_relative 'easy_io/logger'
|
34
|
+
require_relative 'easy_io/registry' if OS.windows?
|
35
|
+
require_relative 'easy_io/run'
|
36
|
+
require_relative 'easy_io/terminal'
|
data/lib/easy_io/disk.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module EasyIO
|
2
|
+
module Disk
|
3
|
+
unless defined? GetDiskFreeSpaceEx
|
4
|
+
GetDiskFreeSpaceEx = Win32API.new('kernel32', 'GetDiskFreeSpaceEx', 'PPPP', 'I') if OS.windows? # TODO: make this cross platform. sys-filesystem gem maybe?
|
5
|
+
end
|
6
|
+
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def free_space(path)
|
10
|
+
raise 'Cannot check free space for path provided. Path is empty or nil.' if path.nil? || path.empty? || path == 'null'
|
11
|
+
root_folder = root_directory(path)
|
12
|
+
|
13
|
+
raise "Cannot check free space for #{path} - The path was not found." if root_folder.nil? || root_folder.empty?
|
14
|
+
root_folder = EasyFormat::Directory.ensure_trailing_slash(root_folder)
|
15
|
+
|
16
|
+
free = [0].pack('Q')
|
17
|
+
GetDiskFreeSpaceEx.call(root_folder, 0, 0, free)
|
18
|
+
free = free.unpack1('Q')
|
19
|
+
|
20
|
+
(free / 1024.0 / 1024.0).round(2)
|
21
|
+
end
|
22
|
+
|
23
|
+
def size(path)
|
24
|
+
raise 'Cannot check free space for path provided. Path is empty or nil.' if path.nil? || path.empty? || path == 'null'
|
25
|
+
root_folder = root_directory(path)
|
26
|
+
|
27
|
+
raise "Cannot check free space for #{path} - The path was not found." if root_folder.nil? || root_folder.empty?
|
28
|
+
root_folder = EasyFormat::Directory.ensure_trailing_slash(root_folder)
|
29
|
+
|
30
|
+
total = [0].pack('Q')
|
31
|
+
GetDiskFreeSpaceEx.call(root_folder, 0, total, 0)
|
32
|
+
total = total.unpack1('Q')
|
33
|
+
|
34
|
+
(total / 1024.0 / 1024.0).round(2)
|
35
|
+
end
|
36
|
+
|
37
|
+
def move_files(search_string, destination_folder)
|
38
|
+
files = Dir.glob(search_string.tr('\\', '/'))
|
39
|
+
FileUtils.mkdir_p destination_folder unless ::File.directory? destination_folder
|
40
|
+
FileUtils.move(files, destination_folder) { true }
|
41
|
+
end
|
42
|
+
|
43
|
+
def read_files(search_string)
|
44
|
+
files = Dir.glob(search_string.tr('\\', '/'))
|
45
|
+
files.map { |file| ::File.read(file) }
|
46
|
+
end
|
47
|
+
|
48
|
+
# Gets the root directory of a path. Local and UNC paths accepted
|
49
|
+
def root_directory(path)
|
50
|
+
computed_path = path
|
51
|
+
computed_path = File.dirname(computed_path) while computed_path != File.dirname(computed_path)
|
52
|
+
computed_path
|
53
|
+
end
|
54
|
+
|
55
|
+
def valid_checksum?(file, sha256_checksum)
|
56
|
+
return false if sha256_checksum.nil? || !::File.exist?(file)
|
57
|
+
Digest::SHA256.file(file).hexdigest.downcase == sha256_checksum.downcase
|
58
|
+
end
|
59
|
+
|
60
|
+
# Opens a file in the filesystem and locks it exclusively. If it fails, it will keep trying until the timeout.
|
61
|
+
# Pass a block to be executed while the file is locked. The ::File object is passed to the block.
|
62
|
+
def open_file_and_wait_for_exclusive_lock(path, timeout: 60, status_interval: 15, silent: false)
|
63
|
+
start_time = Time.now
|
64
|
+
raise "Cannot create #{::File.basename(path)} - the parent directory does not exist (#{::File.dirname(path)})!" unless ::File.directory?(::File.dirname(path))
|
65
|
+
::File.open(path, ::File::RDWR | ::File::CREAT) do |file|
|
66
|
+
loop do
|
67
|
+
if Time.now >= start_time + timeout # locking timed out.
|
68
|
+
file.close
|
69
|
+
raise "Failed to gain exclusive lock on #{path}! Timed out after #{timeout} seconds."
|
70
|
+
end
|
71
|
+
lock_success = file.flock(File::LOCK_EX | File::LOCK_NB)
|
72
|
+
if lock_success
|
73
|
+
yield(file)
|
74
|
+
file.close
|
75
|
+
break
|
76
|
+
end
|
77
|
+
seconds_elapsed = Time.now - start_time
|
78
|
+
EasyIO.logger.info "Waiting for another process to unlock #{path}... Time elapsed: #{seconds_elapsed}" if seconds_elapsed % status_interval == 0 && !silent # Output status every (status_interval) seconds
|
79
|
+
sleep(1)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Add a couple methods to the Logger class
|
2
|
+
module LoggerEnhancement
|
3
|
+
unless respond_to?(:line)
|
4
|
+
def line(filler_character)
|
5
|
+
info filler_character * (EasyIO::Terminal.columns - 25)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
unless respond_to?(:header)
|
10
|
+
def header(header_text = '', header_type: :standard, filler_character: '*')
|
11
|
+
terminal_columns = EasyIO::Terminal.columns
|
12
|
+
max_header_size = terminal_columns - 31 # Allow at least 1 character on either side of the header
|
13
|
+
header_text = header_text[0...max_header_size] if header_text.length > max_header_size # Truncate the header if it is too big
|
14
|
+
filler = filler_character * ((terminal_columns - (header_text.length + 29)) / 2)
|
15
|
+
info filler_character * (terminal_columns - 27) if [:primary, :secondary].include?(header_type)
|
16
|
+
info "#{filler} #{header_text} #{filler}" + filler_character * ((terminal_columns + header_text.length + 1) % 2)
|
17
|
+
info filler_character * (terminal_columns - 27) if header_type == :primary
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module EasyIO
|
23
|
+
Logger.class_eval { include LoggerEnhancement }
|
24
|
+
@logger = Logger.new(STDOUT)
|
25
|
+
@logger.formatter = proc do |severity, datetime, _progname, msg|
|
26
|
+
"#{datetime.strftime('%Y-%m-%d %H:%M:%S')} #{severity}: #{msg}\n"
|
27
|
+
end
|
28
|
+
|
29
|
+
module_function
|
30
|
+
|
31
|
+
# For portability, can be overridden with a class that has methods :level, :fatal, :error, :warn, :info, :debug and the others specified below.
|
32
|
+
# See https://ruby-doc.org/stdlib-2.4.0/libdoc/logger/rdoc/Logger.html
|
33
|
+
#
|
34
|
+
# For example, when using with Chef, set the logger to Chef::Log
|
35
|
+
def logger=(value)
|
36
|
+
@logger = value
|
37
|
+
@logger.class.class_eval { include LoggerEnhancement }
|
38
|
+
end
|
39
|
+
|
40
|
+
def logger
|
41
|
+
@logger
|
42
|
+
end
|
43
|
+
|
44
|
+
def levels
|
45
|
+
{
|
46
|
+
'info' => Logger::INFO,
|
47
|
+
'debug' => Logger::DEBUG,
|
48
|
+
'warn' => Logger::WARN,
|
49
|
+
'error' => Logger::ERROR,
|
50
|
+
'fatal' => Logger::FATAL,
|
51
|
+
'unknown' => Logger::UNKNOWN,
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module EasyIO
|
2
|
+
module Registry
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def read(key_path, value_name)
|
6
|
+
::Win32::Registry::HKEY_LOCAL_MACHINE.open(key_path, ::Win32::Registry::KEY_READ).read_s(value_name)
|
7
|
+
end
|
8
|
+
|
9
|
+
def key_exists?(path)
|
10
|
+
Win32::Registry::HKEY_LOCAL_MACHINE.open(path, ::Win32::Registry::KEY_READ)
|
11
|
+
true
|
12
|
+
rescue
|
13
|
+
false
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/easy_io/run.rb
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
module EasyIO
|
2
|
+
module_function
|
3
|
+
|
4
|
+
# execute a command with real-time output. Any stdout you want returned to the caller must come after the :output_separator which defaults to '#return_data#:'
|
5
|
+
# return_all_stdout: return all output to the caller instead after process completion
|
6
|
+
def execute_out(command, pid_logfile: nil, working_folder: Dir.pwd, regex_error_filters: [], info_on_exception: '', exception_exceptions: [], powershell: false, show_command_on_error: false, raise_on_first_error: true, return_all_stdout: false, output_separator: nil)
|
7
|
+
output_separator ||= '#return_data#:'
|
8
|
+
if return_all_stdout
|
9
|
+
result = ''
|
10
|
+
return_data_flag = true
|
11
|
+
else
|
12
|
+
STDOUT.sync = true
|
13
|
+
result = nil
|
14
|
+
return_data_flag = false
|
15
|
+
end
|
16
|
+
exit_status = nil
|
17
|
+
error_messages = []
|
18
|
+
info_on_exception = "#{info_on_exception}\n" unless info_on_exception.end_with?("\n")
|
19
|
+
error_options = { 'show_command_on_error' => show_command_on_error, 'info_on_exception' => info_on_exception, 'regex_error_filters' => regex_error_filters, 'raise_on_first_error' => raise_on_first_error }
|
20
|
+
if powershell
|
21
|
+
ps_script_file = "#{EasyIO.config['paths']['cache']}/easy_io/scripts/ps_script-thread_id-#{Thread.current.object_id}.ps1"
|
22
|
+
FileUtils.mkdir_p(::File.dirname(ps_script_file)) unless ::File.directory? ::File.dirname(ps_script_file)
|
23
|
+
::File.write(ps_script_file, command)
|
24
|
+
end
|
25
|
+
popen_arguments = powershell ? ['powershell.exe', ps_script_file] : [command]
|
26
|
+
Open3.popen3(*popen_arguments, chdir: working_folder) do |_stdin, stdout, stderr, wait_thread|
|
27
|
+
unless pid_logfile.nil? # Log pid in case job or script dies
|
28
|
+
FileUtils.mkdir_p(::File.dirname(pid_logfile)) unless ::File.directory? ::File.dirname(pid_logfile)
|
29
|
+
::File.write(pid_logfile, wait_thread.pid)
|
30
|
+
end
|
31
|
+
buffers = [stdout, stderr]
|
32
|
+
queued_buffers = IO.select(buffers) || [[]]
|
33
|
+
queued_buffers.first.each do |buffer|
|
34
|
+
case buffer
|
35
|
+
when stdout
|
36
|
+
while (line = buffer.gets)
|
37
|
+
if return_data_flag
|
38
|
+
result += line
|
39
|
+
next
|
40
|
+
end
|
41
|
+
stdout_split = line.split(output_separator)
|
42
|
+
stdout_message = stdout_split.first.strip
|
43
|
+
_parse_for_errors(stdout_message, error_messages, error_options, command)
|
44
|
+
EasyIO.logger.info stdout_message unless stdout_message.empty?
|
45
|
+
if stdout_split.count > 1
|
46
|
+
return_data_flag = true
|
47
|
+
result = stdout_split.last
|
48
|
+
end
|
49
|
+
end
|
50
|
+
when stderr
|
51
|
+
error_message = ''
|
52
|
+
error_message += line while (line = buffer.gets)
|
53
|
+
next if error_message.empty?
|
54
|
+
if exception_exceptions.any? { |ignore_filter| error_message =~ ignore_filter }
|
55
|
+
EasyIO.logger.info error_message.strip
|
56
|
+
next
|
57
|
+
end
|
58
|
+
_process_error_message(error_message, error_messages, error_options, command)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
exit_status = wait_thread.value
|
62
|
+
end
|
63
|
+
unless error_messages.empty?
|
64
|
+
last_error = _full_error_message(error_messages.pop, error_options, command)
|
65
|
+
error_messages.map! { |error_message| _full_error_message(error_message, error_options, nil) }
|
66
|
+
error_messages.push(last_error)
|
67
|
+
raise error_messages.join("\n")
|
68
|
+
end
|
69
|
+
[result, exit_status]
|
70
|
+
end
|
71
|
+
|
72
|
+
# execute a powershell script with real-time output. Any stdout you want returned to the caller must come after the :output_separator which defaults to '#return_data#:'
|
73
|
+
# return_all_stdout: return all output to the caller instead after process completion
|
74
|
+
def powershell_out(ps_script, pid_logfile: nil, working_folder: Dir.pwd, regex_error_filters: [], info_on_exception: '', exception_exceptions: [], show_command_on_error: false, return_all_stdout: false, output_separator: nil)
|
75
|
+
execute_out(ps_script, pid_logfile: pid_logfile, working_folder: working_folder, regex_error_filters: regex_error_filters, info_on_exception: info_on_exception, exception_exceptions: exception_exceptions, powershell: true, show_command_on_error: show_command_on_error, return_all_stdout: return_all_stdout, output_separator: output_separator)
|
76
|
+
end
|
77
|
+
|
78
|
+
def run_remote_powershell_command(remote_host, command, credentials, set_as_trusted_host: false)
|
79
|
+
add_as_winrm_trusted_host(remote_host) if set_as_trusted_host
|
80
|
+
|
81
|
+
remote_command = <<-EOS
|
82
|
+
$securePassword = ConvertTo-SecureString -AsPlainText '#{credentials['password']}' -Force
|
83
|
+
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList #{credentials['user']}, $securePassword
|
84
|
+
Invoke-Command -ComputerName #{remote_host} -Credential $credential -ScriptBlock { #{command} }
|
85
|
+
EOS
|
86
|
+
output = powershell_out(remote_command, return_all_stdout: true)
|
87
|
+
{
|
88
|
+
'stdout' => output.first,
|
89
|
+
'exitcode' => output.last,
|
90
|
+
}
|
91
|
+
rescue => ex
|
92
|
+
{
|
93
|
+
'exception' => ex,
|
94
|
+
'stderr' => ex.message,
|
95
|
+
'exitcode' => 1,
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def run_command_on_remote_hosts(remote_hosts, command, credentials, command_message: nil, shell_type: :cmd, tail_count: nil, set_as_trusted_host: false)
|
100
|
+
tail_count ||= 1 # Return the last (1) line from each remote_host's log to the console
|
101
|
+
supported_shell_types = [:cmd, :powershell] # TODO: implement shell_type :bash
|
102
|
+
raise "Unsupported shell_type for running remote commands: '#{shell_type}'" unless supported_shell_types.include?(shell_type)
|
103
|
+
|
104
|
+
threads = {}
|
105
|
+
threads_output = {}
|
106
|
+
log_folder = "#{EasyIO.config['paths']['cache']}/easy_io/logs"
|
107
|
+
::FileUtils.mkdir_p log_folder unless ::File.directory?(log_folder)
|
108
|
+
EasyIO.logger.info "Output logs of processes run on the specified remote hosts will be placed in #{log_folder}..."
|
109
|
+
remote_hosts.each do |remote_host|
|
110
|
+
EasyIO.logger.info "Running `#{command_message || command}` on #{remote_host}..."
|
111
|
+
# threads_output[remote_host] = run_remote_powershell_command(remote_host, command, credentials, set_as_trusted_host: set_as_trusted_host)
|
112
|
+
threads[remote_host] = Thread.new do
|
113
|
+
threads_output[remote_host] = run_remote_powershell_command(remote_host, command, credentials, set_as_trusted_host: set_as_trusted_host)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
threads.values.each(&:join) # Wait for all commands to complete
|
117
|
+
# threads.each { |remote_host, thread| pp thread }
|
118
|
+
threads_output.each do |remote_host, output|
|
119
|
+
::File.write("#{log_folder}/#{EasyFormat::File.windows_friendly_name(remote_host)}.#{::Time.now.strftime('%Y%m%d_%H%M%S')}.log", "#{output['stdout']}\n#{output['stderr']}")
|
120
|
+
tail_output = output['stdout'].nil? ? '--no standard output--' : output['stdout'].split("\n").last(tail_count).join("\n")
|
121
|
+
EasyIO.logger.info "[#{remote_host}]: #{tail_output}"
|
122
|
+
raise "Failed to run command on #{remote_host}: #{output['stderr']}\n#{output['exception'].cause}\n#{output['exception'].message}" if output['exception']
|
123
|
+
raise "The script exited with exit code #{output['exitcode']}.\n\n#{output['stderr']}" unless output['exitcode'] == 0
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def add_as_winrm_trusted_host(remote_host)
|
128
|
+
trusted_hosts = EasyIO.powershell_out('(Get-Item WSMan:\localhost\Client\TrustedHosts).value', return_all_stdout: true)
|
129
|
+
EasyIO.powershell_out("Set-Item WSMan:\\localhost\\Client\\TrustedHosts -Value 'trusted_hosts, #{remote_host}' -Force") unless trusted_hosts.include?(remote_host)
|
130
|
+
end
|
131
|
+
|
132
|
+
def _parse_for_errors(message, error_messages, error_options, command)
|
133
|
+
errors_found = error_options['regex_error_filters'].any? { |regex_filter| message =~ regex_filter }
|
134
|
+
_process_error_message(message, error_messages, error_options, command) if errors_found
|
135
|
+
end
|
136
|
+
|
137
|
+
def _process_error_message(error_message, error_messages, error_options, command)
|
138
|
+
raise _full_error_message(error_message, error_options, command) if error_options['raise_on_first_error']
|
139
|
+
error_messages.push(error_message) # if we're not raising right away, add to the list of errors
|
140
|
+
end
|
141
|
+
|
142
|
+
def _full_error_message(error_message, error_options, command)
|
143
|
+
command_message = error_options['show_command_on_error'] && command ? "\nCommand causing exception: " + command + "\n" : ''
|
144
|
+
"Exception: #{error_message}\n#{error_options['info_on_exception']}#{'=' * 120}\n#{command_message}"
|
145
|
+
end
|
146
|
+
|
147
|
+
def pid_running?(pid)
|
148
|
+
begin
|
149
|
+
Process.kill(0, pid) # Does not actually kill process, checks if it's running.
|
150
|
+
rescue Errno::ESRCH
|
151
|
+
nil
|
152
|
+
end == 1
|
153
|
+
end
|
154
|
+
|
155
|
+
def notepad_prompt(text_file_path, comment)
|
156
|
+
::FileUtils.mkdir_p ::File.dirname(text_file_path) unless ::File.directory?(::File.dirname(text_file_path))
|
157
|
+
::File.write(text_file_path, "; #{comment}") unless ::File.exist?(text_file_path)
|
158
|
+
EasyIO.logger.info comment.gsub('here', 'in the notepad window')
|
159
|
+
`notepad #{text_file_path}`
|
160
|
+
notepad_content = ::File.read(text_file_path)
|
161
|
+
notepad_content.gsub(/;[^\r\n]*(\r\n|\r|\n)/i, '') # remove comments in text file
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module EasyIO
|
2
|
+
module Terminal
|
3
|
+
module_function
|
4
|
+
|
5
|
+
# Forces real-time output
|
6
|
+
def sync_output
|
7
|
+
$stdout.sync = true
|
8
|
+
$stderr.sync = true
|
9
|
+
end
|
10
|
+
|
11
|
+
# returns [rows, columns]
|
12
|
+
def dimensions
|
13
|
+
require 'io/console'
|
14
|
+
IO.console.winsize
|
15
|
+
rescue LoadError
|
16
|
+
# This works with older Ruby, but only with systems
|
17
|
+
# that have a tput(1) command, such as Unix clones.
|
18
|
+
[Integer(`tput li`), Integer(`tput co`)]
|
19
|
+
end
|
20
|
+
|
21
|
+
def rows
|
22
|
+
dimensions.first
|
23
|
+
end
|
24
|
+
|
25
|
+
def columns
|
26
|
+
dimensions.last
|
27
|
+
end
|
28
|
+
|
29
|
+
def line(filler_character)
|
30
|
+
filler_character * (Terminal.columns - 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
def interactive?
|
34
|
+
$stdout.isatty
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Munoz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-05-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: easy_format
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: easy_json_config
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: logger
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: open3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- amunoz951@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE
|
77
|
+
- lib/easy_io.rb
|
78
|
+
- lib/easy_io/config.rb
|
79
|
+
- lib/easy_io/disk.rb
|
80
|
+
- lib/easy_io/logger.rb
|
81
|
+
- lib/easy_io/registry.rb
|
82
|
+
- lib/easy_io/run.rb
|
83
|
+
- lib/easy_io/terminal.rb
|
84
|
+
homepage: https://github.com/amunoz951/easy_io
|
85
|
+
licenses:
|
86
|
+
- Apache-2.0
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '2.3'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.5.2.3
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Ruby library for ease of running commands with realtime output and return
|
108
|
+
results, logging, retrieving disk space info, emailing, and more.
|
109
|
+
test_files: []
|