def add_discovered_switches(fab_id, tiers, switch_with_ip): from bootstrap import bootstrap logger.debug("Adding discovered switches in fabric") switch_id_details = dict() switch_id_details[SPINE] = dict() switch_id_details[LEAF] = dict() switch_id_details[CORE] = dict() switch_id_details[BORDER] = dict() for spine_switch in tiers[SPINE]: logger.debug("Adding spine switch:%s in fabric", spine_switch[0]) switch = Switch() switch.tier = SPINE switch.name = spine_switch[0] switch.dummy = False switch.serial_num = spine_switch[1] find_duplicate([spine_switch[1]]) switch.topology_id = fab_id switch.mgmt_ip = switch_with_ip[spine_switch[0]] switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Spine added") switch_id_details[SPINE][spine_switch[0]] = switch.id for leaf_switch in tiers[LEAF]: logger.debug("Adding leaf switch:%s to fabric", leaf_switch[0]) switch = Switch() switch.tier = LEAF switch.name = leaf_switch[0] switch.serial_num = leaf_switch[1] find_duplicate([leaf_switch[1]]) switch.topology_id = fab_id switch.mgmt_ip = switch_with_ip[leaf_switch[0]] switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Leaf added") switch_id_details[LEAF][leaf_switch[0]] = switch.id for core_switch in tiers[CORE]: logger.debug("Adding core switch:%s in fabric", core_switch) switch = Switch() switch.tier = CORE switch.name = core_switch switch.dummy = False switch.topology_id = fab_id switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("core added") switch_id_details[CORE][core_switch] = switch.id for border_switch in tiers[BORDER]: logger.debug("Adding border switch:%s in fabric", border_switch) switch = Switch() switch.tier = BORDER switch.name = border_switch switch.dummy = False switch.topology_id = fab_id switch.model = SwitchModel.objects.get(id=1) switch.save() bootstrap.update_boot_detail(switch, match_type=DISCOVERY, discovery_rule=None, boot_time=timezone.now(), boot_status=BOOT_SUCCESS, model_type='') logger.debug("Border added") switch_id_details[BORDER][border_switch] = switch.id return switch_id_details
def ignite_request(request): if ACCESS_PROTOCOL not in ACCESS_PROTOCOLS: msg = ERR_PROTO_NOT_FOUND + "- " + ACCESS_PROTOCOL return _get_server_response(False, err_msg=msg) serial_number = request[SERIAL_NUM] model_type = request[MODEL_TYPE] switch, match_type = search_fabric(request) if switch: logger.info("config type - " + switch.config_type) cfg_file = None if switch.config_type == POAP_CONFIG: if (switch.topology.feature_profile or get_switch_feature_profile(switch)): logger.debug("Switch have feture profiles applied") build_config(switch.topology.id) else: # delete cfg file try: os.remove(os.path.join(REPO_PATH + str(switch.id) + '.cfg')) except OSError as e: pass # build new cfg build_switch_config(switch) cfg_file = str(switch.id) + CFG_FILE_EXT cfg_file = get_cfg_file_path(REPO_PATH, cfg_file, FILE_REPO_PATH) if not cfg_file: logger.error(ERR_CFG_NOT_FOUND) return _get_server_response(False, err_msg=ERR_CFG_NOT_FOUND) logger.info("config file found for " + switch.config_type) elif switch.config_type == RUNNING_CONFIG: running_config = get_latest_version(switch.id) cfg_file = get_cfg_file_path(SWITCH_CONFIG_PATH, running_config, FILE_SWITCH_PATH) if not cfg_file: logger.error(ERR_CFG_NOT_FOUND) return _get_server_response(False, err_msg=ERR_CFG_NOT_FOUND) logger.info("config file found for " + switch.config_type) # fetch switch workflow wf = get_switch_workflow(switch) # fetch switch image image = get_switch_image(switch) wf_file = str(switch.id) + YAML_FILE_EXT logger.debug("Build workflow") with open(os.path.join(REPO_PATH, wf_file), 'w') as output_fh: output_fh.write(yaml.safe_dump(build_workflow(wf, image, cfg_file, serial_number), default_flow_style=False)) update_boot_detail(switch, match_type=match_type, boot_status=BOOT_PROGRESS, boot_time=timezone.now(), model_type=model_type) #update switch serial number if switch.serial_num != serial_number: logger.debug("Received serial number % s\ configured serial number % s" % (serial_number, switch.serial_num)) try: discoveryrule.find_duplicate([serial_number]) except IgniteException: logger.debug(ERR_SERIAL_NUM_IN_USE) return _get_server_response(False, err_msg=ERR_SERIAL_NUM_IN_USE) switch.serial_num = serial_number switch.save() if ACCESS_PROTOCOL in [PROTO_SCP, PROTO_SFTP]: wf_file = os.path.join(REPO_PATH, wf_file) elif ACCESS_PROTOCOL == PROTO_HTTP: wf_file = os.path.join(DOWNLOAD_URL, YAML, wf_file) else: wf_file = os.path.join(FILE_REPO_PATH, wf_file) return _get_server_response(True, wf_file) logger.debug("No match found in fabric") rule = discoveryrule.match_discovery_rules(request) wf = None if rule: try: switch = Switch.objects.get(name=serial_number, topology__isnull=True) except Switch.DoesNotExist: switch = Switch() switch.name = serial_number switch.serial_num = serial_number switch.save() if rule.workflow: wf = rule.workflow else: wf = get_workflow(BOOTSTRAP_WORKFLOW_ID) logger.debug("Discovery rule match- workflow: %s, config: %s" % (wf.name, rule.config.name)) # delete cfg file try: os.remove(os.path.join(REPO_PATH + str(switch.id) + '.cfg')) except OSError as e: pass # build new cfg build_switch_config(switch, switch_cfg=rule.config) if rule.match == SERIAL_NUM: match_type = SERIAL_NUMBER else: match_type = NEIGHBOR update_boot_detail(switch, match_type=match_type, discovery_rule=rule, boot_time=timezone.now(), boot_status=BOOT_PROGRESS, model_type=model_type) cfg_file = str(switch.id) + CFG_FILE_EXT wf_file = str(switch.id) + YAML_FILE_EXT logger.debug("Build workflow") if ACCESS_PROTOCOL in [PROTO_SCP, PROTO_SFTP]: cfg_file = os.path.join(REPO_PATH, cfg_file) elif ACCESS_PROTOCOL == PROTO_HTTP: cfg_file = os.path.join(DOWNLOAD_URL, CONFIG, cfg_file) else: cfg_file = os.path.join(FILE_REPO_PATH, cfg_file) with open(os.path.join(REPO_PATH, wf_file), 'w') as output_fh: output_fh.write(yaml.safe_dump(build_workflow(wf, rule.image, cfg_file, serial_number), default_flow_style=False)) if ACCESS_PROTOCOL in [PROTO_SCP, PROTO_SFTP]: wf_file = os.path.join(REPO_PATH, wf_file) elif ACCESS_PROTOCOL == PROTO_HTTP: wf_file = os.path.join(DOWNLOAD_URL, YAML, wf_file) else: wf_file = os.path.join(FILE_REPO_PATH, wf_file) return _get_server_response(True, wf_file) return _get_server_response(False)