def sync(verbose=False): ignore_devices_on_servers_re = None if config.has_option('inventory', 'ignore_devices_on_servers_re'): ignore_devices_on_servers_re = config.get('inventory', 'ignore_devices_on_servers_re') from_inv = get_devices( config.get('inventory', 'url'), config.get('inventory', 'filter'), config.get('inventory', 'username'), config.get('inventory', 'password'), ignore_devices_on_servers_re, verbose=verbose) # dump the db second, since otherwise the mysql server can go away while # get_devices is still running, which is no fun from_db = data.dump_devices() for task in merge_devices(from_db, from_inv): if task[0] == 'insert': if verbose: print "insert device", task[1]['fqdn'] data.insert_device(task[1]) elif task[0] == 'delete': if verbose: print "delete device", task[2] data.delete_device(task[1]) elif task[0] == 'update': if verbose: print "update device", task[2] data.update_device(task[1], task[2]) else: raise AssertionError('%s is not a task' % task[0])
def sync(db, verbose=False, ship_it=False): ignore_devices_on_servers_re = None if config.has_option('inventory', 'ignore_devices_on_servers_re'): ignore_devices_on_servers_re = config.get('inventory', 'ignore_devices_on_servers_re') from_inv = get_devices( config.get('inventory', 'url'), config.get('inventory', 'filter'), config.get('inventory', 'username'), config.get('inventory', 'password'), ignore_devices_on_servers_re, verbose=verbose) # dump the db second, since otherwise the mysql server can go away while # get_devices is still running, which is no fun from_db = db.inventorysync.dump_devices() ## get a list of relay_boards derived from the inventory dump relay_boards_from_inv = get_relay_boards(from_inv) ## get existing relay_board list from DB relay_boards_from_db = db.inventorysync.dump_relays() # get the list of changes that need to be made tasks = list(merge_devices(from_db, from_inv)) # If there are too many changes, bail out and await human interaction. # "Too many" means more than 5 and more than a tenth of the larger of the # set of devices currently in inventory and the set in the DB. This is a # failsafe to keep the inventory sync from unexpectedly erasing all # devices. if len(tasks) > max(5, len(from_db) / 10, len(from_inv) / 10) and not ship_it: raise RuntimeError("%d changes: pass --ship-it to make these changes" % len(tasks)) # start merging devices for task in tasks: if task[0] == 'insert': if verbose: print "insert device", task[1]['fqdn'] db.inventorysync.insert_device(task[1]) elif task[0] == 'delete': if verbose: print "delete device", task[2] db.inventorysync.delete_device(task[1]) elif task[0] == 'update': if verbose: print "update device", task[2] db.inventorysync.update_device(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0]) # start merging relay_boards for task in merge_relay_boards(relay_boards_from_db, relay_boards_from_inv): if task[0] == 'insert': if verbose: print "insert relay_board", task[1]['fqdn'] db.inventorysync.insert_relay_board(task[1]) elif task[0] == 'delete': if verbose: print "delete relay_board", task[2] db.inventorysync.delete_relay_board(task[1]) elif task[0] == 'update': if verbose: print "update relay_board", task[2] db.inventorysync.update_relay_board(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0])
def sync(db, verbose=False): ignore_devices_on_servers_re = None if config.has_option('inventory', 'ignore_devices_on_servers_re'): ignore_devices_on_servers_re = config.get('inventory', 'ignore_devices_on_servers_re') from_inv = get_devices( config.get('inventory', 'url'), config.get('inventory', 'filter'), config.get('inventory', 'username'), config.get('inventory', 'password'), ignore_devices_on_servers_re, verbose=verbose) # dump the db second, since otherwise the mysql server can go away while # get_devices is still running, which is no fun from_db = db.inventorysync.dump_devices() ## get a list of relay_boards derived from the inventory dump relay_boards_from_inv = get_relay_boards(from_inv) ## get existing relay_board list from DB relay_boards_from_db = db.inventorysync.dump_relays() # start merging devices for task in merge_devices(from_db, from_inv): if task[0] == 'insert': if verbose: print "insert device", task[1]['fqdn'] db.inventorysync.insert_device(task[1]) elif task[0] == 'delete': if verbose: print "delete device", task[2] db.inventorysync.delete_device(task[1]) elif task[0] == 'update': if verbose: print "update device", task[2] db.inventorysync.update_device(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0]) # start merging relay_boards for task in merge_relay_boards(relay_boards_from_db, relay_boards_from_inv): if task[0] == 'insert': if verbose: print "insert relay_board", task[1]['fqdn'] db.inventorysync.insert_relay_board(task[1]) elif task[0] == 'delete': if verbose: print "delete relay_board", task[2] db.inventorysync.delete_relay_board(task[1]) elif task[0] == 'update': if verbose: print "update relay_board", task[2] db.inventorysync.update_relay_board(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0])
def sync(db, verbose=False, ship_it=False): ignore_devices_on_servers_re = None if config.has_option('inventory', 'ignore_devices_on_servers_re'): ignore_devices_on_servers_re = config.get( 'inventory', 'ignore_devices_on_servers_re') from_inv = get_devices(config.get('inventory', 'url'), config.get('inventory', 'filter'), config.get('inventory', 'username'), config.get('inventory', 'password'), ignore_devices_on_servers_re, verbose=verbose) # dump the db second, since otherwise the mysql server can go away while # get_devices is still running, which is no fun from_db = db.inventorysync.dump_devices() ## get a list of relay_boards derived from the inventory dump relay_boards_from_inv = get_relay_boards(from_inv) ## get existing relay_board list from DB relay_boards_from_db = db.inventorysync.dump_relays() # get the list of changes that need to be made tasks = list(merge_devices(from_db, from_inv)) # If there are too many changes, bail out and await human interaction. # "Too many" means more than 5 and more than a tenth of the larger of the # set of devices currently in inventory and the set in the DB. This is a # failsafe to keep the inventory sync from unexpectedly erasing all # devices. if len(tasks) > max(5, len(from_db) / 10, len(from_inv) / 10) and not ship_it: raise RuntimeError("%d changes: pass --ship-it to make these changes" % len(tasks)) # start merging devices for task in tasks: if task[0] == 'insert': if verbose: print "insert device", task[1]['fqdn'] db.inventorysync.insert_device(task[1]) elif task[0] == 'delete': if verbose: print "delete device", task[2] db.inventorysync.delete_device(task[1]) elif task[0] == 'update': if verbose: print "update device", task[2] db.inventorysync.update_device(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0]) # start merging relay_boards for task in merge_relay_boards(relay_boards_from_db, relay_boards_from_inv): if task[0] == 'insert': if verbose: print "insert relay_board", task[1]['fqdn'] db.inventorysync.insert_relay_board(task[1]) elif task[0] == 'delete': if verbose: print "delete relay_board", task[2] db.inventorysync.delete_relay_board(task[1]) elif task[0] == 'update': if verbose: print "update relay_board", task[2] db.inventorysync.update_relay_board(task[1], task[2]) else: raise RuntimeError('%s is not a task' % task[0])