Differentiate between creating a fresh container and recreating an already existing one.

This commit is contained in:
Fabian Schlenz 2020-09-04 06:14:16 +02:00
parent 33a8df5ac1
commit 1ee6de03c1
1 changed files with 13 additions and 1 deletions

14
dup.rb
View File

@ -120,14 +120,16 @@ def action_help
exit 1
end
def run_cmd(cmd, ignore_returnvalue=false, catch_interrupt=false, ignore_dry_run: false)
verbose "+ #{cmd}"
returnvalue=false
begin
returnvalue = $dry_run ? true : system("bash -c #{cmd.shellescape}")
returnvalue = ($dry_run && !ignore_dry_run) ? true : system("bash -c #{cmd.shellescape}")
rescue Interrupt
raise if not catch_interrupt
end
raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue
return returnvalue
end
def esc(obj, escape=true)
@ -203,6 +205,10 @@ class Container
@name = name
end
def exists?
run_cmd("docker inspect --format='1' #{@name.shellescape} >/dev/null 2>&1", true, ignore_dry_run: true)
end
def load
raise "File #{self.filename} not found." unless File.exists?(self.filename)
@ -333,6 +339,12 @@ class Container
self.pull
end
if self.exists?
puts "Recreating #{@name}..."
else
puts "Creating #{@name}..."
end
self.stop_and_remove
self.create_networks