docker-tools/ofelias.rb

47 lines
1.2 KiB
Ruby
Raw Normal View History

2021-11-02 13:38:12 +00:00
#!/usr/bin/env ruby
require 'socket'
require 'json'
require 'terminal-table'
def fetch_data
socket = UNIXSocket.new("/var/run/docker.sock")
request = "GET /v1.24/containers/json?all=1 HTTP/1.0\r\n\r\n"
socket.write(request)
response = ""
loop do
break if socket.eof?
line = socket.gets
break if line=="\r\n"
end
until socket.eof?
line = socket.gets
response += line
end
return JSON.parse(response, symbolize_names: true)
end
rows = []
data = fetch_data
data.each do |d|
next unless labels = d[:Labels]
name = d[:Names][0][1..-1]
next unless labels[:"ofelia.enabled"]
tasks = labels.keys.map(&:to_s).map{|s| s.scan(/\Aofelia\.job-([a-z]+)\.([^.]*)\./).first}.uniq.compact
next unless tasks.count>0
tasks.each do |task|
type, taskname = *task
schedule = labels[:"ofelia.job-#{type}.#{taskname}.schedule"]
command = labels[:"ofelia.job-#{type}.#{taskname}.command"]
rows << [name, type, taskname, schedule, command]
end
end
exit 0 unless rows.count>0
puts Terminal::Table.new(rows: rows, headings: ["Container", "Type", "Task", "Schedule", "Command"])