longshoreman 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b1314644d0e203677e27d0596b13e43fc72cb8f
4
+ data.tar.gz: d01724ed6a95bbc0c7e98da936a69c5465e2b2e4
5
+ SHA512:
6
+ metadata.gz: 61643f53551a9245566324a1643bb7b24ac608cf261b99deb66d788d46cb58bf45d64d57ff75ddfcaaf8a2a30a8c9019310ae1c13ad2e3c4752bbb61bf01c906
7
+ data.tar.gz: 0f3550aebfe7261919c4d89300d7b177187f56013fbee3107f1f93efd0ab5a39d9dca551a03dec8e432c2728b6f7315bd30b553df91eed1cde1725cc8f85875e
@@ -0,0 +1,44 @@
1
+ require 'docker'
2
+
3
+ class Longshoreman
4
+
5
+ def initialize(image = nil, name = nil, extra_args = {})
6
+ Docker.options = { :write_timeout => 300, :read_timeout => 300 }
7
+ Docker.validate_version!
8
+
9
+ @ip = get_host_ip
10
+ @container = Longshoreman::Container.new(image, name, extra_args)
11
+ @image = Longshoreman::Image.new
12
+ end
13
+
14
+ # Figure out which IP address the Docker host is at
15
+ def get_host_ip
16
+ # Let the crazy one-liner definition begin:
17
+ # Docker.url.split(':')[1][2..-1]
18
+ # Docker.url = tcp://192.168.123.205:2375
19
+ # split(':') = ["tcp", "//192.168.123.205", "2375"]
20
+ # [1] = "//192.168.123.205"
21
+ # [2..-1] = "192.168.123.205"
22
+ # This last bit prunes the leading //
23
+ url = Docker.url
24
+ case url.split(':')[0]
25
+ when 'unix'
26
+ "127.0.0.1"
27
+ when 'tcp'
28
+ url.split(':')[1][2..-1]
29
+ end
30
+ end
31
+
32
+ def cleanup
33
+ @container.cleanup
34
+ @image.cleanup
35
+ end
36
+
37
+ attr_reader :ip
38
+ attr_accessor :container
39
+ attr_accessor :image
40
+
41
+ end
42
+
43
+ require 'longshoreman/image'
44
+ require 'longshoreman/container'
@@ -0,0 +1,88 @@
1
+ class Longshoreman::Container
2
+
3
+ def initialize(image = nil, name = nil, extra_args = {})
4
+ if image && name
5
+ create(image, name, extra_args)
6
+ start
7
+ else
8
+ @raw = nil
9
+ end
10
+ end
11
+
12
+ def all(opts = {})
13
+ @raw.all(opts)
14
+ end
15
+
16
+ # Wrapper to clean up container in a single call
17
+ # name can be a name or id
18
+ def cleanup
19
+ if @raw
20
+ @raw.stop
21
+ @raw.delete(:force => true)
22
+ end
23
+ end
24
+
25
+ # Create a docker container from an image (or repository:tag string), name,
26
+ # and optional extra args.
27
+ def create(image, name, extra_args={})
28
+ # If this ends up getting integrated, we'll use @logger.error here
29
+ case image.class.to_s
30
+ when "Docker::Image"
31
+ i = image.id
32
+ when "String"
33
+ if image.include? ':' # repository:tag format
34
+ i = image
35
+ else
36
+ puts "Image string must be in 'repository:tag' format."
37
+ return
38
+ end
39
+ else
40
+ puts "image must be Docker::Image or in 'repository:tag' format"
41
+ return
42
+ end
43
+
44
+ # Don't change the non-capitalized 'name' here. The Docker API gem extracts
45
+ # this key and uses it to name the container on create.
46
+ main_args = {
47
+ 'name' => name,
48
+ 'Hostname' => name,
49
+ 'Image' => i,
50
+ 'PublishAllPorts' => true,
51
+ 'CapAdd' => 'NET_ADMIN',
52
+ }
53
+ @raw = Docker::Container.create(main_args.merge(extra_args))
54
+ end
55
+
56
+ # This get method is not a "pass thru"
57
+ def get(id, opts = {})
58
+ @raw = Docker::Container.get(id, opts)
59
+ end
60
+
61
+ # Return the randomized port number associated with the exposed port.
62
+ def rport(exposed_port, protocol="tcp")
63
+ # Get all mapped ports
64
+ ports = @raw.json["NetworkSettings"]["Ports"]
65
+ # We're going to expect 1:1 mapping here
66
+ ports["#{exposed_port.to_s}/#{protocol}"][0]["HostPort"].to_i
67
+ end
68
+
69
+ def id
70
+ @raw.id
71
+ end
72
+
73
+ def remove
74
+ @raw.remove(:force => true)
75
+ end
76
+ alias_method :delete, :remove
77
+
78
+ def start
79
+ @raw.start
80
+ end
81
+
82
+ def stop
83
+ @raw.stop
84
+ end
85
+
86
+ attr_accessor :raw
87
+
88
+ end
@@ -0,0 +1,43 @@
1
+ class Longshoreman::Image
2
+
3
+ def initialize
4
+ @raw = nil
5
+ end
6
+
7
+ # Build an image from a Dockerfile
8
+ def build(dockerfile_path)
9
+ dockerfile = IO.read(dockerfile_path)
10
+ @raw = Docker::Image.build(dockerfile)
11
+ @raw # Return the image without having to add .raw
12
+ end
13
+
14
+ # This is useful if you're building from Dockerfiles each run
15
+ def remove
16
+ if @raw
17
+ @raw.remove(:force => true) if @raw.exist?(@raw.id)
18
+ end
19
+ end
20
+ alias_method :cleanup, :remove
21
+
22
+ def exist?(id, opts = {})
23
+ @raw.exist?(id, opts)
24
+ end
25
+
26
+ # Return the image object identified by repository:tag
27
+ def get(repository, tag)
28
+ images = Docker::Image.all.select { |i|
29
+ i.info["RepoTags"].include? "#{repository}:#{tag}"
30
+ }
31
+ # I don't think it's possible to have multiple images from the same
32
+ # repository with the same tag, so this shouldn't be an issue.
33
+ @raw = images[0]
34
+ end
35
+
36
+ # Return the id
37
+ def id
38
+ @raw.id
39
+ end
40
+
41
+ attr_accessor :raw
42
+
43
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: longshoreman
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Aaron Mildenstein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ~>
17
+ - !ruby/object:Gem::Version
18
+ version: '1.0'
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.21.4
22
+ name: docker-api
23
+ prerelease: false
24
+ type: :runtime
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 1.21.4
33
+ description: This gem is intended to aid in using Docker images and containers, specifically with regards to integration testing in RSpec.
34
+ email: aaron@mildensteins.com
35
+ executables: []
36
+ extensions: []
37
+ extra_rdoc_files: []
38
+ files:
39
+ - lib/longshoreman.rb
40
+ - lib/longshoreman/container.rb
41
+ - lib/longshoreman/image.rb
42
+ homepage: http://github.com/untergeek/longshoreman
43
+ licenses:
44
+ - Apache License (2.0)
45
+ metadata: {}
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 2.4.5
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: A helper Gem for using the Docker API
66
+ test_files: []