sidedock 2.0.0.pre.beta1 → 2.0.0.pre.beta2

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: '021845a2dc38661f8b5a7869e3a7602b36fd1cc3'
4
- data.tar.gz: 7c93f4e8c3e97c89003cd4965a28388ce4ead82a
3
+ metadata.gz: 773d21903cb8ef30df471830bc42a5ecd7e37ee2
4
+ data.tar.gz: 4f716d782a3ca186190faccc4d36ea2aa41cb1b4
5
5
  SHA512:
6
- metadata.gz: 1bd0f7b8e8148f11dab56bf0db2c558e8cce6dd4366e1fa44fe1a2c4a13db5ee3f61064f4a2ceaa5723b4766fd3e7fd5b3118f61c76f2e5a7823731029f9c7f0
7
- data.tar.gz: 796aa3d93253a00a94cda04bc082a727ea9d8e4bddad26bd7cb1829a3df3016ac08f0a842dd486c2f12d2bfd316629df0ef89e6bbb537d35d7f55be840f39004
6
+ metadata.gz: 95735346161c7b6af544be9aac823467a8df1483e3b959f6d22d8a4b23539ceda62fb69376b226579f0c0d50337e31891cd5ac9c6983cb37323253d83a9e1008
7
+ data.tar.gz: 8a766c71866130d1ed196964782ba3fdc2e85dc0718a3c4e612ff70bdf2a3104b6e6a8c3a6e4f008c9bfd2d2c05b6f7b2d93fc7e025ff6a94768d8a4ddac584b
data/lib/sidedock/base.rb CHANGED
@@ -1,11 +1,13 @@
1
1
  module Sidedock
2
2
  class Base
3
- def machine
4
- self.class.machine
3
+ def cli
4
+ self.class.cli
5
5
  end
6
6
 
7
- def self.machine
8
- @@machine ||= Machine.new 'sidedock'
7
+ def self.cli
8
+ args = '--debug' if Sidedock.configuration.debug
9
+ args ||= ''
10
+ @@cli ||= DockerCLI.new args
9
11
  end
10
12
  end
11
13
  end
@@ -1,5 +1,7 @@
1
1
  module Sidedock
2
2
  class DockerCLI < CLI
3
3
  self.program = 'docker'
4
+
5
+ command_wrapper :create
4
6
  end
5
7
  end
data/lib/sidedock/cli.rb CHANGED
@@ -1,14 +1,16 @@
1
1
  require 'tty/command'
2
+ require 'uber/inheritable_attr'
2
3
 
3
4
  module Sidedock
5
+ # Base class for command line utilities used by sidedock.
4
6
  class CLI
5
- class_attribute :program
7
+ extend Uber::InheritableAttr
8
+ inheritable_attr :program # System command name
6
9
 
7
10
  def initialize(default_options = '')
8
11
  @default_options = default_options
9
12
  end
10
13
 
11
-
12
14
  def execute(command)
13
15
  full_command = "#{program} #{@default_options} #{command}"
14
16
  stdout, stderr = command_runner.run full_command
@@ -16,6 +18,25 @@ module Sidedock
16
18
  stdout.strip
17
19
  end
18
20
 
21
+ protected
22
+ # Create wrapper for a CLI command of the program.
23
+ # Mainly useful for testing purposes because you want to mock output of a given command
24
+ def self.command_wrapper(*commands)
25
+ commands.each do |command|
26
+ raise "#{command} cannot be used as command wrapper because method already exists for #{self}" if respond_to? command
27
+
28
+ define_method command do |args|
29
+ execute "#{command} #{args}"
30
+ end
31
+ end
32
+ end
33
+
34
+ private
35
+
36
+ def program
37
+ self.class.program
38
+ end
39
+
19
40
  def command_runner
20
41
  if Sidedock.configuration.debug
21
42
  debug_command_runner
@@ -34,4 +55,4 @@ module Sidedock
34
55
  end
35
56
  end
36
57
 
