import json from celery import bootsteps, Task import redis from inventory_provider.tasks.app import app from inventory_provider import config from inventory_provider import juniper, snmp class InventoryTask(Task): config = None def __init__(self): pass @staticmethod def save_key(hostname, key, data): r = redis.StrictRedis( host=InventoryTask.config["redis"]["hostname"], port=InventoryTask.config["redis"]["port"]) r.hset( name=hostname, key=key, value=json.dumps(data)) return "OK" class WorkerArgs(bootsteps.Step): def __init__(self, worker, config_filename, **options): with open(config_filename) as f: InventoryTask.config = config.load(f) def worker_args(parser): parser.add_argument( "--config_filename", dest="config_filename", action='store', type=str, help="Configuration filename") app.user_options['worker'].add(worker_args) app.steps['worker'].add(WorkerArgs) @app.task(bind=InventoryTask) def juniper_refresh_bgp(self, hostname): InventoryTask.save_key( hostname, "bgp", juniper.fetch_bgp_config(hostname, InventoryTask.config["ssh"])) @app.task(bind=InventoryTask) def juniper_refresh_vrr(self, hostname): InventoryTask.save_key( hostname, "vrr", juniper.fetch_vrr_config(hostname, InventoryTask.config["ssh"])) @app.task(bind=InventoryTask) def juniper_refresh_interfaces(self, hostname): InventoryTask.save_key( hostname, "interfaces", juniper.fetch_interfaces(hostname, InventoryTask.config["ssh"])) @app.task(bind=InventoryTask) def snmp_refresh_interfaces(self, hostname, community): InventoryTask.save_key( hostname, "interfaces", list(snmp.get_router_interfaces(hostname, community, InventoryTask.config)))