ucscsdk/utils/ucscbackup.py
def _import_config(handle, file_name, file_location="ucscentral",
                   file_dir=None, merge=True, protocol=None,
                   hostname="localhost",
                   username=None, password="", timeout=120):
    """
    This internal method imports a UcsCentral config from local, remote or
    ucscentral

    Args:
        handle (UcscHandle): connection handle
        file_name (str): backup file name to be imported
        file_location (str): file location where the config file is, it can be:
                             ['ucscentral','local','remote']
        file_dir (str): directory containing ucscentral backup file,
                        used only for import from local or remote
        merge (boolean): True/False, specifies whether to merge the backup
                        config with the existing UCS Central configuration

        protocol (str): Transfer protocol for remote import
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote import
        username (str): Username for remote import
        password (str): Password for remote import
        timeout (number) : time in seconds for which method waits for
                           import operation success before timing out

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_export.tgz"\n
        import from ucscentral:
        _import_config(handle, file_name=file_name,
                       file_location="ucscentral")

        import from local:
        _import_config(handle, file_name=file_name,
                       file_location="local",
                       file_dir=file_dir,
                       merge=True)\n

        import from remote:
        _import_config(handle, file_name=file_name,
                       file_location="remote",
                       file_dir=file_dir
                       protocol='scp',hostname='192.168.1.1',
                       username='admin',password='password')\n

    """

    from ..mometa.top.TopSystem import TopSystem
    from ..mometa.mgmt.MgmtDataImporter import MgmtDataImporter, \
        MgmtDataImporterConsts

    if not file_name:
        raise UcscValidationException("Missing file_name argument")

    if file_location != "ucscentral":
        if not file_dir:
            raise UcscValidationException("Missing file_dir argument")

    if (not file_name.endswith('.tgz')):
        raise UcscValidationException("file_name must be .tgz format")

    top_system = TopSystem()

    if file_location == "remote":
        file_path = os.path.join(file_dir, file_name)
        _validate_remote_host_args(protocol, hostname, username, password)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname=hostname,
            remote_file=file_path,
            proto=protocol,
            user=username,
            pwd=password,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)

    elif file_location == "local":
        file_path = os.path.join(file_dir, file_name)
        if not os.path.exists(file_path):
            raise UcscOperationError("Import config",
                                     "Backup File '%s' not found" %
                                     file_path)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname="localhost",
            remote_file='/' + file_name,
            proto=MgmtDataImporterConsts.PROTO_HTTP,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)

    elif file_location == "ucscentral":
        if not _is_backup_file_on_server(handle, "ucs-central", file_name):
            raise UcscOperationError("Import config",
                                     "Backup File '%s' not found "
                                     "on UcsCentral" % file_name)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname="localhost",
            remote_file='/ucs-central/cfg-backups/' + file_name,
            proto=MgmtDataImporterConsts.PROTO_TFTP,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)

    else:
        raise UcscOperationError(
                "Import config",
                "Invalid file_location argument."
                "It must be either ucscentral,local or remote")

    if merge:
        mgmt_importer.action = MgmtDataImporterConsts.ACTION_MERGE
    else:
        mgmt_importer.action = MgmtDataImporterConsts.ACTION_REPLACE

    if file_location == "local":
        try:
            log.debug("Start uploading config")
            uri_suffix = "operations/file-%s/importconfig.txt?Cookie=%s" % (
                         file_name, handle.cookie)
            handle.file_upload(url_suffix=uri_suffix,
                               file_dir=file_dir,
                               file_name=file_name)

        except Exception as err:
            UcscWarning(str(err))
            raise UcscOperationError("Upload config", "upload failed")

    handle.add_mo(mgmt_importer, modify_present=True)
    handle.commit()

    duration = timeout
    poll_interval = 2
    log.debug("Importing UcsCentral config")
    while True:
        mgmt_importer = handle.query_dn(dn=mgmt_importer.dn)
        admin_state = mgmt_importer.admin_state

        # Break condition:- if state id disabled then break
        if admin_state == MgmtDataImporterConsts.ADMIN_STATE_DISABLED:
            break

        time.sleep(min(duration, poll_interval))
        duration = max(0, (duration - poll_interval))
        if duration == 0:
            raise UcscOperationError(
                  "Import config", "operation timed out")

    if mgmt_importer.over_all_status != \
            MgmtDataImporterConsts.OVER_ALL_STATUS_ALL_SUCCESS:
            raise UcscOperationError(
                  "Import config",
                  ("operational status %s " % mgmt_importer.over_all_status))

    log.debug("Import config to UcsCentral was successfull")
    return mgmt_importer

    This internal method imports a UcsCentral config from local, remote or
    ucscentral

    Args:
        handle (UcscHandle): connection handle
        file_name (str): backup file name to be imported
        file_location (str): file location where the config file is, it can be:
                             ['ucscentral','local','remote']
        file_dir (str): directory containing ucscentral backup file,
                        used only for import from local or remote
        merge (boolean): True/False, specifies whether to merge the backup
                        config with the existing UCS Central configuration

        protocol (str): Transfer protocol for remote import
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote import
        username (str): Username for remote import
        password (str): Password for remote import
        timeout (number) : time in seconds for which method waits for
                           import operation success before timing out

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_export.tgz"\n
        import from ucscentral:
        _import_config(handle, file_name=file_name,
                       file_location="ucscentral")

        import from local:
        _import_config(handle, file_name=file_name,
                       file_location="local",
                       file_dir=file_dir,
                       merge=True)\n

        import from remote:
        _import_config(handle, file_name=file_name,
                       file_location="remote",
                       file_dir=file_dir
                       protocol='scp',hostname='192.168.1.1',
                       username='admin',password='password')\n

    
