Refactor Up to use funkos

This commit is contained in:
Roberto Alsina 2024-06-29 12:29:53 -03:00
parent 4b6985d3f4
commit a2a6633104

View File

@ -5,7 +5,10 @@ require "file_utils"
require "uuid" require "uuid"
require "./funko.cr" require "./funko.cr"
# TODO: Write documentation for `Faaso` # FIXME make it configurable
REPO = "localhost:5000"
# Functions as a Service, Ops!
module Faaso module Faaso
VERSION = "0.1.0" VERSION = "0.1.0"
@ -52,7 +55,7 @@ module Faaso
slug = funko.name slug = funko.name
# FIXME: this should be configurable # FIXME: this should be configurable
repo = "localhost:5000" repo = REPO
tag = "#{repo}/#{funko.name}:latest" tag = "#{repo}/#{funko.name}:latest"
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
@ -78,60 +81,60 @@ module Faaso
end end
def run def run
@arguments.each do |arg| funkos = Funko.from_paths(@arguments)
slug = arg.gsub("/", "_").strip("_") funkos.each do |funko|
repo = "localhost:5000" repo = REPO
tag = "#{repo}/#{slug}:latest" tag = "#{repo}/#{funko.name}:latest"
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
# Pull image from registry # Pull image from registry
docker_api.images.create(image: tag) docker_api.images.create(image: tag)
# TODO: Start a container with the image
containers = docker_api.containers.list(all: true) containers = docker_api.containers.list(all: true)
pp! containers
# If it's running, do nothing # If it's running, do nothing
if containers.any? { |c| if containers.any? { |container|
c.@image == tag && c.@state == "running" container.@image == tag && container.@state == "running"
} }
puts "#{arg} is already running" puts "#{funko.name} is already running"
return 0 next
end end
# If it is paused, unpause it # If it is paused, unpause it
paused = containers.select { |c| paused = containers.select { |container|
c.@image == tag && c.@state == "paused" container.@image == tag && container.@state == "paused"
} }
if paused.size > 0 if paused.size > 0
puts "Resuming existing paused container" puts "Resuming existing paused container"
docker_api.containers.unpause(paused[0].@id) docker_api.containers.unpause(paused[0].@id)
return 0 next
end end
# If it is exited, start it # If it is exited, start it
existing = containers.select { |c| existing = containers.select { |container|
c.@image == tag && c.@state == "exited" container.@image == tag && container.@state == "exited"
} }
puts "Starting function #{arg}" puts "Starting function #{funko.name}"
if existing.size > 0 if existing.size > 0
puts "Restarting existing exited container" puts "Restarting existing exited container"
docker_api.containers.start(existing[0].@id) docker_api.containers.start(existing[0].@id)
return 0 next
end end
# Creating from scratch # Creating from scratch
puts "Creating new container" puts "Creating new container"
conf = Docr::Types::CreateContainerConfig.new( conf = Docr::Types::CreateContainerConfig.new(
image: tag, image: tag,
hostname: "foo", hostname: funko.name,
# Port in the container side # Port in the container side
exposed_ports: {"3000/tcp" => {} of String => String}, exposed_ports: {"#{funko.port}/tcp" => {} of String => String},
host_config: Docr::Types::HostConfig.new( host_config: Docr::Types::HostConfig.new(
port_bindings: {"3000/tcp" => [Docr::Types::PortBinding.new( port_bindings: {"#{funko.port}/tcp" => [Docr::Types::PortBinding.new(
host_port: "3000", # Host port host_port: "", # Host port, empty means random
host_ip: "127.0.0.1", # Host IP host_ip: "127.0.0.1", # Host IP
)]} )]}
) )
) )
# FIXME: name should be unique # FIXME: name should be unique
response = docker_api.containers.create(name: "fungus", config: conf) response = docker_api.containers.create(name: "fungus", config: conf)
docker_api.containers.start(response.@id) docker_api.containers.start(response.@id)