vx-container_connector 0.2.9 → 0.3.0

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: 6e82339895966cc9dcd2dca92e3c2111eb5ed2c7
4
- data.tar.gz: bbbd8bb14d7bdeebb27fdd190e529ca20bdf8a99
3
+ metadata.gz: c84082fa207a3f63e5f75028ddc74136a95c4629
4
+ data.tar.gz: dc1ce66f51fac9b33871bc76fcdab207315b2a6c
5
5
  SHA512:
6
- metadata.gz: 01723b6a46258b646d24217240a0c1b21eddb90f8e17969b73549e364e0210d7eae93b06c68c2d0c2c8046c9f529f57e975cac1b53a9f6b0c63398c096cb15bc
7
- data.tar.gz: b23d9992e75b45fe30b98fe14eb463d4a185901e76d86e1b225c82c8f29306930af0398cc1747990134199f92fe2880300b64ff354afdd76bd270a03360ad467
6
+ metadata.gz: b760b4adf849aadb3bab434bbd74f686510073c05363df48ad3f09333f32f3691a18cc16b24dcdb8fbe47b2f97752ae6773fea35186b9490eb5a778fd08d742c
7
+ data.tar.gz: cc7278b8aa10f12bdff3faa504b5207ed97fb48fa1a01b1a1c30a279d70012b061814cc4f7cc2bd1f0d48e0084dca96718cd1ec74faa3f85dd6b876a62d5ec2c
@@ -0,0 +1,52 @@
1
+ module Vx
2
+ module ContainerConnector
3
+
4
+ class Docker
5
+
6
+ class Default
7
+
8
+ class << self
9
+
10
+ def ssh_host
11
+ if testing?
12
+ 'localhost'
13
+ else
14
+ nil
15
+ end
16
+ end
17
+
18
+ def ssh_port
19
+ if testing?
20
+ 2122
21
+ else
22
+ nil
23
+ end
24
+ end
25
+
26
+ def create_container_options
27
+ if testing?
28
+ { "ExposedPorts" => { "22/tcp" => {} } }
29
+ else
30
+ {}
31
+ end
32
+ end
33
+
34
+ def start_container_options
35
+ if testing?
36
+ { "PortBindings" => { "22/tcp" => [{ "HostPort" => "2022" }] } }
37
+ else
38
+ {}
39
+ end
40
+ end
41
+
42
+ def testing?
43
+ ENV['VX_ENV'] == 'test'
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,4 @@
1
1
  require 'docker'
2
- require 'logger'
3
2
  require 'vx/common/spawn'
4
3
  require 'net/ssh'
5
4
 
@@ -9,31 +8,13 @@ module Vx
9
8
  class Docker
10
9
 
11
10
  autoload :Spawner, File.expand_path("../docker/spawner", __FILE__)
11
+ autoload :Default, File.expand_path("../docker/default", __FILE__)
12
12
 
13
13
  include Vx::Common::Spawn
14
14
  include ContainerConnector::Retriable
15
+ include Instrument
15
16
 
16
- attr_reader :user, :password, :init, :image, :remote_dir, :logger
17
-
18
- @@default_container_options = {}
19
- @@default_ssh_port = 22
20
- @@host_to_connect_override = nil
21
-
22
- class << self
23
- def default_container_options
24
- @@default_container_options
25
- end
26
-
27
- def default_ssh_port(val = nil)
28
- @@default_ssh_port = val if val
29
- @@default_ssh_port
30
- end
31
-
32
- def host_to_connect_override(val = nil)
33
- @@host_to_connect_override = val if val
34
- @@host_to_connect_override
35
- end
36
- end
17
+ attr_reader :user, :password, :init, :image, :remote_dir
37
18
 
38
19
  def initialize(options = {})
39
20
  @user = options[:user] || "vexor"
@@ -41,7 +22,6 @@ module Vx
41
22
  @init = options[:init] || %w{ /sbin/init --startup-event dockerboot }
42
23
  @image = options[:image] || "dmexe/precise"
43
24
  @remote_dir = options[:remote_dir] || "/home/#{user}"
44
- @logger = options[:logger] || ::Logger.new(STDOUT)
45
25
  end
46
26
 
47
27
  def start(&block)
@@ -50,51 +30,70 @@ module Vx
50
30
  end
51
31
  end
52
32
 
