diff --git a/.ameba.yml b/.ameba.yml index 00b413c..4440b96 100644 --- a/.ameba.yml +++ b/.ameba.yml @@ -1,36 +1,16 @@ # This configuration file was generated by `ameba --gen-config` -# on 2024-06-28 23:57:47 UTC using Ameba version 1.6.1. +# on 2024-06-30 03:32:11 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: 2 -# Run `ameba --only Layout/TrailingBlankLines` for details -Layout/TrailingBlankLines: - Description: Disallows trailing blank lines - Excluded: - - tmp/d1808c39-8dff-4267-9bec-6961ec16c41f/function.cr - - tmp/26456ab0-43e4-44fb-8b6f-986880ff5a41/function.cr - Enabled: true - Severity: Convention - -# Problems found: 2 -# Run `ameba --only Lint/Formatting` for details -Lint/Formatting: - Description: Reports not formatted sources - FailOnError: false - Excluded: - - tmp/d1808c39-8dff-4267-9bec-6961ec16c41f/function.cr - - tmp/26456ab0-43e4-44fb-8b6f-986880ff5a41/function.cr - Enabled: true - Severity: Warning - -# Problems found: 13 +# Problems found: 9 # Run `ameba --only Documentation/DocumentationAdmonition` for details Documentation/DocumentationAdmonition: Description: Reports documentation admonitions Timezone: UTC Excluded: - src/faaso.cr + - src/funko.cr - spec/faaso_spec.cr Admonitions: - TODO @@ -38,49 +18,3 @@ Documentation/DocumentationAdmonition: - BUG Enabled: true Severity: Warning - -# Problems found: 1 -# Run `ameba --only Lint/DebugCalls` for details -Lint/DebugCalls: - Description: Disallows debug-related calls - Excluded: - - src/faaso.cr - MethodNames: - - p - - p! - - pp - - pp! - Enabled: true - Severity: Warning - -# Problems found: 3 -# Run `ameba --only Naming/BlockParameterName` for details -Naming/BlockParameterName: - Description: Disallows non-descriptive block parameter names - MinNameLength: 3 - AllowNamesEndingInNumbers: true - Excluded: - - src/faaso.cr - AllowedNames: - - _ - - e - - i - - j - - k - - v - - x - - y - - ex - - io - - ws - - op - - tx - - id - - ip - - k1 - - k2 - - v1 - - v2 - ForbiddenNames: [] - Enabled: true - Severity: Convention diff --git a/src/faaso.cr b/src/faaso.cr index 52f0ffb..721c381 100644 --- a/src/faaso.cr +++ b/src/faaso.cr @@ -85,80 +85,61 @@ module Faaso container_name = "faaso-#{funko.name}" docker_api = Docr::API.new(Docr::Client.new) - images = funko.image_history - if images.empty? + if funko.image_history.empty? puts "Error: no images available for #{funko.name}:latest" next end - # sort list of funko containers newer image first - containers = funko.containers.sort { |a, b| - (images.index(b.@image_id) || 9999) <=> (images.index(a.@image_id) || 9999) - } - - # If it's already up, do nothing - # FIXME: bring back out-of-date warning - if funko.running? + case funko + when .running? + # If it's already up, do nothing + # FIXME: bring back out-of-date warning puts "#{funko.name} is already up" - next - end - - # If it is paused, unpause it - paused = containers.select { |container| - container.@state == "paused" - } - if paused.size > 0 + when .paused? + # If it is paused, unpause it puts "Resuming existing paused container" - docker_api.containers.unpause(paused[0].@id) - next - end - - # If it is exited, start it - existing = containers.select { |container| - container.@state == "exited" - } - - puts "Starting function #{funko.name}" - if existing.size > 0 + funko.unpause + when .exited? + puts "Starting function #{funko.name}" puts "Restarting existing exited container" - docker_api.containers.start(existing[0].@id) - next - end - - # Deploy from scratch - Faaso.setup_network # We need it - puts "Creating new container" - conf = Docr::Types::CreateContainerConfig.new( - image: "#{funko.name}:latest", - hostname: funko.name, - # Port in the container side - exposed_ports: {"#{funko.port}/tcp" => {} of String => String}, - host_config: Docr::Types::HostConfig.new( - network_mode: "faaso-net", - port_bindings: {"#{funko.port}/tcp" => [Docr::Types::PortBinding.new( - host_port: "", # Host port, empty means random - host_ip: "127.0.0.1", # Host IP - )]} + funko.start + else + # FIXME: move into Funko class + # Deploy from scratch + Faaso.setup_network # We need it + puts "Creating new container" + conf = Docr::Types::CreateContainerConfig.new( + image: "#{funko.name}:latest", + hostname: funko.name, + # Port in the container side + exposed_ports: {"#{funko.port}/tcp" => {} of String => String}, + host_config: Docr::Types::HostConfig.new( + network_mode: "faaso-net", + port_bindings: {"#{funko.port}/tcp" => [Docr::Types::PortBinding.new( + host_port: "", # Host port, empty means random + host_ip: "127.0.0.1", # Host IP + )]} + ) ) - ) - response = docker_api.containers.create(name: container_name, config: conf) - response.@warnings.each { |msg| puts "Warning: #{msg}" } - docker_api.containers.start(response.@id) - containers = docker_api.containers.list( - all: true, - filters: {"name" => [container_name]} - ) + response = docker_api.containers.create(name: container_name, config: conf) + response.@warnings.each { |msg| puts "Warning: #{msg}" } + docker_api.containers.start(response.@id) + containers = docker_api.containers.list( + all: true, + filters: {"name" => [container_name]} + ) - (1..5).each { |_| - break if containers[0].state == "running" - sleep 0.1.seconds - } - if containers[0].state != "running" - puts "Container for #{funko.name} is not running yet" - next + (1..5).each { |_| + break if containers[0].state == "running" + sleep 0.1.seconds + } + if containers[0].state != "running" + puts "Container for #{funko.name} is not running yet" + next + end + puts "Container for #{funko.name} is running" end - puts "Container for #{funko.name} is running" end end end diff --git a/src/funko.cr b/src/funko.cr index 2e1c573..6f543d1 100644 --- a/src/funko.cr +++ b/src/funko.cr @@ -59,10 +59,49 @@ class Funko ) end - # Is this funko running? + # Is any instance of this funko running? def running? self.containers.any? { |container| container.@state == "running" } end + + # Is any instance of this funko paused? + def paused? + self.containers.any? { |container| + container.@state == "paused" + } + end + + # Unpause paused container with the newer image + def unpause + docker_api = Docr::API.new(Docr::Client.new) + images = self.image_history + paused = self.containers.select { |container| + container.@state == "paused" + }.sort! { |i, j| + (images.index(j.@image_id) || 9999) <=> (images.index(i.@image_id) || 9999) + } + docker_api.containers.unpause(paused[0].@id) unless paused.empty? + end + + # Is any instance of this funko exited? + def exited? + self.containers.any? { |container| + container.@state == "exited" + } + end + + # Restart exited container with the newer image + def start + # FIXME refactor DRY with unpause + docker_api = Docr::API.new(Docr::Client.new) + images = self.image_history + exited = self.containers.select { |container| + container.@state == "exited" + }.sort! { |i, j| + (images.index(j.@image_id) || 9999) <=> (images.index(i.@image_id) || 9999) + } + docker_api.containers.restart(exited[0].@id) unless exited.empty? + end end