Ejemplo n.º 1
0
def compare(jaide, commands):
    """ Perform a show | compare with some set commands.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: The set commands to send to the device to compare with.
    @type commands: str or list

    @returns: The output from the device.
    @rtype str
    """
    output = color("show | compare:\n", 'yel')
    return output + color_diffs(jaide.compare_config(commands))
Ejemplo n.º 2
0
Archivo: wrap.py Proyecto: suakow/jaide
def compare(jaide, commands):
    """ Perform a show | compare with some set commands.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: The set commands to send to the device to compare with.
    @type commands: str or list

    @returns: The output from the device.
    @rtype str
    """
    output = color("show | compare:\n", 'yel')
    return output + color_diffs(jaide.compare_config(commands))
Ejemplo n.º 3
0
Archivo: wrap.py Proyecto: suakow/jaide
def diff_config(jaide, second_host, mode):
    """ Perform a show | compare with some set commands.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param second_host: The device IP or hostname of the second host to
                      | compare with.
    @type second_host: str
    @param mode: How to compare the configuration, either in 'set' mode or
               | 'stanza' mode.
    @type mode: str

    @returns: The comparison between the two devices.
    @rtype str
    """
    try:
        # create a list of all the lines that differ, and merge it.
        output = '\n'.join(
            [diff for diff in jaide.diff_config(second_host, mode.lower())])
    except errors.SSHError:
        output = color(
            'Unable to connect to port %s on device: %s\n' %
            (str(jaide.port), second_host), 'red')
    except errors.AuthenticationError:  # NCClient auth failure
        output = color('Authentication failed for device: %s' % second_host,
                       'red')
    except AuthenticationException:  # Paramiko auth failure
        output = color('Authentication failed for device: %s' % second_host,
                       'red')
    except SSHException as e:
        output = color(
            'Error connecting to device: %s\nError: %s' %
            (second_host, str(e)), 'red')
    except socket.timeout:
        output = color(
            'Timeout exceeded connecting to device: %s' % second_host, 'red')
    except socket.gaierror:
        output = color(
            'No route to host, or invalid hostname: %s' % second_host, 'red')
    except socket.error:
        output = color(
            'The device refused the connection on port %s, or '
            'no route to host.' % jaide.port, 'red')
    if output.strip() == '':
        output = color(
            "There were no config differences between %s and %s\n" %
            (jaide.host, second_host), 'yel')
    else:
        # color removals red, and additions green
        return color_diffs(output)
    return output
Ejemplo n.º 4
0
def diff_config(jaide, second_host, mode):
    """ Perform a show | compare with some set commands.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param second_host: The device IP or hostname of the second host to
                      | compare with.
    @type second_host: str
    @param mode: How to compare the configuration, either in 'set' mode or
               | 'stanza' mode.
    @type mode: str

    @returns: The comparison between the two devices.
    @rtype str
    """
    try:
        # create a list of all the lines that differ, and merge it.
        output = '\n'.join([diff for diff in
                            jaide.diff_config(second_host, mode.lower())])
    except errors.SSHError:
        output = color('Unable to connect to port %s on device: %s\n' %
                       (str(jaide.port), second_host), 'red')
    except errors.AuthenticationError:  # NCClient auth failure
        output = color('Authentication failed for device: %s' %
                       second_host, 'red')
    except AuthenticationException:  # Paramiko auth failure
        output = color('Authentication failed for device: %s' %
                       second_host, 'red')
    except SSHException as e:
        output = color('Error connecting to device: %s\nError: %s' %
                       (second_host, str(e)), 'red')
    except socket.timeout:
        output = color('Timeout exceeded connecting to device: %s' %
                       second_host, 'red')
    except socket.gaierror:
        output = color('No route to host, or invalid hostname: %s' %
                       second_host, 'red')
    except socket.error:
        output = color('The device refused the connection on port %s, or '
                       'no route to host.' % jaide.port, 'red')
    if output.strip() == '':
        output = color("There were no config differences between %s and %s\n" %
                       (jaide.host, second_host), 'yel')
    else:
        # color removals red, and additions green
        return color_diffs(output)
    return output
Ejemplo n.º 5
0
def commit(jaide, commands, check, sync, comment, confirm, at_time, blank):
    """ Execute a commit against the device.

    Purpose: This function will send set commands to a device, and commit
           | the changes. Options exist for confirming, comments,
           | synchronizing, checking, blank commits, or delaying to a later
           | time/date.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: String containing the set command to be sent to the
                   | device. It can be a python list of strings, a single set
                   | command, a comma separated string of commands, or a
                   | string filepath pointing to a file with set commands
                   | on each line.
    @type commands: str or list
    @param check: A bool set to true to only run a commit check, and not
                | commit any changes. Useful for checking syntax of set
                | commands.
    @type check: bool
    @param sync: A bool set to true to sync the commit across both REs.
    @type sync: bool
    @param comment: A string that will be logged to the commit log
                  | describing the commit.
    @type comment: str
    @param confirm: An integer of seconds to commit confirm for.
    @type confirm: int
    @param at_time: A string containing the time or time and date of when
                  | the commit should happen. Junos is expecting one of two
                  | formats:
                  | A time value of the form hh:mm[:ss] (hours, minutes,
                  |     and optionally seconds)
                  | A date and time value of the form yyyy-mm-dd hh:mm[:ss]
                  |     (year, month, date, hours, minutes, and optionally
                  |      seconds)
    @type at_time: str
    @param blank: A bool set to true to only make a blank commit. A blank
                | commit makes a commit, but doesn't have any set commands
                | associated with it, so no changes are made, but a commit
                | does happen.
    @type blank: bool

    @returns: The output from the device.
    @rtype: str
    """
    # set the commands to do nothing if the user wants a blank commit.
    if blank:
        commands = 'annotate system ""'
    output = ""
    # add show | compare output
    if commands != "":
        output += color("show | compare:\n", 'yel')
        try:
            output += color_diffs(jaide.compare_config(commands)) + '\n'
        except RPCError as e:
            output += color("Could not get config comparison results before"
                            " committing due to the following error:\n%s" %
                            str(e))
    # If they just want to validate the config, without committing
    if check:
        output += color("Commit check results from: %s\n" % jaide.host, 'yel')
        try:
            output += jaide.commit_check(commands) + '\n'
        except RPCError:
            output += color("Uncommitted changes left on the device or someone"
                            " else is in edit mode, couldn't lock the "
                            "candidate configuration.\n", 'red')
        except:
            output += color("Failed to commit check on device %s for an "
                            "unknown reason.\n" % jaide.host, 'red')
    # Actually make a commit
    else:
        output += color("Attempting to commit on device: %s\n" % jaide.host,
                        'yel')
        try:
            results = jaide.commit(confirmed=confirm, comment=comment,
                                   at_time=at_time, synchronize=sync,
                                   commands=commands)
        except RPCError as e:
            output += color('Commit could not be completed on this device, due'
                            ' to the following error(s):\n' + str(e), 'red')
        # Jaide command succeeded, parse results
        else:
            if 'commit complete' in results:
                output += results.split('commit complete')[0] + '\n'
                output += color('Commit complete on device: %s\n' % jaide.host)
                if confirm:
                    output += color('Commit confirm will rollback in %s '
                                    'minutes unless you commit again.\n' %
                                    str(confirm/60))
            elif 'commit at' in results:
                output += results.split('commit at will be executed at')[0]
                output += color('Commit staged to happen at: %s\n' % at_time)
            else:
                if 'failed' in results:
                    output += (results.replace('failed', color('failed',
                               'red')))
                if 'red' in results:
                    output += (results.replace('red', color('red',
                               'red')))
                output += color('Commit Failed on device: %s\n' % jaide.host,
                                'red')
    return output