This internal method imports a UcsCentral config from local, remote or
ucscentral
This internal method imports a UcsCentral config from local remote or ucscentral
def _import_config(handle, file_name, file_location="ucscentral",
                   file_dir=None, merge=True, protocol=None,
                   hostname="localhost",
                   username=None, password="", timeout=120):
    from ..mometa.top.TopSystem import TopSystem
    from ..mometa.mgmt.MgmtDataImporter import MgmtDataImporter, \
        MgmtDataImporterConsts
    if not file_name:
        raise UcscValidationException("Missing file_name argument")
    if file_location != "ucscentral":
        if not file_dir:
            raise UcscValidationException("Missing file_dir argument")
    if (not file_name.endswith('.tgz')):
        raise UcscValidationException("file_name must be .tgz format")
    top_system = TopSystem()
    if file_location == "remote":
        file_path = os.path.join(file_dir, file_name)
        _validate_remote_host_args(protocol, hostname, username, password)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname=hostname,
            remote_file=file_path,
            proto=protocol,
            user=username,
            pwd=password,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)
    elif file_location == "local":
        file_path = os.path.join(file_dir, file_name)
        if not os.path.exists(file_path):
            raise UcscOperationError("Import config",
                                     "Backup File '%s' not found" %
                                     file_path)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname="localhost",
            remote_file='/' + file_name,
            proto=MgmtDataImporterConsts.PROTO_HTTP,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)
    elif file_location == "ucscentral":
        if not _is_backup_file_on_server(handle, "ucs-central", file_name):
            raise UcscOperationError("Import config",
                                     "Backup File '%s' not found "
                                     "on UcsCentral" % file_name)
        mgmt_importer = MgmtDataImporter(
            parent_mo_or_dn=top_system,
            hostname="localhost",
            remote_file='/ucs-central/cfg-backups/' + file_name,
            proto=MgmtDataImporterConsts.PROTO_TFTP,
            admin_state=MgmtDataImporterConsts.ADMIN_STATE_ENABLED)
    else:
        raise UcscOperationError(
                "Import config",
                "Invalid file_location argument."
                "It must be either ucscentral,local or remote")
    if merge:
        mgmt_importer.action = MgmtDataImporterConsts.ACTION_MERGE
    else:
        mgmt_importer.action = MgmtDataImporterConsts.ACTION_REPLACE
    if file_location == "local":
        try:
            log.debug("Start uploading config")
            uri_suffix = "operations/file-%s/importconfig.txt?Cookie=%s" % (
                         file_name, handle.cookie)
            handle.file_upload(url_suffix=uri_suffix,
                               file_dir=file_dir,
                               file_name=file_name)
        except Exception as err:
            UcscWarning(str(err))
            raise UcscOperationError("Upload config", "upload failed")
    handle.add_mo(mgmt_importer, modify_present=True)
    handle.commit()
    duration = timeout
    poll_interval = 2
    log.debug("Importing UcsCentral config")
    while True:
        mgmt_importer = handle.query_dn(dn=mgmt_importer.dn)
        admin_state = mgmt_importer.admin_state
        if admin_state == MgmtDataImporterConsts.ADMIN_STATE_DISABLED:
            break
        time.sleep(min(duration, poll_interval))
        duration = max(0, (duration - poll_interval))
        if duration == 0:
            raise UcscOperationError(
                  "Import config", "operation timed out")
    if mgmt_importer.over_all_status != \
            MgmtDataImporterConsts.OVER_ALL_STATUS_ALL_SUCCESS:
            raise UcscOperationError(
                  "Import config",
                  ("operational status %s " % mgmt_importer.over_all_status))
    log.debug("Import config to UcsCentral was successfull")
    return mgmt_importer
