berkeley_library-docker 0.0.1 → 0.1.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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d133859ef409d32d22e2b40723ca7fcfd34a551fb5176f7c3cc039cbe4f72037
|
4
|
+
data.tar.gz: be63d139a376a470995b4bd60a6e9ebf4619d4f3f8be9317ae6541acad8e999c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a3d87d5d45aeed03e0a925151a747fd76fa5063f8f874536caec060668fe2707243148f31d51462a6afaa986732f8384b4b0c7f90cdd64a384f90098fb02d6f
|
7
|
+
data.tar.gz: 14a858e4d44f9cf038664f950c4aa706fdb0e0645e5b48b49f858b378bcc6e54ee5a3f5e88e8f372598718023c94520175bac9815387d765a21f9d819675509c
|
@@ -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
|
10
|
+
VERSION = '0.1.0'.freeze
|
11
11
|
HOMEPAGE = 'https://github.com/BerkeleyLibrary/docker'.freeze
|
12
12
|
|
13
13
|
private_class_method :new
|
@@ -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
|
@@ -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
|
4
|
+
version: 0.1.0
|
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
|