feat: add support for port selection
Some checks failed
continuous-integration/drone/push Build is failing

Add support for base port selection
- add --port PORT and -p PORT options on command line
- add naive (not implemented) support for port detection
- docs container is now mapped to PORT
- slides container is now mapped to PORT+100
This commit is contained in:
Glenn Y. Rolland 2024-02-14 14:29:41 +01:00
parent a2272230e2
commit 81b845fc66
3 changed files with 40 additions and 18 deletions

View file

@ -16,12 +16,16 @@ module DocMachine::Build
opts.separator ""
opts.separator "Builder Options:"
opts.on("-a", "--action ACTION", "Action (watch, build, shell, etc.)") do |action|
config.action = action
end
opts.on("-d", "--data-dir DIR", "Content directory") do |dir|
config.data_dir = dir
end
opts.on("-a", "--action ACTION", "Action (watch, build, shell, etc.)") do |action|
config.action = action
opts.on("-p", "--port PORT", "Set base port to PORT") do |port|
config.port = port.to_i
end
opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do

View file

@ -4,6 +4,7 @@ module DocMachine::Build
property data_dir : String = Dir.current
property action : String = "watch"
property enable_tty : Bool = false
property port : Int32 = 5100
def initialize(@parent : DocMachine::Config)
end

View file

@ -30,6 +30,17 @@ module DocMachine::Build
self._pull_image()
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)
true
end
private def _avoid_duplicates
docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
@ -119,7 +130,7 @@ module DocMachine::Build
## Detect Mkdocs configuration - old format (full)
if File.exists?("#{@config.data_dir}/mkdocs.yml")
Log.info { "Mkdocs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" }
Log.info { "Docs: detected mkdocs.yml file. Please rename to mkdocs-patch.yml" }
exit 1
end
@ -127,27 +138,33 @@ module DocMachine::Build
if File.exists?("#{@config.data_dir}/mkdocs-patch.yml")
docker_opt_mkdocs_config = ["-v", "#{@config.data_dir}/mkdocs-patch.yml:/app/mkdocs-patch.yml"]
docker_opts.concat docker_opt_mkdocs_config
Log.info { "Mkdocs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" }
Log.info { "Docs: detected mkdocs-patch.yml file. Adding option to command line (#{docker_opt_mkdocs_config})" }
else
Log.info { "Mkdocs: no mkdocs-patch.yml detected. Using default files" }
end
## Detect slides
if Dir.exists?("#{@config.data_dir}/slides")
docker_opt_marp_port = ["-p", "5200:5200"]
docker_opts.concat docker_opt_marp_port
Log.info { "Slides: detected slides directory. Adding option to command line (#{docker_opt_marp_port})" }
else
Log.info { "Slides: no slides directory detected." }
Log.info { "Docs: no mkdocs-patch.yml detected. Using default files" }
end
## Detect docs
if Dir.exists?("#{@config.data_dir}/docs")
docker_opt_marp_port = ["-p", "5100:5100"]
docker_opts.concat docker_opt_marp_port
Log.info { "Slides: detected docs directory. Adding option to command line (#{docker_opt_marp_port})" }
Log.info { "Docs: detected docs directory." }
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" }
Log.info { "Docs: Adding option to command line (#{docker_opt_marp_port})" }
else
Log.info { "Slides: no slides docs detected." }
Log.info { "Docs: no docs detected." }
end
## Detect slides
if Dir.exists?("#{@config.data_dir}/slides")
Log.info { "Slides: detected slides directory." }
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})" }
Log.notice { "Slides: Using port #{marp_port} for slides" }
else
Log.info { "Slides: no slides directory detected." }
end
docker_opts << @docker_image