#!/usr/bin/env oo-ruby
#--
# Copyright 2010 Red Hat, Inc.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#++

require 'rubygems'
require 'parseconfig'
require 'getoptlong'

def usage
  puts <<USAGE
== Synopsis

oo-register-dns: Register node's DNS name with Bind
  This command must be run as root.

== Usage

oo-register-dns --with-node-hostname node1 \\
                --with-node-ip 192.168.0.1 \\
                --domain example.com \\
                --dns-server broker.example.com

== List of arguments
  -h|--with-node-hostname   host        Hostname for the node (required)
  -n|--with-node-ip         ip          IP of the node (required)
  -d|--domain               domain      Domain name for this node (optional, default: example.com)  
  -s|--dns-server           server      IP address or hostname of DNS server to update (optional, default: 127.0.0.1)  
  -k|--key-file             file        Bind key (optional, default: /var/named/<domain name>.key)  
  -g|--gss-tsig                         Use GSS-TSIG Kerberos credentials to bind (optional)
  -?|--help                             Print this message

USAGE
  exit 255
end

opts = GetoptLong.new(
    ["--with-node-hostname",  "-h", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-node-ip",        "-n", GetoptLong::REQUIRED_ARGUMENT],
    ["--domain",              "-d", GetoptLong::OPTIONAL_ARGUMENT],    
    ["--dns-server",          "-s", GetoptLong::OPTIONAL_ARGUMENT],
    ["--key-file",            "-k", GetoptLong::OPTIONAL_ARGUMENT],        
    ["--gss-tsig",            "-g", GetoptLong::NO_ARGUMENT],
    ["--help",                "-?", GetoptLong::NO_ARGUMENT]
)

args = {}
begin
  opts.each{ |k,v| args[k]=v }
rescue GetoptLong::Error => e
  usage
end

ip = args["--with-node-ip"]
node_hostname = args["--with-node-hostname"]
node_domain = args["--domain"] || "example.com"
server = args["--dns-server"] || "127.0.0.1"
key = args["--key-file"] || "/var/named/#{node_domain}.key"

if args["--help"] || (ip.nil? || ip.empty? || node_hostname.nil? || node_hostname.empty?)
  usage
end

command =<<-EOF
server #{server}
update delete #{node_hostname}.#{node_domain} A
update add #{node_hostname}.#{node_domain} 180 A #{ip}
send
EOF

if args["--gss-tsig"]
  system "nsupdate -g <<EOF\n#{command}\nEOF"
elsif args["--key-file"] or File.exists? key
  # We were explicitly told to use a keyfile, or a keyfile exists at the
  # default location.
  system "nsupdate -k #{key} <<EOF\n#{command}\nEOF"
else
  # Try to do an unauthenticated update.
  system "nsupdate <<EOF\n#{command}\nEOF"
end
