Working terminal/shell/logs but container selection is broken

This commit is contained in:
Roberto Alsina 2024-07-04 14:44:16 -03:00
parent 56e59ae4a0
commit 05438d13cf
7 changed files with 37 additions and 15 deletions

View File

@ -1,8 +1,6 @@
{ {
https_port 8888 https_port 8888
http_port 8887 http_port 8887
local_certs
} }
localhost:8888 { localhost:8888 {
@ -12,7 +10,4 @@ localhost:8888 {
handle_path /admin/* { handle_path /admin/* {
reverse_proxy /* http://127.0.0.1:3000 reverse_proxy /* http://127.0.0.1:3000
} }
} }
handle_path /faaso/hello/* {
reverse_proxy /* http://hello-d89veq:3000
}

View File

@ -10,7 +10,7 @@ RUN shards build -d --error-trace
RUN strip bin/* RUN strip bin/*
FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship FROM --platform=${TARGETPLATFORM:-linux/amd64} alpine as ship
RUN apk update && apk add caddy multirun openssl zlib yaml pcre2 gc libevent libgcc libxml2 ttyd && apk cache clean RUN apk update && apk add caddy multirun docker openssl zlib yaml pcre2 gc libevent libgcc libxml2 ttyd && apk cache clean
# Unprivileged user # Unprivileged user
RUN addgroup -S app && adduser app -S -G app RUN addgroup -S app && adduser app -S -G app

View File

@ -32,6 +32,7 @@
<tbody id="funko-list"> <tbody id="funko-list">
</tbody> </tbody>
</table> </table>
<div id="terminal" style="resize: vertical; overflow: auto;"></div>
</main> </main>
<script> <script>
update_funkos = function () { update_funkos = function () {

View File

@ -124,6 +124,28 @@ module Funko
funko.wait_for(1, 1) funko.wait_for(1, 1)
end end
# Return an iframe that shows the container's logs
get "/funkos/:name/terminal/logs" do |env|
name = env.params.url["name"]
funko = Funko.from_names([name])[0]
# FIXME: Just getting the 1st one for now, it
# may not even be running
container_name = funko.containers.map { |c| c.@names[0] }[0]
Terminal.start_terminal(["docker", "logs", "-f", container_name.to_s])
"<iframe src='terminal/' width='100%' height='100%'></iframe>"
end
# Get an iframe with a shell into the container
get "/funkos/:name/terminal/shell" do |env|
name = env.params.url["name"]
funko = Funko.from_names([name])[0]
# FIXME: Just getting the 1st one for now, it
# may not even be running
container_name = funko.containers.map { |c| c.@names[0] }[0].lstrip("/")
Terminal.start_terminal(["docker", "exec", "-ti", container_name, "/bin/sh"], readonly: false)
"<iframe src='terminal/' width='100%' height='100%'></iframe>"
end
# Helper to run faaso locally and get a response back # Helper to run faaso locally and get a response back
def run_faaso(args : Array(String)) def run_faaso(args : Array(String))
Log.info { "Running faaso [#{args.join(", ")}, -l]" } Log.info { "Running faaso [#{args.join(", ")}, -l]" }

View File

@ -47,7 +47,7 @@ localhost:8888 {
handle_path /faaso/#{funko.split("-")[0]}/* { handle_path /faaso/#{funko.split("-")[0]}/* {
reverse_proxy /* http://#{funko}:3000 reverse_proxy /* http://#{funko}:3000
} }
)}.join("\n") +"}" ) }.join("\n") + "}"
if @@current_config != config if @@current_config != config
File.open("Caddyfile", "w") do |file| File.open("Caddyfile", "w") do |file|

View File

@ -3,16 +3,19 @@ module Terminal
@@terminal_process : Process | Nil = nil @@terminal_process : Process | Nil = nil
def start_terminal(_args = ["sh"], readonly = false) def start_terminal(_args = ["sh"], readonly = true)
args = ["-p", "7681", "-c", "admin:admin", "-o"] args = ["-p", "7681", "-c", "admin:admin", "-o"]
args += ["-W"] unless readonly args += ["-W"] unless readonly
args += _args args += _args
# We have a process there, kill it # We have a process there, kill it
@@terminal_process.as(Process).terminate if !@@terminal_process.nil? begin
@@terminal_process.as(Process).terminate if !@@terminal_process.nil?
rescue e : RuntimeError
Log.error { "Error terminating terminal process: #{e.message}" }
end
@@terminal_process = Process.new( @@terminal_process = Process.new(
command: "/usr/bin/ttyd", command: "/usr/bin/ttyd",
args: args) args: args)
Log.info {"Terminal started on port 7681"} Log.info { "Terminal started on port 7681" }
end end
end end

View File

@ -10,12 +10,13 @@
<%- if f["scale"].to_i > 0 -%> <%- if f["scale"].to_i > 0 -%>
<button disabled hx-get="funkos/<%= f["name"] %>/start">Start</button> <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 hx-get="funkos/<%= f["name"] %>/stop" hx-on:htmx:after-request="update_funkos()">Stop</button>
<button hx-get="funkos/<%= f["name"] %>/restart" hx-on:htmx:after-request="update_funkos()">Restart</button>
<%- else -%> <%- else -%>
<button hx-get="funkos/<%= f["name"] %>/start" hx-on:htmx:after-request="update_funkos()">Start</button> <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 disabled hx-get="funkos/<%= f["name"] %>/stop" hx-on:htmx:after-request="update_funkos()">Stop</button>
<button hx-get="funkos/<%= f["name"] %>/restart" hx-on:htmx:after-request="update_funkos()">Restart</button>
<%- end -%> <%- end -%>
<button hx-get="funkos/<%= f["name"] %>/restart" hx-on:htmx:after-request="update_funkos()">Restart</button>
<button hx-target="#terminal" hx-get="funkos/<%= f["name"] %>/terminal/logs/">Logs</button>
<button hx-target="#terminal" hx-get="funkos/<%= f["name"] %>/terminal/shell/">Shell</button>
<%- end -%> <%- end -%>
<img id="spinner-<%= f["name"] %>" src="bars.svg" class="htmx-indicator"> <img id="spinner-<%= f["name"] %>" src="bars.svg" class="htmx-indicator">
</td> </td>