refactor: move static network functions to a reusable class
This commit is contained in:
parent
fdeccbda0c
commit
6f316ff78e
2 changed files with 22 additions and 19 deletions
|
@ -5,6 +5,7 @@ require "socket"
|
||||||
|
|
||||||
require "./module"
|
require "./module"
|
||||||
require "./config"
|
require "./config"
|
||||||
|
require "../common/network"
|
||||||
|
|
||||||
module DocMachine::Build
|
module DocMachine::Build
|
||||||
class Run
|
class Run
|
||||||
|
@ -19,7 +20,6 @@ module DocMachine::Build
|
||||||
@process = nil
|
@process = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# cleanup environment
|
# cleanup environment
|
||||||
# create directories
|
# create directories
|
||||||
# setup permissions
|
# setup permissions
|
||||||
|
@ -32,22 +32,6 @@ module DocMachine::Build
|
||||||
self._avoid_duplicates() unless @config.enable_multiple
|
self._avoid_duplicates() unless @config.enable_multiple
|
||||||
end
|
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
|
private def _avoid_duplicates
|
||||||
Log.info { "Multiple Instances: stopping duplicate containers (for #{@docker_name})" }
|
Log.info { "Multiple Instances: stopping duplicate containers (for #{@docker_name})" }
|
||||||
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
|
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
|
||||||
|
@ -161,7 +145,7 @@ module DocMachine::Build
|
||||||
## Detect docs
|
## Detect docs
|
||||||
if Dir.exists?("#{@config.data_dir}/docs")
|
if Dir.exists?("#{@config.data_dir}/docs")
|
||||||
Log.info { "Docs: detected docs directory." }
|
Log.info { "Docs: detected docs directory." }
|
||||||
mkdocs_port = _find_port(@config.port)
|
mkdocs_port = Network.find_port(@config.port)
|
||||||
docker_opt_mkdocs_port = ["-p", "#{mkdocs_port}:5100"]
|
docker_opt_mkdocs_port = ["-p", "#{mkdocs_port}:5100"]
|
||||||
docker_opts.concat docker_opt_mkdocs_port
|
docker_opts.concat docker_opt_mkdocs_port
|
||||||
Log.notice { "Using port #{mkdocs_port} for docs" }
|
Log.notice { "Using port #{mkdocs_port} for docs" }
|
||||||
|
@ -173,7 +157,7 @@ module DocMachine::Build
|
||||||
## Detect slides
|
## Detect slides
|
||||||
if Dir.exists?("#{@config.data_dir}/slides")
|
if Dir.exists?("#{@config.data_dir}/slides")
|
||||||
Log.info { "Slides: detected slides directory." }
|
Log.info { "Slides: detected slides directory." }
|
||||||
marp_port = _find_port(@config.port+100)
|
marp_port = Network.find_port(@config.port+100)
|
||||||
docker_opt_marp_port = ["-p", "#{marp_port}:5200"]
|
docker_opt_marp_port = ["-p", "#{marp_port}:5200"]
|
||||||
docker_opts.concat docker_opt_marp_port
|
docker_opts.concat docker_opt_marp_port
|
||||||
Log.info { "Slides: Adding option to command line (#{docker_opt_marp_port})" }
|
Log.info { "Slides: Adding option to command line (#{docker_opt_marp_port})" }
|
||||||
|
|
19
src/common/network.cr
Normal file
19
src/common/network.cr
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in a new issue