class RakeTasksDocker::Services

Public Class Methods

from_args(args) click to toggle source
# File lib/rake-tasks-docker/services.rb, line 5
def self.from_args(args)
  self.new(args[:services] ? args[:services].split(' ') : [])
end
new(services = []) click to toggle source
# File lib/rake-tasks-docker/services.rb, line 9
def initialize(services = [])
  @services = services
end
task(*task_args, &block) click to toggle source
# 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

Public Instance Methods

down() click to toggle source
# File lib/rake-tasks-docker/services.rb, line 72
def down
  Process.spawn 'docker-compose', 'down', '--volumes', '--rmi', 'local'
end
logs(tail_amount = 50) click to toggle source
# 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
refresh() click to toggle source
# 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
states() click to toggle source
# 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
status() click to toggle source
# File lib/rake-tasks-docker/services.rb, line 55
def status
  refresh unless @inspections
  status_from_states(states)
end
status_from_states(states) click to toggle source
# 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
stop() click to toggle source
# File lib/rake-tasks-docker/services.rb, line 68
def stop
  Process.spawn 'docker-compose', 'stop', *@services
end
up() click to toggle source
# File lib/rake-tasks-docker/services.rb, line 60
def up
  Process.spawn 'docker-compose', 'up', '-d', *@services
end

Protected Instance Methods

containers() click to toggle source
# File lib/rake-tasks-docker/services.rb, line 78
def containers
  `docker-compose ps -q #{@services.join(' ')}`.split("\n")
end