Parsing command line arguments via GetoptLong.

This commit is contained in:
Fabian Schlenz 2017-02-10 09:00:05 +01:00
parent 8900173016
commit 1b770d45d8

140
dup.rb
View File

@ -2,6 +2,8 @@
require 'yaml' require 'yaml'
require 'pp' require 'pp'
require 'shellwords' require 'shellwords'
require 'getoptlong'
def get_sample(name="container") def get_sample(name="container")
return <<~HEREDOC return <<~HEREDOC
@ -49,75 +51,113 @@ def get_sample(name="container")
HEREDOC HEREDOC
end end
if ARGV[0]=="--sample" def action_sample
puts get_sample puts get_sample
exit 1 exit 1
end 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) def run(cmd, ignore_returnvalue=false)
puts "+ #{cmd}" puts "+ #{cmd}"
returnvalue = true returnvalue = $dry_run ? true : system(cmd)
returnvalue = system(cmd)
raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue raise "Command returned a non-zero exit value." if returnvalue!=true && !ignore_returnvalue
end end
if ARGV[0]=="--create" def action_create(container, file)
filename = ARGV[1]
else
filename = ARGV[0]
end
if filename==nil || filename==""
raise "No name given."
end
file = "%s/%s.yml" % [ "/data/fabian/projects/dup", filename ]
if ARGV[0]=="--create"
raise "File #{file} already exists" if File.exists?(file) raise "File #{file} already exists" if File.exists?(file)
File.open(file, "w") {|f| f.write(get_sample(filename))} File.open(file, "w") {|f| f.write(get_sample(container))}
exec("joe #{file.shellescape}") exec("joe #{file.shellescape}")
exit 1 # will never be reached because exec replaces this process. But just in case... ;-) exit 1 # will never be reached because exec replaces this process. But just in case... ;-)
end end
raise "File #{file} not found." unless File.exists?(file) def action_run(container, file)
raise "File #{file} not found." unless File.exists?(file)
data = File.open(file, "r") {|f| YAML.load(f.read)} data = File.open(file, "r") {|f| YAML.load(f.read)}
raise "Expected #{file} to define (at least) a container named #{filename}." unless data.has_key?(filename) raise "Expected #{file} to define (at least) a container named #{container}." unless data.has_key?(container)
data.each do |key, data| data.each do |key, data|
if data["build"] if data["build"]
(data["before_build"] || []).each{|cmd| run(cmd)} (data["before_build"] || []).each{|cmd| run(cmd)}
cmd = ["docker", "build", "-t", data["image"].shellescape, data["build"].shellescape] 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(" ")) run(cmd.join(" "))
(data["after_build"] || []).each{|cmd| run(cmd)}
(data["after_run"] || []).each{|cmd| run(cmd)}
end 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