From d276c91f22c64739f9a5b00e0bf54a8e926d6279 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 28 Jun 2024 20:07:27 -0300 Subject: [PATCH] Some work in UP --- src/faaso.cr | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/src/faaso.cr b/src/faaso.cr index 053f833..6fa2b89 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -24,7 +24,7 @@ module Faaso # TODO: decide template based on file extensions or other metadata template = "templates/crystal" tmp_dir = "tmp/#{UUID.random}" - slug = arg.gsub("/","_").strip("_") + slug = arg.gsub("/", "_").strip("_") repo = "localhost:5000" tag = "#{repo}/#{slug}:latest" puts "Building function... #{arg} in #{tmp_dir}" @@ -37,6 +37,8 @@ module Faaso docker_api.images.build(context: tmp_dir, tags: [tag, "#{slug}:latest"]) { } puts "Pushing to repo as #{tag}" docker_api.images.tag(repo: repo, name: slug, tag: "latest") + # FIXME: pushing is broken because my test registry has no auth + # docker_api.images.push(name: slug, tag: "latest", auth: "") end end end @@ -52,9 +54,42 @@ module Faaso def run @arguments.each do |arg| - puts "Starting function... #{arg}" - # TODO: Check that we have an image for the function + slug = arg.gsub("/", "_").strip("_") + repo = "localhost:5000" + tag = "#{repo}/#{slug}:latest" + docker_api = Docr::API.new(Docr::Client.new) + # Pull image from registry + docker_api.images.create(image: tag) # TODO: Start a container with the image + containers = docker_api.containers.list(all: true) + pp! containers + # If it's running, do nothing + if (containers.any? { |c| + c.@image == tag && c.@state == "running" + }) + puts "#{arg} is already running" + return 0 + end + + # If it is exited, start it + existing = containers.select { |c| + c.@image == tag && c.@state == "exited" + } + + puts "Starting function #{arg}" + if (existing.size > 0) + puts "Restarting existing exited container" + docker_api.containers.start(existing[0].@id) + else + conf = Docr::Types::CreateContainerConfig.new( + image: tag, + hostname: "foo", + host_config: Docr::Types::HostConfig.new + ) + # FIXME: name should be unique + response = docker_api.containers.create(name: "fungus", config: conf) + docker_api.containers.start(response.@id) + end # TODO: Run test for healthcheck # TODO: Map route in reverse proxy to function # TODO: Return function URL for testing