brocket 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 82018431e2aef0462f2a1197c516853c210f55d1
4
- data.tar.gz: 096d374c5c647fd2a5d7463c1e865e1ce38ceaf1
3
+ metadata.gz: 54c63386ef0806f1a1e9ac34e9692c8529ad2cc0
4
+ data.tar.gz: 5965c22451aec6eb6e003df5bbba94219383d1b8
5
5
  SHA512:
6
- metadata.gz: 96f605adec1bea26494c75bf5c6831aa43d1b6d180fc27d9821ef50821ad3ef85a91ee5906715b2d62879289f037ed7a850154e926f58049754a06aac8ff5c95
7
- data.tar.gz: 169748729b2b2531b6b24ba9e382922940657df14944df8c9156c0fc6ccfad6cd0a80e4debb48c55bc61dc656d3522f6b23df441051d17a83d1165058761109f
6
+ metadata.gz: 2e80157b476a1ff419dcf54501b44dbaabb419ea62ed3d7e4c842fba6f1a2b46f7d0f1d21072097f2e4597b2642b6e809b212d8334a4ab1bdacbf285adb2bd07
7
+ data.tar.gz: 1cfadc60e0bf8e3b6cd42536d31311e394663b619dd6f80b5afcd21481764c709c295aa01a54ab755bc895ddefbb60efeb9635f536168a183c10279878fc3bdb
data/.rspec CHANGED
@@ -1,2 +1,2 @@
1
- --format documentation
1
+ --format Fuubar
2
2
  --color
data/Gemfile CHANGED
@@ -2,3 +2,11 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in brocket.gemspec
4
4
  gemspec
5
+
6
+ group :development, :test do
7
+ gem "fuubar"
8
+ gem "yard" , require: false
9
+ gem 'simplecov' , require: false
10
+ gem 'simplecov-rcov', require: false
11
+ gem "pry"
12
+ end
data/README.md CHANGED
@@ -12,11 +12,12 @@ config IMAGE_NAME and hooks like BEFORE_BUILD and AFTER_BUILD.
12
12
  3. push by git
13
13
  4. push by docker
14
14
  - `brocket docker build`
15
- 1. call BEFORE_BUILD
16
- 2. `docker build` with arguments
15
+ 1. cd WORKING_DIR
16
+ 2. call BEFORE_BUILD
17
+ 3. `docker build` with arguments
17
18
  - call ON_BUILD_COMPLETE on success
18
19
  - call ON_BUILD_ERROR on failure
19
- 3. call AFTER_BUILD
20
+ 4. call AFTER_BUILD
20
21
 
21
22
  ## Installation
22
23
 
@@ -54,6 +55,7 @@ add following line to your Dockerfile.
54
55
 
55
56
  example https://github.com/tengine/brocket/blob/master/spec/brocket/Dockerfiles/Dockerfile-basic#L2
56
57
 
58
+
57
59
  ### bump up VERSION
58
60
 
59
61
  When you increment VERSION file, you can use theese commands.
@@ -71,13 +73,14 @@ You can define commands to execute around `docker build` like this:
71
73
  ```
72
74
  # [config] IMAGE_NAME: "groovenauts/rails-example"
73
75
  # [config]
76
+ # [config] WORKING_DIR: ".."
77
+ # [config]
74
78
  # [config] BEFORE_BUILD:
75
- # [config] - abc
76
- # [config] - def ghi
79
+ # [config] - rm log/*.log
80
+ # [config] - cp some/files dest
77
81
  # [config]
78
82
  # [config] AFTER_BUILD:
79
- # [config] - "jkl"
80
- # [config] - mno
83
+ # [config] - rm -rf tmp/build
81
84
  # [config]
82
85
  # [config] ON_BUILD_COMPLETE: foo bar
83
86
  # [config]
@@ -85,9 +88,40 @@ You can define commands to execute around `docker build` like this:
85
88
  # [config]
86
89
  ```
87
90
 
88
-
89
91
  https://github.com/tengine/brocket/blob/master/spec/brocket/Dockerfiles/Dockerfile-hook
90
92
 
