berkeley_library-docker 0.0.1 → 0.1.1

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
  SHA256:
3
- metadata.gz: 1a8f366b628cad8dd6d516fe9180667fb4bd2908d331ad79437990ee4e0d865b
4
- data.tar.gz: 70dd01ff23d3177c3430597abd7cbdee9657829599791f04299052b070efdee2
3
+ metadata.gz: 75df4187f362680f782f8ce0e4d7a5aa438c4f86f44d1337c0495253e1b4733a
4
+ data.tar.gz: 51fdf2f4e9f29080bda4d7fe93096c0d314def1649f6a7da70290324ca7f642a
5
5
  SHA512:
6
- metadata.gz: 986553998d692b12fdf140ee55f08b75b7c4754ec829049242283b81894dd6ff3205a47830d9d4d3a7a1178198b3a18d255374a21eb0614943268bbebdf93406
7
- data.tar.gz: 792ac78d25c7c14f7384ba968a0578a373fdc66d0e2e9765e4221db04c3b8665c1030af8edfc0835009e518bb0bdbd186d968a61759c8ad6ddc76ba5658f0caa
6
+ metadata.gz: 24714d86ebb5416d303acca6d31f46e1d3e4c7514afbf2fcee166638c7ba25c64cbc5e1d4fdbc49e97babc5568198d32c09909ab1bc43c0652c56a3140a9ff98
7
+ data.tar.gz: 31b41f95fe42a25a725c9d8702d00e4a922c41afb8df1b9503ed3efab626a206511ffbe69e925c8d26a0c6779afbb977c4427c4c81ced59e4edb1273c9baced2
@@ -7,7 +7,7 @@ module BerkeleyLibrary
7
7
  SUMMARY = 'Utility functions for Dockerizing Ruby apps'.freeze
8
8
  DESCRIPTION = 'Utility functions for making Ruby apps "just work" in Docker containers.'.freeze
9
9
  LICENSE = 'MIT'.freeze
10
- VERSION = '0.0.1'.freeze
10
+ VERSION = '0.1.1'.freeze
11
11
  HOMEPAGE = 'https://github.com/BerkeleyLibrary/docker'.freeze
12
12
 
13
13
  private_class_method :new
@@ -3,7 +3,9 @@ require 'berkeley_library/docker/secret'
3
3
  module BerkeleyLibrary
4
4
  module Docker
5
5
  class Railtie < Rails::Railtie
6
- config.before_configuration { Secret.load_secrets! }
6
+ NAME = 'berkeley_library-docker.load_secrets'.freeze
7
+
8
+ initializer(NAME, after: :initialize_logger) { Secret.load_secrets! }
7
9
  end
8
10
  end
9
11
  end
@@ -5,6 +5,20 @@ require 'berkeley_library/docker/railtie' if defined?(Rails::Railtie)
5
5
 
6
6
  module BerkeleyLibrary
7
7
  module Docker
8
- #
8
+ class << self
9
+ def running_in_container?
10
+ File.exist?('/.dockerenv') || init_cgroup_is_dockerish?
11
+ end
12
+
13
+ private
14
+
15
+ def init_cgroup_is_dockerish?
16
+ begin
17
+ File.open('/proc/1/cgroup').read.match?(%r{(/docker|/lxc)})
18
+ rescue
19
+ false
20
+ end
21
+ end
22
+ end
9
23
  end
10
24
  end
@@ -5,18 +5,33 @@ require 'spec_helper'
5
5
  module BerkeleyLibrary
6
6
  module Docker
7
7
  describe Railtie do
8
- before do
9
- expect(ENV['API_TOKEN']).to be nil
8
+ class TestApp < Rails::Application; end
9
+
10
+ let(:app) { TestApp.create }
11
+ let(:initializers) { app.initializers.tsort_each.collect(&:name) }
12
+ let(:railtie_name) { BerkeleyLibrary::Docker::Railtie::NAME }
13
+
14
+ it 'has the expected initializer name' do
15
+ expect(railtie_name).to eq 'berkeley_library-docker.load_secrets'
10
16
  end
11
17
 
12
18
  it 'causes rails to load the environment' do
13
19
  with_secret('API_TOKEN', 'd33db55f') do
14
- class TestApp < Rails::Application; end
15
- TestApp.create.initialize!
20
+ app = TestApp.create
21
+ expect(ENV['API_TOKEN']).to be nil
16
22
 
23
+ app.initialize!
17
24
  expect(ENV['API_TOKEN']).to eq 'd33db55f'
