docker_tools 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d95a7b733044ad62c0ac49ab1839bfd376e48f05
4
+ data.tar.gz: 9298bccdafeb8db7bc01ac0b332e18e2a12d9463
5
+ SHA512:
6
+ metadata.gz: ad1e4b270feab34b26166496a9bdeaa650aad3a6126da9c185544e53eb99cc0619abb81eb769d9fa5fd7bb717bf999152e59797f7d3740ea4d854dcb66de6071
7
+ data.tar.gz: 73d1de6bcb001a79bb9b867612c6457f2dc8def1e6fc099c82ee92b9903c3f98d2eec040b012b3022e4c715b4b06e73d6da857640948afd5e3780ee39aaac407
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.swp
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in docker_tools.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
2
+
3
+ MIT License
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:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
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.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # DockerTools
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'docker_tools'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install docker_tools
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
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
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'docker_tools/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docker_tools"
8
+ spec.version = DockerTools::VERSION
9
+ spec.authors = ["Chris Jansen"]
10
+ spec.email = ["noone@nowhere.com"]
11
+ spec.description = %q{Wrapper around ruby docker API to help with builds and stuff}
12
+ spec.summary = %q{See desc}
13
+ spec.homepage = "https://github.com/janstenpickle/docker_tools"
14
+ spec.license = "MIT"
15
+
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)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "bundler", "~> 1.3"
22
+ spec.add_dependency "rake"
23
+ spec.add_dependency "docker-api"
24
+ spec.add_dependency "erubis"
25
+ spec.add_dependency "json"
26
+ end
@@ -0,0 +1,25 @@
1
+ module DockerTools
2
+ class Debootstrap
3
+ attr_accessor :archive
4
+
5
+ def initialize(name, distro, tmpdir='/tmp')
6
+ @name = name
7
+ @distro = distro
8
+ @tmpdir = tmpdir
9
+ @archive = "#{@tmpdir}/#{@name}.tar"
10
+ end
11
+
12
+ def run
13
+ output = `debootstrap --download-only #{@distro} #{@tmpdir}/#{@name}`
14
+ unless $?.to_i == 0
15
+ puts output
16
+ throw "Could not run debootstrap"
17
+ end
18
+ `cd #{@tmpdir}/#{@name} && tar -cvf ../#{@name}.tar .`
19
+ end
20
+
21
+ def cleanup
22
+ `rm -rf #{@tmpdir}/#{@name}*`
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,30 @@
1
+ require "docker"
2
+ require "erubis"
3
+ require "docker_tools/image"
4
+
5
+
6
+ module DockerTools
7
+ class Dependency
8
+ def initialize(name, registry, tag, fallback_tag)
9
+ @name = name
10
+ @registry = registry
11
+ @tag = tag
12
+ @fallback_tag = fallback_tag
13
+ end
14
+
15
+ def run
16
+ tag = @tag
17
+ image = DockerTools::Image.new(@name, @registry, @tag)
18
+ image.pull
19
+ if image.image.nil?
20
+ puts "Falling back to image #{@registry}/#{@name}:#{@fallback_tag}"
21
+ image = DockerTools::Image.new(@name, @registry, @fallback_tag)
22
+ image.pull
23
+ throw "Cannot find image" if image.image.nil?
24
+ tag = @fallback_tag
25
+ end
26
+ tag
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,119 @@
1
+ require "docker"
2
+ require "json"
3
+ require "erubis"
4
+ require "docker_tools"
5
+
6
+ module DockerTools
7
+ class Image
8
+ attr_accessor :name, :registry, :tag, :dir, :full_name, :image
9
+
10
+
11
+ def initialize(name, registry, tag, dir=nil, lookup=true)
12
+ @name = name
13
+ @registry = registry
14
+ @tag = tag
15
+ @full_name = "#{registry}/#{name}:#{tag}" unless registry.nil?
16
+ @full_name = "#{name}:#{tag}" if registry.nil?
17
+ @image_name = "#{registry}/#{name}" unless registry.nil?
18
+ @image_name = name if registry.nil?
19
+ @dir = dir
20
+ @dockerfile = "#{dir}/Dockerfile.template" unless dir.nil?
21
+ @image = lookup_image if lookup
22
+ # Set the read timeout for the connection
23
+ Excon.defaults[:read_timeout] = DockerTools.image_timeout
24
+ end
25
+
26
+ def pull
27
+ puts "Pulling image #{@image_name}"
28
+ Docker::Image.create('fromImage' => @image_name, 'tag' => @tag)
29
+ @image = lookup_image
30
+ end
31
+
32
+ def build(registry: @registry, tag: @tag, method: 'image', distro: 'precise', fallback_tag: DockerTools.dependency_fallback_tag, no_pull: DockerTools.no_pull, template_vars: {}, rm: false, no_cache: false)
33
+ case method
34
+ when 'image'
35
+ dependency_tag = dependency['tag']
36
+ dependency_tag = DockerTools::Dependency.new(dependency['repository'], dependency['registry'], dependency['tag'], fallback_tag).run unless no_pull
37
+
38
+ dockerfile_path = "#{@dir}/Dockerfile"
39
+ dockerfile_contents = dockerfile(@name, registry, dependency_tag, template_vars)
40
+ File.open(dockerfile_path, 'w') { | file | file.write(dockerfile_contents) }
41
+ @image = Docker::Image.build_from_dir(@dir, { 'rm' => rm, 'nocache' => no_cache }) do | chunk |
42
+ Docker::Util.parse_output(chunk) do | output |
43
+ if output.kind_of?(Hash)
44
+ if output.has_key?('error')
45
+ puts output['error']
46
+ raise output['error']
47
+ end
48
+ puts output['stream'] if output.has_key?('stream')
49
+ else
50
+ puts output
51
+ end
52
+ end
53
+ end
54
+ File.delete(dockerfile_path)
55
+ when 'debootstrap'
56
+ debootstrap = DockerTools::Debootstrap.new(@name, distro)
57
+ debootstrap.run
58
+ @image = Docker::Image.import(debootstrap.archive)
59
+ debootstrap.cleanup
60
+ else
61
+ raise "Invalid value for method: #{method}"
62
+ end
63
+ @image.tag('repo' => "#{@registry}/#{@name}", 'tag' => @tag, 'force' => true)
64
+ @image
65
+ end
66
+
67
+ def tag (new_tag)
68
+ @image.tag('repo' => "#{@registry}/#{@name}", 'tag' => new_tag, 'force' => true) if image_present?
69
+ end
70
+
71
+ def dependency
72
+ if @dockerfile.nil?
73
+ return nil
74
+ else
75
+ template = dockerfile
76
+ dependency = {}
77
+ if template =~ /(FROM|from)\s+(\S+)\/(\S+):(\S+)/
78
+ dependency['registry'] = $2
79
+ dependency['repository'] = $3
80
+ dependency['tag'] = $4
81
+ return dependency
82
+ elsif template =~ /(FROM|from)\s+(\S+):(\S+)/
83
+ dependency['registry'] = nil
84
+ dependency['repository'] = $2
85
+ dependency['tag'] = $3
86
+ elsif template =~ /(FROM|from)\s+(\S+)$/
87
+ dependency['registry'] = nil
88
+ dependency['repository'] = $2
89
+ dependency['tag'] = nil
90
+ end
91
+ dependency
92
+ end
93
+ end
94
+
95
+ private
96
+ def image_present?
97
+ throw "Image #{@full_name} is not present on the local machine" if @image.nil?
98
+ return true
99
+ end
100
+
101
+ def dockerfile(name = @name, registry = @registry, tag = @tag, args = {})
102
+ raise "Must specify Dockerfile location in image" if @dockerfile.nil?
103
+ template = Erubis::Eruby.new(File.read(@dockerfile))
104
+ template_vars = { :registry => registry, :tag => tag, :name => name }
105
+ template_vars.merge!(args) if args.kind_of?(Hash)
106
+ template.result(template_vars)
107
+ end
108
+
109
+ def lookup_image
110
+ images = Docker::Image.all
111
+ images.each do | image |
112
+ if image.info['RepoTags'].include?(@full_name)
113
+ return image
114
+ end
115
+ end
116
+ nil
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,31 @@
1
+ require "docker"
2
+
3
+ module DockerTools
4
+ class Run
5
+ attr_accessor :container
6
+
7
+ def initialize(name, registry, tag, image = nil, command = nil, volumes = [])
8
+ @name = name
9
+ @registry = registry
10
+ @tag = tag
11
+ @command = command
12
+ @volumes = volumes
13
+ @image = "#{@registry}/#{@name}:#{@tag}" if image = nil?
14
+ @image = image unless image = nil?
15
+ @container = create_container(@image, @command, @volumes)
16
+ end
17
+
18
+ private
19
+ def create_container(image, command, volumes)
20
+ create_args = { 'Image' => image, 'Tty' => false }
21
+ create_args['Cmd'] = command.strip.split(/\s+/) unless command.nil?
22
+ create_args['Volumes'] = volumes_create if volumes.kind_of?(Array) and volumes.size > 0
23
+ container = Docker::Container.create(create_args)
24
+ container.start('Binds' => volumes)
25
+ container
26
+ end
27
+ def volumes_create
28
+ @volumes.each_with_object({}) { | volume, acc | acc[volume.split(':').first] = {} }
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module DockerTools
2
+ VERSION = "0.0.11"
3
+ end
@@ -0,0 +1,56 @@
1
+ require "docker_tools/version"
2
+ require "docker_tools/image"
3
+ require "docker_tools/run"
4
+ require "docker_tools/debootstrap"
5
+ require "docker_tools/dependency"
6
+ require "docker"
7
+
8
+ module DockerTools
9
+ #Default the docker url to docker http service
10
+ Docker.url = 'http://localhost:4243'
11
+
12
+ def default_dependency_fallback_tag
13
+ 'latest'
14
+ end
15
+
16
+ def env_dependency_fallback_tag
17
+ ENV['DOCKER_DEPENDENCY_FALLBACK_TAG']
18
+ end
19
+
20
+ def default_no_pull
21
+ false
22
+ end
23
+
24
+ def env_no_pull
25
+ ENV['DOCKER_NO_PULL']
26
+ end
27
+
28
+ def no_pull
29
+ @no_pull ||= env_no_pull || default_no_pull
30
+ end
31
+
32
+ def no_pull=(new_no_pull)
33
+ @no_pull = new_no_pull
34
+ end
35
+
36
+ def dependency_fallback_tag
37
+ @dependency_fallback_tag ||= env_dependency_fallback_tag || default_dependency_fallback_tag
38
+ end
39
+
40
+ def dependency_fallback_tag=(new_dependency_fallback_tag)
41
+ @dependency_fallback_tag = new_dependency_fallback_tag
42
+ end
43
+
44
+ def image_timeout
45
+ @image_timeout = 1000
46
+ end
47
+
48
+ def image_timeout=(new_image_timeout)
49
+ @image_timeout = new_image_timeout
50
+ end
51
+
52
+ module_function :default_dependency_fallback_tag, :env_dependency_fallback_tag,
53
+ :dependency_fallback_tag, :dependency_fallback_tag=,
54
+ :image_timeout, :image_timeout=, :default_no_pull, :env_no_pull,
55
+ :no_pull, :no_pull=
56
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docker_tools
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ platform: ruby
6
+ authors:
7
+ - Chris Jansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: docker-api
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: erubis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Wrapper around ruby docker API to help with builds and stuff
84
+ email:
85
+ - noone@nowhere.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - docker_tools.gemspec
96
+ - lib/docker_tools.rb
97
+ - lib/docker_tools/debootstrap.rb
98
+ - lib/docker_tools/dependency.rb
99
+ - lib/docker_tools/image.rb
100
+ - lib/docker_tools/run.rb
101
+ - lib/docker_tools/version.rb
102
+ homepage: https://github.com/janstenpickle/docker_tools
103
+ licenses:
104
+ - MIT
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.0.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: See desc
126
+ test_files: []
127
+ has_rdoc: