docker 0.0.1 → 0.3.0

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: 8a5fe14311f0acab32460ca8923bab196447407f
4
+ data.tar.gz: 0b41e3f8300a3378bd4523d1b9f1a030229c6555
5
+ SHA512:
6
+ metadata.gz: c1f63091d8bf8a553084f77e009ce5669660407856cc6ce051549d908d94f26cc95f94c4afdced54670e759042d74971ee00e7dddcb417886976f9ed4efd6800
7
+ data.tar.gz: c7f76f824a1b2e6e548e94f483edbe6097b478c8960cbb1494f4c1340f9b4849eca9bebad08341bf9864e3e0175764888cb443d6e9b738206cbebb0cc33829dd
data/.gitignore CHANGED
@@ -1,17 +1,11 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
11
+
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.2.2
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.7
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in docker.gemspec
4
4
  gemspec
5
+
6
+ group :development do
7
+ gem 'pry'
8
+ gem 'pry-byebug'
9
+ end
@@ -1,22 +1,21 @@
1
- Copyright (c) 2013 Vladimir Pouzanov
1
+ The MIT License (MIT)
2
2
 
3
- MIT License
3
+ Copyright (c) 2015 Tony Spataro
4
4
 
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
12
11
 
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
15
14
 
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,12 +1,23 @@
1
1
  # Docker
2
2
 