18
25
  end
19
26
  end
27
+
28
+ it 'loads after the logger and before configuration' do
29
+ railtie_index = initializers.find_index(railtie_name)
30
+ logger_index = initializers.find_index(:initialize_logger)
31
+ config_index = initializers.find_index(:load_config_initializers)
32
+
33
+ expect(railtie_index).to be_between(logger_index, config_index)
34
+ end
20
35
  end
21
36
  end
22
37
  end
@@ -0,0 +1,77 @@
1
+ require 'rails' # require Rails first to mimic load order
2
+ require 'berkeley_library/docker'
3
+ require 'spec_helper'
4
+
5
+ module BerkeleyLibrary
6
+ describe Docker do
7
+ describe '#running_in_container?' do
8
+ DOCKERISH_CGROUP = <<~EOL
9
+ 11:cpuacct,cpu:/docker/12345
10
+ 10:devices:/docker/12345
11
+ 9:hugetlb:/docker/12345
12
+ 8:freezer:/docker/12345
13
+ 7:blkio:/docker/12345
14
+ 6:net_prio,net_cls:/docker/12345
15
+ 5:memory:/docker/12345
16
+ 4:pids:/docker/12345
17
+ 3:perf_event:/docker/12345
18
+ 2:cpuset:/docker/12345
19
+ 1:name=systemd:/docker/12345
20
+ EOL
21
+
22
+ TRADITIONAL_CGROUP = <<~EOL
23
+ 11:cpuacct,cpu:/
24
+ 10:devices:/
25
+ 9:hugetlb:/
26
+ 8:freezer:/
27
+ 7:blkio:/
28
+ 6:net_prio,net_cls:/
29
+ 5:memory:/
30
+ 4:pids:/
31
+ 3:perf_event:/
32
+ 2:cpuset:/
33
+ 1:name=systemd:/
34
+ EOL
35
+
36
+ it 'is true when /.dockerenv exists' do
37
+ mock_dockerenv
38
+ expect(BerkeleyLibrary::Docker.running_in_container?).to be true
39
+ end
40
+
41
+ it 'is true when /proc/1/cgroup is docker-like' do
42
+ mock_dockerenv(false)
43
+ mock_init_cgroup
44
+ expect(BerkeleyLibrary::Docker.running_in_container?).to be true
45
+ end
46
+
47
+ it 'is false when /proc/1/cgroup is traditional' do
48
+ mock_dockerenv(false)
49
+ mock_init_cgroup(false)
50
+ expect(BerkeleyLibrary::Docker.running_in_container?).to be false
51
+ end
52
+
53
+ it 'is false when /proc/1/cgroup does not exist' do
54
+ mock_dockerenv(false)
55
+ expect(File)
56
+ .to receive(:open).with('/proc/1/cgroup')
57
+ .and_raise(Errno::ENOENT)
58
+ expect(BerkeleyLibrary::Docker.running_in_container?).to be false
59
+ end
60
+
61
+ private
62
+
63
+ def mock_dockerenv(exists = true)
64
+ expect(File)
65
+ .to receive(:exist?).with('/.dockerenv')
66
+ .and_return(exists)
67
+ end
68
+
69
+ def mock_init_cgroup(dockerish = true)
70
+ expect(File)
71
+ .to receive(:open).with('/proc/1/cgroup')
72
+ .and_return(
73
+ StringIO.new(dockerish ? DOCKERISH_CGROUP : TRADITIONAL_CGROUP))
74
+ end
75
+ end
76
+ end
77
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: berkeley_library-docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Schmidt
@@ -101,6 +101,7 @@ files:
101
101
  - lib/berkeley_library/docker/secret.rb
102
102
  - spec/berkeley_library/docker/railtie_spec.rb
103
103
  - spec/berkeley_library/docker/secret_spec.rb
104
+ - spec/berkeley_library/docker_spec.rb
104
105
  - spec/spec_helper.rb
105
106
  - spec/spec_utils.rb
106
107
  homepage: https://github.com/BerkeleyLibrary/docker
@@ -130,5 +131,6 @@ summary: Utility functions for Dockerizing Ruby apps
130
131
  test_files:
131
132
  - spec/berkeley_library/docker/railtie_spec.rb
132
133
  - spec/berkeley_library/docker/secret_spec.rb
134
+ - spec/berkeley_library/docker_spec.rb
133
135
  - spec/spec_helper.rb
134
136
  - spec/spec_utils.rb