Class: RakeTasksDocker::Services

Inherits:
Object
  • Object
show all
Defined in:
lib/rake-tasks-docker/services.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services = []) ⇒ Services

Returns a new instance of Services.



9
10
11
# File 'lib/rake-tasks-docker/services.rb', line 9

def initialize(services = [])
  @services = services
end

Class Method Details

.from_args(args) ⇒ Object



5
6
7
# File 'lib/rake-tasks-docker/services.rb', line 5

def self.from_args(args)
  self.new(args[:services] ? args[:services].split(' ') : [])
end

.task(*task_args, &block) ⇒ Object



13
14
15
16
17
# File 'lib/rake-tasks-docker/services.rb', line 13

def self.task(*task_args, &block)
  Rake::Task.define_task *task_args do |task, args|
    block.call task, self.from_args(args)
  end
end

Instance Method Details

#downObject



72
73
74
# File 'lib/rake-tasks-docker/services.rb', line 72

def down
  Process.spawn 'docker-compose', 'down', '--volumes', '--rmi', 'local'
end

#logs(tail_amount = 50) ⇒ Object



64
65
66
# File 'lib/rake-tasks-docker/services.rb', line 64

def logs(tail_amount = 50)
  Process.spawn 'docker-compose', 'logs', '-f', '--tail=' + tail_amount.to_s, *@services
end

#refreshObject



19
20
21
22
23
24
# File 'lib/rake-tasks-docker/services.rb', line 19

def refresh
  @inspections = []
  containers.each do |container_ref|
    @inspections << JSON.parse(`docker inspect #{container_ref}`).first
  end
end

#statesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rake-tasks-docker/services.rb', line 26

def states
  states = {}
  @inspections.each do |inspection|
    if inspection['State']
      state = inspection['State']
      if state['Running'] && state['Health']
        states[inspection['Name']] = "#{state['Status']} (#{state['Health']['Status']})"
      elsif state['ExitCode'] > 0
        states[inspection['Name']] = "#{state['Status']} (non-zero exit code)"
      else
        states[inspection['Name']] = state['Status']
      end
    end
  end
  states
end

#statusObject



55
56
57
58
# File 'lib/rake-tasks-docker/services.rb', line 55

def status
  refresh unless @inspections
  status_from_states(states)
end

#status_from_states(states) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rake-tasks-docker/services.rb', line 43

def status_from_states(states)
  if states.empty?
    return 'not started'
  elsif !(states.values & ['exited (non-zero exit code)', 'running (unhealthy)', 'restarting', 'dead']).empty?
    return 'failed'
  elsif !(states.values & ['created', 'running (starting)']).empty?
    return 'starting'
  else
    return 'started'
  end
end

#stopObject



68
69
70
# File 'lib/rake-tasks-docker/services.rb', line 68

def stop
  Process.spawn 'docker-compose', 'stop', *@services
end

#upObject



60
61
62
# File 'lib/rake-tasks-docker/services.rb', line 60

def up
  Process.spawn 'docker-compose', 'up', '-d', *@services
end