Refactored funko module

This commit is contained in:
Roberto Alsina 2024-07-02 19:08:14 -03:00
parent 2a6f64a53e
commit 6ca518ae32
5 changed files with 210 additions and 190 deletions

View File

@ -2,7 +2,9 @@ require "docr"
require "kemal" require "kemal"
require "../funko.cr" require "../funko.cr"
module Funkos module Funko
extend self
get "/funkos/" do |env| get "/funkos/" do |env|
funkos = Funko.from_docker funkos = Funko.from_docker
funkos.sort! { |a, b| a.name <=> b.name } funkos.sort! { |a, b| a.name <=> b.name }
@ -22,6 +24,7 @@ module Funkos
result << { result << {
"name" => funko.name, "name" => funko.name,
"state" => state, "state" => state,
"status" => funko.status,
} }
end end

View File

@ -1,4 +1,4 @@
require "./funkos.cr" require "./funko.cr"
require "./proxyconf.cr" require "./proxyconf.cr"
require "./secrets.cr" require "./secrets.cr"
require "compress/gzip" require "compress/gzip"

View File

@ -38,7 +38,7 @@ module Faaso
end end
def run def run
funkos = Funko.from_paths(@arguments) funkos = Funko::Funko.from_paths(@arguments)
local = @options.@bool["local"] local = @options.@bool["local"]
if local if local
@ -120,7 +120,7 @@ module Faaso
end end
def run def run
funkos = Funko.from_names(@arguments) funkos = Funko::Funko.from_names(@arguments)
funkos.each do |funko| funkos.each do |funko|
local = @options.@bool["local"] local = @options.@bool["local"]
@ -188,7 +188,7 @@ module Faaso
end end
def run def run
funkos = Funko.from_paths(@arguments) funkos = Funko::Funko.from_paths(@arguments)
funkos.each do |funko| funkos.each do |funko|
# Create temporary build location # Create temporary build location
dst_path = Path.new("export", funko.name) dst_path = Path.new("export", funko.name)

View File

@ -3,6 +3,9 @@ require "file_utils"
require "yaml" require "yaml"
# A funko, built from its source metadata # A funko, built from its source metadata
module Funko
extend self
class Funko class Funko
include YAML::Serializable include YAML::Serializable
@ -68,14 +71,19 @@ class Funko
# Get all the funkos docker knows about. # Get all the funkos docker knows about.
def self.from_docker : Array(Funko) def self.from_docker : Array(Funko)
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
names = Set(String).new names = [] of String
docker_api.images.list(all: true).select { |i| funko_containers = docker_api.containers.list(
next if i.@repo_tags.nil? all: true,
i.@repo_tags.as(Array(String)).each { |tag| ).each { |container|
names << tag.split(":", 2)[0].split("-", 2)[1] if tag.starts_with?("faaso-") p! container.@names
container.@names.each { |name|
names << name.split("-", 2)[1].lstrip("/") if name.starts_with?("/faaso-")
} }
} }
from_names(names.to_a)
pp! names
from_names(names.to_a.sort!)
end end
# Setup the target directory `path` with all the files needed # Setup the target directory `path` with all the files needed
@ -136,6 +144,14 @@ class Funko
) )
end end
# Descriptive status for the funko
def status
status = self.containers.map { |container|
container.@status
}.join(", ")
status.empty? ? "Stopped" : status
end
# Is any instance of this funko running? # Is any instance of this funko running?
def running? def running?
self.containers.any? { |container| self.containers.any? { |container|
@ -209,3 +225,4 @@ class Funko
response.@id response.@id
end end
end end
end

View File

@ -1,7 +1,7 @@
<%- result.each do |f| -%> <%- result.each do |f| -%>
<tr> <tr>
<td><%= f["name"] %></td> <td><%= f["name"] %></td>
<td><%= f["state"] %></td> <td><%= f["status"] %></td>
<td> <td>
<%- if f["state"] == "running" -%> <%- if f["state"] == "running" -%>
<button state="disabled">Start</button> <button state="disabled">Start</button>