Compare commits

..

No commits in common. "0d4db9518630fb80b7addfb7dd448345b8f3bf41" and "87db3aaad1a13d85c12bb26d059d9d55932ea023" have entirely different histories.

2 changed files with 19 additions and 22 deletions

View file

@ -5,7 +5,6 @@ require "socket"
require "./module"
require "./config"
require "../common/network"
module DocMachine::Build
class Run
@ -20,6 +19,7 @@ module DocMachine::Build
@process = nil
end
# cleanup environment
# create directories
# setup permissions
@ -32,6 +32,22 @@ module DocMachine::Build
self._avoid_duplicates() unless @config.enable_multiple
end
private def _find_port(port_base)
(port_base..65535).each do |port|
return port if _port_available?(port)
end
raise "No port available"
end
private def _port_available?(port)
sock = Socket.new(Socket::Family::INET, Socket::Type::STREAM)
sock.bind(Socket::IPAddress.new("0.0.0.0", port))
sock.close
return true
rescue ex : Socket::BindError
return false
end
private def _avoid_duplicates
Log.info { "Multiple Instances: stopping duplicate containers (for #{@docker_name})" }
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
@ -145,7 +161,7 @@ module DocMachine::Build
## Detect docs
if Dir.exists?("#{@config.data_dir}/docs")
Log.info { "Docs: detected docs directory." }
mkdocs_port = Network.find_port(@config.port)
mkdocs_port = _find_port(@config.port)
docker_opt_mkdocs_port = ["-p", "#{mkdocs_port}:5100"]
docker_opts.concat docker_opt_mkdocs_port
Log.notice { "Using port #{mkdocs_port} for docs" }
@ -157,7 +173,7 @@ module DocMachine::Build
## Detect slides
if Dir.exists?("#{@config.data_dir}/slides")
Log.info { "Slides: detected slides directory." }
marp_port = Network.find_port(@config.port+100)
marp_port = _find_port(@config.port+100)
docker_opt_marp_port = ["-p", "#{marp_port}:5200"]
docker_opts.concat docker_opt_marp_port
Log.info { "Slides: Adding option to command line (#{docker_opt_marp_port})" }

View file

@ -1,19 +0,0 @@
class Network
def self.find_port(port_base)
(port_base..65535).each do |port|
return port if self.port_available?(port)
end
raise "No port available"
end
def self.port_available?(port)
sock = Socket.new(Socket::Family::INET, Socket::Type::STREAM)
sock.bind(Socket::IPAddress.new("0.0.0.0", port))
sock.close
return true
rescue ex : Socket::BindError
return false
end
end