Refactor, update ameba config to ignore TODO/FIXME

This commit is contained in:
Roberto Alsina 2024-06-30 00:32:31 -03:00
parent c1308c3a36
commit 08930b9ee5
3 changed files with 87 additions and 133 deletions

View File

@ -1,36 +1,16 @@
# This configuration file was generated by `ameba --gen-config` # 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 # The point is for the user to remove these configuration records
# one by one as the reported problems are removed from the code base. # one by one as the reported problems are removed from the code base.
# Problems found: 2 # Problems found: 9
# 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
# Run `ameba --only Documentation/DocumentationAdmonition` for details # Run `ameba --only Documentation/DocumentationAdmonition` for details
Documentation/DocumentationAdmonition: Documentation/DocumentationAdmonition:
Description: Reports documentation admonitions Description: Reports documentation admonitions
Timezone: UTC Timezone: UTC
Excluded: Excluded:
- src/faaso.cr - src/faaso.cr
- src/funko.cr
- spec/faaso_spec.cr - spec/faaso_spec.cr
Admonitions: Admonitions:
- TODO - TODO
@ -38,49 +18,3 @@ Documentation/DocumentationAdmonition:
- BUG - BUG
Enabled: true Enabled: true
Severity: Warning 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

View File

@ -85,80 +85,61 @@ module Faaso
container_name = "faaso-#{funko.name}" container_name = "faaso-#{funko.name}"
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
images = funko.image_history if funko.image_history.empty?
if images.empty?
puts "Error: no images available for #{funko.name}:latest" puts "Error: no images available for #{funko.name}:latest"
next next
end end
# sort list of funko containers newer image first case funko
containers = funko.containers.sort { |a, b| when .running?
(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 it's already up, do nothing
# FIXME: bring back out-of-date warning
if funko.running?
puts "#{funko.name} is already up" puts "#{funko.name} is already up"
next when .paused?
end # If it is paused, unpause it
# If it is paused, unpause it
paused = containers.select { |container|
container.@state == "paused"
}
if paused.size > 0
puts "Resuming existing paused container" puts "Resuming existing paused container"
docker_api.containers.unpause(paused[0].@id) funko.unpause
next when .exited?
end puts "Starting function #{funko.name}"
# If it is exited, start it
existing = containers.select { |container|
container.@state == "exited"
}
puts "Starting function #{funko.name}"
if existing.size > 0
puts "Restarting existing exited container" puts "Restarting existing exited container"
docker_api.containers.start(existing[0].@id) funko.start
next else
end # FIXME: move into Funko class
# Deploy from scratch
# Deploy from scratch Faaso.setup_network # We need it
Faaso.setup_network # We need it puts "Creating new container"
puts "Creating new container" conf = Docr::Types::CreateContainerConfig.new(
conf = Docr::Types::CreateContainerConfig.new( image: "#{funko.name}:latest",
image: "#{funko.name}:latest", hostname: funko.name,
hostname: funko.name, # Port in the container side
# Port in the container side exposed_ports: {"#{funko.port}/tcp" => {} of String => String},
exposed_ports: {"#{funko.port}/tcp" => {} of String => String}, host_config: Docr::Types::HostConfig.new(
host_config: Docr::Types::HostConfig.new( network_mode: "faaso-net",
network_mode: "faaso-net", port_bindings: {"#{funko.port}/tcp" => [Docr::Types::PortBinding.new(
port_bindings: {"#{funko.port}/tcp" => [Docr::Types::PortBinding.new( host_port: "", # Host port, empty means random
host_port: "", # Host port, empty means random host_ip: "127.0.0.1", # Host IP
host_ip: "127.0.0.1", # Host IP )]}
)]} )
) )
)
response = docker_api.containers.create(name: container_name, config: conf) response = docker_api.containers.create(name: container_name, config: conf)
response.@warnings.each { |msg| puts "Warning: #{msg}" } response.@warnings.each { |msg| puts "Warning: #{msg}" }
docker_api.containers.start(response.@id) docker_api.containers.start(response.@id)
containers = docker_api.containers.list( containers = docker_api.containers.list(
all: true, all: true,
filters: {"name" => [container_name]} filters: {"name" => [container_name]}
) )
(1..5).each { |_| (1..5).each { |_|
break if containers[0].state == "running" break if containers[0].state == "running"
sleep 0.1.seconds sleep 0.1.seconds
} }
if containers[0].state != "running" if containers[0].state != "running"
puts "Container for #{funko.name} is not running yet" puts "Container for #{funko.name} is not running yet"
next next
end
puts "Container for #{funko.name} is running"
end end
puts "Container for #{funko.name} is running"
end end
end end
end end

View File

@ -59,10 +59,49 @@ class Funko
) )
end end
# Is this funko running? # Is any instance of this funko running?
def running? def running?
self.containers.any? { |container| self.containers.any? { |container|
container.@state == "running" container.@state == "running"
} }
end 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 end