Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kennethkalmer/505732d880aa420d647d to your computer and use it in GitHub Desktop.
Save kennethkalmer/505732d880aa420d647d to your computer and use it in GitHub Desktop.
Apply a PowerDNS on Rails macro to a bunch of domains on the command line

Bulk macros

Throw this script in the tmp directory of where your PowerDNS on Rails installation lives, make is executable and run like this:

$ ./tmp/apply-macro.rb 1 example.com example.org example.net

It will load the macro with ID 1, and apply it against each domain passed as CLI arguments.

#!/usr/bin/env ruby
# Usage:
# ./tmp/apply-macro.rb MACRO_ID [domains [domain [domain]]]
# Pass all the domains as arguments to the script, or modify the script to read from a file...
unless Object.new.respond_to?(:require_relative, true)
def require_relative(relative_feature)
file = caller.first.split(/:\d/,2).first
raise LoadError, "require_relative is called in #{$1}" if /\A\((.*)\)/ =~ file
require File.expand_path(relative_feature, File.dirname(file))
end
end
require_relative('../config/environment')
MACRO = nil
begin
MACRO = Macro.find( ARGV.shift )
rescue ActiveRecord::RecordNotFound
puts "Invalid macro id specified as first argument"
puts
exit 1
end
domains = Domain.find( :all, :conditions => { :name => ARGV } )
Domain.transaction do
domains.each do |domain|
puts "Applying macro to #{domain.name}"
MACRO.apply_to( domain )
end
end
puts "Applied macro to #{domains.count} domains."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment