From 1b2bddf2d2d576e444a1c01fb26056c4f4e50826 Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Fri, 5 Jul 2024 16:36:02 -0300 Subject: [PATCH] Add 'delete' action for funkos that removes all trace --- Dockerfile | 8 +++----- src/daemon/funko.cr | 10 ++++++++++ src/funko.cr | 26 ++++++++++++++++++++++++++ src/main.cr | 9 ++------- src/runtime.cr | 5 +++++ src/views/funkos.ecr | 2 ++ 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 47d47b0..9529d7b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,12 @@ FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as build -RUN apk update && apk add crystal shards yaml-dev openssl-dev zlib-dev libxml2-dev && apk cache clean +RUN apk update && apk add crystal shards yaml-dev openssl-dev zlib-dev libxml2-dev make && apk cache clean RUN addgroup -S app && adduser app -S -G app WORKDIR /home/app -COPY shard.yml ./ +COPY shard.yml Makefile ./ RUN mkdir src/ COPY src/ src/ COPY runtimes/ runtimes/ -RUN shards install -RUN shards build -d --error-trace -RUN cat .rucksack >> bin/faaso +RUN make # RUN strip bin/* FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship diff --git a/src/daemon/funko.cr b/src/daemon/funko.cr index 6e44f6d..6574a7b 100644 --- a/src/daemon/funko.cr +++ b/src/daemon/funko.cr @@ -126,6 +126,16 @@ module Funko funko.wait_for(1, 1) end + # Delete => scale to 0, remove all containers and images + delete "/funkos/:name/" do |env| + name = env.params.url["name"] + funko = Funko.from_names([name])[0] + funko.scale(0) + funko.wait_for(0, 1) + funko.remove_all_containers + funko.remove_all_images + end + # Return an iframe that shows the container's logs get "/funkos/terminal/logs/:instance/" do |env| instance = env.params.url["instance"] diff --git a/src/funko.cr b/src/funko.cr index 5d2ba1a..f37efda 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -224,6 +224,32 @@ module Funko end end + # Remove all containers related to this funko + def remove_all_containers + docker_api = Docr::API.new(Docr::Client.new) + docker_api.containers.list(all: true).select { |container| + container.@names.any?(&.starts_with?("/faaso-#{name}-")) + }.each { |container| + begin + docker_api.containers.stop(container.@id) if container.status != "exited" + rescue ex : Docr::Errors::DockerAPIError + Log.error { "#{ex}" } unless ex.status_code == 304 # This just happens + end + docker_api.containers.delete(container.@id) + } + end + + # Remove all images related to this funko + def remove_all_images + docker_api = Docr::API.new(Docr::Client.new) + docker_api.images.list.select { |image| + return false if image.@repo_tags.nil? + true if image.@repo_tags.as(Array(String)).any?(&.starts_with?("faaso-#{name}:")) + }.each { |image| + docker_api.images.delete(image.@id) + } + end + # Create a container for this funko def create_container(autostart : Bool = true) : String # The path to secrets is tricky. On the server it will be in diff --git a/src/main.cr b/src/main.cr index 9a2b96d..281589d 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,8 +1,8 @@ require "./faaso.cr" require "colorize" require "docopt" +require "rucksack" -# Log formatter for struct LogFormat < Log::StaticFormatter @@colors = { "FATAL" => :red, @@ -84,9 +84,4 @@ when .fetch("status", false) status = Faaso::Commands::Status.new.run(ans, ans["FUNKO"].as(String)) end -exit(status) - -# Embed runtimes in the faaso binary using rucksack -{% for name in `find ./runtimes -type f`.split('\n') %} - rucksack({{name}}) -{% end %} +exit(status) \ No newline at end of file diff --git a/src/runtime.cr b/src/runtime.cr index c8ee299..41932d4 100644 --- a/src/runtime.cr +++ b/src/runtime.cr @@ -91,3 +91,8 @@ module Runtime end end end + +# Embed runtimes in the faaso binary using rucksack +{% for name in `find ./runtimes -type f`.split('\n') %} + rucksack({{name}}) +{% end %} diff --git a/src/views/funkos.ecr b/src/views/funkos.ecr index e4219a2..c7e0efa 100644 --- a/src/views/funkos.ecr +++ b/src/views/funkos.ecr @@ -39,9 +39,11 @@ <%- if f["scale"].as(String).to_i > 0 -%> + <%- else -%> + <%- end -%> <%- end -%>