docker-tools/dup.rb

164 lines
4.2 KiB
Ruby
Executable File

#!/usr/bin/ruby
require 'yaml'
require 'pp'
require 'shellwords'
require 'getoptlong'
def get_sample(name="container")
return <<~HEREDOC
#{name}:
image: "my/image:1.2.3"
restart: always
command: "/bin/bash"
build: "/data/dir"
ports:
- "1234:1234"
- "8080"
- "1001:1001/udp"
environment:
VERSION: 1.7.0
TZ:
volumes:
- "/etc:/etc:ro"
links:
- "another_container"
container_name: "Something"
mem_limit: 125M
interactive: false
detach: true
remove: false
stop_signal: SIGUSR1
stop_grace_period: 5s
before_build:
- "echo 'Starting build'"
after_build:
- "echo 'After build'"
before_run:
- "echo 'Starting run'"
after_run:
- "echo 'Run has finished.'"
- "docker restart container"
devices:
- "/dev/ttyUSB0:/dev/ttyUSB0"
HEREDOC
end
def action_sample
puts get_sample
exit 1
end
def action_help
puts "-h, --help Show this help."
puts "-n, --dry-run Don't execute any commands."
puts "-s, --sample Outputs a sample yml file."
puts "-c, --create Create a new container yml file."
exit 1
end
def run(cmd, ignore_returnvalue=false)
puts "+ #{cmd}"
returnvalue = $dry_run ? true : system(cmd)
raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue
end
def action_create(container, file)
raise "File #{file} already exists" if File.exists?(file)
File.open(file, "w") {|f| f.write(get_sample(container))}
exec("joe #{file.shellescape}")
exit 1 # will never be reached because exec replaces this process. But just in case... ;-)
end
def action_run(container, file)
raise "File #{file} not found." unless File.exists?(file)
data = File.open(file, "r") {|f| YAML.load(f.read)}
raise "Expected #{file} to define (at least) a container named #{container}." unless data.has_key?(container)
data.each do |key, data|
if data["build"]
(data["before_build"] || []).each{|cmd| run(cmd)}
cmd = ["docker", "build", "-t", data["image"].shellescape, data["build"].shellescape]
run(cmd.join(" "))
(data["after_build"] || []).each{|cmd| run(cmd)}
end
(data["before_run"] || []).each{|cmd| run(cmd)}
cmd = ["docker", "run"]
data["interactive"] && cmd << "--tty" << "--interactive"
data["detach"]===false || cmd << "--detach"
data["remove"] && cmd << "--rm"
cmd << "--name=#{(data["container_name"] || key).shellescape}"
(data["ports"] || []).each {|p| cmd << "--publish" << p.to_s.shellescape}
data["restart"] && cmd << "--restart=#{data["restart"].shellescape}"
(data["environment"] || []).each {|key, val| cmd << "--env" << (val ? "#{key}=#{val.to_s.shellescape}" : key)}
(data["volumes"] || []).each {|vol| cmd << "--volume" << vol.shellescape}
data["mem_limit"] && cmd << "--memory=#{data["mem_limit"].shellescape}"
(data["links"] || []).each {|link| cmd << "--link" << link.shellescape}
data["stop_signal"] && cmd << "--stop-signal=#{data["stop_signal"].shellescape}"
data["stop_grace_period"] && cmd << "--stop-timeout=#{data["stop_grace_period"].shellescape}"
(data["devices"] || []).each {|dev| cmd << "--device" << dev.shellescape}
cmd << data["image"].shellescape
data["command"] && cmd << data["command"]
run("docker rm -f #{(data["container_name"] || key).shellescape}", true)
run(cmd.join(" "))
(data["after_run"] || []).each{|cmd| run(cmd)}
end
end
action = :run
container = nil
$dry_run = false
opts = GetoptLong.new(
[ '--sample', '-s', GetoptLong::NO_ARGUMENT ],
[ '--create', '-c', GetoptLong::NO_ARGUMENT ],
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
[ '--dry-run', '-n', GetoptLong::NO_ARGUMENT ]
)
opts.each do |opt, arg|
case opt
when '--sample'
action_sample
when '--create'
action = :create
when '--help'
action_help
when '--dry-run'
puts "Dry-run. Not going to execute any command."
$dry_run = true
end
end
container = ARGV.shift
if container==nil || container==""
raise "No container given."
end
file = "%s/%s.yml" % [ "/data/fabian/projects/dup", container ]
if action == :create
action_create(container, file)
elsif action == :run
action_run(container, file)
end