3
- TODO: Write a gem description
3
+ This is a Ruby OOP wrapper for the [Docker](https://github.com/docker)
4
+ container management tool from Docker Inc. It wraps Docker's command-line
5
+ interface with objects.
6
+
7
+ Throughout this documentation we will refer to this gem as `docker.gem`
8
+ as opposed to the `docker` tool that this gem wraps.
9
+
10
+ ### Why?
11
+
12
+
4
13
 
5
14
  ## Installation
6
15
 
7
16
  Add this line to your application's Gemfile:
8
17
 
9
- gem 'docker'
18
+ ```ruby
19
+ gem 'docker'
20
+ ```
10
21
 
11
22
  And then execute:
12
23
 
@@ -18,12 +29,32 @@ Or install it yourself as:
18
29
 
19
30
  ## Usage
20
31
 
21
- TODO: Write usage instructions here
32
+ ### Invoking from Ruby code
33
+
34
+ ```ruby
35
+ require 'docker'
36
+
37
+ # Create a new session talking to `ENV['DOCKER_HOST']`. For fine-grained
38
+ # control over options, see Docker::Session#new
39
+ docker = Docker.new
40
+
41
+ docker.version
42
+
43
+ docker.run(t:true, i:true, 'busybox', '/bin/sh')
44
+ ```
45
+
46
+ ## Development
47
+
48
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
49
+
50
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
22
51
 
23
52
  ## Contributing
24
53
 
25
- 1. Fork it
26
- 2. Create your feature branch (`git checkout -b my-new-feature`)
27
- 3. Commit your changes (`git commit -am 'Add some feature'`)
28
- 4. Push to the branch (`git push origin my-new-feature`)
29
- 5. Create new Pull Request
54
+ Bug reports and pull requests are welcome on GitHub at https://github.com/xeger/docker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
55
+
56
+
57
+ ## License
58
+
59
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60
+
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'docker'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require 'pry'
11
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -6,18 +6,22 @@ require 'docker/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "docker"
8
8
  spec.version = Docker::VERSION
9
- spec.authors = ["Vladimir Pouzanov"]
10
- spec.email = ["farcaller@gmail.com"]
11
- spec.description = "Docker wrapper to manage docker.io based lxc containers"
12
- spec.summary = "Docker wrapper to manage docker.io based lxc containers"
13
- spec.homepage = ""
9
+ spec.authors = ["Tony Spataro"]
10
+ spec.email = ["xeger@xeger.net"]
11
+
12
+ spec.summary = %q{Docker CLI wrapper.}
13
+ spec.description = %q{Provides an OOP interface to docker without relying on its HTTP API.}
14
+ spec.homepage = "https://github.com/xeger/docker"
14
15
  spec.license = "MIT"
15
16
 
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
20
  spec.require_paths = ["lib"]
20
21
 
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
22
+ spec.add_dependency "backticks", "~> 0.3"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
23
27
  end
@@ -1,5 +1,10 @@
1
- require "docker/version"
1
+ require_relative 'docker/version'
2
+ require_relative 'docker/error'
3
+ require_relative 'docker/session'
2
4
 
3
5
  module Docker
4
- # Your code goes here...
6
+ # Create a new session with default options.
7
+ def self.new
8
+ Session.new
9
+ end
5
10
  end
@@ -0,0 +1,16 @@
1
+ module Docker
2
+ class Error < RuntimeError
3
+ attr_reader :status, :detail
4
+
5
+ # @param [String] cmd
6
+ # @param [Integer] status
7
+ # @param [String] detail
8
+ def initialize(cmd, status, detail)
9
+ @status = status
10
+ @detail = detail
11
+ brief = detail.split("\n").first || '(no output)'
12
+ message = format("'%s' failed with status %d: %s", cmd, status, brief)
13
+ super(message)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,147 @@
1
+ require 'backticks'
2
+
3
+ module Docker
4
+ # A Ruby OOP interface to a docker session. A session is bound to
5
+ # a particular docker host (which is set at initialize time)
6
+ # time) and invokes whichever docker command is resident in $PATH.
7
+ #
8
+ # Run docker commands by calling instance methods of this class and
9
+ # passing positional and kwargs that are equivalent to the CLI options
10
+ # you would pass to the command-line tool.
11
+ #
12
+ # Note that the Ruby command methods usually expose a _subset_ of the options
13
+ # allowed by the docker CLI, and that options are sometimes renamed
14
+ # for clarity. Each command method is extensively documented.
15
+ class Session
16
+ # @return [String] URL of the Docker host associated with this session
17
+ attr_reader :host
18
+
19
+ # @return [#command]
20
+ attr_reader :shell
21
+
22
+ def initialize(shell=Backticks::Runner.new(interactive:true), host:ENV['DOCKER_HOST'])
23
+ @host = host
24
+ @shell = shell
25
+ end
26
+
27
+ # Kill a running container.
28
+ #
29
+ # @param [String] container id or name of container to kill
30
+ # @param [String] signal Unix signal to send: KILL, TERM, QUIT, HUP, etc
31
+ def kill(container, signal:nil)
32
+ run!('kill', {signal:signal}, container)
33
+ end
34
+
35
+ # Run a command in a new container.
36
+ #
37
+ # @example open a busybox shell
38
+ # session.run('busybox', '/bin/sh', tty:true, interactive:true)
39
+ #
40
+ # @param [String] image id or name of base image to use for container
41
+ # @param [Array] command_and_args optional command to run in container
42
+ # @param [Integer] cpu_period scheduler period (μs)
43
+ # @param [Integer] cpu_quota maximum runtime (μs) during one scheduler period
44
+ # @param [Boolean] detach run container in background and return immediately
45
+ # @param [Array,Hash] env environment variables; map of {K:V} pairs or list of ["K=V"] assignments
46
+ # @param [String] env_file name of file to read environment variables from
47
+ # @param [Array] expose list of Integer/String ports or port-ranges to expose to other containers e.g. 80, "1024-2048"
48
+ # @param [String] hostname Unix hostname inside container
49
+ # @param [Boolean] interactive allocate an STDIN for the container
50
+ # @param [Array] link list of container ids or names to link to the container
51
+ # @param [String] memory limit on memory consumption e.g. "640k", "32m" or "4g"
52
+ # @param [String] name name of container; leave blank to let Docker generate one
53
+ # @param [Array] publish list of Integer/String ports or port-ranges to publish to the host; use "X:Y" to map container's X to host's Y
54
+ # @param [Boolean] publish_all automatically publish all of container's ports to the host
55
+ # @param [Boolean] restart automatically restart container when it fails
56
+ # @param [Boolean] rm clean up container once it exits
57
+ # @param [Boolean] tty allocate a pseudo-TTY for the container's STDOUT
58
+ # @param [String] user name or uid of Unix user to run container as
59
+ # @param [Array] volume list of volumes to mount inside container; use "X:Y" to map host's X to container's Y
60
+ # @param [String] volumes_from id or name of container to import all volumes from
61
+ def run(image, *command_and_args,
62
+ add_host:[],
63
+ attach:[],
64
+ cpu_period:nil,
65
+ cpu_quota:nil,
66
+ detach:false,
67
+ env:{},
68
+ env_file:nil,
69
+ expose:[],
70
+ hostname:nil,
71
+ interactive:false,
72
+ link:[],
73
+ memory:nil,
74
+ name:nil,
75
+ publish:[],
76
+ publish_all:false,
77
+ restart:false,
78
+ rm:false,
79
+ tty:false,
80
+ user:nil,
81
+ volume:[],
82
+ volumes_from:nil)
83
+
84
+ cmd = []
85
+
86
+ # if env was provided as a hash, turn it into an array
87
+ env = env.map { |k, v| "#{k}=#{v}" } if env.is_a?(Hash)
88
+
89
+ # our keyword args are formatted properly for run! to handle them; echo
90
+ # them into the command line verbatim.
91
+ # TODO find a way to DRY out this repetitive mess...
92
+ cmd << {add_host:add_host, attach:attach, cpu_period:cpu_period,
93
+ cpu_quota:cpu_quota, detach:detach, env_file:env_file, env:env,
94
+ expose:expose, hostname:hostname, interactive:interactive,
95
+ link:link, memory:memory, name:name, publish:publish,
96
+ publish_all:publish_all, restart:restart, rm:rm, tty:tty,
97
+ user:user, volume:volume, volumes_from:volumes_from}
98
+
99
+ # after the options come the image and command
100
+ cmd << image
101
+ cmd.concat(command_and_args)
102
+
103
+ # return the output of `docker run` minus extra whitespace
104
+ run!('run', *cmd).strip
105
+ end
106
+
107
+ # Provide version information about the Docker client and server.
108
+ #
109
+ # @return [Hash] dictionary of strings describing version/build info
110
+ # @raise [Error] if command fails
111
+ def version
112
+ result = run!('version')
113
+
114
+ lines = result.split(/[\r\n]+/)
115
+
116
+ info = {}
117
+ prefix = ''
118
+
119
+ lines.each do |line|
120
+ if line =~ /^Client/
121
+ prefix = 'Client '
122
+ elsif line =~ /^Server/
123
+ prefix = 'Server '
124
+ else
125
+ pair = line.split(':',2).map { |e| e.strip }
126
+ info["#{prefix}#{pair[0]}"] = pair[1]
127
+ end
128
+ end
129
+
130
+ info
131
+ end
132
+
133
+ # Run a docker command without validating that the CLI parameters
134
+ # make sense. Prepend implicit options if suitable.
135
+ #
136
+ # @param [Array] args command-line arguments in the format accepted by
137
+ # Backticks::Runner#command
138
+ # @return [String] output of the command
139
+ # @raise [RuntimeError] if command fails
140
+ def run!(*args)
141
+ cmd = @shell.run('docker', *args).join
142
+ status, out, err = cmd.status, cmd.captured_output, cmd.captured_error
143
+ status.success? || raise(Error.new(args.first, status, err))
144
+ out
145
+ end
146
+ end
147
+ end
@@ -1,3 +1,3 @@
1
1
  module Docker
2
- VERSION = "0.0.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,87 +1,116 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
- - Vladimir Pouzanov
7
+ - Tony Spataro
9
8
  autorequire:
10
- bindir: bin
9
+ bindir: exe
11
10
  cert_chain: []
12
- date: 2013-05-01 00:00:00.000000000 Z
11
+ date: 2016-01-03 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: backticks
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.3'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: bundler
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
- - - ~>
31
+ - - "~>"
20
32
  - !ruby/object:Gem::Version
21
- version: '1.3'
33
+ version: '1.10'
22
34
  type: :development
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ~>
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
- version: '1.3'
40
+ version: '1.10'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: rake
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
36
60
  - !ruby/object:Gem::Version
37
61
  version: '0'
38
62
  type: :development
39
63
  prerelease: false
40
64
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
65
  requirements:
43
- - - ! '>='
66
+ - - ">="
44
67
  - !ruby/object:Gem::Version
45
68
  version: '0'
46
- description: Docker wrapper to manage docker.io based lxc containers
69
+ description: Provides an OOP interface to docker without relying on its HTTP API.
47
70
  email:
48
- - farcaller@gmail.com
71
+ - xeger@xeger.net
49
72
  executables: []
50
73
  extensions: []
51
74
  extra_rdoc_files: []
52
75
  files:
53
- - .gitignore
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
54
81
  - Gemfile
55
82
  - LICENSE.txt
56
83
  - README.md
57
84
  - Rakefile
85
+ - bin/console
86
+ - bin/setup
58
87
  - docker.gemspec
59
88
  - lib/docker.rb
89
+ - lib/docker/error.rb
90
+ - lib/docker/session.rb
60
91
  - lib/docker/version.rb
61
- homepage: ''
92
+ homepage: https://github.com/xeger/docker
62
93
  licenses:
63
94
  - MIT
95
+ metadata: {}
64
96
  post_install_message:
65
97
  rdoc_options: []
66
98
  require_paths:
67
99
  - lib
68
100
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
101
  requirements:
71
- - - ! '>='
102
+ - - ">="
72
103
  - !ruby/object:Gem::Version
73
104
  version: '0'
74
105
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
106
  requirements:
77
- - - ! '>='
107
+ - - ">="
78
108
  - !ruby/object:Gem::Version
79
109
  version: '0'
80
110
  requirements: []
81
111
  rubyforge_project:
82
- rubygems_version: 1.8.21
112
+ rubygems_version: 2.4.5
83
113
  signing_key:
84
- specification_version: 3
85
- summary: Docker wrapper to manage docker.io based lxc containers
114
+ specification_version: 4
115
+ summary: Docker CLI wrapper.
86
116
  test_files: []
87
- has_rdoc: