def sync_push(SESSION_TK, YAML_TK, sync_diffgen_dict):
    '''
    PUSH
    REQ: SESSION_TK, YAML_TK, sync_diffgen_dict
    RTN: sync_push_status
    '''
    sync_push_log = []

    sync_push_status = False

    if YAML_TK['YAML_driver'] == 'ios':  # Cisco IOS

        if SESSION_TK['ARG_commit']:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                ': > PUSH Module (IOS NETMIKO) Initialised. Commit Mode && ...')

            netmko_mode = 'set'

            for item, objects in sync_diffgen_dict.items():
                if not objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' +
                                         item + '] No DIFF Found')

                else:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' +
                                         item + '] DIFF Found...')

                    # Send the objects list [] to NetMiko Set Mode
                    netmko_status, netmko_log, netmko_list = \
                        netmko(SESSION_TK, YAML_TK, netmko_mode, item, objects)

                    for line in netmko_log:
                        sync_push_log.append(line)

                    if netmko_status:
                        sync_push_status = True

                    if netmko_list:  # List return will be empty. Nothing to do. PEP8
                        pass

            sync_push_log.append(YAML_TK['YAML_fqdn'] +
                                 ': * PUSH Commit Successful')

        else:  # SESSION_TK['ARG_commit'] == False:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                ': > PUSH Module (IOS NETMIKO) Initialised. Non-Commit Mode !! ...')

            for item, objects in sync_diffgen_dict.items():
                for obj in objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                        ': - [' + item + '] Config Payload !! : "' + str(obj) + '"')

            sync_push_log.append(YAML_TK['YAML_fqdn'] +
                                 ': * PUSH Proposal Generated')

        sync_push_status = True

    elif YAML_TK['YAML_driver'] == 'nxos_ssh':  # Cisco NXOS

        if SESSION_TK['ARG_commit']:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                ': > PUSH Module (NX-OS NXAPI) Initialised. Commit Mode && ...')

            nxapi_mode = 'set'

            for item, objects in sync_diffgen_dict.items():

                if not objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' +
                                         item + '] No DIFF Found')

                else:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' +
                                         item + '] DIFF Found...')

                    #for obj in objects:
                    nxapi_status, nxapi_log, nxapi_list = \
                        nxapi(SESSION_TK, YAML_TK, nxapi_mode, item, objects)

                    for line in nxapi_log:
                        sync_push_log.append(line)

                    if nxapi_status:
                        sync_push_status = True

                    if nxapi_list:  # List return will be empty. Nothing to do. PEP8
                        pass

            sync_push_log.append(YAML_TK['YAML_fqdn'] +
                                 ': * PUSH Commit Successful')

        else:  # SESSION_TK['ARG_commit'] == False:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                ': > PUSH Module (NX-OS NXAPI) Initialised. Non-Commit Mode !! ...')

            for item, objects in sync_diffgen_dict.items():
                for obj in objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + \
                        ': - [' + item + '] Config Payload !! : "' + str(obj) + '"')

            sync_push_log.append(YAML_TK['YAML_fqdn'] +
                                 ': * PUSH Proposal Generated')

        sync_push_status = True

    return sync_push_status, sync_push_log
