Use base58 for random strings

This commit is contained in:
Roberto Alsina 2024-07-08 22:33:52 -03:00
parent 56ff326098
commit 6489ec0dc2
3 changed files with 6 additions and 8 deletions

View File

@ -1,3 +1,5 @@
require "base58"
module Faaso module Faaso
module Commands module Commands
# Build images for one or more funkos from source # Build images for one or more funkos from source
@ -7,7 +9,7 @@ module Faaso
# Create temporary build location # Create temporary build location
funkos.each do |funko| funkos.each do |funko|
tmp_dir = Path.new("tmp", UUID.random.to_s) tmp_dir = Path.new("tmp", Random.base58(8))
Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir
funko.runtime = nil if options["--no-runtime"] funko.runtime = nil if options["--no-runtime"]

View File

@ -1,3 +1,4 @@
require "base58"
require "docr" require "docr"
require "kemal" require "kemal"
require "../funko.cr" require "../funko.cr"
@ -34,7 +35,7 @@ module Funko
# mosquito-cr/mosquito to make it a job queue # mosquito-cr/mosquito to make it a job queue
post "/funkos/build/" do |env| post "/funkos/build/" do |env|
# Create place to build funko # Create place to build funko
tmp_dir = Path.new("tmp", UUID.random.to_s) tmp_dir = Path.new("tmp", Random.base58(8))
Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir Dir.mkdir_p(tmp_dir) unless File.exists? tmp_dir
# Expand tarball in there # Expand tarball in there

View File

@ -309,7 +309,7 @@ module Funko
) )
docker_api = Docr::API.new(Docr::Client.new) docker_api = Docr::API.new(Docr::Client.new)
response = docker_api.containers.create(name: "faaso-#{name}-#{randstr}", config: conf) response = docker_api.containers.create(name: "faaso-#{name}-#{Random.base58(6)}", config: conf)
response.@warnings.each { |msg| Log.warn { msg } } response.@warnings.each { |msg| Log.warn { msg } }
docker_api.containers.start(response.@id) if autostart docker_api.containers.start(response.@id) if autostart
response.@id response.@id
@ -349,8 +349,3 @@ module Funko
end end
end end
end end
def randstr(length = 6) : String
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
String.new(Bytes.new(chars.to_slice.sample(length).to_unsafe, length))
end