percheron 0.4.0 → 0.5.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: ddfc86ead3b58968ec07cccf692a841643a45060
4
- data.tar.gz: a09225865990c275d6812e26ab9dd2496d1171f6
3
+ metadata.gz: c71cf3ea4de4f1a1d09949fcb09b55384594c707
4
+ data.tar.gz: 20c785069bff584a52244d90496e5f8038f22ee4
5
5
  SHA512:
6
- metadata.gz: a8a42293482b1d1ac38891aa44a2dbb1dd7d1d1000a1e2879ee78e07230e3af1d8f0747d8d8e9ab945f4534338362d561e6f2dfc53df2100311139c9934fb726
7
- data.tar.gz: c153bc4ca7252f604143e96d55a6e5d8f0fe7684cf14e893657e00ab150fd227d4e78ccbe88169a85832325492a57d958d185f40b5198ae6b1389c6f724c2a04
6
+ metadata.gz: 5dc2ec2e73b073c075b66980dc74dea1ccc65be4686c98c4f381e850717aa4bdfe4cd61541e0bb05f482efbf72b833c3c08ab85cbd7169326bc592803e5ad379
7
+ data.tar.gz: e8ec04f906e78f28bae614abd4fa2a2b16c795ba11e6fe8b23e89d197570dd86180182b6fd8b81b6accf8eb9017ec0efbf27aa9743461906db98f88ac0c3545e
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Percheron
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/percheron.svg)](http://badge.fury.io/rb/percheron)
4
- [![Travis CI](https://travis-ci.org/ashmckenzie/percheron.svg)](https://travis-ci.org/ashmckenzie/percheron)
4
+ [![Build Status](https://travis-ci.org/ashmckenzie/percheron.svg?branch=master)](https://travis-ci.org/ashmckenzie/percheron)
5
5
  [![Code Climate GPA](https://codeclimate.com/github/ashmckenzie/percheron/badges/gpa.svg)](https://codeclimate.com/github/ashmckenzie/percheron)
6
6
  [![Test Coverage](https://codeclimate.com/github/ashmckenzie/percheron/badges/coverage.svg)](https://codeclimate.com/github/ashmckenzie/percheron)
7
7
  [![Dependency Status](https://gemnasium.com/ashmckenzie/percheron.svg)](https://gemnasium.com/ashmckenzie/percheron)
data/bin/percheron CHANGED
@@ -16,10 +16,12 @@ if ENV['VERBOSE'] == 'true'
16
16
  logger_level = Logger::INFO
17
17
  end
18
18
 
19
- if ENV['DEBUG'] == 'true'
19
+ if ENV['DEBUG'] == 'true' || ENV['DOCKER_DEBUG'] == 'true'
20
20
  require 'pry-byebug'
21
21
  require 'awesome_print'
22
22
  logger_level = Logger::DEBUG
23
+
24
+ Docker.logger = $logger if ENV['DOCKER_DEBUG'] == 'true'
23
25
  end
24
26
 
25
27
  $logger.level = logger_level
@@ -0,0 +1,21 @@
1
+ module Percheron
2
+ module Container
3
+ module Actions
4
+ module Base
5
+
6
+ private
7
+
8
+ def base_dir
9
+ container.dockerfile.dirname.to_s
10
+ end
11
+
12
+ def in_working_directory(new_dir)
13
+ old_dir = Dir.pwd
14
+ Dir.chdir(new_dir)
15
+ yield
16
+ Dir.chdir(old_dir)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,23 +1,17 @@
1
- require 'open3'
2
-
3
1
  module Percheron
4
2
  module Container
5
3
  module Actions
6
4
  class Build
7
5
 
6
+ include Base
7
+
8
8
  def initialize(container, nocache: false)
9
9
  @container = container
10
10
  @nocache = nocache
11
11
  end
12
12
 
13
13
  def execute!
14
- in_working_directory(base_dir) do
15
- execute_pre_build_scripts!
16
- $logger.debug "Building '#{container.image}'"
17
- Docker::Image.build_from_dir(base_dir, build_opts) do |out|
18
- $logger.debug '%s' % [ out.strip ]
19
- end
20
- end
14
+ build!
21
15
  end
22
16
 
23
17
  private
@@ -27,28 +21,28 @@ module Percheron
27
21
  def build_opts
28
22
  {
29
23
  'dockerfile' => container.dockerfile.basename.to_s,
30
- 't' => container.image,
24
+ 't' => container.image_name,
31
25
  'forcerm' => true,
32
26
  'nocache' => nocache
33
27
  }
34
28
  end
35
29
 
36
- def base_dir
37
- container.dockerfile.dirname.to_s
38
- end
30
+ def build!
31
+ in_working_directory(base_dir) do
32
+ execute_pre_build_scripts!
39
33
 
40
- def in_working_directory(new_dir)
41
- old_dir = Dir.pwd
42
- Dir.chdir(new_dir)
43
- yield
44
- Dir.chdir(old_dir)
34
+ $logger.debug "Building '#{container.image_name}'"
35
+ Docker::Image.build_from_dir(base_dir, build_opts) do |out|
36
+ $logger.debug '%s' % [ out.strip ]
37
+ end
38
+ end
45
39
  end
46
40
 
47
41
  def execute_pre_build_scripts!
48
42
  container.pre_build_scripts.each do |script|
49
43
  in_working_directory(base_dir) do
50
44
  command = '/bin/bash -x %s 2>&1' % Pathname.new(File.expand_path(script))
51
- $logger.debug "Executing '#{command}' for '#{container.name}' container"
45
+ $logger.debug "Executing PRE build '#{command}' for '#{container.name}' container"
52
46
  Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
53
47
  while line = stdout_err.gets
54
48
  $logger.debug line.strip
@@ -3,18 +3,16 @@ module Percheron
3
3
  module Actions
4
4
  class Create
5
5
 
6
+ include Base
7
+
6
8
  def initialize(container)
7
9
  @container = container
8
10
  end
9
11
 
10
12
  def execute!
11
- unless image_exists?
12
- $logger.debug "Creating '#{container.image}' image"
13
- Container::Actions::Build.new(container).execute!
14
- end
15
-
16
- $logger.debug "Creating '#{container.name}' container"
17
- Docker::Container.create(create_opts)
13
+ build!
14
+ insert_post_start_scripts!
15
+ create!
18
16
  end
19
17
 
20
18
  private
@@ -24,7 +22,7 @@ module Percheron
24
22
  def create_opts
25
23
  {
26
24
  'name' => container.name,
27
- 'Image' => container.image,
25
+ 'Image' => container.image_name,
28
26
  'Hostname' => container.name,
29
27
  'Env' => container.env,
30
28
  'ExposedPorts' => container.exposed_ports,
@@ -32,8 +30,24 @@ module Percheron
32
30
  }
33
31
  end
34
32
 
35
- def image_exists?
36
- Docker::Image.exist?(container.image)
33
+ def build!
34
+ unless container.image
35
+ $logger.debug "Creating '#{container.image_name}' image"
36
+ Container::Actions::Build.new(container).execute!
37
+ end
38
+ end
39
+
40
+ def create!
41
+ $logger.debug "Creating '#{container.name}' container"
42
+ Docker::Container.create(create_opts)
43
+ end
44
+
45
+ def insert_post_start_scripts!
46
+ container.post_start_scripts.each do |script|
47
+ file = Pathname.new(File.expand_path(script, base_dir))
48
+ new_image = container.image.insert_local('localPath' => file.to_s, 'outputPath' => "/tmp/#{file.basename}")
49
+ new_image.tag(repo: container.name, tag: container.version.to_s, force: true)
50
+ end
37
51
  end
38
52
 
39
53
  end
@@ -3,6 +3,8 @@ module Percheron
3
3
  module Actions
4
4
  class Recreate
5
5
 
6
+ include Base
7
+
6
8
  def initialize(container)
7
9
  @container = container
8
10
  end
@@ -24,7 +26,7 @@ module Percheron
24
26
  def create_opts
25
27
  {
26
28
  'name' => temporary_name,
27
- 'Image' => container.image,
29
+ 'Image' => container.image_name,
28
30
  'Hostname' => container.name,
29
31
  'Env' => container.env,
30
32
  'ExposedPorts' => container.exposed_ports,
@@ -36,7 +38,7 @@ module Percheron
36
38
  '%s_wip' % container.name
37
39
  end
38
40
 
39
- # FIXME
41
+ # FIXME: brittle
40
42
  def temporary_container_exists?
41
43
  temporary_container.info.empty?
42
44
  rescue Docker::Error::NotFoundError
@@ -44,8 +46,8 @@ module Percheron
44
46
  end
45
47
 
46
48
  def create_image!
47
- unless image_exists?
48
- $logger.debug "Creating '#{container.image}' image"
49
+ unless container.image
50
+ $logger.debug "Creating '#{container.image_name}' image"
49
51
  Container::Actions::Build.new(container).execute!
50
52
  end
51
53
  end
@@ -89,7 +91,7 @@ module Percheron
89
91
  end
90
92
 
91
93
  def rename_wip_to_current!
92
- # FIXME
94
+ # FIXME: brittle
93
95
  $logger.debug "Renaming '#{temporary_name}' container to '#{container.name}'"
94
96
  Docker::Container.get(temporary_name).rename(container.name)
95
97
  end
@@ -98,10 +100,6 @@ module Percheron
98
100
  container.start!
99
101
  end
100
102
 
101
- def image_exists?
102
- Docker::Image.exist?(container.image)
103
- end
104
-
105
103
  def container_was_running?
106
104
  @container_was_running
107
105
  end
@@ -3,14 +3,16 @@ module Percheron
3
3
  module Actions
4
4
  class Start
5
5
 
6
+ include Base
7
+
6
8
  def initialize(container)
7
9
  @container = container
8
10
  end
9
11
 
10
12
  def execute!
11
13
  if container.exists?
12
- $logger.debug "Starting '#{container.name}'"
13
- container.docker_container.start!(start_opts)
14
+ start!
15
+ execute_post_start_scripts!
14
16
  else
15
17
  raise Errors::ContainerDoesNotExist.new
16
18
  end
@@ -34,6 +36,22 @@ module Percheron
34
36
  }
35
37
  end
36
38
 
39
+ def start!
40
+ $logger.debug "Starting '#{container.name}'"
41
+ container.docker_container.start!(start_opts)
42
+ end
43
+
44
+ def execute_post_start_scripts!
45
+ container.post_start_scripts.each do |script|
46
+ in_working_directory(base_dir) do
47
+ file = Pathname.new(File.expand_path(script, base_dir))
48
+ command = '/bin/bash -x /tmp/%s 2>&1' % file.basename
49
+ $logger.debug "Executing POST create '#{command}' for '#{container.name}' container"
50
+ container.docker_container.exec(command.split(' '))
51
+ end
52
+ end
53
+ end
54
+
37
55
  end
38
56
  end
39
57
  end
@@ -3,24 +3,30 @@ module Percheron
3
3
  module Actions
4
4
  class Stop
5
5
 
6
+ include Base
7
+
6
8
  def initialize(container)
7
9
  @container = container
8
10
  end
9
11
 
10
12
  def execute!
11
- if container.running?
12
- $logger.debug "Stopping '#{container.name}'"
13
- container.docker_container.stop!
14
- else
15
- $logger.debug "Not stopping '#{container.name}' container as it's not running"
16
- raise Errors::ContainerNotRunning.new
17
- end
13
+ stop!
18
14
  end
19
15
 
20
16
  private
21
17
 
22
18
  attr_reader :container
23
19
 
20
+ def stop!
21
+ if container.running?
22
+ $logger.debug "Stopping '#{container.name}'"
23
+ container.docker_container.stop!
24
+ else
25
+ $logger.debug "Not stopping '#{container.name}' container as it's not running"
26
+ raise Errors::ContainerNotRunning.new
27
+ end
28
+ end
29
+
24
30
  end
25
31
  end
26
32
  end
@@ -1,3 +1,6 @@
1
+ require 'open3'
2
+
3
+ require 'percheron/container/actions/base'
1
4
  require 'percheron/container/actions/stop'
2
5
  require 'percheron/container/actions/start'
3
6
  require 'percheron/container/actions/create'
@@ -8,7 +8,7 @@ module Percheron
8
8
  def_delegators :container_config, :name
9
9
 
10
10
  def_config_item_with_default :container_config, false, :auto_recreate
11
- def_config_item_with_default :container_config, [], :env, :ports, :volumes, :dependant_container_names, :pre_build_scripts
11
+ def_config_item_with_default :container_config, [], :env, :ports, :volumes, :dependant_container_names, :pre_build_scripts, :post_start_scripts
12
12
 
13
13
  alias_method :auto_recreate?, :auto_recreate
14
14
 
@@ -25,6 +25,12 @@ module Percheron
25
25
  end
26
26
 
27
27
  def image
28
+ Docker::Image.get(image_name)
29
+ rescue Docker::Error::NotFoundError
30
+ nil
31
+ end
32
+
33
+ def image_name
28
34
  '%s:%s' % [ name, version.to_s ]
29
35
  end
30
36
 
@@ -1,3 +1,3 @@
1
1
  module Percheron
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: percheron
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ash McKenzie
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-06 00:00:00.000000000 Z
11
+ date: 2015-03-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: clamp
@@ -211,6 +211,7 @@ files:
211
211
  - lib/percheron/config_delegator.rb
212
212
  - lib/percheron/container.rb
213
213
  - lib/percheron/container/actions.rb
214
+ - lib/percheron/container/actions/base.rb
214
215
  - lib/percheron/container/actions/build.rb
215
216
  - lib/percheron/container/actions/create.rb
216
217
  - lib/percheron/container/actions/recreate.rb