From c1308c3a36c0542a793319b2cf2a5210b4a4199f Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Sat, 29 Jun 2024 23:54:14 -0300 Subject: [PATCH] Refactor behaviour into the Funko class --- Makefile | 8 +++++--- runtimes/crystal/main.cr | 2 +- src/faaso.cr | 30 ++++++++---------------------- src/funko.cr | 30 ++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 968631a..f1d6cac 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/runtimes/crystal/main.cr b/runtimes/crystal/main.cr index e719143..20c87cb 100644 --- a/runtimes/crystal/main.cr +++ b/runtimes/crystal/main.cr @@ -2,7 +2,7 @@ require "kemal" require "./funko.cr" get "/ping/" do - "OK" + "OK" end Kemal.run diff --git a/src/faaso.cr b/src/faaso.cr index 79bb84e..52f0ffb 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -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 diff --git a/src/funko.cr b/src/funko.cr index bcbbb19..2e1c573 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -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