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

View File

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

View File

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

View File

@ -3,7 +3,10 @@ require "file_utils"
require "yaml"
# A funko, built from its source metadata
class Funko
module Funko
extend self
class Funko
include YAML::Serializable
# Required, the name of the funko. Must be unique across FaaSO
@ -68,14 +71,19 @@ class Funko
# Get all the funkos docker knows about.
def self.from_docker : Array(Funko)
docker_api = Docr::API.new(Docr::Client.new)
names = Set(String).new
docker_api.images.list(all: true).select { |i|
next if i.@repo_tags.nil?
i.@repo_tags.as(Array(String)).each { |tag|
names << tag.split(":", 2)[0].split("-", 2)[1] if tag.starts_with?("faaso-")
names = [] of String
funko_containers = docker_api.containers.list(
all: true,
).each { |container|
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
# Setup the target directory `path` with all the files needed
@ -136,6 +144,14 @@ class Funko
)
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?
def running?
self.containers.any? { |container|
@ -208,4 +224,5 @@ class Funko
docker_api.containers.start(response.@id) if autostart
response.@id
end
end
end

View File

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