capistrano-dockerbuild 0.1.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: 20a884c1147fd81d4f831378fd286fedf5498d4a
4
+ data.tar.gz: 227ca91c65a6227320fb8b275befc12cca072ba1
5
+ SHA512:
6
+ metadata.gz: a3ee23aff127683494ba9ddd071492e4d197b3a21e9ff400924e99017d9f2d82379beeba57802e2071c95048b5d56d508e7ee08e243f66f7a881576dc699cb1a
7
+ data.tar.gz: 273a531f78913872dd10e2ebf288f16978e3dde3bbbaba9e273c4dccf22aadaa2ead72599329badc3b7aefd7a45c1090ae6d7fccd77bb47be0b79c36dbde354c
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in capistrano-dockerbuild.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 joker1007
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.
@@ -0,0 +1,97 @@
1
+ # Capistrano::Dockerbuild
2
+
3
+ Capistrano tasks for `docker build` on remote server.
4
+
5
+ These tasks depends on Git.
6
+ And so remote server must have git command.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'capistrano-dockerbuild'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install capistrano-dockerbuild
23
+
24
+ ## Usage
25
+
26
+ Add `require` and `install_plugin` to Capfile.
27
+
28
+ ```ruby
29
+ require 'capistrano/dockerbuild'
30
+ install_plugin Capistrano::Dockerbuild
31
+ ```
32
+
33
+ ## Variables
34
+
35
+ #### Common Variables
36
+ Use common variables
37
+ - repo_url
38
+ - branch
39
+
40
+ | name | required | default | desc |
41
+ | ---- | ---- | ---- | ---- |
42
+ | docker_build_server_host | yes | nil | Build server hostname or SSH::Host object |
43
+ | docker_build_base_dir | yes | nil | Repository clone to here, and execute build command here |
44
+ | docker_registry | no | nil | Docker registry hostname. if use DockerHub, keep nil |
45
+ | docker_build_cmd | no | `-> { [:docker, "build", "-t", fetch(:docker_tag_full), "."] }` | Execute command for image building |
46
+ | docker_repository_name | no | `-> { fetch(:application) }` | Use by `docker tag {{docker_repository_name}}:tag` |
47
+ | docker_tag | no | `-> { fetch(:branch) }` | Use by `docker tag repository:{{docker_tag}}` |
48
+ | docker_tag_full | no | `-> { #{fetch(:docker_repository_name)}:#{fetch(:docker_tag)}" }` | Use by `docker tag {{docker_tag_full}}` |
49
+ | docker_remote_repository_name | no | `-> { fetch(:docker_repository_name) }` | Use by `docker push docker_registry/{{docker_remote_repository_name}}:docker_remote_tag` |
50
+ | docker_remote_tag | no | `-> { fetch(:docker_tag) }` | Use by `docker push docker_registry/docker_remote_repository_name:{{docker_remote_tag}}` |
51
+ | docker_remote_tag_full | no | `-> { "#{fetch(:docker_registry) &.+ "/"}#{fetch(:docker_remote_repository_name)}:#{fetch(:docker_remote_tag)}" }` | Use by `docker push {{docker_remote_tag_full}}` |
52
+ | keep_docker_image_count | no | 10 | |
53
+
54
+
55
+ ## Tasks
56
+
57
+ #### docker:check
58
+ - Ensure `#{docker_build_base_dir}`
59
+ - Ensure git reachable
60
+
61
+ #### docker:clone
62
+ - Clone repo to `#{docker_build_base_dir}` as mirror
63
+
64
+ #### docker:update_mirror
65
+ - git remote update `#{docker_build_base_dir}`
66
+
67
+ #### docker:build
68
+ - Create `#{branch}` worktree to `#{docker_tag}-#{timestamp}`
69
+ - With in worktree dir, execute `#{docker_build_cmd}`
70
+ - Clear worktree
71
+
72
+ #### docker:push
73
+ - docker tag `#{docker_tag_full}` `#{docker_remote_tag_full}`
74
+ - docker push `#{docker_remote_tag_full}`
75
+
76
+ #### docker:cleanup_local_images
77
+ - Remove docker images
78
+ - Leave `keep_docker_image_count` images
79
+ - Remove from the oldest image
80
+
81
+ ## Task Dependency
82
+
83
+ docker:push => docker:build => docker:update_mirror => docker:clone => docker:check
84
+
85
+ ## Development
86
+
87
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
+
89
+ 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).
90
+
91
+ ## Contributing
92
+
93
+ Bug reports and pull requests are welcome on GitHub at https://github.com/reproio/capistrano-dockerbuild.
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "capistrano/dockerbuild"
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(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "capistrano-dockerbuild"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["joker1007"]
9
+ spec.email = ["kakyoin.hierophant@gmail.com"]
10
+
11
+ spec.summary = %q{Capistrano task definition for `docker build`}
12
+ spec.description = %q{Capistrano task definition for `docker build`}
13
+ spec.homepage = "https://github.com/reproio/capistrano-dockerbuild"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_runtime_dependency "capistrano", ">= 3.2"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,21 @@
1
+ class Capistrano::Dockerbuild < Capistrano::Plugin
2
+ def set_defaults
3
+ set_if_empty :docker_build_cmd, -> { [:docker, "build", "-t", fetch(:docker_tag_full), "."] }
4
+ set_if_empty :docker_repository_name, -> { fetch(:application) }
5
+ set_if_empty :docker_tag, -> { fetch(:branch) }
6
+ set_if_empty :docker_tag_full, -> { "#{fetch(:docker_repository_name)}:#{fetch(:docker_tag)}" }
7
+ set_if_empty :docker_remote_repository_name, -> { fetch(:docker_repository_name) }
8
+ set_if_empty :docker_remote_tag, -> { fetch(:docker_tag) }
9
+ set_if_empty :docker_remote_tag_full, -> { "#{fetch(:docker_registry) &.+ "/"}#{fetch(:docker_remote_repository_name)}:#{fetch(:docker_remote_tag)}" }
10
+ set_if_empty :keep_docker_image_count, 10
11
+ end
12
+
13
+ def define_tasks
14
+ eval_rakefile File.expand_path("../tasks/docker.rake", __FILE__)
15
+ end
16
+
17
+ def docker_build_base_path
18
+ raise "Need to set :docker_build_base_dir" unless fetch(:docker_build_base_dir)
19
+ Pathname(fetch(:docker_build_base_dir))
20
+ end
21
+ end
@@ -0,0 +1,80 @@
1
+ dockerbuild_plugin = self
2
+
3
+ namespace :docker do
4
+ desc "check directory exist"
5
+ task :check do
6
+ on fetch(:docker_build_server_host) do
7
+ execute :mkdir, "-p", dockerbuild_plugin.docker_build_base_path.dirname.to_s
8
+ execute :git, :'ls-remote', repo_url, "HEAD"
9
+ end
10
+ end
11
+
12
+ desc "Clone the repo to docker build base directory"
13
+ task :clone => [:'docker:check'] do
14
+ on fetch(:docker_build_server_host) do
15
+ if test " [ -f #{dockerbuild_plugin.docker_build_base_path}/HEAD ] "
16
+ info t(:mirror_exists, at: dockerbuild_plugin.docker_build_base_path.to_s)
17
+ else
18
+ within dockerbuild_plugin.docker_build_base_path.dirname do
19
+ execute :git, :clone, "--mirror", repo_url, dockerbuild_plugin.docker_build_base_path.to_s
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ desc "Update the repo mirror to reflect the origin state"
26
+ task update_mirror: :'docker:clone' do
27
+ on fetch(:docker_build_server_host) do
28
+ within dockerbuild_plugin.docker_build_base_path do
29
+ execute :git, :remote, :update, "--prune"
30
+ end
31
+ end
32
+ end
33
+
34
+ desc "build and push docker image on remote host"
35
+ task :build => [:'docker:update_mirror'] do
36
+ on fetch(:docker_build_server_host) do
37
+ within dockerbuild_plugin.docker_build_base_path do
38
+ timestamp = Time.now.to_i
39
+ worktree_dir_name = "worktree-#{fetch(:docker_tag)}-#{timestamp}"
40
+
41
+ execute(:git, :worktree, :add, worktree_dir_name, fetch(:branch))
42
+
43
+ begin
44
+ within worktree_dir_name do
45
+ execute(*fetch(:docker_build_cmd))
46
+ end
47
+ ensure
48
+ execute(:rm, "-rf", worktree_dir_name)
49
+ execute(:git, :worktree, :prune)
50
+ # Execute "git gc" manually to avoid "There are too many unreachable loose objects" warning
51
+ execute(:git, :gc, "--auto", "--prune=3.days.ago")
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ task :push => [:'docker:build'] do
58
+ on fetch(:docker_build_server_host) do
59
+ execute(:docker, :tag, fetch(:docker_tag_full), fetch(:docker_remote_tag_full))
60
+ execute(:docker, :push, fetch(:docker_remote_tag_full))
61
+ end
62
+ end
63
+
64
+ task :cleanup_local_images do
65
+ on fetch(:docker_build_server_host) do
66
+ local_images = []
67
+ capture(:docker, "images --format='{{.ID}}\t{{.Repository}}\t{{.CreatedAt}}'").split("\n").map do |line|
68
+ id, repository, created_at = line.split("\t")
69
+ if repository == fetch(:docker_repository_name)
70
+ local_images << { id: id, created_at: created_at }
71
+ end
72
+ end
73
+ local_images.uniq! # Same image could exist in different repository.
74
+ if local_images.size > fetch(:keep_docker_image_count)
75
+ deleting_image_ids = local_images.sort_by { |i| i[:created_at] }.first(local_images.size - fetch(:keep_docker_image_count)).map { |i| i[:id] }
76
+ execute(:docker, "rmi -f #{deleting_image_ids.join(" ")}")
77
+ end
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capistrano-dockerbuild
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - joker1007
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: capistrano
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
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
+ description: Capistrano task definition for `docker build`
56
+ email:
57
+ - kakyoin.hierophant@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - capistrano-dockerbuild.gemspec
70
+ - lib/capistrano/dockerbuild.rb
71
+ - lib/capistrano/tasks/docker.rake
72
+ homepage: https://github.com/reproio/capistrano-dockerbuild
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.6.12
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Capistrano task definition for `docker build`
96
+ test_files: []