Skip to content

Commit

Permalink
Add ability to output the selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
tknarr committed Jun 14, 2016
1 parent 5e358f3 commit 55e4ec6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ OpenDKIM package before using this tool.
* `-n` : use next month instead of this month when automatically generating a selector
* `-v` : log additional informational messages while processing
* `--no-dns` : do not attempt to automatically update DNS records
* `--selector` : output the selector and do nothing else

If no `selector` is specified, one will be automatically generated based on the current
month (or the next month if `-n` was used). Standard practice would be to omit the
Expand All @@ -56,6 +57,12 @@ records with the data in the generated `.txt` files, otherwise the script will t
to automatically update the DNS records for all domains it's got DNS API support and
information for.

The `--selector` option can be used to cause the tool to output the generated selector
on standard output for capture by a script. The `-n` option can be used in conjunction
with `--selector`, other options will have no effect when `--selector` is specified.
This is to assist with scripts to automatically upload the generated data files to a
server for installation.

The following options are also available for development and debugging. They should
not be used under normal circumstances ("If you don't know what it's going to do, _DO
NOT_ push the button." is a good rule to live by).
Expand Down
45 changes: 28 additions & 17 deletions genkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ def find_dnsapi_modules( pn ):
help = "Log debugging info and do not update DNS" )
parser.add_argument( "--use-null", dest = 'use_null_dnsapi', action = 'store_true',
help = "Silently use the null DNS API instead of the real API" )
parser.add_argument( "--selector", dest='output_selector', action = 'store_true',
help = "Causes the generated selector to be output" )
parser.add_argument( "selector", nargs = '?', default = None, help = "Selector to use" )
args = parser.parse_args()

Expand All @@ -215,12 +217,37 @@ def find_dnsapi_modules( pn ):
level = logging.WARN
if args.log_debug:
level = logging.DEBUG
logging.basicConfig( level = level, format = "%(levelname)s: %(message)s" )

should_update_dns = args.update_dns
if never_update_dns:
should_update_dns = False

should_output_selector = args.output_selector
if should_output_selector:
should_update_dns = False
level = logging.ERROR

logging.basicConfig( level = level, format = "%(levelname)s: %(message)s" )

# If we weren't given an explicit selector, the default is YYYYMM based on
# either this month or next month.
selector = args.selector
if selector == None:
selector_date = datetime.date.today().replace( day = 1 )
if args.next_month:
y = selector_date.year
m = selector_date.month
m += 1
if m > 12:
m = 1
y += 1
selector_date = selector_date.replace( year = y, month = m )
selector = selector_date.strftime( "%Y%m" )
logging.info( "Selector: %s", selector )
if should_output_selector:
print selector
sys.exit( 0 )

# Process dnsapi.ini
# If we're supposed to update DNS records but don't have any definitions for
# the DNS APIs, we record an error but we can continue to generate the keys
Expand All @@ -246,22 +273,6 @@ def find_dnsapi_modules( pn ):
if item[1] not in key_names:
key_names.append( item[1] )

# If we weren't given an explicit selector, the default is YYYYMM based on
# either this month or next month.
selector = args.selector
if selector == None:
selector_date = datetime.date.today().replace( day = 1 )
if args.next_month:
y = selector_date.year
m = selector_date.month
m += 1
if m > 12:
m = 1
y += 1
selector_date = selector_date.replace( year = y, month = m )
selector = selector_date.strftime( "%Y%m" )
logging.info( "Selector: %s", selector )

# Generate our keys, one per key name
keys = {} # Key = key name (field 1) from domain_data[n], Value = key data dict
for target in key_names:
Expand Down

0 comments on commit 55e4ec6

Please sign in to comment.