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
proxy-image: build
docker build . -t faaso-proxy --no-cache
start-proxy:
docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 -p 3000:3000 faas
o-proxy
docker run --network=faaso-net -v /var/run/docker.sock:/var/run/docker.sock -p 8888:8888 -p 3000:3000 faaso-proxy
.PHONY: build proxy-image start-proxy

View File

@ -2,7 +2,7 @@ require "kemal"
require "./funko.cr"
get "/ping/" do
"OK"
"OK"
end
Kemal.run

View File

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

View File

@ -26,6 +26,7 @@ class Funko
@[YAML::Field(ignore: true)]
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)
paths.map { |path| Path.new(path, "funko.yml") }
.select { |path| File.exists?(path) }
@ -35,4 +36,33 @@ class Funko
f
}
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