docker 0.3.2 → 0.4.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
  SHA1:
3
- metadata.gz: 22875585676480f67e4a5a2f7d132b8b3849636e
4
- data.tar.gz: ff1433914619dd2be968f2931a7d934911351003
3
+ metadata.gz: eab088e132fb6bb2bec9c3ab6d381a6ff2cd488e
4
+ data.tar.gz: 9e450fce4107d32904575463018de7276be29a14
5
5
  SHA512:
6
- metadata.gz: 44a5792ac7eedd48522d0723fb9e3100d7bcdb1e88c7aa4c2dc070c3ac3fb963311ef25e44770b8851d40d8076f987c3f3b662c918d5a2e26fe4bf35d2ce0481
7
- data.tar.gz: 08caac7bab4d76cb59b270a81bfbe39c12202153b827afd4af1911518279dae03bd2aa36a66025cf1502caed620ca99a7edef5c76a76b1be046ee015404ea4e0
6
+ metadata.gz: 31799d1342835cbaf275cd92ff4cb43b986e79f41038b2f4ce2ec85871ecded5c4246aa262005abdb079a14efdec721d54af252195cdc8e745577f96da2978e0
7
+ data.tar.gz: feb853dbbef941bd06d46e6f71b1e00ee63886994c5e68a6b3175c30479404ce1c74ff1036e51a177314688be47b3ca9b56c201fd6a8499757dc6d306f5f05c0
data/.gitignore CHANGED
@@ -9,3 +9,58 @@
9
9
  /tmp/
10
10
  /.idea
11
11
 