def _import_config ( handle , file_name , file_location = "ucscentral" , file_dir = None , merge = True , protocol = None , hostname = "localhost" , username = None , password = "" , timeout = 120 ) : from . . mometa . top . TopSystem import TopSystem from . . mometa . mgmt . MgmtDataImporter import MgmtDataImporter , MgmtDataImporterConsts if not file_name : raise UcscValidationException ( "Missing file_name argument" ) if file_location != "ucscentral" : if not file_dir : raise UcscValidationException ( "Missing file_dir argument" ) if ( not file_name . endswith ( '.tgz' ) ) : raise UcscValidationException ( "file_name must be .tgz format" ) top_system = TopSystem ( ) if file_location == "remote" : file_path = os . path . join ( file_dir , file_name ) _validate_remote_host_args ( protocol , hostname , username , password ) mgmt_importer = MgmtDataImporter ( parent_mo_or_dn = top_system , hostname = hostname , remote_file = file_path , proto = protocol , user = username , pwd = password , admin_state = MgmtDataImporterConsts . ADMIN_STATE_ENABLED ) elif file_location == "local" : file_path = os . path . join ( file_dir , file_name ) if not os . path . exists ( file_path ) : raise UcscOperationError ( "Import config" , "Backup File '%s' not found" % file_path ) mgmt_importer = MgmtDataImporter ( parent_mo_or_dn = top_system , hostname = "localhost" , remote_file = '/' + file_name , proto = MgmtDataImporterConsts . PROTO_HTTP , admin_state = MgmtDataImporterConsts . ADMIN_STATE_ENABLED ) elif file_location == "ucscentral" : if not _is_backup_file_on_server ( handle , "ucs-central" , file_name ) : raise UcscOperationError ( "Import config" , "Backup File '%s' not found " "on UcsCentral" % file_name ) mgmt_importer = MgmtDataImporter ( parent_mo_or_dn = top_system , hostname = "localhost" , remote_file = '/ucs-central/cfg-backups/' + file_name , proto = MgmtDataImporterConsts . PROTO_TFTP , admin_state = MgmtDataImporterConsts . ADMIN_STATE_ENABLED ) else : raise UcscOperationError ( "Import config" , "Invalid file_location argument." "It must be either ucscentral,local or remote" ) if merge : mgmt_importer . action = MgmtDataImporterConsts . ACTION_MERGE else : mgmt_importer . action = MgmtDataImporterConsts . ACTION_REPLACE if file_location == "local" : try : log . debug ( "Start uploading config" ) uri_suffix = "operations/file-%s/importconfig.txt?Cookie=%s" % ( file_name , handle . cookie ) handle . file_upload ( url_suffix = uri_suffix , file_dir = file_dir , file_name = file_name ) except Exception as err : UcscWarning ( str ( err ) ) raise UcscOperationError ( "Upload config" , "upload failed" ) handle . add_mo ( mgmt_importer , modify_present = True ) handle . commit ( ) duration = timeout poll_interval = 2 log . debug ( "Importing UcsCentral config" ) while True : mgmt_importer = handle . query_dn ( dn = mgmt_importer . dn ) admin_state = mgmt_importer . admin_state if admin_state == MgmtDataImporterConsts . ADMIN_STATE_DISABLED : break time . sleep ( min ( duration , poll_interval ) ) duration = max ( 0 , ( duration - poll_interval ) ) if duration == 0 : raise UcscOperationError ( "Import config" , "operation timed out" ) if mgmt_importer . over_all_status != MgmtDataImporterConsts . OVER_ALL_STATUS_ALL_SUCCESS : raise UcscOperationError ( "Import config" , ( "operational status %s " % mgmt_importer . over_all_status ) ) log . debug ( "Import config to UcsCentral was successfull" ) return mgmt_importer
This internal method imports a UcsCentral config from local, remote or
ucscentral
This internal method imports a UcsCentral config from local remote or ucscentral
"""
    This internal method imports a UcsCentral config from local, remote or
    ucscentral

    Args:
        handle (UcscHandle): connection handle
        file_name (str): backup file name to be imported
        file_location (str): file location where the config file is, it can be:
                             ['ucscentral','local','remote']
        file_dir (str): directory containing ucscentral backup file,
                        used only for import from local or remote
        merge (boolean): True/False, specifies whether to merge the backup
                        config with the existing UCS Central configuration

        protocol (str): Transfer protocol for remote import
                        ['ftp','sftp','tftp','scp']
        hostname (str): Hostname/IP for the remote import
        username (str): Username for remote import
        password (str): Password for remote import
        timeout (number) : time in seconds for which method waits for
                           import operation success before timing out

    Example:
        file_dir = "/home/user/backup"\n
        file_name = "config_export.tgz"\n
        import from ucscentral:
        _import_config(handle, file_name=file_name,
                       file_location="ucscentral")

        import from local:
        _import_config(handle, file_name=file_name,
                       file_location="local",
                       file_dir=file_dir,
                       merge=True)\n

        import from remote:
        _import_config(handle, file_name=file_name,
                       file_location="remote",
                       file_dir=file_dir
                       protocol='scp',hostname='192.168.1.1',
                       username='admin',password='password')\n

    """ # Break condition:- if state id disabled then break
handle
None
file_name
backup file name to be imported
backup file name to be imported
file_location
file location where the config file is, it can be:
['ucscentral','local','remote']
file location where the config file is it can be : [ ' ucscentral ' ' local ' ' remote ' ]
file_dir
directory containing ucscentral backup file,
used only for import from local or remote
directory containing ucscentral backup file used only for import from local or remote
merge
True/False, specifies whether to merge the backup
config with the existing UCS Central configuration
True / False specifies whether to merge the backup config with the existing UCS Central configuration
protocol
Transfer protocol for remote import
['ftp','sftp','tftp','scp']
Transfer protocol for remote import [ ' ftp ' ' sftp ' ' tftp ' ' scp ' ]
hostname
Hostname/IP for the remote import
Hostname / IP for the remote import
username
Username for remote import
Username for remote import
password
Password for remote import
Password for remote import
timeout
time in seconds for which method waits for
import operation success before timing out
time in seconds for which method waits for import operation success before timing out
examples
file_dir = "/home/user/backup"\n
file_name = "config_export.tgz"\n
import from ucscentral:
_import_config(handle, file_name=file_name,
file_location="ucscentral")

import from local:
_import_config(handle, file_name=file_name,
file_location="local",
file_dir=file_dir,
merge=True)\n

import from remote:
_import_config(handle, file_name=file_name,
file_location="remote",
file_dir=file_dir
protocol='scp',hostname='192.168.1.1',
username='admin',password='password')\n
file_dir = " / home / user / backup " \ n file_name = " config_export . tgz " \ n import from ucscentral : _import_config ( handle file_name = file_name file_location = " ucscentral " ) import from local : _import_config ( handle file_name = file_name file_location = " local " file_dir = file_dir merge = True ) \ n import from remote : _import_config ( handle file_name = file_name file_location = " remote " file_dir = file_dir protocol = ' scp ' hostname = ' 192 . 168 . 1 . 1 ' username = ' admin ' password = ' password ' ) \ n