bin/expand-zone
changeset 5 1eb454630f47
parent 1 ea30c9b619b8
equal deleted inserted replaced
4:3a2221124592 5:1eb454630f47
    54             help="Expand given template variable in zone")
    54             help="Expand given template variable in zone")
    55 
    55 
    56     parser.add_option('--serial',               metavar='FILE',
    56     parser.add_option('--serial',               metavar='FILE',
    57             help="Read/expand serial from given .serial file")
    57             help="Read/expand serial from given .serial file")
    58 
    58 
    59     parser.add_option('--update-serial',        action='store_true',
       
    60             help="Update serial in given .serial file")
       
    61 
       
    62     # defaults
    59     # defaults
    63     parser.set_defaults(
    60     parser.set_defaults(
    64         loglevel            = logging.WARN,
    61         loglevel            = logging.WARN,
    65         expand              = [],
    62         expand              = [],
    66     )
    63     )
   107 
   104 
   108     else :
   105     else :
   109         # open
   106         # open
   110         return codecs.open(path, mode, charset)
   107         return codecs.open(path, mode, charset)
   111 
   108 
   112 def process_serial (path, update=False) :
   109 def process_serial (path) :
   113     """
   110     """
   114         Update/process new serial number from given file, based on date.
   111         Use serial number from given file.
   115 
   112 
   116         Returns the new serial as a string.
   113         Returns the new serial as a string.
   117     """
   114     """
   118 
   115 
   119     DATE_FMT = '%Y%m%d'
   116     if not os.path.exists(path) :
   120     DATE_LEN = 8
   117         raise Exception("Given --serial does not exist: %s" % path)
   121 
   118 
   122     SERIAL_FMT = "{date:8}{count:02}"
   119     return open(path).read().strip()
   123     SERIAL_LEN = 10
       
   124 
       
   125     if os.path.exists(path) :
       
   126         # read current
       
   127         serial = open(path).read().strip()
       
   128 
       
   129         assert len(serial) == SERIAL_LEN
       
   130 
       
   131         old_serial = int(serial)
       
   132 
       
   133         old_date = datetime.strptime(serial[:DATE_LEN], DATE_FMT).date()
       
   134         old_count = int(serial[DATE_LEN:])
       
   135         
       
   136     else :
       
   137         log.warn("given .serial does not exist, assuming from today: %s", path)
       
   138         old_serial = old_date = old_count = None
       
   139 
       
   140     if update :
       
   141         # update
       
   142         today = datetime.now().date()
       
   143 
       
   144         if not old_serial :
       
   145             # fresh start
       
   146             date = today
       
   147             count = 1
       
   148             
       
   149             log.info("Starting with fresh serial: %s:%s", date, count)
       
   150         
       
   151         elif old_date < today :
       
   152             # update date
       
   153             date = today
       
   154             count = 1
       
   155             
       
   156             log.info("Updating to today: %s -> %s", old_date, date)
       
   157 
       
   158         elif old_date == today :
       
   159             # keep date, update count
       
   160             date = old_date
       
   161             count = old_count + 1
       
   162 
       
   163             if count > 99 :
       
   164                 raise Exception("Serial update rollover: %s, %s", date, count)
       
   165 
       
   166             log.info("Updating today's count: %s, %s", date, count)
       
   167 
       
   168         else :
       
   169             raise Exception("Invalid serial: %s:%s", old_date, old_count)
       
   170 
       
   171     else :
       
   172         date = old_date
       
   173         count = old_count
       
   174 
       
   175     serial = SERIAL_FMT.format(date=date.strftime(DATE_FMT), count=count)
       
   176 
       
   177     open(path, 'w').write(serial)
       
   178 
       
   179     return serial
       
   180 
   120 
   181 def parse_expand (expand) :
   121 def parse_expand (expand) :
   182     """
   122     """
   183         Parse an --expand foo=bar to (key, value)
   123         Parse an --expand foo=bar to (key, value)
   184     """
   124     """
   195     # expands
   135     # expands
   196     expand = dict(parse_expand(expand) for expand in options.expand)
   136     expand = dict(parse_expand(expand) for expand in options.expand)
   197 
   137 
   198     # serial?
   138     # serial?
   199     if options.serial :
   139     if options.serial :
   200         serial = process_serial(options.serial, update=options.update_serial)
   140         serial = process_serial(options.serial)
   201 
   141 
   202         expand['serial'] = serial
   142         expand['serial'] = serial
   203 
   143 
   204     # input
   144     # input
   205     if args :
   145     if args :