sidedock 0.0.1 → 1.0.1

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: 59b6425b145656fef62e0ee9e5b0956685f45fda
4
- data.tar.gz: 4a169cd4c4d91bb6b4c524f3da33e48264bd8d35
3
+ metadata.gz: 92788013f2d5b7639cfc7019d1a31ca8ae3530cf
4
+ data.tar.gz: e8a6675dac27fb06acca0fb3cd7e8815e62d1094
5
5
  SHA512:
6
- metadata.gz: 42fa733a06553078161cec106ed47f46cee448284900fec04e396f9d4943390c184e2b8933aeae55692ca1998af30f21d9c53d7cf73960d781f26876bf301942
7
- data.tar.gz: fc73b043294323a1e947aed92310f902b1bf7760c4567ef00dd2763d29eff6cad280e0fdd4f462dcdea2e9ae045dda3e88c293b052d762c48ebb17bcef9e37d3
6
+ metadata.gz: 928fd851e3813ca8548783e60ea911c85966541ddde878e3a5ef19ead5a2e4b91534f22e15877d6aaa8596142b866b3fa664c6ccaa47211ea5863fafe094b89b
7
+ data.tar.gz: 4f8ccc2cfe822ff9e7464ec1a04efa0f0b46232656d791fd8c02d8656f36dcb165d0e971d67adc9b912d49d1899d1b48e6e8207ce0b8d0b375d0b9c8382ad535
@@ -0,0 +1,11 @@
1
+ module Sidedock
2
+ class Base
3
+ def machine
4
+ self.class.machine
5
+ end
6
+
7
+ def self.machine
8
+ @@machine ||= Machine.new 'sidedock'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Sidedock
2
+ class DockerCLI < CLI
3
+ self.program = 'docker'
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Sidedock
2
+ class MachineCLI < CLI
3
+ self.program = 'docker-machine'
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'tty/command'
2
+
3
+ module Sidedock
4
+ class CLI
5
+ class_attribute :program
6
+
7
+ def initialize(default_options = '')
8
+ @default_options = default_options
9
+ @cmd = TTY::Command.new
10
+ end
11
+
12
+ def execute(command)
13
+ full_command = "#{program} #{@default_options} #{command}"
14
+ stdout, stderr = @cmd.run full_command
15
+ raise "`#{command}` failed" if stderr.present?
16
+ stdout.strip
17
+ end
18
+ end
19
+ end
20
+
21
+ %w( docker machine ).each { |file| require "sidedock/cli/#{file}_cli" }
@@ -0,0 +1,54 @@
1
+ module Sidedock
2
+ class Container < Base
3
+ attr_accessor :ports, :id
4
+
5
+ def initialize(id, port_mapping: {})
6
+ @id = id
7
+ @port_mapping = port_mapping
8
+ end
9
+
10
+ def self.create(image_name_or_id, options)
11
+ raise "No image name given" unless image_name_or_id.present?
12
+ id = machine.execute "create -P #{image_name_or_id}"
13
+ new id, **options
14
+ end
15
+
16
+ def start
17
+ raise "already started" if running?
18
+ machine.execute "start #{@id}"
19
+ end
20
+
21
+ def stop
22
+ raise "not running" unless running?
23
+ machine.execute "stop #{@id}"
24
+ end
25
+
26
+ def remove
27
+ machine.execute "rm -f #{@id}"
28
+ end
29
+
30
+ def ports
31
+ Ports.new(@id, @port_mapping) if @port_mapping.present?
32
+ end
33
+
34
+ def running?
35
+ machine.execute('ps -q --no-trunc').include? @id
36
+ end
37
+
38
+ def bash(command)
39
+ machine.execute "exec -t #{@id} bash -c '#{command}'"
40
+ end
41
+
42
+ def ip
43
+ machine.ip
44
+ end
45
+
46
+ def self.using_image(image)
47
+ ps_output = machine.execute "ps -a -q --filter ancestor=#{image.id}"
48
+
49
+ ps_output.each_line.map do |id|
50
+ new id
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ module Sidedock
2
+ class Image < Base
3
+ attr_accessor :id
4
+
5
+ def initialize(id)
6
+ @id = id
7
+ end
8
+
9
+ def remove
10
+ remove_containers_using_it
11
+ machine.execute "rmi -f #{@id}"
12
+ end
13
+
14
+ def remove_containers_using_it
15
+ Container.using_image(self).each(&:remove)
16
+ end
17
+
18
+ def self.build(dockerfile_path)
19
+ built_id = machine.execute "build -q #{dockerfile_path}"
20
+ Rails.logger.info "Built #{dockerfile_path}, image ID: #{built_id}"
21
+ new built_id
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,57 @@
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 running?
19
+ docker_machine("ls -q --filter state=Running").include? @name
20
+ end
21
+
22
+ def start
23
+ docker_machine "start #{@name}"
24
+ end
25
+
26
+ def create
27
+ puts "Creating docker machine `#{@name}`. This may take a while."
28
+ docker_machine "create -d virtualbox #{@name}"
29
+ end
30
+
31
+ def exists?
32
+ docker_machine("ls -q").include? @name
33
+ end
34
+
35
+ def ip
36
+ docker_machine "ip #{@name}"
37
+ end
38
+
39
+ def execute(command)
40
+ ensure_running
41
+ cli.execute command
42
+ end
43
+
44
+ def docker_machine(command)
45
+ @machine_cli ||= MachineCLI.new
46
+ @machine_cli.execute command
47
+ end
48
+
49
+ def cli
50
+ @cli ||= DockerCLI.new options_to_let_docker_connect_to_it
51
+ end
52
+
53
+ def options_to_let_docker_connect_to_it
54
+ docker_machine("config #{@name}").gsub("\n", ' ')
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,13 @@
1
+ module Sidedock
2
+ class PortConfiguration < Base
3
+ attr_accessor :internal, :external, :protocol
4
+ CLI_FORMAT = /(?<internal>\d*)\/(?<protocol>tcp|udp) -> .*\:(?<external>\d*)/
5
+
6
+ def initialize(raw_configuration)
7
+ match = CLI_FORMAT.match raw_configuration
8
+ @internal = match[:internal].to_i
9
+ @external = match[:external].to_i
10
+ @protocol = match[:protocol]
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,43 @@
1
+ module Sidedock
2
+ class Ports < Base
3
+ include Enumerable
4
+
5
+ def initialize(machine_id, port_mapping)
6
+ @port_mapping = port_mapping
7
+ @machine_id = machine_id
8
+ define_port_accessors if @port_mapping.present?
9
+ end
10
+
11
+ def define_port_accessors
12
+ @port_mapping.each do |name, port_number|
13
+ raise "#{name} cannot be used as port mapping key" if respond_to? :name
14
+
15
+ port = find do |port|
16
+ port.internal == port_number
17
+ end
18
+
19
+ raise "Port #{port_number} not exposed by Dockerfile, " \
20
+ "change or remove it in the `port_mapping` option. "\
21
+ "Available: #{each.to_h}" unless port.present?
22
+
23
+ define_singleton_method name do
24
+ port.external
25
+ end
26
+ end
27
+ end
28
+
29
+ def each(&block)
30
+ ports.each &block
31
+ end
32
+
33
+ def ports
34
+ @ports ||= raw_configuration_lines.map do |raw_configuration|
35
+ PortConfiguration.new raw_configuration
36
+ end
37
+ end
38
+
39
+ def raw_configuration_lines
40
+ @raw_configuration_lines ||= machine.execute("port #{@machine_id}").strip.split("\n")
41
+ end
42
+ end
43
+ end
data/lib/sidedock.rb ADDED
@@ -0,0 +1,39 @@
1
+ %w( cli machine base image container ports port_configuration
2
+ ).each { |file| require "sidedock/#{file}" }
3
+
4
+ module Sidedock
5
+ class << self
6
+ def with_docker_image(image, options = {}, &block)
7
+ container = Sidedock::Container.create image, options
8
+ container.start
9
+ yield container
10
+ container.stop
11
+ container.remove
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
37
+ end
38
+ end
39
+ 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: 0.0.1
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Raasveld
@@ -9,13 +9,37 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-06-23 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: tty-command
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
13
27
  description: Simplify working with Docker by running Dockerfiles from your Rails app
14
28
  email: ttimitri@gmail.com
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files: []
18
- files: []
32
+ files:
33
+ - lib/sidedock.rb
34
+ - lib/sidedock/base.rb
35
+ - lib/sidedock/cli.rb
36
+ - lib/sidedock/cli/docker_cli.rb
37
+ - lib/sidedock/cli/machine_cli.rb
38
+ - lib/sidedock/container.rb
39
+ - lib/sidedock/image.rb
40
+ - lib/sidedock/machine.rb
41
+ - lib/sidedock/port_configuration.rb
42
+ - lib/sidedock/ports.rb
19
43
  homepage: https://github.com/timraasveld/sidedock
20
44
  licenses:
21
45
  - MIT