2024-06-29 17:36:11 +00:00
|
|
|
require "./funko.cr"
|
2024-06-28 17:09:58 +00:00
|
|
|
require "commander"
|
2024-06-28 20:42:10 +00:00
|
|
|
require "docr"
|
|
|
|
require "docr/utils.cr"
|
2024-06-28 19:24:52 +00:00
|
|
|
require "file_utils"
|
|
|
|
require "uuid"
|
2024-06-28 17:09:58 +00:00
|
|
|
|
2024-06-29 15:29:53 +00:00
|
|
|
# Functions as a Service, Ops!
|
2024-06-28 15:41:21 +00:00
|
|
|
module Faaso
|
|
|
|
VERSION = "0.1.0"
|
|
|
|
|
2024-06-30 00:01:28 +00:00
|
|
|
# Ensure the faaso-net network exists
|
2024-06-29 21:58:49 +00:00
|
|
|
def self.setup_network
|
|
|
|
docker_api = Docr::API.new(Docr::Client.new)
|
|
|
|
docker_api.networks.create(Docr::Types::NetworkConfig.new(
|
|
|
|
name: "faaso-net",
|
|
|
|
check_duplicate: false,
|
|
|
|
driver: "bridge"
|
|
|
|
))
|
2024-06-30 00:01:28 +00:00
|
|
|
rescue ex : Docr::Errors::DockerAPIError
|
|
|
|
raise ex if ex.status_code != 409 # Network already exists
|
|
|
|
|
2024-06-29 21:58:49 +00:00
|
|
|
end
|
|
|
|
|
2024-06-28 17:09:58 +00:00
|
|
|
module Commands
|
|
|
|
class Build
|
|
|
|
@arguments : Array(String) = [] of String
|
|
|
|
@options : Commander::Options
|
|
|
|
|
|
|
|
def initialize(options, arguments)
|
|
|
|
@options = options
|
|
|
|
@arguments = arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2024-06-29 15:22:59 +00:00
|
|
|
funkos = Funko.from_paths(@arguments)
|
|
|
|
funkos.each do |funko|
|
|
|
|
# Create temporary build location
|
|
|
|
tmp_dir = Path.new("tmp", UUID.random.to_s)
|
|
|
|
Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir
|
|
|
|
|
|
|
|
# Copy runtime if requested
|
|
|
|
if !funko.runtime.nil?
|
|
|
|
runtime_dir = Path.new("runtimes", funko.runtime.to_s)
|
|
|
|
if !File.exists? runtime_dir
|
|
|
|
puts "Error: runtime #{funko.runtime} not found"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
Dir.glob("#{runtime_dir}/*").each { |src|
|
|
|
|
FileUtils.cp_r(src, tmp_dir)
|
|
|
|
}
|
2024-06-28 19:24:52 +00:00
|
|
|
end
|
2024-06-29 15:22:59 +00:00
|
|
|
|
|
|
|
# Copy funko
|
|
|
|
if funko.path.empty?
|
|
|
|
puts "Internal error: empty funko path for #{funko.name}"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
Dir.glob("#{funko.path}/*").each { |src|
|
|
|
|
FileUtils.cp_r(src, tmp_dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
puts "Building function... #{funko.name} in #{tmp_dir}"
|
|
|
|
|
2024-06-28 20:42:10 +00:00
|
|
|
docker_api = Docr::API.new(Docr::Client.new)
|
2024-06-29 15:22:59 +00:00
|
|
|
docker_api.images.build(
|
|
|
|
context: tmp_dir.to_s,
|
2024-06-29 17:36:11 +00:00
|
|
|
tags: ["#{funko.name}:latest"]) { }
|
2024-06-28 17:09:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-30 03:41:47 +00:00
|
|
|
# Bring up one or more funkos.
|
|
|
|
#
|
|
|
|
# This doesn't guarantee that they will be running the latest
|
|
|
|
# version, and it will try to recicle paused and exited containers.
|
|
|
|
#
|
|
|
|
# If there is no other way, it will create a brand new container with
|
|
|
|
# the latest known image and start it.
|
|
|
|
#
|
|
|
|
# If there are no images for the funko, it will fail to bring it up.
|
2024-06-28 17:09:58 +00:00
|
|
|
class Up
|
|
|
|
@arguments : Array(String) = [] of String
|
|
|
|
@options : Commander::Options
|
|
|
|
|
|
|
|
def initialize(options, arguments)
|
|
|
|
@options = options
|
|
|
|
@arguments = arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2024-06-29 15:29:53 +00:00
|
|
|
funkos = Funko.from_paths(@arguments)
|
|
|
|
funkos.each do |funko|
|
2024-06-29 17:36:11 +00:00
|
|
|
container_name = "faaso-#{funko.name}"
|
2024-06-28 23:07:27 +00:00
|
|
|
docker_api = Docr::API.new(Docr::Client.new)
|
2024-06-29 17:36:11 +00:00
|
|
|
|
2024-06-30 03:32:31 +00:00
|
|
|
if funko.image_history.empty?
|
2024-06-29 17:36:11 +00:00
|
|
|
puts "Error: no images available for #{funko.name}:latest"
|
|
|
|
next
|
|
|
|
end
|
2024-06-29 15:29:53 +00:00
|
|
|
|
2024-06-30 03:32:31 +00:00
|
|
|
case funko
|
|
|
|
when .running?
|
|
|
|
# If it's already up, do nothing
|
|
|
|
# FIXME: bring back out-of-date warning
|
2024-06-29 17:36:11 +00:00
|
|
|
puts "#{funko.name} is already up"
|
2024-06-30 03:32:31 +00:00
|
|
|
when .paused?
|
|
|
|
# If it is paused, unpause it
|
2024-06-28 23:57:04 +00:00
|
|
|
puts "Resuming existing paused container"
|
2024-06-30 03:32:31 +00:00
|
|
|
funko.unpause
|
|
|
|
when .exited?
|
|
|
|
puts "Starting function #{funko.name}"
|
2024-06-28 23:07:27 +00:00
|
|
|
puts "Restarting existing exited container"
|
2024-06-30 03:32:31 +00:00
|
|
|
funko.start
|
|
|
|
else
|
|
|
|
# FIXME: move into Funko class
|
|
|
|
# Deploy from scratch
|
|
|
|
Faaso.setup_network # We need it
|
|
|
|
puts "Creating new container"
|
2024-06-30 03:49:48 +00:00
|
|
|
id = funko.create_container
|
|
|
|
puts "Starting container"
|
|
|
|
docker_api.containers.start(id)
|
2024-06-30 03:32:31 +00:00
|
|
|
|
|
|
|
(1..5).each { |_|
|
2024-06-30 03:36:25 +00:00
|
|
|
break if funko.running?
|
2024-06-30 03:32:31 +00:00
|
|
|
sleep 0.1.seconds
|
|
|
|
}
|
2024-06-30 03:36:25 +00:00
|
|
|
if !funko.running?
|
2024-06-30 03:32:31 +00:00
|
|
|
puts "Container for #{funko.name} is not running yet"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
puts "Container for #{funko.name} is running"
|
2024-06-29 18:19:56 +00:00
|
|
|
end
|
2024-06-28 17:09:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Down
|
|
|
|
@arguments : Array(String) = [] of String
|
|
|
|
@options : Commander::Options
|
|
|
|
|
|
|
|
def initialize(options, arguments)
|
|
|
|
@options = options
|
|
|
|
@arguments = arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@arguments.each do |arg|
|
|
|
|
puts "Stopping function... #{arg}"
|
|
|
|
# TODO: check if function is running
|
|
|
|
# TODO: stop function container
|
|
|
|
# TODO: delete function container
|
|
|
|
# TODO: remove route from reverse proxy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-29 17:36:11 +00:00
|
|
|
|
|
|
|
class Deploy
|
|
|
|
@arguments : Array(String) = [] of String
|
|
|
|
@options : Commander::Options
|
|
|
|
|
|
|
|
def initialize(options, arguments)
|
|
|
|
@options = options
|
|
|
|
@arguments = arguments
|
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
|
|
|
@arguments.each do |arg|
|
|
|
|
puts "Stopping function... #{arg}"
|
|
|
|
# TODO: Everything
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-06-28 17:09:58 +00:00
|
|
|
end
|
2024-06-28 15:41:21 +00:00
|
|
|
end
|