faaso build works both local and remote. Basic auth for faaso API (lame, hardcoded)

This commit is contained in:
2024-06-30 15:10:49 -03:00
parent eb063beb2c
commit f5467551a1
9 changed files with 128 additions and 13 deletions

View File

@ -73,7 +73,7 @@ post "/funko/build/" do |env|
stdout = IO::Memory.new
status = Process.run(
command: "faaso",
args: ["build", tmp_dir.to_s],
args: ["build", "-l", tmp_dir.to_s],
output: stdout,
error: stderr,
)

View File

@ -1,9 +1,17 @@
require "./funko.cr"
require "commander"
require "crest"
require "docr"
require "docr/utils.cr"
require "json"
require "uuid"
# API if you just ran faaso-daemon
FAASO_API = "http://localhost:3000/"
# API if you are running the proxy image locally
# FAASO_API="http://localhost:8888/admin/"
# Functions as a Service, Ops!
module Faaso
VERSION = "0.1.0"
@ -34,14 +42,65 @@ module Faaso
def run
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
funko.prepare_build tmp_dir
local = @options.@bool["local"]
puts "Building function... #{funko.name} in #{tmp_dir}"
funko.build tmp_dir
if local
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
funko.prepare_build tmp_dir
puts "Building function... #{funko.name} in #{tmp_dir}"
funko.build tmp_dir
end
else # Running against a server
funkos.each do |funko|
# Create a tarball for the funko
buf = IO::Memory.new
Compress::Gzip::Writer.open(buf) do |gzip|
Crystar::Writer.open(gzip) do |tw|
Dir.glob("#{funko.path}/**/*").each do |path|
next unless File.file? path
rel_path = Path[path].relative_to funko.path
file_info = File.info(path)
hdr = Crystar::Header.new(
name: rel_path.to_s,
mode: file_info.permissions.to_u32,
size: file_info.size,
)
tw.write_header(hdr)
tw.write(File.read(path).to_slice)
end
end
end
tmp = File.tempname
File.open(tmp, "w") do |outf|
outf << buf
end
url = "#{FAASO_API}funko/build/"
begin
_response = Crest.post(
url,
{"funko.tgz" => File.open(tmp), "name" => "funko.tgz"},
user: "admin", password: "admin"
)
puts "Build finished successfully."
# body = JSON.parse(_response.body)
# puts body["stdout"]
# puts body["stderr"]
rescue ex : Crest::InternalServerError
puts "Error building image."
body = JSON.parse(ex.response.body)
puts body["stdout"]
puts body["stderr"]
puts "Error building funko #{funko.name} from #{funko.path}"
exit 1
end
end
end
end
end

View File

@ -80,7 +80,7 @@ class Funko
docker_api = Docr::API.new(Docr::Client.new)
docker_api.images.build(
context: path.to_s,
tags: ["#{name}:latest"]) { }
tags: ["#{name}:latest"]) { |x| puts x }
end
# Return a list of image IDs for this funko, most recent first

View File

@ -5,6 +5,15 @@ cli = Commander::Command.new do |cmd|
cmd.use = "faaso"
cmd.long = "Functions as a Service, Open"
cmd.flags.add do |flag|
flag.name = "local"
flag.short = "-l"
flag.long = "--local"
flag.description = "Run commands locally instead of against a FaaSO server."
flag.default = false
flag.persistent = true
end
cmd.commands.add do |command|
command.use = "build"
command.short = "Build a funko"