def sync_getcfg(SESSION_TK, YAML_TK, sync_getset_payload):

    sync_getcfg_log = []

    sync_getcfg_log.append(YAML_TK['YAML_fqdn'] + ': > GETCFG Module Initialised...')

    sync_getcfg_dict = {}
    sync_getcfg_status = True

    if YAML_TK['YAML_driver'] == 'ios': # Cisco IOS
        netmko_mode = 'get'
        sync_getcfg_log.append(YAML_TK['YAML_fqdn'] + ': * IOS NetMiko Method')

        for item, objects in sync_getset_payload.items():
            for object in objects:

                if sync_getcfg_status == False: # If status set to False on previous loop. do not proceed!
                    break

                netmko_status, netmko_log, netmko_list = netmko (SESSION_TK, YAML_TK, netmko_mode, item, object['CMD'])

                for line in netmko_log: # Append to Master Log
                    sync_getcfg_log.append(line)

                if netmko_status == True: # If Payload Valid, add to response_dict {}
                    sync_getcfg_dict[item] = netmko_list
                    sync_getcfg_status = True

                else:
                    sync_getcfg_dict[item] = ''
                    sync_getcfg_status = False

    elif YAML_TK['YAML_driver'] == 'nxos_ssh': # Cisco NXOS
        nxapi_mode = 'get'
        sync_getcfg_log.append(YAML_TK['YAML_fqdn'] + ': * NX-OS NXAPI Method')

        for item, objects in sync_getset_payload.items():
            for object in objects:

                if sync_getcfg_status == False: # If status set to False on previous loop. do not proceed!
                    break

                # Send the payload object to the NXAPI Module. Example object
                # {'jsonrpc': '2.0', 'method': 'cli_ascii', 'params': {'cmd': 'show run snmp', 'version': 1.2}, 'id': 2}
                nxapi_status, nxapi_log, nxapi_list = nxapi(SESSION_TK, YAML_TK, nxapi_mode, item, object['CMD'])

                for line in nxapi_log: # Append to Global Log
                    sync_getcfg_log.append(line)

                if nxapi_status == True: # If Payload Valid, add to response_dict {}
                    sync_getcfg_dict[item] = nxapi_list
                    sync_getcfg_status = True

                else:
                    sync_getcfg_dict[item] = ''
                    sync_getcfg_status = False

    else:
        sync_getcfg_log.append(YAML_TK['YAML_fqdn'] + ':  * Driver ' + YAML_TK['YAML_driver'] )+ ' Not Supported!'
        sync_getcfg_status = False

    if SESSION_TK['ARG_debug'] == True:
        print('\n**DEBUG (_network_borg_sync.py) : GETCFG Dictionary Returned:')
        print(sync_getcfg_dict)

    return sync_getcfg_status, sync_getcfg_log, sync_getcfg_dict
Example #3
0
def sync_push(SESSION_TK, YAML_TK, sync_diffgen_dict):

    sync_push_log = []

    sync_push_status = False

    if YAML_TK['YAML_driver'] == 'ios': # Cisco IOS

        if SESSION_TK['ARG_commit'] == True:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': > PUSH Module (IOS NETMIKO) Initialised. Commit Mode && ...')

            netmko_mode = 'set'

            for item, objects in sync_diffgen_dict.items():

                if not objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] No DIFF Found')

                else:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] DIFF Found...')

                for object in objects:
                    netmko_status, netmko_log, netmko_list = netmko (SESSION_TK, YAML_TK, netmko_mode, item, object)

                    for line in netmko_log:
                        sync_push_log.append(line)

                    if netmko_status == True:
                        sync_push_status = True

            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': * PUSH Commit Successful')

        else: # SESSION_TK['ARG_commit'] == False:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': > PUSH Module (IOS NETMIKO) Initialised. Non-Commit Mode !! ...')

            for item, objects in sync_diffgen_dict.items():
                for object in objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] Config Payload !! : "' + str(object) + '"')

            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': * PUSH Proposal Generated')

        sync_push_status = True

    elif YAML_TK['YAML_driver'] == 'nxos_ssh': # Cisco NXOS

        if SESSION_TK['ARG_commit'] == True:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': > PUSH Module (NX-OS NXAPI) Initialised. Commit Mode && ...')

            nxapi_mode = 'set'

            for item, objects in sync_diffgen_dict.items():

                if not objects:
                     sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] No DIFF Found')

                else:
                     sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] DIFF Found...')

                for object in objects:
                    nxapi_status, nxapi_log, nxapi_dict = nxapi (SESSION_TK, YAML_TK, nxapi_mode, item, object)

                    for line in nxapi_log:
                        sync_push_log.append(line)

                    if nxapi_status == True:
                        sync_push_status = True

            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': * PUSH Commit Successful')

        else: # SESSION_TK['ARG_commit'] == False:
            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': > PUSH Module (NX-OS NXAPI) Initialised. Non-Commit Mode !! ...')

            for item, objects in sync_diffgen_dict.items():
                for object in objects:
                    sync_push_log.append(YAML_TK['YAML_fqdn'] + ': - [' + item + '] Config Payload !! : "' + str(object) + '"')

            sync_push_log.append(YAML_TK['YAML_fqdn'] + ': * PUSH Proposal Generated')

        sync_push_status = True

    return sync_push_status, sync_push_log