From 126cae6c1835d2524d9a1acb40dc5c74cc69e66b Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Wed, 3 Jul 2024 15:18:45 -0300 Subject: [PATCH] Status local/remote --- .ameba.yml | 6 ++++-- src/commands/status.cr | 24 +++++++++++++++++------- src/daemon/funko.cr | 12 ++++++++++++ src/funko.cr | 26 ++++++++++++++++++++------ tinyproxy.conf | 1 + 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/.ameba.yml b/.ameba.yml index b35720f..ce61d04 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -1,9 +1,9 @@ # This configuration file was generated by `ameba --gen-config` -# on 2024-07-03 14:31:04 UTC using Ameba version 1.6.1. +# on 2024-07-03 18:18:32 UTC using Ameba version 1.6.1. # The point is for the user to remove these configuration records # one by one as the reported problems are removed from the code base. -# Problems found: 5 +# Problems found: 6 # Run `ameba --only Documentation/DocumentationAdmonition` for details Documentation/DocumentationAdmonition: Description: Reports documentation admonitions @@ -12,6 +12,8 @@ Documentation/DocumentationAdmonition: - src/secrets.cr - src/daemon/main.cr - src/daemon/secrets.cr + - src/daemon/funko.cr + - src/funko.cr - spec/faaso_spec.cr Admonitions: - TODO diff --git a/src/commands/status.cr b/src/commands/status.cr index 0b242bc..038eac4 100644 --- a/src/commands/status.cr +++ b/src/commands/status.cr @@ -5,21 +5,31 @@ module Faaso funko = Funko::Funko.from_names([name])[0] status = funko.docker_status - Log.info { "Name: #{status["name"]}" } - Log.info { "Scale: #{status["scale"]}" } + Log.info { "Name: #{status.@name}" } + Log.info { "Scale: #{status.scale}" } - Log.info { "Containers: #{status["containers"].size}" } - status["containers"].each do |container| + Log.info { "Containers: #{status.containers.size}" } + status.containers.each do |container| Log.info { " #{container.@names[0]} #{container.status}" } end - Log.info { "Images: #{status["images"].size}" } - status["images"].each do |image| - Log.info { " #{image.repo_tags} #{image.created}" } + Log.info { "Images: #{status.images.size}" } + status.images.each do |image| + Log.info { " #{image.repo_tags} #{Time.unix(image.created)}" } end end def remote(options, name) + response = Crest.get( + "#{FAASO_SERVER}funkos/#{name}/status/", \ + user: "admin", password: "admin") + body = JSON.parse(response.body) + Log.info { body["output"] } + rescue ex : Crest::InternalServerError + Log.error { "Error scaling funko #{name}" } + body = JSON.parse(ex.response.body) + Log.info { body["output"] } + exit 1 end def run(options, name) diff --git a/src/daemon/funko.cr b/src/daemon/funko.cr index 1549779..515ac38 100644 --- a/src/daemon/funko.cr +++ b/src/daemon/funko.cr @@ -5,6 +5,18 @@ require "../funko.cr" module Funko extend self + # Get the funko's status + get "/funkos/:name/status/" do |env| + name = env.params.url["name"] + response = run_faaso(["status", name]) + + if response["exit_code"] != 0 + halt env, status_code: 500, response: response.to_json + else + response.to_json + end + end + # Get the funko's scale get "/funkos/:name/scale/" do |env| name = env.params.url["name"] diff --git a/src/funko.cr b/src/funko.cr index d4c6a23..09ff7b3 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -6,6 +6,20 @@ require "yaml" module Funko extend self + struct Status + property name : String = "" + property scale : Int32 = 0 + property containers : Array(Docr::Types::ContainerSummary) = [] of Docr::Types::ContainerSummary + property images : Array(Docr::Types::ImageSummary) = [] of Docr::Types::ImageSummary + + def initialize(name, scale, containers, images) + @name = name + @scale = scale + @containers = containers + @images = images + end + end + class Funko include YAML::Serializable @@ -168,12 +182,12 @@ module Funko # A comprehensive status for the funko: def docker_status - { - "name" => name, - "containers" => containers, - "images" => images, - "scale" => scale, - } + Status.new( + name: name, + containers: containers, + images: images, + scale: scale, + ) end # Descriptive status for the funko diff --git a/tinyproxy.conf b/tinyproxy.conf index 6ad239d..bca96e0 100644 --- a/tinyproxy.conf +++ b/tinyproxy.conf @@ -23,6 +23,7 @@ ReversePath "/faaso/hello-7273704811390/" "http://hello-7273704811390:3000/" ReversePath "/faaso/hello-761221081008155/" "http://hello-761221081008155:3000/" ReversePath "/faaso/hello-9798100678476/" "http://hello-9798100678476:3000/" ReversePath "/faaso/hello-98103104100103100/" "http://hello-98103104100103100:3000/" +ReversePath "/faaso/hello-e24ojr/" "http://hello-e24ojr:3000/" ReversePath "/faaso/hello-foo/" "http://hello-foo:3000/" ReversePath "/faaso/hello-gfvij3/" "http://hello-gfvij3:3000/" ReversePath "/faaso/hello-ngisvh/" "http://hello-ngisvh:3000/"