37
- %w( docker machine ).each { |file| require "sidedock/cli/#{file}_cli" }
58
+ %w( docker ).each { |file| require "sidedock/cli/#{file}_cli" }
@@ -7,7 +7,6 @@
7
7
  module Sidedock
8
8
  class << self
9
9
  attr_accessor :configuration
10
-
11
10
  end
12
11
 
13
12
  def self.configure
@@ -9,22 +9,22 @@ module Sidedock
9
9
 
10
10
  def self.create(image_name_or_id, options)
11
11
  raise "No image name given" unless image_name_or_id.present?
12
- id = machine.execute "create -P #{image_name_or_id}"
12
+ id = cli.create "-P #{image_name_or_id}"
13
13
  new id, **options
14
14
  end
15
15
 
16
16
  def start
17
17
  raise "already started" if running?
18
- machine.execute "start #{@id}"
18
+ cli.execute "start #{@id}"
19
19
  end
20
20
 
21
21
  def stop
22
22
  raise "not running" unless running?
23
- machine.execute "stop #{@id}"
23
+ cli.execute "stop #{@id}"
24
24
  end
25
25
 
26
26
  def remove
27
- machine.execute "rm -f #{@id}"
27
+ cli.execute "rm -f #{@id}"
28
28
  end
29
29
 
30
30
  def ports
@@ -32,19 +32,25 @@ module Sidedock
32
32
  end
33
33
 
34
34
  def running?
35
- machine.execute('ps -q --no-trunc').include? @id
35
+ cli.execute('ps -q --no-trunc').include? @id
36
36
  end
37
37
 
38
- def bash(command)
39
- machine.execute "exec -t #{@id} bash -c '#{command}'"
38
+ def sh(command)
39
+ cli.execute "exec -t #{@id} bash -c '#{command}'"
40
40
  end
41
41
 
42
42
  def ip
43
- machine.ip
43
+ cli.ip
44
+ end
45
+
46
+ def self.all
47
+ cli.execute('ps -q --no-trunc').split.map do |id|
48
+ new id
49
+ end
44
50
  end
45
51
 
46
52
  def self.using_image(image)
47
- ps_output = machine.execute "ps -a -q --filter ancestor=#{image.id}"
53
+ ps_output = cli.execute "ps -a -q --filter ancestor=#{image.id}"
48
54
 
49
55
  ps_output.each_line.map do |id|
50
56
  new id
@@ -0,0 +1,32 @@
1
+ module Sidedock
2
+ class Error < StandardError; end
3
+ class DockerfileNotFound < Error
4
+ def initialize(dockerfile)
5
+ @dockerfile = dockerfile
6
+ end
7
+
8
+ def message
9
+ "Expected Dockerfile to be defined in #{@dockerfile}"
10
+ end
11
+ end
12
+
13
+ class DirectoryNotFound < Error
14
+ def initialize(search_paths)
15
+ @search_paths = search_paths
16
+ end
17
+
18
+ def message
19
+ "Expected one of directories #{@search_paths} to exists"
20
+ end
21
+ end
22
+
23
+ class MethodInUse < Error
24
+ def initialize(klass, method)
25
+ @klass, @method = klass, method
26
+ end
27
+
28
+ def message
29
+ "Cannot define #{@klass}##{@method} because it is already implemented"
30
+ end
31
+ end
32
+ end
@@ -8,15 +8,15 @@ module Sidedock
8
8
 
9
9
  def remove
10
10
  remove_containers_using_it
11
- machine.execute "rmi -f #{@id}"
11
+ cli.execute "rmi -f #{@id}"
12
12
  end
13
13
 
14
14
  def remove_containers_using_it
15
15
  Container.using_image(self).each(&:remove)
16
16
  end
17
17
 
18
- def self.build(dockerfile_path)
19
- built_id = machine.execute "build -q #{dockerfile_path}"
18
+ def self.build(dockerfile_path, dockerfile = 'Dockerfile')
19
+ built_id = cli.execute "build -q #{dockerfile_path} --file #{dockerfile}"
20
20
  new built_id
