Cleanup, added Funko object, support funko metadata

This commit is contained in:
Roberto Alsina 2024-06-29 12:22:59 -03:00
parent f5d1320add
commit 4b6985d3f4
9 changed files with 83 additions and 16 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
/bin/ /bin/
/.shards/ /.shards/
*.dwarf *.dwarf
tmp/

View File

@ -0,0 +1,2 @@
name: hello
runtime: crystal

View File

@ -9,6 +9,6 @@ shards:
version: 0.4.0 version: 0.4.0
docr: docr:
git: https://github.com/marghidanu/docr.git git: https://github.com/ralsina/docr.git
version: 0.1.1+git.commit.59fb466b748d5d29a8f2de88a54b71dbfe0285aa version: 0.1.1+git.commit.18f15cc7111b1d0c63347c7cca07aee9ec87a7a8

View File

@ -16,5 +16,6 @@ license: MIT
dependencies: dependencies:
docr: docr:
github: ralsina/docr github: ralsina/docr
branch: add_exposed_ports
commander: commander:
github: mrrooijen/commander github: mrrooijen/commander

View File

@ -3,6 +3,7 @@ require "docr"
require "docr/utils.cr" require "docr/utils.cr"
require "file_utils" require "file_utils"
require "uuid" require "uuid"
require "./funko.cr"
# TODO: Write documentation for `Faaso` # TODO: Write documentation for `Faaso`
module Faaso module Faaso
@ -19,22 +20,46 @@ module Faaso
end end
def run def run
@arguments.each do |arg| funkos = Funko.from_paths(@arguments)
# A function is a folder with stuff in it funkos.each do |funko|
# TODO: decide template based on file extensions or other metadata # Create temporary build location
template = "templates/crystal" tmp_dir = Path.new("tmp", UUID.random.to_s)
tmp_dir = "tmp/#{UUID.random}" Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir
slug = arg.gsub("/", "_").strip("_")
repo = "localhost:5000" # Copy runtime if requested
tag = "#{repo}/#{slug}:latest" if !funko.runtime.nil?
puts "Building function... #{arg} in #{tmp_dir}" runtime_dir = Path.new("runtimes", funko.runtime.to_s)
Dir.mkdir_p("tmp") unless File.exists? "tmp" if !File.exists? runtime_dir
FileUtils.cp_r(template, tmp_dir) puts "Error: runtime #{funko.runtime} not found"
Dir.glob(arg + "/**/*").each do |file| next
FileUtils.cp(file, tmp_dir) end
Dir.glob("#{runtime_dir}/*").each { |src|
FileUtils.cp_r(src, tmp_dir)
}
end end
# 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}"
slug = funko.name
# FIXME: this should be configurable
repo = "localhost:5000"
tag = "#{repo}/#{funko.name}:latest"
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
docker_api.images.build(context: tmp_dir, tags: [tag, "#{slug}:latest"]) { } docker_api.images.build(
context: tmp_dir.to_s,
tags: [tag, "#{funko.name}:latest"]) { }
puts "Pushing to repo as #{tag}" puts "Pushing to repo as #{tag}"
docker_api.images.tag(repo: repo, name: slug, tag: "latest") docker_api.images.tag(repo: repo, name: slug, tag: "latest")
# FIXME: pushing is broken because my test registry has no auth # FIXME: pushing is broken because my test registry has no auth

38
src/funko.cr Normal file
View File

@ -0,0 +1,38 @@
require "yaml"
class Funko
include YAML::Serializable
# Required, the name of the funko. Must be unique across FaaSO
property name : String
# if Nil, it has no template whatsoever
property runtime : (String | Nil)? = nil
# Port of the funko process (optional, default is 3000)
property port : UInt32? = 3000
# Extra packages, passed as EXTRA_PACKAGES argument
# to the Dockerfile, use it for installing things in
# the SHIPPED docker image
property extra_packages : Array(String)?
# Extra packages, passed as DEVEL_PACKAGES argument
# to the Dockerfile, use it for installing things in
# the docker stage used to build the code
property devel_packages : Array(String)?
# Where this is located in the filesystem
@[YAML::Field(ignore: true)]
property path : String = ""
def self.from_paths(paths : Array(String | Path)) : Array(Funko)
paths.map { |path| Path.new(path, "funko.yml") }
.select { |path| File.exists?(path) }
.map { |path|
f = Funko.from_yaml(File.read(path.to_s))
f.path = path.parent.to_s
f
}
end
end