dtk-common 0.10.1 → 0.10.3
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.
- checksums.yaml +4 -4
- data/dtk-common.gemspec +0 -1
- data/lib/dtk-common/version.rb +1 -1
- metadata +15 -31
- data/lib/docker/commander.rb +0 -98
- data/lib/utils/open3.rb +0 -103
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4240d1834319c588e8bae9375fc59825457c5611
|
|
4
|
+
data.tar.gz: db63e6764fc469d4f911dbd9a746720c33aff142
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: baf009fac4b24d1b1290b2cbdda04d7eccc2460905f419453e844cef3ce5b69b5bea6e3c9420110b019baa2e5b4c0d69138bf4cd3382f06beeeb6b445164715b
|
|
7
|
+
data.tar.gz: d4a9e3c887a8c9ba708d31ef25b90b3118f89483e010701059cff3a49b22dc28d8c7a14207fe82ae8c2ad928676465ed133bdf43799ae953f67b561d78ac4577
|
data/dtk-common.gemspec
CHANGED
|
@@ -18,7 +18,6 @@ Gem::Specification.new do |gem|
|
|
|
18
18
|
gem.add_dependency 'rugged','~> 0.17.0.b7'
|
|
19
19
|
gem.add_dependency 'dtk-common-core','0.10.1'
|
|
20
20
|
gem.add_dependency 'colorize','~> 0.5.8'
|
|
21
|
-
gem.add_dependency 'docker-api', '~> 1.26'
|
|
22
21
|
gem.add_dependency 'grit', '~> 2.5'
|
|
23
22
|
gem.add_dependency 'erubis', '~> 2.7'
|
|
24
23
|
# gem.add_dependency 'sequel','~> 3.40.0'
|
data/lib/dtk-common/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: dtk-common
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.10.
|
|
4
|
+
version: 0.10.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rich PELAVIN
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rugged
|
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
|
16
16
|
requirements:
|
|
17
|
-
- - ~>
|
|
17
|
+
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
19
|
version: 0.17.0.b7
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
|
-
- - ~>
|
|
24
|
+
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: 0.17.0.b7
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
@@ -42,56 +42,42 @@ dependencies:
|
|
|
42
42
|
name: colorize
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
44
44
|
requirements:
|
|
45
|
-
- - ~>
|
|
45
|
+
- - "~>"
|
|
46
46
|
- !ruby/object:Gem::Version
|
|
47
47
|
version: 0.5.8
|
|
48
48
|
type: :runtime
|
|
49
49
|
prerelease: false
|
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
51
|
requirements:
|
|
52
|
-
- - ~>
|
|
52
|
+
- - "~>"
|
|
53
53
|
- !ruby/object:Gem::Version
|
|
54
54
|
version: 0.5.8
|
|
55
|
-
- !ruby/object:Gem::Dependency
|
|
56
|
-
name: docker-api
|
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
|
58
|
-
requirements:
|
|
59
|
-
- - ~>
|
|
60
|
-
- !ruby/object:Gem::Version
|
|
61
|
-
version: '1.26'
|
|
62
|
-
type: :runtime
|
|
63
|
-
prerelease: false
|
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
-
requirements:
|
|
66
|
-
- - ~>
|
|
67
|
-
- !ruby/object:Gem::Version
|
|
68
|
-
version: '1.26'
|
|
69
55
|
- !ruby/object:Gem::Dependency
|
|
70
56
|
name: grit
|
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
|
72
58
|
requirements:
|
|
73
|
-
- - ~>
|
|
59
|
+
- - "~>"
|
|
74
60
|
- !ruby/object:Gem::Version
|
|
75
61
|
version: '2.5'
|
|
76
62
|
type: :runtime
|
|
77
63
|
prerelease: false
|
|
78
64
|
version_requirements: !ruby/object:Gem::Requirement
|
|
79
65
|
requirements:
|
|
80
|
-
- - ~>
|
|
66
|
+
- - "~>"
|
|
81
67
|
- !ruby/object:Gem::Version
|
|
82
68
|
version: '2.5'
|
|
83
69
|
- !ruby/object:Gem::Dependency
|
|
84
70
|
name: erubis
|
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
|
86
72
|
requirements:
|
|
87
|
-
- - ~>
|
|
73
|
+
- - "~>"
|
|
88
74
|
- !ruby/object:Gem::Version
|
|
89
75
|
version: '2.7'
|
|
90
76
|
type: :runtime
|
|
91
77
|
prerelease: false
|
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
|
93
79
|
requirements:
|
|
94
|
-
- - ~>
|
|
80
|
+
- - "~>"
|
|
95
81
|
- !ruby/object:Gem::Version
|
|
96
82
|
version: '2.7'
|
|
97
83
|
description: Dtk common is needed to use dtk-client gem, provides common libraries
|
|
@@ -102,8 +88,8 @@ executables: []
|
|
|
102
88
|
extensions: []
|
|
103
89
|
extra_rdoc_files: []
|
|
104
90
|
files:
|
|
105
|
-
- .gitignore
|
|
106
|
-
- .license_header
|
|
91
|
+
- ".gitignore"
|
|
92
|
+
- ".license_header"
|
|
107
93
|
- Gemfile
|
|
108
94
|
- Gemfile.lock
|
|
109
95
|
- LICENSE
|
|
@@ -111,7 +97,6 @@ files:
|
|
|
111
97
|
- Rakefile
|
|
112
98
|
- dtk-common.gemspec
|
|
113
99
|
- lib/common_patch.rb
|
|
114
|
-
- lib/docker/commander.rb
|
|
115
100
|
- lib/dsl.rb
|
|
116
101
|
- lib/dsl/directory_parser.rb
|
|
117
102
|
- lib/dsl/directory_parser/git.rb
|
|
@@ -145,7 +130,6 @@ files:
|
|
|
145
130
|
- lib/module_parser/module_parser.rb
|
|
146
131
|
- lib/module_version.rb
|
|
147
132
|
- lib/require_first.rb
|
|
148
|
-
- lib/utils/open3.rb
|
|
149
133
|
- rich_tests/README.md
|
|
150
134
|
- rich_tests/dsl_test1.rb
|
|
151
135
|
- rich_tests/dsl_test2.rb
|
|
@@ -234,17 +218,17 @@ require_paths:
|
|
|
234
218
|
- lib
|
|
235
219
|
required_ruby_version: !ruby/object:Gem::Requirement
|
|
236
220
|
requirements:
|
|
237
|
-
- -
|
|
221
|
+
- - ">="
|
|
238
222
|
- !ruby/object:Gem::Version
|
|
239
223
|
version: '0'
|
|
240
224
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
241
225
|
requirements:
|
|
242
|
-
- -
|
|
226
|
+
- - ">="
|
|
243
227
|
- !ruby/object:Gem::Version
|
|
244
228
|
version: '0'
|
|
245
229
|
requirements: []
|
|
246
230
|
rubyforge_project:
|
|
247
|
-
rubygems_version: 2.
|
|
231
|
+
rubygems_version: 2.4.6
|
|
248
232
|
signing_key:
|
|
249
233
|
specification_version: 4
|
|
250
234
|
summary: Common libraries used for DTK CLI client.
|
data/lib/docker/commander.rb
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
require 'docker'
|
|
2
|
-
require File.expand_path('../utils/open3.rb', File.dirname(__FILE__))
|
|
3
|
-
|
|
4
|
-
module DTK
|
|
5
|
-
module Docker
|
|
6
|
-
class Commander
|
|
7
|
-
|
|
8
|
-
include Common::Open3
|
|
9
|
-
|
|
10
|
-
def initialize(docker_image, docker_command, puppet_manifest, execution_type, dockerfile, workdir = '/usr/share/dtk/docker-worker')
|
|
11
|
-
@docker_image = docker_image
|
|
12
|
-
@docker_command = docker_command
|
|
13
|
-
@dockerfile = dockerfile
|
|
14
|
-
@puppet_manifest = puppet_manifest
|
|
15
|
-
@execution_type = execution_type
|
|
16
|
-
@workdir = workdir
|
|
17
|
-
|
|
18
|
-
unless ::Docker::Image.exist?(docker_image) && !@dockerfile
|
|
19
|
-
Log.info "Getting docker image '#{docker_image}', this may take a while"
|
|
20
|
-
::Docker::Image.create('fromImage' => docker_image)
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
# build required docker image if requested
|
|
24
|
-
if @dockerfile
|
|
25
|
-
Log.info "Building docker image: #{docker_image}"
|
|
26
|
-
image = ::Docker::Image.build(@dockerfile)
|
|
27
|
-
image.tag('repo' => docker_image, 'force' => true)
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def run
|
|
32
|
-
# require 'debugger'; debugger
|
|
33
|
-
docker_container_name = "dtk#{Time.now.to_i}"
|
|
34
|
-
output_dir = "#{@workdir}/#{docker_container_name}"
|
|
35
|
-
output_dir_tmp = "#{output_dir}/tmp"
|
|
36
|
-
output_dir_container = "/host_volume"
|
|
37
|
-
output_file = "#{output_dir}/report.yml"
|
|
38
|
-
puppet_modules_dir = '/usr/share/dtk/puppet-modules'
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
FileUtils.mkdir_p output_dir_tmp
|
|
42
|
-
# make sure dtkyaml reporter is available to puppet
|
|
43
|
-
FileUtils.ln_sf '/usr/share/dtk/dtk-arbiter/dtkyaml', '/etc/puppet/modules/dtkyaml' unless File.exist? '/etc/puppet/modules/dtkyaml'
|
|
44
|
-
# write puppet manifest
|
|
45
|
-
File.open("#{output_dir}/manifest.pp", 'w') { |file| file.write(@puppet_manifest) }
|
|
46
|
-
# make sure r8 module is available
|
|
47
|
-
FileUtils.cp_r "/etc/puppet/modules/r8", puppet_modules_dir unless File.exist? "#{puppet_modules_dir}/r8"
|
|
48
|
-
|
|
49
|
-
docker_cli_cmd = "docker run --name #{docker_container_name} -v #{output_dir}:#{output_dir_container} -v #{output_dir}/tmp:/tmp" +
|
|
50
|
-
((@execution_type.eql? 'puppet') ? " -v #{puppet_modules_dir}:/etc/puppet/modules" : "") + " #{@docker_image} #{@docker_command}"
|
|
51
|
-
|
|
52
|
-
Log.info "Starting Docker container..."
|
|
53
|
-
|
|
54
|
-
docker_run_stdout, docker_run_stderr, exit_status, results = capture3_with_timeout(docker_cli_cmd)
|
|
55
|
-
|
|
56
|
-
if @execution_type.eql? 'puppet'
|
|
57
|
-
begin
|
|
58
|
-
docker_puppet_report = Hash.new
|
|
59
|
-
docker_puppet_report = YAML.load_file(output_file) if File.exist? output_file
|
|
60
|
-
rescue Exception => e
|
|
61
|
-
Log.error("Docker (puppet) report error: #{e.message}", e.backtrace)
|
|
62
|
-
end
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
container = ::Docker::Container.get(docker_container_name)
|
|
66
|
-
|
|
67
|
-
@results = Hash.new
|
|
68
|
-
|
|
69
|
-
@results[:puppet_report] = docker_puppet_report ||= ''
|
|
70
|
-
@results[:stdout] = docker_run_stdout
|
|
71
|
-
@results[:stderr] = docker_run_stderr
|
|
72
|
-
@results[:status] = exit_status.exitstatus
|
|
73
|
-
@results[:dynamic_attributes] = read_dynamic_attributes(output_dir_tmp)
|
|
74
|
-
|
|
75
|
-
# cleanup
|
|
76
|
-
Log.info("Deleting container and doing cleanup")
|
|
77
|
-
container.delete(:force => true)
|
|
78
|
-
|
|
79
|
-
# http://unix.stackexchange.com/a/117848
|
|
80
|
-
system("find #{output_dir} -depth -type f -exec shred -n 1 -z -u {} \\;")
|
|
81
|
-
FileUtils.rm_rf(output_dir)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def results
|
|
85
|
-
@results
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
private
|
|
89
|
-
|
|
90
|
-
def read_dynamic_attributes(path, identifier = 'dtk_exported_variables')
|
|
91
|
-
full_path = File.join(path, identifier)
|
|
92
|
-
return nil unless File.exists?(full_path)
|
|
93
|
-
Marshal.load(File.read(full_path))
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
end
|
|
97
|
-
end
|
|
98
|
-
end
|
data/lib/utils/open3.rb
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
require 'timeout'
|
|
2
|
-
require 'open3'
|
|
3
|
-
|
|
4
|
-
module DTK
|
|
5
|
-
module Common
|
|
6
|
-
module Open3
|
|
7
|
-
|
|
8
|
-
STREAM_TIMEOUT = 5
|
|
9
|
-
|
|
10
|
-
# Running puppet directly on system and from ruby process has proven different. Following is set of environment variables
|
|
11
|
-
# that assures proper execution from bundler / rvm ruby
|
|
12
|
-
AGNOSTIC_PUPPET_VARS = {
|
|
13
|
-
"BUNDLE_GEMFILE" => nil,
|
|
14
|
-
"BUNDLE_BIN_PATH" => nil,
|
|
15
|
-
"RUBYOPT" => nil,
|
|
16
|
-
"rvm_" => nil,
|
|
17
|
-
"RACK_ENV" => nil,
|
|
18
|
-
"RAILS_ENV" => nil
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
##
|
|
22
|
-
# Open3 method extended with timeout, more info https://gist.github.com/pasela/9392115
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
def capture3_with_timeout(*cmd)
|
|
26
|
-
spawn_opts = Hash === cmd.last ? cmd.pop.dup : {}
|
|
27
|
-
opts = {
|
|
28
|
-
:stdin_data => "",
|
|
29
|
-
:timeout => @timeout || 0,
|
|
30
|
-
:signal => :TERM,
|
|
31
|
-
:kill_after => nil,
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
in_r, in_w = IO.pipe
|
|
35
|
-
out_r, out_w = IO.pipe
|
|
36
|
-
err_r, err_w = IO.pipe
|
|
37
|
-
in_w.sync = true
|
|
38
|
-
|
|
39
|
-
spawn_opts[:in] = in_r
|
|
40
|
-
spawn_opts[:out] = out_w
|
|
41
|
-
spawn_opts[:err] = err_w
|
|
42
|
-
|
|
43
|
-
result = {
|
|
44
|
-
:pid => nil,
|
|
45
|
-
:status => nil,
|
|
46
|
-
:stdout => nil,
|
|
47
|
-
:stderr => nil,
|
|
48
|
-
:timeout => false,
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
out_reader = nil
|
|
52
|
-
err_reader = nil
|
|
53
|
-
wait_thr = nil
|
|
54
|
-
|
|
55
|
-
begin
|
|
56
|
-
Timeout.timeout(opts[:timeout]) do
|
|
57
|
-
result[:pid] = spawn(AGNOSTIC_PUPPET_VARS, *cmd, spawn_opts)
|
|
58
|
-
wait_thr = Process.detach(result[:pid])
|
|
59
|
-
in_r.close
|
|
60
|
-
out_w.close
|
|
61
|
-
err_w.close
|
|
62
|
-
|
|
63
|
-
out_reader = Thread.new { out_r.read }
|
|
64
|
-
err_reader = Thread.new { err_r.read }
|
|
65
|
-
|
|
66
|
-
in_w.close
|
|
67
|
-
|
|
68
|
-
result[:status] = wait_thr.value
|
|
69
|
-
end
|
|
70
|
-
rescue Timeout::Error
|
|
71
|
-
result[:timeout] = true
|
|
72
|
-
pid = result[:pid]
|
|
73
|
-
Process.kill(opts[:signal], pid)
|
|
74
|
-
if opts[:kill_after]
|
|
75
|
-
unless wait_thr.join(opts[:kill_after])
|
|
76
|
-
Process.kill(:KILL, pid)
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
ensure
|
|
80
|
-
result[:status] = wait_thr.value if wait_thr
|
|
81
|
-
begin
|
|
82
|
-
# there is a bug where there is infinite leg on out_reader (e.g. hohup) commands
|
|
83
|
-
Timeout.timeout(STREAM_TIMEOUT) do
|
|
84
|
-
result[:stdout] = out_reader.value if out_reader
|
|
85
|
-
result[:stderr] = err_reader.value if err_reader
|
|
86
|
-
end
|
|
87
|
-
rescue Timeout::Error
|
|
88
|
-
result[:stdout] ||= ''
|
|
89
|
-
result[:stderr] ||= ''
|
|
90
|
-
end
|
|
91
|
-
out_r.close unless out_r.closed?
|
|
92
|
-
err_r.close unless err_r.closed?
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
result[:stdout] = result[:stdout].gsub(/\e\[([;\d]+)?m/, '') if result[:stdout]
|
|
96
|
-
result[:stderr] = result[:stderr].gsub(/\e\[([;\d]+)?m/, '') if result[:stderr]
|
|
97
|
-
|
|
98
|
-
[result[:stdout], result[:stderr], result[:status], result]
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
end
|