21
21
  end
22
22
  end
@@ -9,5 +9,9 @@ module Sidedock
9
9
  @external = match[:external].to_i
10
10
  @protocol = match[:protocol]
11
11
  end
12
+
13
+ def to_h
14
+ { internal: @internal, external: @external, protocol: @protocol }
15
+ end
12
16
  end
13
17
  end
@@ -2,15 +2,15 @@ module Sidedock
2
2
  class Ports < Base
3
3
  include Enumerable
4
4
 
5
- def initialize(machine_id, port_mapping)
5
+ def initialize(container_id, port_mapping)
6
6
  @port_mapping = port_mapping
7
- @machine_id = machine_id
7
+ @container_id = container_id
8
8
  define_port_accessors if @port_mapping.present?
9
9
  end
10
10
 
11
11
  def define_port_accessors
12
12
  @port_mapping.each do |name, port_number|
13
- raise "#{name} cannot be used as port mapping key" if respond_to? :name
13
+ raise MethodInUse.new(self.class) if respond_to? name
14
14
 
15
15
  port = find do |port|
16
16
  port.internal == port_number
@@ -37,7 +37,7 @@ module Sidedock
37
37
  end
38
38
 
39
39
  def raw_configuration_lines
40
- @raw_configuration_lines ||= machine.execute("port #{@machine_id}").strip.split("\n")
40
+ @raw_configuration_lines ||= cli.execute("port #{@container_id}").strip.split("\n")
41
41
  end
42
42
  end
43
43
  end