53
- def container_options
54
- self.class.default_container_options.merge(
55
- 'Cmd' => init,
56
- 'Image' => image,
33
+ def create_container_options
34
+ Default.create_container_options.merge(
35
+ 'Cmd' => init,
36
+ 'Image' => image,
57
37
  )
58
38
  end
59
39
 
40
+ def start_container_options
41
+ Default.start_container_options
42
+ end
43
+
60
44
  private
61
45
 
62
46
  def open_ssh_session(container)
63
- host = self.class.host_to_connect_override || container.json['NetworkSettings']['IPAddress']
47
+ host = Default.ssh_host || container.json['NetworkSettings']['IPAddress']
64
48
 
65
49
  ssh_options = {
66
50
  password: password,
67
- port: self.class.default_ssh_port,
51
+ port: Default.ssh_port,
68
52
  paranoid: false,
69
53
  forward_agent: false
70
54
  }
71
- logger.info "open ssh session to #{user}@#{host}"
55
+
56
+ instrumentation = {
57
+ container_type: "docker",
58
+ container: container.json,
59
+ ssh_host: host
60
+ }
61
+
72
62
  with_retries ::Net::SSH::AuthenticationFailed, limit: 3, sleep: 3 do
63
+ instrument("starting_ssh_session", instrumentation)
73
64
  open_ssh(host, user, ssh_options) do |ssh|
74
- logger.info "ssh session opened"
75
65
  yield Spawner.new(container, ssh, remote_dir)
76
66
  end
77
67
  end
78
68
  end
79
69
 
80
70
  def start_container(&block)
81
- container = ::Docker::Container.create container_options
71
+ container = instrument("create_container", container_type: "docker", container_options: create_container_options) do
72
+ ::Docker::Container.create create_container_options
73
+ end
74
+
75
+ instrumentation = {
76
+ container_type: "docker",
77
+ container: container.json,
78
+ container_options: start_container_options,
79
+ }
82
80
 
83
81
  with_retries ::Docker::Error::NotFoundError, limit: 3, sleep: 3 do
84
- container.start
82
+ instrument("start_container", instrumentation) do
83
+ container.start start_container_options
84
+ end
85
85
  end
86
86
 
87
87
  begin
88
- logger.info "start container #{container.id}"
89
88
  sleep 3
90
89
  yield container
91
90
  ensure
92
- container.kill
93
- logger.info "kill container #{container.id}"
91
+ instrument("kill_container", instrumentation) do
92
+ container.kill
93
+ end
94
94
  end
95
95
  end
96
96
 
97
-
98
97
  end
99
98
  end
100
99
  end
@@ -7,29 +7,32 @@ module Vx
7
7
 
8
8
  class Local
9
9
 
10
+ include Instrument
11
+
10
12
  autoload :Spawner, File.expand_path("../local/spawner", __FILE__)
11
13
 
12
- attr_reader :work_dir, :logger
14
+ attr_reader :work_dir
13
15
 
14
16
  def initialize(options = {})
15
17
  @work_dir = options[:work_dir] || default_work_dir
16
18
  @work_dir = File.expand_path(@work_dir)
17
- @logger = options[:logger] || Logger.new(STDOUT)
18
19
  end
19
20
 
20
21
  def start(&block)
21
- FileUtils.rm_rf(work_dir)
22
- FileUtils.mkdir_p(work_dir)
22
+ instrument( "start_container", container_type: 'local', container: { work_dir: work_dir }) do
23
+ FileUtils.rm_rf(work_dir)
24
+ FileUtils.mkdir_p(work_dir)
25
+ end
23
26
 
24
27
  spawner = Spawner.new(work_dir)
25
- logger.info "inside #{work_dir}"
28
+
26
29
  yield spawner
27
30
  end
28
31
 
29
32
  private
30
33
 
31
34
  def default_work_dir
32
- "#{Dir.tmpdir}/.local_connector"
35
+ "#{Dir.tmpdir}/.vx_local_connector"
33
36
  end
34
37
 
35
38
  end
@@ -0,0 +1,21 @@
1
+ module Vx
2
+ module ContainerConnector
3
+ module Instrument
4
+
5
+ def instrument(name, payload, &block)
6
+ name = "#{name}.container_connector.vx"
7
+
8
+ if ENV['VX_CONTAINER_CONNECTOR_DEBUG']
9
+ $stdout.puts " --> #{name}: #{payload}"
10
+ end
11
+
12
+ if inst = ContainerConnector.instrumenter
13
+ inst.instrument(name, payload, &block)
14
+ else
15
+ yield if block_given?
16
+ end
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -26,7 +26,6 @@ module Vx
26
26
  begin
27
27
  yield
28
28
  rescue *exceptions => e
29
- $stderr.puts "got: #{inspect}, attempt #{retried}"
30
29
  if retried + 1 < options[:limit]
31
30
  retried += 1
32
31
  sleep options[:sleep]
@@ -1,5 +1,5 @@
1
1
  module Vx
2
2
  module ContainerConnector
3
- VERSION = "0.2.9"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -4,13 +4,15 @@ require File.expand_path("../container_connector/errors", __FILE__)
4
4
  module Vx
5
5
  module ContainerConnector
6
6
 
7
- autoload :Local, File.expand_path("../container_connector/local", __FILE__)
8
- autoload :Docker, File.expand_path("../container_connector/docker", __FILE__)
9
- autoload :Retriable, File.expand_path("../container_connector/mixin/retriable", __FILE__)
10
-
7
+ autoload :Local, File.expand_path("../container_connector/local", __FILE__)
8
+ autoload :Docker, File.expand_path("../container_connector/docker", __FILE__)
9
+ autoload :Retriable, File.expand_path("../container_connector/mixin/retriable", __FILE__)
10
+ autoload :Instrument, File.expand_path("../container_connector/mixin/instrument", __FILE__)
11
11
 
12
12
  extend self
13
13
 
14
+ attr_accessor :instrumenter
15
+
14
16
  def lookup(name, options = {})
15
17
  case name.to_sym
16
18
  when :docker
@@ -5,14 +5,6 @@ describe Vx::ContainerConnector::Docker do
5
5
 
6
6
  it { should be }
7
7
 
8
- its(:logger) { should be }
9
-
10
- context "container_options" do
11
- subject { conn.container_options }
12
- it { should eq("Cmd" => ["/sbin/init", "--startup-event", "dockerboot"],
13
- "Image" => "dmexe/precise") }
14
- end
15
-
16
8
  context "user" do
17
9
  subject { conn.user }
18
10
 
@@ -8,7 +8,7 @@ describe Vx::ContainerConnector::Local do
8
8
 
9
9
  context "work_dir" do
10
10
  it "by default should be inside Dir.tmpdir" do
11
- expect(conn.work_dir).to eq("#{Dir.tmpdir}/.local_connector")
11
+ expect(conn.work_dir).to eq("#{Dir.tmpdir}/.vx_local_connector")
12
12
  end
13
13
 
14
14
  it "when passed via options, should be" do
@@ -37,7 +37,7 @@ describe Vx::ContainerConnector::Local do
37
37
  end
38
38
  end
39
39
 
40
- dir = "#{Dir.tmpdir}/.local_connector\n"
40
+ dir = "#{Dir.tmpdir}/.vx_local_connector\n"
41
41
  if RUBY_PLATFORM =~ /darwin/
42
42
  dir.gsub!(/^\/var/, '/private/var')
43
43
  end
data/spec/spec_helper.rb CHANGED
@@ -1,20 +1,12 @@
1
- require File.expand_path '../../lib/vx/container_connector', __FILE__
2
-
3
1
  Bundler.require(:test)
2
+
3
+ ENV['VX_ENV'] = 'test'
4
+ ENV['DOCKER_URL'] = "tcp://localhost:4243"
5
+
6
+ require File.expand_path '../../lib/vx/container_connector', __FILE__
4
7
  require 'rspec/autorun'
5
8
 
6
9
  Dir[File.expand_path("../..", __FILE__) + "/spec/support/**/*.rb"].each {|f| require f}
7
10
 
8
11
  RSpec.configure do |config|
9
- config.mock_with :rr
10
-
11
- config.before(:suite) do
12
- =begin
13
- Vx::ContainerConnector::Docker.default_container_options.merge!(
14
- 'PortSpecs' => ['2022:22']
15
- )
16
- Vx::ContainerConnector::Docker.default_ssh_port 2223
17
- Vx::ContainerConnector::Docker.host_to_connect_override 'localhost'
18
- =end
19
- end
20
12
  end
@@ -24,5 +24,4 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "rspec"
27
- spec.add_development_dependency "rr"
28
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vx-container_connector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-03 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: docker-api
@@ -80,20 +80,6 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rr
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - '>='
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - '>='
95
- - !ruby/object:Gem::Version
96
- version: '0'
97
83
  description: ' description '
98
84
  email:
99
85
  - dima.exe@gmail.com
@@ -109,10 +95,12 @@ files:
109
95
  - Rakefile
110
96
  - lib/vx/container_connector.rb
111
97
  - lib/vx/container_connector/docker.rb
98
+ - lib/vx/container_connector/docker/default.rb
112
99
  - lib/vx/container_connector/docker/spawner.rb
113
100
  - lib/vx/container_connector/errors.rb
114
101
  - lib/vx/container_connector/local.rb
115
102
  - lib/vx/container_connector/local/spawner.rb
103
+ - lib/vx/container_connector/mixin/instrument.rb
116
104
  - lib/vx/container_connector/mixin/retriable.rb
117
105
  - lib/vx/container_connector/version.rb
118
106
  - spec/lib/container_connector_spec.rb