Class: Guard::Docker

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/docker.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Docker

Initializes a Guard plugin. Don’t do any work here, especially as Guard plugins get initialized even if they are not in an active group!

Parameters:

  • options (Hash) (defaults to: {})

    the custom Guard plugin options

Options Hash (options):

  • watchers (Array<Guard::Watcher>)

    the Guard plugin file watchers

  • group (Symbol)

    the group this Guard plugin belongs to

  • any_return (Boolean)

    allow any object to be returned from a watcher



14
15
16
17
18
19
20
21
22
# File 'lib/guard/docker.rb', line 14

def initialize(options = {})
  
  @image = options.fetch(:image)
  @host_port = options.fetch(:host_port)
  @container_port = options.fetch(:container_port)
  @env_vars = options.fetch(:env_vars)

  super
end

Instance Method Details

#failed(message) ⇒ Object



115
116
117
# File 'lib/guard/docker.rb', line 115

def failed message
  notify message, :image => :failed
end

#notify(message, options = {}) ⇒ Object



119
120
121
# File 'lib/guard/docker.rb', line 119

def notify(message, options = {})
  Notifier.notify(message, options)
end

#pending(message) ⇒ Object



107
108
109
# File 'lib/guard/docker.rb', line 107

def pending message
  notify message, :image => :pending
end

#reloadObject

Called when ‘reload|r|z + enter` is pressed. This method should be mainly used for “reload” (really!) actions like reloading passenger/spork/bundler/…

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when reload has failed



68
69
# File 'lib/guard/docker.rb', line 68

def reload
end

#run_allObject

Called when just ‘enter` is pressed This method should be principally used for long action like running all specs/tests/…

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when run_all has failed



77
78
# File 'lib/guard/docker.rb', line 77

def run_all
end

#run_on_additions(paths) ⇒ Object

Called on file(s) additions that the Guard plugin watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when run_on_additions has failed



86
87
# File 'lib/guard/docker.rb', line 86

def run_on_additions(paths)
end

#run_on_modifications(paths) ⇒ Object

Called on file(s) modifications that the Guard plugin watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when run_on_modifications has failed



95
96
# File 'lib/guard/docker.rb', line 95

def run_on_modifications(paths)
end

#run_on_removals(paths) ⇒ Object

Called on file(s) removals that the Guard plugin watches.

Parameters:

  • paths (Array<String>)

    the changes files or paths

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when run_on_removals has failed



104
105
# File 'lib/guard/docker.rb', line 104

def run_on_removals(paths)
end

#startObject

Called once when Guard starts. Please override initialize method to init stuff.

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when start has failed



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/guard/docker.rb', line 29

def start
  failed 'You must specify an image' unless @image
  return false unless @image

  system("docker stop guard-#{@image}")

  cmd = []
  cmd << 'docker run --rm'
  cmd << "-p #{@host_port}:#{@container_port}" if @host_port && @container_port

  @env_vars.each do |key, value|
    cmd << "-e #{key}=#{value}"
  end if @env_vars

  cmd << "--name=guard-#{@image}"
  cmd << @image

  spawn(cmd.join(' '))

  success "#{@image} is running"
end

#stopObject

Called when ‘stop|quit|exit|s|q|e + enter` is pressed (when Guard quits).

Returns:

  • (Object)

    the task result

Raises:

  • (:task_has_failed)

    when stop has failed



56
57
58
59
60
# File 'lib/guard/docker.rb', line 56

def stop
  cmd = []
  cmd << "docker stop guard-#{@image}"
  system(cmd.join(' '))
end

#success(message) ⇒ Object



111
112
113
# File 'lib/guard/docker.rb', line 111

def success message
  notify message, :image => :success
end