feat: add option for multiple instances
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Glenn Y. Rolland 2024-02-14 15:29:36 +01:00
parent a007211e29
commit 9d13f8216f
3 changed files with 12 additions and 4 deletions

View file

@ -28,6 +28,10 @@ module DocMachine::Build
config.port = port.to_i config.port = port.to_i
end end
opts.on("-m", "--multiple", "Allow multiple instances per dir" ) do |port|
config.multiple_instances = true
end
opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do opts.on("-t", "--tty", "Enable TTY mode (needed for shell)") do
config.enable_tty = true config.enable_tty = true
end end

View file

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

View file

@ -11,7 +11,8 @@ module DocMachine::Build
Log = DocMachine::Build::Log.for("run") Log = DocMachine::Build::Log.for("run")
def initialize(@config : DocMachine::Build::Config) def initialize(@config : DocMachine::Build::Config)
@basehash = Digest::SHA256.hexdigest(@config.data_dir)[0..6] data = "#{@config.data_dir}:#{@config.port}"
@basehash = Digest::SHA256.hexdigest(data)[0..6]
@docker_name = "docmachine-#{@basehash}" @docker_name = "docmachine-#{@basehash}"
@docker_image = "glenux/docmachine:latest" @docker_image = "glenux/docmachine:latest"
@docker_opts = [] of String @docker_opts = [] of String
@ -27,8 +28,8 @@ module DocMachine::Build
Log.info { "docker_image = #{@docker_image}" } Log.info { "docker_image = #{@docker_image}" }
Log.info { "action = #{@config.action}" } Log.info { "action = #{@config.action}" }
self._avoid_duplicates()
self._pull_image() self._pull_image()
self._avoid_duplicates() unless @config.multiple_instances
end end
private def _find_port(port_base) private def _find_port(port_base)
@ -48,13 +49,15 @@ module DocMachine::Build
end end
private def _avoid_duplicates 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 docker_cid = %x{docker ps -f "name=#{@docker_name}" -q}.strip
Log.info { "docker_name: #{@docker_name}" } Log.info { "Multiple Instances: docker_name: #{@docker_name}" }
Log.info { "docker_cid: #{docker_cid}" } Log.info { "Multiple Instances: docker_cid: #{docker_cid || "-"}" }
if !docker_cid.empty? if !docker_cid.empty?
Process.run("docker", ["kill", @docker_name]) Process.run("docker", ["kill", @docker_name])
Process.run("docker", ["rm", @docker_name])
end end
end end