|
@@ -66,6 +66,8 @@ Example: '{"database_file": "/path/to/dbfile/db.sqlite3"}'""",
|
|
|
parser.add_option("-d", "--debug", dest="debug_level",
|
|
|
type='int', action="store", default=None,
|
|
|
help="enable debug logs with the specified level [0-99]")
|
|
|
+ parser.add_option("-e", "--empty", dest="empty_zone",
|
|
|
+ action="store_true", help="empty zone content (no load)")
|
|
|
parser.add_option("-i", "--report-interval", dest="report_interval",
|
|
|
type='int', action="store",
|
|
|
default=LOAD_INTERVAL_DEFAULT,
|
|
@@ -113,6 +115,7 @@ class LoadZoneRunner:
|
|
|
self._datasrc_type = None
|
|
|
self._log_severity = 'INFO'
|
|
|
self._log_debuglevel = 0
|
|
|
+ self._empty_zone = False
|
|
|
self._report_interval = LOAD_INTERVAL_DEFAULT
|
|
|
self._start_time = None
|
|
|
# This one will be used in (rare) cases where we want to allow tests to
|
|
@@ -140,7 +143,8 @@ class LoadZoneRunner:
|
|
|
'''
|
|
|
|
|
|
usage_txt = \
|
|
|
- 'usage: %prog [options] -c datasrc_config zonename zonefile'
|
|
|
+ 'usage: %prog [options] -c datasrc_config zonename zonefile\n' + \
|
|
|
+ ' %prog [options] -c datasrc_config -e zonename'
|
|
|
parser = OptionParser(usage=usage_txt)
|
|
|
set_cmd_options(parser)
|
|
|
(options, args) = parser.parse_args(args=self.__command_args)
|
|
@@ -174,15 +178,22 @@ class LoadZoneRunner:
|
|
|
'Invalid report interval (must be non negative): %d' %
|
|
|
self._report_interval)
|
|
|
|
|
|
- if len(args) != 2:
|
|
|
- raise BadArgument('Unexpected number of arguments: %d (must be 2)'
|
|
|
- % (len(args)))
|
|
|
+ if options.empty_zone:
|
|
|
+ self._empty_zone = True
|
|
|
+
|
|
|
+ # Check number of non option arguments: must be 1 with -e; 2 otherwise.
|
|
|
+ num_args = 1 if self._empty_zone else 2
|
|
|
+
|
|
|
+ if len(args) != num_args:
|
|
|
+ raise BadArgument('Unexpected number of arguments: %d (must be %d)'
|
|
|
+ % (len(args), num_args))
|
|
|
try:
|
|
|
self._zone_name = Name(args[0])
|
|
|
except Exception as ex: # too broad, but there's no better granurality
|
|
|
raise BadArgument("Invalid zone name '" + args[0] + "': " +
|
|
|
str(ex))
|
|
|
- self._zone_file = args[1]
|
|
|
+ if len(args) > 1:
|
|
|
+ self._zone_file = args[1]
|
|
|
|
|
|
def _get_datasrc_config(self, datasrc_type):
|
|
|
''''Return the default data source configuration of given type.
|
|
@@ -254,6 +265,34 @@ class LoadZoneRunner:
|
|
|
else:
|
|
|
logger.info(LOADZONE_ZONE_UPDATING, self._zone_name,
|
|
|
self._zone_class)
|
|
|
+ if self._empty_zone:
|
|
|
+ self.__make_empty_zone(datasrc_client)
|
|
|
+ else:
|
|
|
+ self.__load_from_file(datasrc_client)
|
|
|
+ except Exception as ex:
|
|
|
+ if created:
|
|
|
+ datasrc_client.delete_zone(self._zone_name)
|
|
|
+ logger.error(LOADZONE_CANCEL_CREATE_ZONE, self._zone_name,
|
|
|
+ self._zone_class)
|
|
|
+ raise LoadFailure(str(ex))
|
|
|
+
|
|
|
+ def __make_empty_zone(self, datasrc_client):
|
|
|
+ """Subroutine of _do_load(), create an empty zone or make it empty."""
|
|
|
+ try:
|
|
|
+ updater = datasrc_client.get_updater(self._zone_name, True)
|
|
|
+ updater.commit()
|
|
|
+ logger.info(LOADZONE_EMPTY_DONE, self._zone_name,
|
|
|
+ self._zone_class)
|
|
|
+ except Exception:
|
|
|
+ # once updater is created, it's very unlikely that commit() fails,
|
|
|
+ # but in case it happens, clear updater to release any remaining
|
|
|
+ # lock.
|
|
|
+ updater = None
|
|
|
+ raise
|
|
|
+
|
|
|
+ def __load_from_file(self, datasrc_client):
|
|
|
+ """Subroutine of _do_load(), load a zone file into data source."""
|
|
|
+ try:
|
|
|
loader = ZoneLoader(datasrc_client, self._zone_name,
|
|
|
self._zone_file)
|
|
|
self._start_time = time.time()
|
|
@@ -279,14 +318,14 @@ class LoadZoneRunner:
|
|
|
sys.stdout.write('\n')
|
|
|
# record the final count of the loaded RRs for logging
|
|
|
self._loaded_rrs = loader.get_rr_count()
|
|
|
- except Exception as ex:
|
|
|
+
|
|
|
+ total_elapsed_txt = "%.2f" % (time.time() - self._start_time)
|
|
|
+ logger.info(LOADZONE_DONE, self._loaded_rrs, self._zone_name,
|
|
|
+ self._zone_class, total_elapsed_txt)
|
|
|
+ except Exception:
|
|
|
# release any remaining lock held in the loader
|
|
|
loader = None
|
|
|
- if created:
|
|
|
- datasrc_client.delete_zone(self._zone_name)
|
|
|
- logger.error(LOADZONE_CANCEL_CREATE_ZONE, self._zone_name,
|
|
|
- self._zone_class)
|
|
|
- raise LoadFailure(str(ex))
|
|
|
+ raise
|
|
|
|
|
|
def _set_signal_handlers(self):
|
|
|
signal.signal(signal.SIGINT, self._interrupt_handler)
|
|
@@ -302,9 +341,6 @@ class LoadZoneRunner:
|
|
|
self._set_signal_handlers()
|
|
|
self._parse_args()
|
|
|
self._do_load()
|
|
|
- total_elapsed_txt = "%.2f" % (time.time() - self._start_time)
|
|
|
- logger.info(LOADZONE_DONE, self._loaded_rrs, self._zone_name,
|
|
|
- self._zone_class, total_elapsed_txt)
|
|
|
return 0
|
|
|
except BadArgument as ex:
|
|
|
logger.error(LOADZONE_ARGUMENT_ERROR, ex)
|