docker_sigh 0.1.0

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: d78e6cd74497a102f6fb6acbc47e378f7fe0c0ec
4
+ data.tar.gz: 3c65de37d31737ed2a01ad47014e5bbf5b863d5b
5
+ SHA512:
6
+ metadata.gz: 3fdd68b0d1e2e1b0e99033e7307f929c478be7df68b921a8188d8359268df25d331af9f4e51c67c91f874b157ffd9b77cc458a9c8a456825de3f531c05cfdbbe
7
+ data.tar.gz: 7332cac90fb68f2a636535fe90c7132d5965218ec1e72780879a64b1411b5f47d289b27491b4e0ec8b9480b2748adedf9e690f8872bdf49f1bf43bc613c90166
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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, 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 ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in docker_sigh.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ed Ropple
4
+
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:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
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 ADDED
@@ -0,0 +1,37 @@
1
+ # `docker_sigh` #
2
+ `docker_sigh` is an (opinionated) way of wrangling Docker containers across multiple repositories. It expects a `git-flow` environment and is designed to operate against it.
3
+
4
+ ## Installation ##
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'docker_sigh'
9
+ ```
10
+
11
+ ## Usage ##
12
+ Add `docker_sigh` to your Rakefile:
13
+
14
+ ```ruby
15
+ require "docker_sigh"
16
+ DockerSigh::load_tasks \
17
+ repository_root: File.expand_path(File.dirname(__FILE__)),
18
+ container_name: "YOURUSERNAME/YOURCONTAINERNAME"
19
+ ```
20
+
21
+ And then invoke it:
22
+
23
+ $ rake sighs:dockerize
24
+
25
+ ## Development ##
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing ##
32
+
33
+ 1. Fork it ( https://github.com/eropple/docker_sigh/fork )
34
+ 2. Create your feature branch (`git checkout -b feature/my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin feature/my-new-feature`)
37
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "docker_sigh"
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
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -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
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'docker_sigh/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "docker_sigh"
8
+ spec.version = DockerSigh::VERSION
9
+ spec.authors = ["Ed Ropple"]
10
+ spec.email = ["ed+dockersigh@edropple.com"]
11
+
12
+ spec.summary = %q{A set of helper Rake tasks to make dealing with Docker a little less horrible.}
13
+ spec.homepage = "https://github.com/eropple/docker_sigh"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.8"
22
+
23
+ spec.add_runtime_dependency "rake", "~> 10.0"
24
+ spec.add_runtime_dependency 'erber', '~> 0.1.1'
25
+ end
@@ -0,0 +1,3 @@
1
+ module DockerSigh
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,204 @@
1
+ require "docker_sigh/version"
2
+
3
+ require "erber/templater"
4
+
5
+ require "fileutils"
6
+ require "logger"
7
+
8
+ require "rake"
9
+ require "rake/file_utils"
10
+
11
+ module DockerSigh
12
+ LOGGER = Logger.new($stderr)
13
+ LOGGER.level = Logger.const_get((ENV["DOCKERSIGH_VERBOSITY"] || "INFO").upcase)
14
+
15
+ def self.load_tasks(opts)
16
+ extend Rake::DSL
17
+ dockerfile_template = Util::template_url(opts)
18
+ dockerfile = dockerfile_template.gsub("Dockerfile.template.erb", "Dockerfile")
19
+
20
+ namespace :ds do
21
+ task :template do
22
+ Util::validate_opts(opts)
23
+ Util::validate_repo(opts)
24
+ LOGGER.debug "Beginning 'template' task."
25
+
26
+ template = Erber::Templater.new(IO.read(dockerfile_template), DockerfileMash)
27
+ IO.write(dockerfile, template.render(Util::generate_template_args(opts)))
28
+
29
+ LOGGER.debug "Ending 'template' task."
30
+ end
31
+
32
+ task :build do
33
+ Util::validate_opts(opts)
34
+ Util::validate_repo(opts)
35
+ LOGGER.debug "Beginning 'build' task."
36
+ Dir.chdir(opts[:repository_root]) do
37
+ name = opts[:container_name]
38
+
39
+ ret = sh "docker build -t '#{name}:working' ."
40
+ raise "failed to build" unless ret
41
+
42
+ [ Util::repo_current_commit(opts),
43
+ Util::tag_from_branch_name(opts),
44
+ Util::repo_current_tags(opts) ].flatten.each do |tag|
45
+ ret = sh "docker tag --force #{name}:working #{name}:#{tag}"
46
+ raise "failed to tag with '#{tag}'" unless ret
47
+ end
48
+
49
+ ret = sh "docker rmi #{name}:working"
50
+ raise "failed to rmi working tag" unless ret
51
+ end
52
+ LOGGER.debug "Ending 'build' task."
53
+ end
54
+
55
+ task :clean do
56
+ Util::validate_opts(opts)
57
+ LOGGER.debug "Beginning 'clean' task."
58
+
59
+ LOGGER.debug "Deleting '#{dockerfile}'."
60
+ FileUtils.rm_f dockerfile
61
+
62
+ LOGGER.debug "Ending 'clean' task."
63
+ end
64
+
65
+ task :push do
66
+ LOGGER.debug "Beginning 'push' task."
67
+ Dir.chdir(opts[:repository_root]) do
68
+ name = opts[:container_name]
69
+ hash = Util::repo_current_commit(opts)
70
+ branch = Util::tag_from_branch_name(opts)
71
+
72
+ ret = sh "docker push #{name}:#{branch}"
73
+ raise "failed to push with branch tag" unless ret
74
+ ret = sh "docker push #{name}:#{hash}"
75
+ raise "failed to push with hash tag" unless ret
76
+
77
+ end
78
+ LOGGER.debug "Ending 'push' task."
79
+ end
80
+
81
+ task :go => [ :template, :build, :clean ] { }
82
+ end
83
+ end
84
+
85
+
86
+ module Util
87
+ CONTAINER_NAME_REGEX = /[a-z0-9_]\/[a-zA-Z0-9\-_.]/
88
+
89
+ def self.validate_opts(opts)
90
+ LOGGER.debug "Validating options: #{opts.inspect}"
91
+
92
+ raise ":container_name must be set." unless opts[:container_name]
93
+ raise ":container_name must match regex '#{CONTAINER_NAME_REGEX.to_s}'." \
94
+ unless opts[:container_name] =~ CONTAINER_NAME_REGEX
95
+
96
+ raise "If set, :default_host must be a valid hostname." \
97
+ unless !opts[:default_host] || Util::valid_hostname?(opts[:default_host])
98
+
99
+ raise "If set, the DOCKER_REMOTE env var must be a valid hostname." \
100
+ unless !ENV["DOCKER_REMOTE"] || Util::valid_hostname?(ENV["DOCKER_REMOTE"])
101
+
102
+ opts[:host] = ENV["DOCKER_REMOTE"] || opts[:default_host]
103
+
104
+ raise ":repository_root must be set." unless opts[:repository_root]
105
+ raise ":repository_root must exist and be a non-bare Git root." \
106
+ unless Dir.exist?(File.join(opts[:repository_root], ".git"))
107
+
108
+ LOGGER.debug "Options successfully validated."
109
+ end
110
+
111
+ def self.validate_repo(opts)
112
+ dockerfile_template = template_url(opts)
113
+ raise "'#{dockerfile_template}' must exist." unless File.exist?(dockerfile_template)
114
+
115
+ gitignore = File.join(opts[:repository_root], ".gitignore")
116
+ raise "'#{gitignore}' must exist." unless File.exist?(gitignore)
117
+ system "grep -e 'Dockerfile\|/Dockerfile' #{gitignore}"
118
+ raise "The repo's .gitignore must ignore the Dockerfile." unless $?
119
+
120
+ raise "The repo must be clean (no outstanding changes)." unless clean_git_root?(opts)
121
+ end
122
+
123
+ def self.template_url(opts)
124
+ File.join(opts[:repository_root], "Dockerfile.template.erb")
125
+ end
126
+
127
+ def self.generate_template_args(opts)
128
+ {
129
+ :host => opts[:host],
130
+ :parent_tag => from_directive_parent_tag(opts)
131
+ }
132
+ end
133
+
134
+ def self.repo_branch(opts)
135
+ Dir.chdir(opts[:repository_root]) do
136
+ name = `git symbolic-ref --short HEAD`.strip
137
+ raise "git failed to find branch" unless $? == 0
138
+ name
139
+ end
140
+ end
141
+
142
+ def self.tag_from_branch_name(opts)
143
+ repo_branch(opts).gsub("/", "-")
144
+ end
145
+
146
+ def self.repo_current_commit(opts)
147
+ hash = `git rev-parse --verify HEAD`.strip
148
+ raise "git failed to get hash" unless $? == 0
149
+ hash
150
+ end
151
+ def self.repo_current_tags(opts)
152
+ LOGGER.warn "Tags are not currently replicated into the Docker repository. Be advised when using release tags."
153
+ []
154
+ end
155
+
156
+ def self.from_directive_parent_tag(opts)
157
+ # TODO: parse .git/config and un-hardcode the git-flow prefixes
158
+
159
+ branch = repo_branch(opts)
160
+ case branch
161
+ when "develop"
162
+ "develop"
163
+ when "master"
164
+ "master"
165
+ else
166
+ if branch.start_with?("feature/")
167
+ "develop"
168
+ elsif branch.start_with("release/") || branch.start_with("hotfix/")
169
+ "master"
170
+ else
171
+ logger.warn "Unrecognized branch; can't figure out a parent tag. Assuming 'develop'."
172
+ "develop"
173
+ end
174
+ end
175
+ end
176
+
177
+ # originally from http://www.dzone.com/snippets/simple-hostname-validation
178
+ def self.valid_hostname?(hostname)
179
+ return false unless hostname
180
+ return false if hostname.length > 255 or hostname.scan('..').any?
181
+ hostname = hostname[0 ... -1] if hostname.index('.', -1)
182
+ return hostname.split('.').collect { |i|
183
+ i.size <= 63 and not (i.rindex('-', 0) or i.index('-', -1) or i.scan(/[^a-z\d-]/i).any?)
184
+ }.all?
185
+ end
186
+
187
+ def self.clean_git_root?(opts)
188
+ Dir.chdir opts[:repository_root] do
189
+ `git status | grep 'nothing to commit, working directory clean'`
190
+ $? == 0
191
+ end
192
+ end
193
+ end
194
+
195
+ class DockerfileMash < Hashie::Mash
196
+ def from_directive(parent_container_name)
197
+ if host
198
+ raise "host not supported yet"
199
+ else
200
+ "FROM #{parent_container_name}:#{parent_tag}"
201
+ end
202
+ end
203
+ end
204
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: docker_sigh
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ed Ropple
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-09 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: erber
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.1
55
+ description:
56
+ email:
57
+ - ed+dockersigh@edropple.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - docker_sigh.gemspec
71
+ - lib/docker_sigh.rb
72
+ - lib/docker_sigh/version.rb
73
+ homepage: https://github.com/eropple/docker_sigh
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.5
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: A set of helper Rake tasks to make dealing with Docker a little less horrible.
97
+ test_files: []
98
+ has_rdoc: