def copy_ifcfg_file(source_interface, dest_interface):
    """Copies an existing ifcfg network script to another

    :param source_interface: String (e.g. 1)
    :param dest_interface: String (e.g. 0:0)
    :return: None
    :raises TypeError, OSError
    """
    log = logging.getLogger(mod_logger + '.copy_ifcfg_file')
    # Validate args
    if not isinstance(source_interface, basestring):
        msg = 'source_interface argument must be a string'
        log.error(msg)
        raise TypeError(msg)
    if not isinstance(dest_interface, basestring):
        msg = 'dest_interface argument must be a string'
        log.error(msg)
        raise TypeError(msg)

    network_script = '/etc/sysconfig/network-scripts/ifcfg-eth'
    source_file = network_script + source_interface
    dest_file = network_script + dest_interface
    command = ['cp', '-f', source_file, dest_file]
    try:
        result = run_command(command)
        code = result['code']
    except CommandError:
        _, ex, trace = sys.exc_info()
        msg = 'Unable to copy the ifcfg file from interface {s} to interface {d}\n{e}'.format(
            s=source_interface, d=dest_interface, e=str(ex))
        raise OSError, msg, trace
    log.info('Copy command exited with code: {c}'.format(c=code))

    if code != 0:
        msg = 'There was a problem copying file {s} file to {d}'.format(s=source, d=dest_file)
        log.error(msg)
        raise OSError(msg)

    # Updating the destination network script DEVICE property
    try:
        sed(file_path=dest_file, pattern='^DEVICE=.*',
            replace_str='DEVICE="eth{i}"'.format(i=dest_interface))
    except CommandError:
        _, ex, trace = sys.exc_info()
        msg = 'Unable to update DEVICE in file: {d}\n{e}'.format(
            d=dest_file, e=str(ex))
        log.error(msg)
        raise CommandError, msg, trace
    log.info('Successfully created file: {d}'.format(d=dest_file))

    log.info('Restarting networking in 10 seconds to ensure the changes take effect...')
    time.sleep(10)
    retry_time = 10
    max_retries = 10
    for i in range(1, max_retries+2):
        if i > max_retries:
            msg = 'Unable to successfully start the networking service after {m} attempts'.format(m=max_retries)
            log.error(msg)
            raise OSError(msg)
        log.info('Attempting to restart the networking service, attempt #{i} of {m}'.format(i=i, m=max_retries))
        try:
            service_network_restart()
        except CommandError:
            _, ex, trace = sys.exc_info()
            log.warn('Attempted unsuccessfully to restart networking on attempt #{i} of {m}, trying again in {t} '
                     'seconds\n{e}'.format(i=i, m=max_retries, t=retry_time, e=str(ex)))
            time.sleep(retry_time)
        else:
            log.info('Successfully restarted networking')
            break
    log.info('Successfully configured interface: {d}'.format(d=dest_interface))
def copy_ifcfg_file ( source_interface , dest_interface ) : log = logging . getLogger ( mod_logger + '.copy_ifcfg_file' ) if not isinstance ( source_interface , basestring ) : msg = 'source_interface argument must be a string' log . error ( msg ) raise TypeError ( msg ) if not isinstance ( dest_interface , basestring ) : msg = 'dest_interface argument must be a string' log . error ( msg ) raise TypeError ( msg ) network_script = '/etc/sysconfig/network-scripts/ifcfg-eth' source_file = network_script + source_interface dest_file = network_script + dest_interface command = [ 'cp' , '-f' , source_file , dest_file ] try : result = run_command ( command ) code = result [ 'code' ] except CommandError : _ , ex , trace = sys . exc_info ( ) msg = 'Unable to copy the ifcfg file from interface {s} to interface {d}\n{e}' . format ( s = source_interface , d = dest_interface , e = str ( ex ) ) raise OSError , msg , trace log . info ( 'Copy command exited with code: {c}' . format ( c = code ) ) if code != 0 : msg = 'There was a problem copying file {s} file to {d}' . format ( s = source , d = dest_file ) log . error ( msg ) raise OSError ( msg ) try : sed ( file_path = dest_file , pattern = '^DEVICE=.*' , replace_str = 'DEVICE="eth{i}"' . format ( i = dest_interface ) ) except CommandError : _ , ex , trace = sys . exc_info ( ) msg = 'Unable to update DEVICE in file: {d}\n{e}' . format ( d = dest_file , e = str ( ex ) ) log . error ( msg ) raise CommandError , msg , trace log . info ( 'Successfully created file: {d}' . format ( d = dest_file ) ) log . info ( 'Restarting networking in 10 seconds to ensure the changes take effect...' ) time . sleep ( 10 ) retry_time = 10 max_retries = 10 for i in range ( 1 , max_retries + 2 ) : if i > max_retries : msg = 'Unable to successfully start the networking service after {m} attempts' . format ( m = max_retries ) log . error ( msg ) raise OSError ( msg ) log . info ( 'Attempting to restart the networking service, attempt #{i} of {m}' . format ( i = i , m = max_retries ) ) try : service_network_restart ( ) except CommandError : _ , ex , trace = sys . exc_info ( ) log . warn ( 'Attempted unsuccessfully to restart networking on attempt #{i} of {m}, trying again in {t} ' 'seconds\n{e}' . format ( i = i , m = max_retries , t = retry_time , e = str ( ex ) ) ) time . sleep ( retry_time ) else : log . info ( 'Successfully restarted networking' ) break log . info ( 'Successfully configured interface: {d}' . format ( d = dest_interface ) )
Copies an existing ifcfg network script to another

    :param source_interface: String (e.g. 1)
    :param dest_interface: String (e.g. 0:0)
    :return: None
    :raises TypeError, OSError