docker 0.3.0 → 0.3.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
  SHA1:
3
- metadata.gz: 8a5fe14311f0acab32460ca8923bab196447407f
4
- data.tar.gz: 0b41e3f8300a3378bd4523d1b9f1a030229c6555
3
+ metadata.gz: 41a18cd99670f903bc99f7277cb27afdf00025c0
4
+ data.tar.gz: 6d4313cd3012dd54a76eff1b5e6cd67f93ccc829
5
5
  SHA512:
6
- metadata.gz: c1f63091d8bf8a553084f77e009ce5669660407856cc6ce051549d908d94f26cc95f94c4afdced54670e759042d74971ee00e7dddcb417886976f9ed4efd6800
7
- data.tar.gz: c7f76f824a1b2e6e548e94f483edbe6097b478c8960cbb1494f4c1340f9b4849eca9bebad08341bf9864e3e0175764888cb443d6e9b738206cbebb0cc33829dd
6
+ metadata.gz: c3bfbdb9b4f2846910d5e3315b71fef671ead204a15e28871a01a1617c7bdabf3811e0444bb32466f2947b9948ad4de4d82f7bba8deea390ad95cdbb460011f9
7
+ data.tar.gz: caa31e851fa10c4d2b6b913087d1350861266fe0aacbb5b705ba2826ebc2f5e3f3c487bae8e0a9701186657888494ba554231c842d9d88225aebc23386b25a2a
data/README.md CHANGED
@@ -29,7 +29,7 @@ Or install it yourself as:
29
29
 
30
30
  ## Usage
31
31
 
32
- ### Invoking from Ruby code
32
+ Instantiate a `Docker::Session` and call its methods to invoke Docker commands.
33
33
 
34
34
  ```ruby
35
35
  require 'docker'
@@ -40,7 +40,7 @@ docker = Docker.new
40
40
 
41
41
  docker.version
42
42
 
43
- docker.run(t:true, i:true, 'busybox', '/bin/sh')
43
+ docker.run('busybox', '/bin/sh', interactive:true, tty:true)
44
44
  ```
45
45
 
46
46
  ## Development
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["xeger@xeger.net"]
11
11
 
12
12
  spec.summary = %q{Docker CLI wrapper.}
