Add 'delete' action for funkos that removes all trace

This commit is contained in:
Roberto Alsina 2024-07-05 16:36:02 -03:00
parent cd868b1c86
commit 1b2bddf2d2
6 changed files with 48 additions and 12 deletions

View File

@ -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

View File

@ -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"]

View File

@ -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

View File

@ -1,8 +1,8 @@
require "./faaso.cr"
require "colorize"
require "docopt"
require "rucksack"
# Log formatter for
struct LogFormat < Log::StaticFormatter
@@colors = {
"FATAL" => :red,
@ -85,8 +85,3 @@ when .fetch("status", false)
end
exit(status)
# Embed runtimes in the faaso binary using rucksack
{% for name in `find ./runtimes -type f`.split('\n') %}
rucksack({{name}})
{% end %}

View File

@ -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 %}

View File

@ -39,9 +39,11 @@
<%- if f["scale"].as(String).to_i > 0 -%>
<button disabled hx-get="funkos/<%= f["name"] %>/start">Start</button>
<button hx-get="funkos/<%= f["name"] %>/stop" hx-on:htmx:after-request="update_funkos()">Stop</button>
<button disabled hx-delete="funkos/<%= f["name"] %>/" hx-on:htmx:after-request="update_funkos()">Delete</button>
<%- else -%>
<button hx-get="funkos/<%= f["name"] %>/start" hx-on:htmx:after-request="update_funkos()">Start</button>
<button disabled hx-get="funkos/<%= f["name"] %>/stop" hx-on:htmx:after-request="update_funkos()">Stop</button>
<button hx-delete="funkos/<%= f["name"] %>/" hx-on:htmx:after-request="update_funkos()">Delete</button>
<%- end -%>
<button hx-get="funkos/<%= f["name"] %>/restart" hx-on:htmx:after-request="update_funkos()">Restart</button>
<%- end -%>