@@ -0,0 +1,68 @@
1
+ require 'active_support/all'
2
+
3
+ module Sidedock
4
+ class Service
5
+ attr_reader :container
6
+
7
+ delegate :sh, :ports, to: :container
8
+
9
+ def self.use(options = {})
10
+ options[:port_mapping] ||= {}
11
+ options[:port_mapping].merge! port_mapping
12
+
13
+ Sidedock.with_docker_image image.id, options do |container|
14
+ yield new(container)
15
+ end
16
+
17
+ remove_image unless options[:keep_image]
18
+ end
19
+
20
+ protected
21
+
22
+ def self.cli_method(*commands)
23
+ commands.each do |command|
24
+ raise MethodInUse.new self, command if method_defined? command
25
+
26
+ define_method command do |args|
27
+ container.sh "#{command} #{args}"
28
+ end
29
+ end
30
+ end
31
+
32
+ cattr_accessor :port_mapping
33
+ self.port_mapping = {}
34
+
35
+ def self.port(name, internal_port)
36
+ port_mapping[name] = internal_port
37
+ end
38
+
39
+ def self.dockerfile_path
40
+ "#{directory}/#{dockerfile}"
41
+ end
42
+
43
+ def self.dockerfile
44
+ 'Dockerfile'
45
+ end
46
+
47
+ cattr_accessor :directory
48
+ def self.inherited(k)
49
+ k.directory = caller.first.match(/^([^:]+)\/.+\.rb/)[1]
50
+ end
51
+
52
+ def self.image
53
+ raise DockerfileNotFound.new(dockerfile_path) unless File.exist? dockerfile_path
54
+ @@image ||= Image.build directory, dockerfile_path
55
+ end
56
+
57
+ private
58
+
59
+ def initialize(container)
60
+ @container = container
61
+ end
62
+
63
+ def self.remove_image
64
+ image.remove
65
+ @@image = nil
66
+ end
67
+ end
68
+ end
data/lib/sidedock.rb CHANGED
@@ -1,4 +1,5 @@
1
- %w( configuration cli machine base image container ports port_configuration
1
+ require 'active_support/core_ext/object/blank'
2
+ %w( service configuration cli base image container ports port_configuration errors
2
3
  ).each { |file| require "sidedock/#{file}" }
3
4
 
4
5
  module Sidedock
@@ -8,32 +9,7 @@ module Sidedock
8
9
  container.start
9
10
  yield container
10
11
  container.stop
11
- container.remove unless options[:keep_image]
12
- end
13
-
14
- def with_dockerfile(name, options = {}, &block)
15
- image = Sidedock::Image.build path_to_dockerfile(name)
16
-
17
- with_docker_image image.id, options do |container|
18
- yield container
19
- end
20
-
21
- image.remove
22
- end
23
-
24
- def path_to_dockerfile(name)
25
- path = Rails.root.join base_directory, 'docker', name
26
- raise "Dockerfile path `#{path}` not found" unless File.exist? path
27
- path
28
- end
29
-
30
- def base_directory
31
- case ENV['RAILS_ENV']
32
- when 'test'
33
- 'spec'
34
- else
35
- 'app'
36
- end
12
+ container.remove
37
13
  end
38
14
  end
39
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidedock
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.beta1
4
+ version: 2.0.0.pre.beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Raasveld
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: uber
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.15
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.0.15
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
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'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -48,21 +76,21 @@ files:
48
76
  - lib/sidedock/base.rb
49
77
  - lib/sidedock/cli.rb
50
78
  - lib/sidedock/cli/docker_cli.rb
51
- - lib/sidedock/cli/machine_cli.rb
52
79
  - lib/sidedock/configuration.rb
53
80
  - lib/sidedock/container.rb
81
+ - lib/sidedock/errors.rb
54
82
  - lib/sidedock/image.rb
55
- - lib/sidedock/machine.rb
56
83
  - lib/sidedock/port_configuration.rb
57
84
  - lib/sidedock/ports.rb
85
+ - lib/sidedock/service.rb
58
86
  homepage: https://github.com/timraasveld/sidedock
59
87
  licenses:
60
88
  - MIT
61
89
  metadata: {}
62
90
  post_install_message: |-
63
- Make sure Docker is installed before installing sidedock.
91
+ Make sure Docker is installed before using sidedock.
64
92
  For example, try: brew cask install docker && open -a 'docker' (Mac)
65
- apt-get install -y docker (Debian / Ubuntu)
93
+ apt-get install -y docker (Debian / Ubuntu)
66
94
  rdoc_options: []
67
95
  require_paths:
68
96
  - lib
@@ -1,5 +0,0 @@
1
- module Sidedock
2
- class MachineCLI < CLI
3
- self.program = 'docker-machine'
4
- end
5
- end
@@ -1,63 +0,0 @@
1
- module Sidedock
2
- class Machine
3
- def initialize(name)
4
- @name = name
5
- end
6
-
7
- def ensure_exists
8
- create unless exists?
9
- raise "Could not create docker machine #{@name}" unless exists?
10
- end
11
-
12
- def ensure_running
13
- ensure_exists
14
- start unless running?
15
- raise "Could not start docker machine #{@name}" unless running?
16
- end
17
-
18
- def start
19
- docker_machine "start #{@name}"
20
- end
21
-
22
- def create
23
- puts "Creating docker machine `#{@name}`. This may take a while."
24
- docker_machine "create -d virtualbox #{@name}"
25
- end
26
-
27
- def running?
28
- return true if @running
29
- if docker_machine("ls -q --filter state=Running").include? @name
30
- @running = true
31
- end
32
- end
33
-
34
- def exists?
35
- return true if @exists
36
- if docker_machine("ls -q").include? @name
37
- @exists = true
38
- end
39
- end
40
-
41
- def ip
42
- docker_machine "ip #{@name}"
43
- end
44
-
45
- def execute(command)
46
- ensure_running
47
- cli.execute command
48
- end
49
-
50
- def docker_machine(command)
51
- @machine_cli ||= MachineCLI.new
52
- @machine_cli.execute command
53
- end
54
-
55
- def cli
56
- @cli ||= DockerCLI.new options_to_let_docker_connect_to_it
57
- end
58
-
59
- def options_to_let_docker_connect_to_it
60
- docker_machine("config #{@name}").gsub("\n", ' ')
61
- end
62
- end
63
- end