12
+ *.gem
13
+ *.rbc
14
+ /.config
15
+ /InstalledFiles
16
+ /pkg/
17
+ /spec/reports/
18
+ /spec/examples.txt
19
+ /test/tmp/
20
+ /test/version_tmp/
21
+ /tmp/
22
+
23
+ # Used by dotenv library to load environment variables.
24
+ # .env
25
+
26
+ ## Specific to RubyMotion:
27
+ .dat*
28
+ .repl_history
29
+ build/
30
+ *.bridgesupport
31
+ build-iPhoneOS/
32
+ build-iPhoneSimulator/
33
+
34
+ # Ignore Byebug/Bash/etc command history file.
35
+ /.*_hist*
36
+
37
+ # Ignore mac .DS_Store files
38
+ .DS_Store
39
+
40
+ ## Specific to RubyMotion (use of CocoaPods):
41
+ #
42
+ # We recommend against adding the Pods directory to your .gitignore. However
43
+ # you should judge for yourself, the pros and cons are mentioned at:
44
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
45
+ #
46
+ # vendor/Pods/
47
+
48
+ ## Documentation cache and generated files:
49
+ /.yardoc/
50
+ /_yardoc/
51
+ /doc/
52
+ /rdoc/
53
+
54
+ ## Environment normalization:
55
+ /.bundle/
56
+ /vendor/bundle
57
+ /lib/bundler/man/
58
+
59
+ # for a library or gem, you might want to ignore these files since the code is
60
+ # intended to run in multiple environments; otherwise, check them in:
61
+ Gemfile.lock
62
+ .ruby-version
63
+ .ruby-gemset
64
+
65
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
66
+ .rvmrc
@@ -0,0 +1,30 @@
1
+ # 1: Use ruby 2.3.3 as base:
2
+ FROM ruby:2.3.3
3
+
4
+ # 2: We'll set this gem path as the working directory
5
+ WORKDIR /usr/src/lib
6
+
7
+ # 3: We'll set the working dir as HOME and add the app's binaries path to $PATH:
8
+ ENV HOME=/usr/src/lib PATH=/usr/src/lib/bin:$PATH
9
+
10
+ # 4: Install docker - we'll need the client to run docker commands on the host engine, by mounting
11
+ # the host docker service's socket.
12
+ # Ripped off from https://hub.docker.com/_/docker Dockerfiles:
13
+ RUN set -ex && \
14
+ export DOCKER_BUCKET=get.docker.com && \
15
+ export DOCKER_VERSION=17.03.1-ce && \
16
+ export DOCKER_SHA256=820d13b5699b5df63f7032c8517a5f118a44e2be548dd03271a86656a544af55 && \
17
+ curl -fSL "https://${DOCKER_BUCKET}/builds/Linux/x86_64/docker-${DOCKER_VERSION}.tgz" -o docker.tgz && \
18
+ echo "${DOCKER_SHA256} *docker.tgz" | sha256sum -c - && \
19
+ tar -xzvf docker.tgz && \
20
+ mv docker/* /usr/local/bin/ && \
21
+ rmdir docker && \
22
+ rm docker.tgz
23
+
24
+ # 5: Install required gems:
25
+ ADD Gemfile docker.gemspec /usr/src/lib/
26
+ ADD lib/docker/version.rb /usr/src/lib/lib/docker/
27
+ RUN gem install guard && bundle install
28
+
29
+ # 6: Set the default command:
30
+ CMD ["guard"]
@@ -0,0 +1,26 @@
1
+ version: '2.1'
2
+
3
+ services:
4
+
5
+ lib:
6
+ image: xeger/docker-gem:development
7
+ build:
8
+ context: .
9
+ dockerfile: dev.Dockerfile
10
+
11
+ volumes:
12
+ # Mount our app code directory (".") into our app containers at the `/usr/src/lib` folder:
13
+ - .:/usr/src/lib
14
+
15
+ # Add the docker socket to enable docker in docker:
16
+ - /var/run/docker.sock:/var/run/docker.sock
17
+
18
+ # Keep the stdin open, so we can attach to our app container's process
19
+ # and do things such as byebug, etc:
20
+ stdin_open: true
21
+
22
+ # Enable sending signals (CTRL+C, CTRL+P + CTRL+Q) into the container:
23
+ tty: true
24
+
25
+ # The command:
26
+ command: guard
@@ -14,12 +14,12 @@ Gem::Specification.new do |spec|
14
14
  spec.homepage = "https://github.com/xeger/docker"
15
15
  spec.license = "MIT"
16
16
 
17
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|dev\.Dockerfile|docker-compose.yml)/}) }
18
18
  spec.bindir = "exe"
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "backticks", "~> 0.3"
22
+ spec.add_dependency "backticks", "~> 1.0.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.10"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
@@ -2,6 +2,7 @@ require_relative 'docker/version'
2
2
  require_relative 'docker/error'
3
3
  require_relative 'docker/asset'
4
4
  require_relative 'docker/container'
5
+ require_relative 'docker/cli'
5
6
  require_relative 'docker/session'
6
7
 
7
8
  module Docker
@@ -0,0 +1,62 @@
1
+ module Docker
2
+ # Docker command-line parameter generator.
3
+ # This is a modified version of Backticks::CLI::Getopt, made specially for converting ruby objects
4
+ # into Docker CLI opts.
5
+ module CLI
6
+ # Translate a series Ruby positional and keyword arguments into Docker command-
7
+ # parameters consisting of words and options.
8
+ #
9
+ # Each positional argument can be a Hash, an Array, or another object.
10
+ # They are handled as follows:
11
+ # - Hash is translated to a sequence of options; see #options
12
+ # - Array is appended to the command line as a sequence of words
13
+ # - other objects are turned into a string with #to_s and appended to the command line as a
14
+ # single word
15
+ #
16
+ # @return [Array] list of String words and options
17
+ #
18
+ # @example recursively find all text files
19
+ # parameters('docker', 'run', 'hello_world')
20
+ #
21
+ # @example run & remove docker hello world
22
+ # parameters('docker', 'run', rm: true, 'hello_world')
23
+ module Getopt
24
+ def self.parameters(*sugar)
25
+ sugar.each_with_object [] do |item, argv|
26
+ argv.concat extract_parameter(item)
27
+ end
28
+ end
29
+
30
+ def self.extract_parameter(item)
31
+ return item.map(&:to_s) if item.is_a? Array
32
+ return options(item) if item.is_a? Hash
33
+ [item.to_s]
34
+ end
35
+
36
+ def self.options(kwargs = {})
37
+ # Transform opts into golang flags-style command line parameters;
38
+ # append them to the command.
39
+ kwargs.map { |kw, arg| convert_option kw, arg }.compact.flatten
40
+ end
41
+
42
+ def self.convert_option(option_name, option_value)
43
+ flag = (option_name.length == 1) ? "-#{option_name}" : "--#{option_name}"
44
+ return [flag] if option_value == true
45
+ return process_list_option flag, option_value if option_value.is_a? Array
46
+ return process_non_null_option flag, option_value if option_value
47
+ end
48
+
49
+ def self.process_list_option(flag, option_value)
50
+ option_value.map do |value_item|
51
+ process_non_null_option flag, value_item if value_item
52
+ end.compact.flatten
53
+ end
54
+
55
+ def self.process_non_null_option(flag, option_value)
56
+ is_long_flag = flag[0..1] == '--'
57
+ return "#{flag}=#{option_value}" if is_long_flag
58
+ [flag, option_value.to_s]
59
+ end
60
+ end
61
+ end
62
+ end
@@ -23,7 +23,7 @@ module Docker
23
23
  # Hint that we are able to parse ps output
24
24
  PS_HEADER = /ID\s+IMAGE\s+COMMAND\s+CREATED\s+STATUS\s+PORTS\s+NAMES$/
25
25
 
26
- def initialize(shell=Backticks::Runner.new, host:ENV['DOCKER_HOST'])
26
+ def initialize(shell=Backticks::Runner.new(cli: Docker::CLI::Getopt), host:ENV['DOCKER_HOST'])
27
27
  @host = host
28
28
  @shell = shell
29
29
  end
@@ -71,6 +71,19 @@ module Docker
71
71
  # @example open a busybox shell
72
72
  # session.run('busybox', '/bin/sh', tty:true, interactive:true)
73
73
  #
74
+ # @example open busybox and remove vowels from user input
75
+ # session.run('busybox', '/bin/sh', tty:true, interactive:true) do |stream, data|
76
+ # if [:stdout, :stderr].include?(stream)
77
+ # puts data
78
+ # else
79
+ # data.gsub(/[aeiouy]/, '')
80
+ # end
81
+ # end
82
+ #
83
+ # @yield [stream, data] intercepts container I/O and passes it to the block
84
+ # @yieldparam [Symbol] stream :stdin, :stdout or :stderr
85
+ # @yieldparam [String] data the intercepted stream activity
86
+ #
74
87
  # @param [String] image id or name of base image to use for container
75
88
  # @param [Array] command_and_args optional command to run in container
76
89
  # @param [Integer] cpu_period scheduler period (μs)
@@ -113,7 +126,7 @@ module Docker
113
126
  tty:false,
114
127
  user:nil,
115
128
  volume:[],
116
- volumes_from:nil)
129
+ volumes_from:nil, &block)
117
130
 
118
131
  cmd = []
119
132
 
@@ -137,7 +150,7 @@ module Docker
137
150
  cmd.concat(command_and_args)
138
151
 
139
152
  # return the output of `docker run` minus extra whitespace
140
- run!('run', *cmd).strip
153
+ run!('run', *cmd, &block).strip
141
154
  end
142
155
 
143
156
  # Remove a container.
@@ -195,13 +208,20 @@ module Docker
195
208
  # Run a docker command without validating that the CLI parameters
196
209
  # make sense. Prepend implicit options if suitable.
197
210
  #
211
+ # @yield [stream, data] intercepts command I/O and passes it to the block
212
+ # @yieldparam [Symbol] stream :stdin, :stdout or :stderr
213
+ # @yieldparam [String] data the intercepted stream activity
214
+ #
198
215
  # @param [Array] args command-line arguments in the format accepted by
199
216
  # Backticks::Runner#command
200
217
  # @return [String] output of the command
201
218
  # @raise [RuntimeError] if command fails
202
- def run!(*args)
219
+ def run!(*args, &block)
203
220
  # STDERR.puts "+ " + (['docker'] + args).inspect
204
- cmd = @shell.run('docker', *args).join
221
+ cmd = @shell.run('docker', *args)
222
+ cmd.tap(&block) if block_given?
223
+ cmd.join
224
+
205
225
  status, out, err = cmd.status, cmd.captured_output, cmd.captured_error
206
226
  status.success? || raise(Error.new(args.first, status, err))
207
227
  out
@@ -1,3 +1,3 @@
1
1
  module Docker
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
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.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Spataro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-02-21 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backticks
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0.3'
19
+ version: 1.0.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0.3'
26
+ version: 1.0.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -85,9 +85,12 @@ files:
85
85
  - Rakefile
86
86
  - bin/console
87
87
  - bin/setup
88
+ - dev.Dockerfile
89
+ - docker-compose.yml
88
90
  - docker.gemspec
89
91
  - lib/docker.rb
90
92
  - lib/docker/asset.rb
93
+ - lib/docker/cli.rb
91
94
  - lib/docker/container.rb
92
95
  - lib/docker/error.rb
93
96
  - lib/docker/session.rb