93
+ ### Other configurations
94
+
95
+ #### WORKING_DIR
96
+
97
+ The directory which the build command and hooks are executed in.
98
+
99
+ #### VERSION_FILE
100
+
101
+ A text file to define the container version. It must be a relative path from Dockerfile.
102
+
103
+ #### VERSION_SCRIPT
104
+
105
+ A script to get the container version. It runs in the directory of Dockerfile.
106
+
107
+ `ruby -r ./lib/test_gem/version.rb -e 'puts TestGem::VERSION'`
108
+
109
+ #### GIT_TAG_PREFIX
110
+
111
+ The prefix for the version tags on git.
112
+
113
+ #### DOCKER_PUSH_COMMAND
114
+
115
+ Set `gcloud docker push` to push to [Google Cloud Registry](https://cloud.google.com/container-registry/docs/).
116
+
117
+ #### DOCKER_PUSH_REGISTRY
118
+
119
+ Set the registry host name to push not to hub.docker.com .
120
+
121
+ #### DOCKER_PUSH_USERNAME
122
+
123
+ Set the user name on the registry host to push.
124
+
91
125
 
92
126
  ### For more information
93
127
 
data/lib/brocket.rb CHANGED
@@ -1,12 +1,31 @@
1
1
  require "brocket/version"
2
2
 
3
+ require 'logger'
4
+
3
5
  module BRocket
4
6
  autoload :Cli , "brocket/cli"
5
7
  autoload :Base , "brocket/base"
8
+ autoload :Configurable, "brocket/configurable"
6
9
  autoload :VersionFile, "brocket/version_file"
7
10
  autoload :Git , "brocket/git"
8
11
  autoload :Docker , "brocket/docker"
9
12
 
10
13
  class BuildError < StandardError
11
14
  end
15
+
16
+ USER_PWD = Dir.pwd
17
+
18
+ class << self
19
+ attr_writer :logger
20
+ attr_writer :user_pwd
21
+
22
+ def logger
23
+ @logger ||= Logger.new($stderr)
24
+ end
25
+
26
+ def user_pwd
27
+ @user_pwd ||= USER_PWD
28
+ end
29
+ end
30
+
12
31
  end
data/lib/brocket/base.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require "brocket"
2
2
 
3
- require 'logger'
4
-
5
3
  require 'thor'
6
4
  require 'logger_pipe'
7
5
 
@@ -18,16 +16,12 @@ module BRocket
18
16
  task
19
17
  end
20
18
 
21
- def opts
22
- @opts ||= options || {}
23
- end
24
-
25
19
  def dryrun?
26
- opts[:dryrun]
20
+ options[:dryrun]
27
21
  end
28
22
 
29
23
  def verbose?
30
- opts[:verbose]
24
+ options[:verbose]
31
25
  end
32
26
 
33
27
  def verbose(msg)
@@ -46,11 +40,11 @@ module BRocket
46
40
  end
47
41
 
48
42
  def logger
49
- @logger ||= Logger.new($stderr)
43
+ BRocket.logger
50
44
  end
51
45
 
52
- def sh(cmd, &block)
53
- LoggerPipe.run(logger, cmd + " 2>&1", dry_run: dryrun?)
46
+ def sh(cmd)
47
+ LoggerPipe.run(logger, cmd, dry_run: dryrun?)
54
48
  end
55
49
 
56
50
  end
data/lib/brocket/cli.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require "brocket"
2
2
 
3
3
  module BRocket
4
- class Cli < Base
4
+ class Cli < Configurable
5
5
 
6
6
  desc "init", "initialize current directory"
7
7
  def init
@@ -0,0 +1,55 @@
1
+ require "brocket"
2
+
3
+ require 'yaml'
4
+ require 'thor'
5
+ require 'pathname'
6
+
7
+ module BRocket
8
+ class Configurable < Base
9
+ CONFIG_LINE_SEP = "[config]".freeze
10
+ CONFIG_LINE_HEADER = /\A\#\s*#{Regexp.escape(CONFIG_LINE_SEP)}\s?/.freeze
11
+
12
+ class_option :dockerfile, aliases: '-f', type: :string, default: "Dockerfile", desc: "Dockerfile to build"
13
+
14
+ no_commands do
15
+ def config_image_name
16
+ img_name = (config_hash['IMAGE_NAME'] || '').strip
17
+ error "No IMAGE_NAME found in Dockerfile. Please add `# #{CONFIG_LINE_SEP} IMAGE_NAME: [IMAGE NAME on DockerHub]` in Dockerfile" if img_name.empty?
18
+ img_name
19
+ end
20
+
21
+ def working_dir
22
+ dir = config_hash['WORKING_DIR'] || '.'
23
+ File.expand_path(dir, File.dirname(config_filepath))
24
+ end
25
+
26
+ def config_hash
27
+ @config_hash ||= load_config_hash
28
+ end
29
+
30
+ def load_config_hash
31
+ content = read_config_file
32
+ lines = content.lines.select{|line| line =~ CONFIG_LINE_HEADER}.
33
+ map{|line| line.sub(CONFIG_LINE_HEADER, "")}
34
+ return (YAML.load(lines.join("\n")) || {})
35
+ end
36
+
37
+ def config_filepath
38
+ @config_filepath ||= File.expand_path(options[:dockerfile] || './Dockerfile', BRocket.user_pwd)
39
+ end
40
+
41
+ def config_relpath
42
+ config_p = Pathname.new(config_filepath)
43
+ base_p = Pathname.new(working_dir)
44
+ config_p.relative_path_from(base_p).to_s
45
+ end
46
+
47
+ def read_config_file
48
+ unless File.readable?(config_filepath)
49
+ error "File not found: #{config_filepath}"
50
+ end
51
+ File.read(config_filepath)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -4,9 +4,7 @@ require 'yaml'
4
4
  require 'thor'
5
5
 
6
6
  module BRocket
7
- class Docker < Base
8
- CONFIG_LINE_SEP = "[config]".freeze
9
- CONFIG_LINE_HEADER = /\A\#\s*#{Regexp.escape(CONFIG_LINE_SEP)}\s?/.freeze
7
+ class Docker < Configurable
10
8
 
11
9
  desc "config", "show configurations in Dockerfile"
12
10
  def config
@@ -17,17 +15,18 @@ module BRocket
17
15
  def build
18
16
  info("[docker build] starting")
19
17
  c = config_hash
20
- img_name = config_image_name(c)
21
- begin
22
- execute(c['BEFORE_BUILD'])
23
- version = sub(VersionFile).current
24
- execute("docker build -t #{img_name}:#{version} .")
25
- execute(c['ON_BUILD_COMPLETE'])
26
- rescue
27
- execute(c['ON_BUILD_ERROR'])
28
- raise
29
- ensure
30
- execute(c['AFTER_BUILD'])
18
+ Dir.chdir(working_dir) do
19
+ begin
20
+ execute(c['BEFORE_BUILD'])
21
+ cmd = build_build_command
22
+ execute(cmd)
23
+ execute(c['ON_BUILD_COMPLETE'])
24
+ rescue
25
+ execute(c['ON_BUILD_ERROR'])
26
+ raise
27
+ ensure
28
+ execute(c['AFTER_BUILD'])
29
+ end
31
30
  end
32
31
  success("[docker build] OK")
33
32
  end
@@ -35,40 +34,53 @@ module BRocket
35
34
  desc "push", "push docker image to docker hub"
36
35
  def push
37
36
  info("[docker push] starting")
38
- c = config_hash
39
- img_name = config_image_name(c)
40
- version = sub(VersionFile).current
41
- cmd = "docker push #{img_name}:#{version}"
37
+ cmd = build_push_command
42
38
  sh(cmd)
43
39
  success("[docker push] OK")
44
40
  end
45
41
 
46
42
  desc "call_before_build", "call BEFORE_BUILD callback manually"
47
43
  def call_before_build
48
- execute(config_hash['BEFORE_BUILD'])
44
+ Dir.chdir(working_dir) do
45
+ execute(config_hash['BEFORE_BUILD'])
46
+ end
49
47
  end
50
48
 
51
49
  desc "call_after_build", "call AFTER_BUILD callback manually"
52
50
  def call_after_build
53
- execute(config_hash['AFTER_BUILD'])
51
+ Dir.chdir(working_dir) do
52
+ execute(config_hash['AFTER_BUILD'])
53
+ end
54
54
  end
55
55
 
56
56
  no_commands do
57
- def config_image_name(c)
58
- img_name = (c['IMAGE_NAME'] || '').strip
59
- error "No IMAGE_NAME found in Dockerfile. Please add `# #{CONFIG_LINE_SEP} IMAGE_NAME: [IMAGE NAME on DockerHub]` in Dockerfile" if img_name.empty?
60
- img_name
61
- end
62
-
63
- def config_hash
64
- content = read_config_file
65
- lines = content.lines.select{|line| line =~ CONFIG_LINE_HEADER}.
66
- map{|line| line.sub(CONFIG_LINE_HEADER, "")}
67
- return (YAML.load(lines.join("\n")) || {})
57
+ def build_build_command
58
+ img_name = config_image_name
59
+ version = sub(VersionFile).current
60
+ cmd = "docker build -t #{img_name}:#{version}"
61
+ if options[:dockerfile]
62
+ fp = config_relpath
63
+ unless fp == "Dockerfile"
64
+ cmd << ' -f ' << config_relpath
65
+ end
66
+ end
67
+ cmd << ' .'
68
+ return cmd
68
69
  end
69
70
 
70
- def read_config_file
71
- File.read("Dockerfile")
71
+ def build_push_command
72
+ registry = config_hash['DOCKER_PUSH_REGISTRY']
73
+ username = config_hash['DOCKER_PUSH_USERNAME']
74
+ img_name = [registry, username, config_image_name].compact.join('/')
75
+ version = sub(VersionFile).current
76
+ build_cmd = config_hash['DOCKER_PUSH_COMMAND'] || 'docker push'
77
+ cmd = [
78
+ (registry || username) ?
79
+ "docker tag -f #{config_image_name}:#{version} #{img_name}:#{version}" :
80
+ nil,
81
+ "#{build_cmd} #{img_name}:#{version}",
82
+ ].compact.join(' && ')
83
+ return cmd
72
84
  end
73
85
 
74
86
  def execute(commands)
data/lib/brocket/git.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  require "brocket"
2
2
 
3
+ require 'logger_pipe/runner'
4
+
3
5
  module BRocket
4
- class Git < Base
6
+ class Git < Configurable
5
7
 
6
8
  desc "guard_clean", "Raise error if some difference exists."
7
9
  def guard_clean
8
- clean? && committed? or error("There are files that need to be committed first. Run `git status`")
9
- success("[git guard_clean] OK")
10
+ (clean? && committed?) ?
11
+ success("[git guard_clean] OK") :
12
+ error("There are files that need to be committed first. Run `git status`")
10
13
  end
11
14
 
12
15
  desc "push", "push commit and tag it"
@@ -18,16 +21,23 @@ module BRocket
18
21
 
19
22
  no_commands do
20
23
 
21
- def commit(filepath, msg)
24
+ # This method is called from BRocket::VersionFile#bump_on
25
+ def add_and_commit(filepath, msg)
22
26
  sh("git add #{filepath} && git commit -m \"#{msg}\"")
23
27
  end
24
28
 
25
29
  def clean?
26
- sh_with_code("git diff --exit-code")[1] == 0
30
+ sh("git diff --exit-code")
31
+ return true
32
+ rescue LoggerPipe::Failure
33
+ return false
27
34
  end
28
35
 
29
36
  def committed?
30
- sh_with_code("git diff-index --quiet --cached HEAD")[1] == 0
37
+ sh("git diff-index --quiet --cached HEAD")
38
+ return true
39
+ rescue LoggerPipe::Failure
40
+ return false
31
41
  end
32
42
 
33
43
  def tag_version
@@ -36,20 +46,18 @@ module BRocket
36
46
  yield if block_given?
37
47
  rescue
38
48
  $stderr.puts "Untagging #{version_tag} due to error."
39
- sh_with_code "git tag -d #{version_tag}"
49
+ sh "git tag -d #{version_tag}"
40
50
  raise
41
51
  end
42
52
 
43
53
  def git_push
44
54
  perform_git_push
45
- perform_git_push ' --tags'
55
+ perform_git_push '--tags'
46
56
  $stdout.puts "Pushed git commits and tags."
47
57
  end
48
58
 
49
- def perform_git_push(options = '')
50
- cmd = "git push #{options}"
51
- out, code = sh_with_code(cmd)
52
- error "Couldn't git push. `#{cmd}' failed with the following output:\n\n#{out}\n" unless code == 0
59
+ def perform_git_push(options = nil)
60
+ sh(['git push', options].compact.join(' ' ))
53
61
  end
54
62
 
55
63
  def already_tagged?
@@ -1,3 +1,3 @@
1
1
  module BRocket
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require "brocket"
2
2
 
3
3
  module BRocket
4
- class VersionFile < Base
4
+ class VersionFile < Configurable
5
5
  FILENAME = "VERSION".freeze
6
6
  INITIAL_VERSION = "0.0.1".freeze
7
7
 
@@ -32,14 +32,22 @@ module BRocket
32
32
 
33
33
  no_commands do
34
34
  def filepath
35
- FILENAME
35
+ File.expand_path(config_hash['VERSION_FILE'] || 'VERSION', File.dirname(config_filepath))
36
36
  end
37
37
 
38
38
  def read_file
39
- if File.readable?(filepath)
40
- File.read(filepath).strip
39
+ vs = config_hash['VERSION_SCRIPT']
40
+ if vs
41
+ Dir.chdir(File.dirname(config_filepath)) do
42
+ res = `#{vs}`.strip
43
+ return $? == 0 ? res : error("Error on VERSION_SCRIPT: #{vs}")
44
+ end
41
45
  else
42
- raise BuildError, "File not found #{filepath}. You can run `#{$0} init`"
46
+ if File.readable?(filepath)
47
+ File.read(filepath).strip
48
+ else
49
+ error "File not found #{filepath}. You can run `#{$0} init`"
50
+ end
43
51
  end
44
52
  end
45
53
  alias_method :current, :read_file
@@ -62,7 +70,7 @@ module BRocket
62
70
  ver = parts.join(".")
63
71
  ver << "-" << suffix if suffix
64
72
  write_file(ver)
65
- sub(Git).commit(filepath, "bump up #{pos.to_s} version: #{ver}")
73
+ sub(Git).add_and_commit(filepath, "bump up #{pos.to_s} version: #{ver}")
66
74
  success("[git #{pos.to_s}] #{ver}")
67
75
  ver
68
76
  end