Ejemplo n.º 6
0
Archivo: wrap.py Proyecto: suakow/jaide
def commit(jaide, commands, check, sync, comment, confirm, at_time, blank):
    """ Execute a commit against the device.

    Purpose: This function will send set commands to a device, and commit
           | the changes. Options exist for confirming, comments,
           | synchronizing, checking, blank commits, or delaying to a later
           | time/date.

    @param jaide: The jaide connection to the device.
    @type jaide: jaide.Jaide object
    @param commands: String containing the set command to be sent to the
                   | device. It can be a python list of strings, a single set
                   | command, a comma separated string of commands, or a
                   | string filepath pointing to a file with set commands
                   | on each line.
    @type commands: str or list
    @param check: A bool set to true to only run a commit check, and not
                | commit any changes. Useful for checking syntax of set
                | commands.
    @type check: bool
    @param sync: A bool set to true to sync the commit across both REs.
    @type sync: bool
    @param comment: A string that will be logged to the commit log
                  | describing the commit.
    @type comment: str
    @param confirm: An integer of seconds to commit confirm for.
    @type confirm: int
    @param at_time: A string containing the time or time and date of when
                  | the commit should happen. Junos is expecting one of two
                  | formats:
                  | A time value of the form hh:mm[:ss] (hours, minutes,
                  |     and optionally seconds)
                  | A date and time value of the form yyyy-mm-dd hh:mm[:ss]
                  |     (year, month, date, hours, minutes, and optionally
                  |      seconds)
    @type at_time: str
    @param blank: A bool set to true to only make a blank commit. A blank
                | commit makes a commit, but doesn't have any set commands
                | associated with it, so no changes are made, but a commit
                | does happen.
    @type blank: bool

    @returns: The output from the device.
    @rtype: str
    """
    # set the commands to do nothing if the user wants a blank commit.
    if blank:
        commands = 'annotate system ""'
    output = ""
    # add show | compare output
    if commands != "":
        output += color("show | compare:\n", 'yel')
        try:
            output += color_diffs(jaide.compare_config(commands)) + '\n'
        except RPCError as e:
            output += color("Could not get config comparison results before"
                            " committing due to the following error:\n%s" %
                            str(e))
    # If they just want to validate the config, without committing
    if check:
        output += color("Commit check results from: %s\n" % jaide.host, 'yel')
        try:
            output += jaide.commit_check(commands) + '\n'
        except RPCError:
            output += color(
                "Uncommitted changes left on the device or someone"
                " else is in edit mode, couldn't lock the "
                "candidate configuration.\n", 'red')
        except:
            output += color(
                "Failed to commit check on device %s for an "
                "unknown reason.\n" % jaide.host, 'red')
    # Actually make a commit
    else:
        output += color("Attempting to commit on device: %s\n" % jaide.host,
                        'yel')
        try:
            results = jaide.commit(confirmed=confirm,
                                   comment=comment,
                                   at_time=at_time,
                                   synchronize=sync,
                                   commands=commands)
        except RPCError as e:
            output += color(
                'Commit could not be completed on this device, due'
                ' to the following error(s):\n' + str(e), 'red')
        # Jaide command succeeded, parse results
        else:
            if 'commit complete' in results:
                output += results.split('commit complete')[0] + '\n'
                output += color('Commit complete on device: %s\n' % jaide.host)
                if confirm:
                    output += color('Commit confirm will rollback in %s '
                                    'minutes unless you commit again.\n' %
                                    str(confirm / 60))
            elif 'commit at' in results:
                output += results.split('commit at will be executed at')[0]
                output += color('Commit staged to happen at: %s\n' % at_time)
            else:
                if 'failed' in results:
                    output += (results.replace('failed',
                                               color('failed', 'red')))
                if 'red' in results:
                    output += (results.replace('red', color('red', 'red')))
                output += color('Commit Failed on device: %s\n' % jaide.host,
                                'red')
    return output