13
- spec.description = %q{Provides an OOP interface to docker without relying on its HTTP API.}
13
+ spec.description = %q{Provides an OOP interface to docker's command-line interface without relying on its HTTP API.}
14
14
  spec.homepage = "https://github.com/xeger/docker"
15
15
  spec.license = "MIT"
16
16
 
@@ -1,5 +1,7 @@
1
1
  require_relative 'docker/version'
2
2
  require_relative 'docker/error'
3
+ require_relative 'docker/asset'
4
+ require_relative 'docker/container'
3
5
  require_relative 'docker/session'
4
6
 
5
7
  module Docker
@@ -0,0 +1,27 @@
1
+ module Docker
2
+ # Base class for containers, images and other Docker assets.
3
+ class Asset
4
+ def initialize(json, session:nil)
5
+ json = JSON.load(json) if json.is_a?(String)
6
+ @json = json
7
+ @session = session
8
+ end
9
+
10
+ def inspect
11
+ %Q{#<#{self.class.name}:#{self.name}>}
12
+ end
13
+
14
+ # @return [String] human-readable name of container, image or volume
15
+ def name
16
+ @json['Name']
17
+ end
18
+
19
+ def to_h
20
+ @json
21
+ end
22
+
23
+ def to_s
24
+ self.inspect
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ module Docker
2
+ # Object that represents a Docker container, its information and operations.
3
+ class Container < Asset
4
+ # @return [String] unique SHA256 container hash
5
+ def id
6
+ @json['Id']
7
+ end
8
+
9
+ # @return [String] human-readable name of container ()minus initial /)
10
+ def name
11
+ super.sub(/^\//, '')
12
+ end
13
+
14
+ # @return [String] SHA256 hash of image the container is derived from
15
+ def image
16
+ @json['Image']
17
+ end
18
+
19
+ # @return [String] running, exited, etc
20
+ def status
21
+ @json['State']['Status']
22
+ end
23
+
24
+ # @return [Integer] master process PID
25
+ def pid
26
+ @json['State']['Pid']
27
+ end
28
+
29
+ # @return [Integer] master process exit code
30
+ def exit_code
31
+ @json['State']['ExitCode']
32
+ end
33
+
34
+ def kill
35
+ raise Error, "Disconnected from session" unless @session
36
+ @session.kill(id)
37
+ end
38
+
39
+ def rm
40
+ raise Error, "Disconnected from session" unless @session
41
+ @session.rm(id)
42
+ end
43
+
44
+ def start
45
+ raise Error, "Disconnected from session" unless @session
46
+ @session.start(id)
47
+ end
48
+
49
+ def stop
50
+ raise Error, "Disconnected from session" unless @session
51
+ @session.stop(id)
52
+ end
53
+ end
54
+ end
@@ -1,4 +1,5 @@
1
1
  require 'backticks'
2
+ require 'json'
2
3
 
3
4
  module Docker
4
5
  # A Ruby OOP interface to a docker session. A session is bound to
@@ -19,19 +20,51 @@ module Docker
19
20
  # @return [#command]
20
21
  attr_reader :shell
21
22
 
22
- def initialize(shell=Backticks::Runner.new(interactive:true), host:ENV['DOCKER_HOST'])
23
+ # Hint that we are able to parse ps output
24
+ PS_HEADER = /ID\s+IMAGE\s+COMMAND\s+CREATED\s+STATUS\s+PORTS\s+NAMES$/
25
+
26
+ def initialize(shell=Backticks::Runner.new, host:ENV['DOCKER_HOST'])
23
27
  @host = host
24
28
  @shell = shell
25
29
  end
26
30
 
31
+ # Get detailed information about a container(s).
32
+ #
33
+ # @return [Container,Array] one container if one was asked about; a list of containers otherwise
34
+ # @param [Array,String] container ID/name or list of IDs/names
35
+ def inspect(container_s)
36
+ containers = container_s
37
+ containers = [containers] unless container_s.is_a?(Array)
38
+ out = run!('inspect', containers)
39
+ result = JSON.parse(out).map { |c| Container.new(c, session:self)}
40
+ if container_s.is_a?(Array)
41
+ result
42
+ else
43
+ result.first
44
+ end
45
+ end
46
+
27
47
  # Kill a running container.
28
48
  #
29
- # @param [String] container id or name of container to kill
49
+ # @param [String] container id or name of container
30
50
  # @param [String] signal Unix signal to send: KILL, TERM, QUIT, HUP, etc
31
51
  def kill(container, signal:nil)
32
52
  run!('kill', {signal:signal}, container)
33
53
  end
34
54
 
55
+ # List containers. This actually does a `ps` followed by a very large
56
+ # `inspect`, so it's expensive but super detailed.
57
+ #
58
+ # @param
59
+ # @return [Array] list of Docker::Container objects
60
+ def ps(all:false, before:nil, latest:false, since:nil)
61
+ out = run!('ps', all:all,before:before,latest:latest,since:since)
62
+ lines = out.split(/[\n\r]+/)
63
+ header = lines.shift
64
+ ids = lines.map { |line| line.split(/\s+/).first }
65
+ inspect(ids)
66
+ end
67
+
35
68
  # Run a command in a new container.
36
69
  #
37
70
  # @example open a busybox shell
@@ -104,6 +137,32 @@ module Docker
104
137
  run!('run', *cmd).strip
105
138
  end
106
139
 
140
+ # Remove a container.
141
+ #
142
+ # @param [String] container id or name of container
143
+ # @param [Boolean] force remove the container even if it's in use
144
+ # @param [Boolean] volumes remove associated data volumes
145
+ def rm(container, force:false, volumes:false)
146
+ run!('rm', {force:force,volumes:volumes},container).strip
147
+ end
148
+
149
+ # Stop a running container.
150
+ #
151
+ # @param [String] container id or name of container
152
+ # @param [Integer] time seconds to wait for stop before killing it
153
+ def stop(container, time:nil)
154
+ run!('stop', {time:time}, container).strip
155
+ end
156
+
157
+ # Start a stopped container.
158
+ #
159
+ # @param [String] container id or name of container
160
+ # @param [Boolean] attach attach STDOUT/STDERR and forward signals
161
+ # @param [Boolean] interactive attach container's STDIN
162
+ def start(container, attach:false, interactive:false)
163
+ run!('start', {attach:attach,interactive:interactive}, container).strip
164
+ end
165
+
107
166
  # Provide version information about the Docker client and server.
108
167
  #
109
168
  # @return [Hash] dictionary of strings describing version/build info
@@ -138,6 +197,7 @@ module Docker
138
197
  # @return [String] output of the command
139
198
  # @raise [RuntimeError] if command fails
140
199
  def run!(*args)
200
+ # STDERR.puts "+ " + (['docker'] + args).inspect
141
201
  cmd = @shell.run('docker', *args).join
142
202
  status, out, err = cmd.status, cmd.captured_output, cmd.captured_error
143
203
  status.success? || raise(Error.new(args.first, status, err))
@@ -1,3 +1,3 @@
1
1
  module Docker
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Spataro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-03 00:00:00.000000000 Z
11
+ date: 2016-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backticks
@@ -66,7 +66,8 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: Provides an OOP interface to docker without relying on its HTTP API.
69
+ description: Provides an OOP interface to docker's command-line interface without
70
+ relying on its HTTP API.
70
71
  email:
71
72
  - xeger@xeger.net
72
73
  executables: []
@@ -86,6 +87,8 @@ files:
86
87
  - bin/setup
87
88
  - docker.gemspec
88
89
  - lib/docker.rb
90
+ - lib/docker/asset.rb
91
+ - lib/docker/container.rb
89
92
  - lib/docker/error.rb
90
93
  - lib/docker/session.rb
91
94
  - lib/docker/version.rb