scripts: match servers by name prefix (SVR-) as well as computer type
This commit is contained in:
@@ -7,8 +7,8 @@ audit history all carry over. Only the extension row is swapped (computers ->
|
|||||||
networkdevices) and the asset's type is flipped, and the network device is given
|
networkdevices) and the asset's type is flipped, and the network device is given
|
||||||
the 'Server' networkdevicetype.
|
the 'Server' networkdevicetype.
|
||||||
|
|
||||||
Identify servers by their computer type name (default 'Server'); pass --type to
|
Identify servers by a name/hostname prefix (default 'SVR-'), or pass --type to
|
||||||
match a different one.
|
match an exact computer type instead.
|
||||||
|
|
||||||
Usage (on the target instance, in the app dir):
|
Usage (on the target instance, in the app dir):
|
||||||
DATABASE_URL=... venv/bin/python scripts/reclassify_servers_to_network.py # dry run
|
DATABASE_URL=... venv/bin/python scripts/reclassify_servers_to_network.py # dry run
|
||||||
@@ -41,9 +41,13 @@ def _get_or_create_networkdevicetype(name):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
parser.add_argument('--type', default='Server',
|
group = parser.add_mutually_exclusive_group()
|
||||||
help="computer type name that holds the servers (default 'Server')")
|
group.add_argument('--prefix', default='SVR-',
|
||||||
|
help="match servers by name/assetnumber/hostname starting "
|
||||||
|
"with this (default 'SVR-')")
|
||||||
|
group.add_argument('--type',
|
||||||
|
help="instead of --prefix, match by an exact computer type name")
|
||||||
parser.add_argument('--commit', action='store_true',
|
parser.add_argument('--commit', action='store_true',
|
||||||
help='apply the change; without it this is a dry run')
|
help='apply the change; without it this is a dry run')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
@@ -54,21 +58,30 @@ def main():
|
|||||||
if not nd_assettype:
|
if not nd_assettype:
|
||||||
sys.exit("No 'network_device' asset type - is the network plugin installed?")
|
sys.exit("No 'network_device' asset type - is the network plugin installed?")
|
||||||
|
|
||||||
ctype = ComputerType.query.filter_by(computertype=args.type).first()
|
query = Computer.query.join(Asset)
|
||||||
if not ctype:
|
if args.type:
|
||||||
sys.exit("No computer type named %r. Pass --type with the exact name."
|
ctype = ComputerType.query.filter_by(computertype=args.type).first()
|
||||||
% args.type)
|
if not ctype:
|
||||||
|
sys.exit("No computer type named %r." % args.type)
|
||||||
|
query = query.filter(Computer.computertypeid == ctype.computertypeid)
|
||||||
|
criterion = "computer type %r" % args.type
|
||||||
|
else:
|
||||||
|
like = args.prefix + '%'
|
||||||
|
query = query.filter(db.or_(
|
||||||
|
Asset.assetnumber.like(like),
|
||||||
|
Asset.name.like(like),
|
||||||
|
Computer.hostname.like(like),
|
||||||
|
))
|
||||||
|
criterion = "name/hostname prefix %r" % args.prefix
|
||||||
|
|
||||||
servers = (Computer.query
|
servers = query.all()
|
||||||
.filter(Computer.computertypeid == ctype.computertypeid)
|
|
||||||
.all())
|
|
||||||
if not servers:
|
if not servers:
|
||||||
print("No computers under type %r - nothing to do." % args.type)
|
print("No computers match %s - nothing to do." % criterion)
|
||||||
return
|
return
|
||||||
|
|
||||||
ndt = _get_or_create_networkdevicetype('Server')
|
ndt = _get_or_create_networkdevicetype('Server')
|
||||||
print("%d server(s) under computer type %r -> network_device / type 'Server':"
|
print("%d server(s) matching %s -> network_device / type 'Server':"
|
||||||
% (len(servers), args.type))
|
% (len(servers), criterion))
|
||||||
|
|
||||||
moved = 0
|
moved = 0
|
||||||
for computer in servers:
|
for computer in servers:
|
||||||
|
|||||||
Reference in New Issue
Block a user