Refactor behaviour into the Funko class

This commit is contained in:
Roberto Alsina 2024-06-29 23:54:14 -03:00
parent f526ccd326
commit c1308c3a36
4 changed files with 44 additions and 26 deletions

View File

@ -1,7 +1,9 @@
build: shard.yml src/* build: shard.yml $(wildcard src/**/*cr)
shards build shards build
proxy-image: build proxy-image: build
docker build . -t faaso-proxy --no-cache docker build . -t faaso-proxy --no-cache
start-proxy: start-proxy:
docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 -p 3000:3000 faas docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 -p 3000:3000 faaso-proxy
o-proxy
.PHONY: build proxy-image start-proxy

View File

@ -85,34 +85,20 @@ module Faaso
container_name = "faaso-#{funko.name}" container_name = "faaso-#{funko.name}"
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
# Get image history, sorted newer image first images = funko.image_history
begin if images.empty?
images = docker_api.images.history(
name: funko.name
).sort { |a, b| b.@created <=> a.@created }.map(&.@id)
rescue ex : Docr::Errors::DockerAPIError
puts "Error: no images available for #{funko.name}:latest" puts "Error: no images available for #{funko.name}:latest"
puts ex
next next
end end
latest_image = images[0] # sort list of funko containers newer image first
containers = funko.containers.sort { |a, b|
# Filter by name so only faaso-thisfunko are affected from now on (images.index(b.@image_id) || 9999) <=> (images.index(a.@image_id) || 9999)
# sorted newer image first }
containers = docker_api.containers.list(
all: true,
filters: {"name" => [container_name]}
).sort { |a, b| (images.index(b.@image_id) || 9999) <=> (images.index(a.@image_id) || 9999) }
# If it's already up, do nothing # If it's already up, do nothing
if containers.any? { |container| # FIXME: bring back out-of-date warning
is_running = container.@state == "running" if funko.running?
is_old = container.@image_id != latest_image
p! container.@image_id
puts "Warning: running outdated version" if is_running && is_old
is_running
}
puts "#{funko.name} is already up" puts "#{funko.name} is already up"
next next
end end

View File

@ -26,6 +26,7 @@ class Funko
@[YAML::Field(ignore: true)] @[YAML::Field(ignore: true)]
property path : String = "" property path : String = ""
# Create an Array of funkos from an Array of folders containing definitions
def self.from_paths(paths : Array(String | Path)) : Array(Funko) def self.from_paths(paths : Array(String | Path)) : Array(Funko)
paths.map { |path| Path.new(path, "funko.yml") } paths.map { |path| Path.new(path, "funko.yml") }
.select { |path| File.exists?(path) } .select { |path| File.exists?(path) }
@ -35,4 +36,33 @@ class Funko
f f
} }
end end
# Return a list of image IDs for this funko, most recent first
def image_history
docker_api = Docr::API.new(Docr::Client.new)
begin
docker_api.images.history(
name: name
).sort { |i, j| j.@created <=> i.@created }.map(&.@id)
rescue ex : Docr::Errors::DockerAPIError
puts "Error: #{ex}"
[] of String
end
end
# Get all containers related to this funko
def containers
docker_api = Docr::API.new(Docr::Client.new)
docker_api.containers.list(
all: true,
filters: {"name" => ["faaso-#{name}"]}
)
end
# Is this funko running?
def running?
self.containers.any? { |container|
container.@state == "running"
}
end
end end