initial prototype
This commit is contained in:
commit
5858499113
5 changed files with 635 additions and 0 deletions
258
sbin/symbiosis-dns-generate
Executable file
258
sbin/symbiosis-dns-generate
Executable file
|
@ -0,0 +1,258 @@
|
|||
#!/usr/bin/ruby
|
||||
#
|
||||
# NAME
|
||||
#
|
||||
# symbiosis-dns-generate - Generate DNS snippet files for Symbiosis domains.
|
||||
#
|
||||
# USAGE
|
||||
#
|
||||
# symbiosis-dns-generate [ --sleep SEC | -s SEC ] [ --template TEMPLATE | -t TEMPLATE ]
|
||||
# [ --force | -f ] [ --verbose | -v ]
|
||||
# [ --help | -h ] [ DOMAIN ]
|
||||
#
|
||||
# SYNOPSIS
|
||||
#
|
||||
# --sleep SEC sleep for a random amount of time before doing
|
||||
# anything, up to a maximum of SEC seconds.
|
||||
#
|
||||
# --template TEMPLATE Specify an alternative template file to read.
|
||||
#
|
||||
# --force Force the re-creation of all DNS data.
|
||||
# --upload Force the upload of all DNS data.
|
||||
# --help Show the help information for this script.
|
||||
# --verbose Show debugging information.
|
||||
#
|
||||
# DETAILS
|
||||
#
|
||||
# This script is designed to iterate over the domains hosted upon a Symbiosis
|
||||
# system, and create TinyDNS snippets for each one. This can then be uploaded
|
||||
# to the Bytemark content DNS service.
|
||||
#
|
||||
# Domains can also be specified manually on the command line, in which case
|
||||
# only those domains will be processed.
|
||||
#
|
||||
# AUTHOR
|
||||
#
|
||||
# Steve Kemp <steve@bytemark.co.uk>
|
||||
# Adapted for Mythic Beasts by Ben Charlton <ben@spod.cx>
|
||||
#
|
||||
|
||||
|
||||
require 'getoptlong'
|
||||
|
||||
|
||||
#
|
||||
# Entry point to the code
|
||||
#
|
||||
force = false
|
||||
help = false
|
||||
$VERBOSE = false
|
||||
|
||||
#
|
||||
# Do we need to re-upload the data?
|
||||
#
|
||||
upload=true
|
||||
|
||||
#
|
||||
# The root directory -- '/' by default.
|
||||
#
|
||||
root = "/"
|
||||
dns_template = nil
|
||||
sleep_for = nil
|
||||
|
||||
opts = GetoptLong.new(
|
||||
[ '--force', '-f', GetoptLong::NO_ARGUMENT ],
|
||||
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
||||
[ '--sleep', '-s', GetoptLong::REQUIRED_ARGUMENT ],
|
||||
[ '--template', '-t', GetoptLong::REQUIRED_ARGUMENT ],
|
||||
[ '--upload', '-u', GetoptLong::NO_ARGUMENT ],
|
||||
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ]
|
||||
)
|
||||
|
||||
opts.each do |opt, arg|
|
||||
case opt
|
||||
when '--help'
|
||||
help = true
|
||||
when '--verbose'
|
||||
$VERBOSE = true
|
||||
when '--template'
|
||||
dns_template = arg
|
||||
when '--sleep'
|
||||
sleep_for = arg
|
||||
when '--root'
|
||||
root = arg
|
||||
when '--force'
|
||||
force = true
|
||||
when '--upload'
|
||||
upload = true
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# CAUTION! Here be quality kode.
|
||||
#
|
||||
if help
|
||||
# Open the file, stripping the shebang line
|
||||
lines = File.open(__FILE__){|fh| fh.readlines}[2..-1]
|
||||
|
||||
lines.each do |line|
|
||||
line.chomp!
|
||||
break if line.empty?
|
||||
puts line[2..-1].to_s
|
||||
end
|
||||
|
||||
exit 0
|
||||
end
|
||||
|
||||
def verbose(s)
|
||||
puts s if $VERBOSE
|
||||
end
|
||||
|
||||
require 'symbiosis/domains'
|
||||
require 'symbiosis/domain'
|
||||
require 'symbiosis/config_files/mythicdns'
|
||||
|
||||
#
|
||||
# Set the default paths.
|
||||
#
|
||||
dns_template = File.join(root, "/etc/symbiosis/dns.d/mythicdns.template.erb") if dns_template.nil?
|
||||
|
||||
#
|
||||
# Bail out if the template is missing
|
||||
#
|
||||
unless File.file?(dns_template)
|
||||
verbose "Unable generate DNS data because the template #{dns_template.inspect} is missing."
|
||||
exit 1
|
||||
end
|
||||
|
||||
#
|
||||
# Work out if we need to sleep.
|
||||
#
|
||||
unless sleep_for.nil?
|
||||
sleep_for = (sleep_for =~ /(\d+)/ ? rand($1.to_i) : 0)
|
||||
verbose "Sleeping for #{sleep_for}s before starting work"
|
||||
sleep sleep_for
|
||||
end
|
||||
|
||||
#
|
||||
# Any arguments on the command line specify which domains to do.
|
||||
#
|
||||
domains_to_configure = ARGV
|
||||
string_to_hash = []
|
||||
|
||||
#
|
||||
# For each domain.
|
||||
#
|
||||
Symbiosis::Domains.each do |domain|
|
||||
|
||||
verbose "Domain: #{domain.name} "
|
||||
|
||||
next unless domains_to_configure.empty? or domains_to_configure.include?(domain.name)
|
||||
|
||||
begin
|
||||
output = File.join(domain.config_dir, "dns", domain.name+".txt")
|
||||
output_dir = File.dirname(output)
|
||||
config = Symbiosis::ConfigFiles::Tinydns.new(output, "#")
|
||||
config.domain = domain
|
||||
config.template = dns_template
|
||||
|
||||
#
|
||||
# Should the snippet be created?
|
||||
#
|
||||
do_create = false
|
||||
|
||||
if ( force )
|
||||
verbose "\tForcing re-creation of snippet due to --force."
|
||||
do_create = true
|
||||
|
||||
elsif config.exists?
|
||||
|
||||
if config.changed?
|
||||
verbose "\tNot updating snippet, as it has been edited by hand."
|
||||
|
||||
elsif config.outdated?
|
||||
verbose "\tRe-creating snippet as it is out of date."
|
||||
do_create = true
|
||||
|
||||
else
|
||||
verbose "\tDomain already present and up-to date."
|
||||
|
||||
end
|
||||
|
||||
else
|
||||
verbose "\tConfiguring site for the first time"
|
||||
do_create = true
|
||||
|
||||
end
|
||||
|
||||
#
|
||||
#
|
||||
# Check the TinyDNS syntax.. TODO!
|
||||
#
|
||||
if do_create
|
||||
if config.ok?
|
||||
|
||||
verbose "\tWriting snippet to #{output}"
|
||||
|
||||
#
|
||||
# Create directory with the same ownership as the parent
|
||||
#
|
||||
domain.create_dir(output_dir) unless File.exist?(output_dir)
|
||||
|
||||
#
|
||||
# Write the snippet
|
||||
#
|
||||
config.write
|
||||
|
||||
#
|
||||
# Make sure the ownership is correct.
|
||||
#
|
||||
File.chown(domain.uid, domain.gid, config.filename)
|
||||
|
||||
else
|
||||
verbose "\tThe new DNS snippet is invalid -- no changes have been made."
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Rescue errors for this domain, but continue for others.
|
||||
#
|
||||
rescue StandardError => err
|
||||
verbose "\tUnable to create DNS data for #{domain.name} because #{err.to_s}"
|
||||
verbose "\t"+err.backtrace.join("\n\t")
|
||||
end
|
||||
end
|
||||
|
||||
begin
|
||||
|
||||
if upload
|
||||
upload_script = "/usr/sbin/symbiosis-mythic-dns"
|
||||
|
||||
verbose "Uploading using #{upload_script}"
|
||||
|
||||
IO.popen("#{upload_script} 2>&1","r") do |io|
|
||||
while !io.eof? do
|
||||
verbose io.readline
|
||||
end
|
||||
end
|
||||
|
||||
unless 0 == $?
|
||||
raise StandardError, "#{upload_script.inspect} failed."
|
||||
end
|
||||
|
||||
else
|
||||
verbose "No need to upload as no changes in the data have been detected."
|
||||
end
|
||||
rescue StandardError => err
|
||||
warn "Unable to upload DNS data because #{err.to_s}"
|
||||
verbose "\t"+err.backtrace.join("\n\t")
|
||||
exit 1
|
||||
end
|
||||
|
||||
#
|
||||
# All done.
|
||||
#
|
||||
|
||||
exit 0
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue