def check_compliance(hostname, test_file):
    opts = salt.config.master_config('/etc/salt/master')
    runner = salt.runner.RunnerClient(opts)
    pillar = runner.cmd('pillar.show_pillar', [hostname])
    config_file_object = {
        "hosts": [
            {
                "device":  pillar['proxy']['host'],
                "username": pillar['proxy']['username'],
                "passwd": pillar['proxy']['passwd']
            }
        ],
        "tests": [
            test_file
        ]
    }
    config_file = yaml.dump(config_file_object)
    js = SnapAdmin()
    snapcheck_output = js.snapcheck(config_file, "automatic_snapshot_from_saltstack")
    json_output = {}
    for item in snapcheck_output:
        json_output = {
            'jsnapy_device_name': hostname,
            'jsnapy_device_ip': item.device,
            'jsnapy_result': item.result,
            'jsnapy_nbr_passed': item.no_passed,
            'jsnapy_nbr_failed': item.no_failed,
            'jsnapy_test_file': test_file
            }
    if json_output['jsnapy_result'] == 'Failed':
        caller = salt.client.LocalClient()
        caller.cmd(hostname, 'event.send', ['jnpr/compliance/failed'], kwarg={'result': json_output})
    return json_output
Ejemplo n.º 2
0
def audit_config(*args, **kvargs):
    CONF_DATA = """
tests:
  - test_telnet.yml

mail: /etc/jsnapy/send_mail.yml

hosts:
"""

    vMX_1_DATA = """
  - device: 10.254.0.41
    username: lab
    passwd: lab123
"""

    vMX_2_DATA = """
  - device: 10.254.0.42
    username: lab
    passwd: lab123
"""

    if "device" in kvargs:
        if kvargs["device"] == "vMX-1":
            CONF_DATA += vMX_1_DATA
        if kvargs["device"] == "vMX-2":
            CONF_DATA += vMX_2_DATA
    else:  # possibly called by salt-run
        CONF_DATA += (vMX_1_DATA + vMX_2_DATA)

    snapadmin = SnapAdmin()
    result = snapadmin.snapcheck(CONF_DATA, "pre")
Ejemplo n.º 3
0
 def __init__(self, hostname, model, ipaddress, username, password):
     self.hostname = hostname
     self.model = model
     self.username = username
     self.password = password
     self.ipaddress = ipaddress
     self.device = Device(host=ipaddress, user=username, password=password)
     self.snap = SnapAdmin()
Ejemplo n.º 4
0
 def pre_check(self):
     """ Run pre-change snapshot """
     js = SnapAdmin()
     try:
         self.pre_snap = js.snap(self.template, "pre")
         self.data = 'Pre-check snapshot complete'
     except Exception as ex:
         self.error = str(ex.args) + "\n" + str(ex.message)
         return
Ejemplo n.º 5
0
 def post_check(self):
     """ Run post-change snapshot and compare with pre """
     js = SnapAdmin()
     try:
         self.post_snap = js.snap(self.template, "post")
     except Exception as ex:
         self.error = str(ex.args) + "\n" + str(ex.message)
         return
     self.post_compare()
Ejemplo n.º 6
0
def jsnapy_post(task):
    host_check_yml = output_dir + task.host.name + '_check.yml'
    with open(host_check_yml, 'r') as f:
        host_check_resutl = f.read()
    js = SnapAdmin()
    config_host = config_data.format(task.host.hostname, task.host.username,
                                     task.host.password, host_check_resutl)
    js.snap(config_host, "post")
    snapchk = js.check(config_host, "pre", "post")
    result_output = '{}{}_{}_result.txt'
    for val in snapchk:
        with open(result_output.format(output_dir, task.host.name, dates),
                  'w') as f:
            f.write(json.dumps(dict(val.test_details), indent=4))
Ejemplo n.º 7
0
def config_verification(services, args):
    """Perform config verification with JSNAPy
    """
    def get_test_text(name, port):

        return """
---
test_app_%s:
- rpc: get-config
- item:
    id: ./name
    xpath: 'applications/application[name="k8s%s"]'
    tests:
    - is-equal: destination-port, %s
      info: "Test Succeeded!!, destination-port is <{{post['destination-port']}}>"
      err: "Test Failed!!!, destination-port is <{{post['destination-port']}}>"
        """ % (name, name, port)

    jsnapy_config = {
        "hosts": [{
            "device": "127.0.0.1",
            "username": "******",
            "passwd": "Juniper",
            "port": args.vsrx_port
        }],
        "tests": []
    }

    for service in services:

        # Create test file for this service
        test_filename = "scripts/jsnapytest_%s.yaml" % service["name"]
        with open(test_filename, 'w') as testfile:
            testfile.write(get_test_text(service["name"], service["port"]))

        # Add reference for this test file to config
        jsnapy_config["tests"].append(test_filename)

    # Write config file to disk
    with open('scripts/jsnapyconfig.yaml', 'w') as configfile:
        yaml.dump(jsnapy_config, configfile, default_flow_style=False)

    # Retrieve instant snapshot and run tests on result
    js = SnapAdmin()
    chk = js.snapcheck('scripts/jsnapyconfig.yaml')

    # Ensure all tests passed
    for check in chk:
        assert check.result == "Passed"
Ejemplo n.º 8
0
 def test_snap_2(self, mock_log, mock_etree, mock_dev, mock_parser,
                 mock_exit, mock_path):
     js = SnapAdmin()
     conf_file = os.path.join(os.path.dirname(__file__), 'configs',
                              'main.yml')
     config_file = open(conf_file, 'r')
     js.main_file = yaml.load(config_file, Loader=yaml.FullLoader)
     dev = jnpr.junos.device.Device(host="1.1.1.1",
                                    user="******",
                                    passwd="xyz")
     dev.open()
     m_op = mock_open()
     with (patch('jnpr.jsnapy.snap.open', m_op, create=True)) as (m_open):
         mock_path.return_value = os.path.join(os.path.dirname(__file__),
                                               'configs')
         js.generate_rpc_reply(dev, self.output_file, "1.1.1.1",
                               js.main_file)
         self.assertTrue(mock_path.called)
     dev.close()
Ejemplo n.º 9
0
def jsnapy_pre(task, jsnapy_test):
    if jsnapy_test:
        task.host['host_checks'] = jsnapy_test
    else:
        task.run(task=gather_data)
        task.run(task=get_host_check)

    host_check_result = ''
    for check in task.host['host_checks']:
        host_check_result += test_yml.format(template_path.format(check))

    host_check_yml = output_dir + task.host.name + '_check.yml'
    with open(host_check_yml, 'w') as f:
        f.write(host_check_result)

    js = SnapAdmin()
    config_host = config_data.format(task.host.hostname, task.host.username,
                                     task.host.password, host_check_result)
    js.snap(config_host, "pre")
Ejemplo n.º 10
0
    def post_compare(self):
        """ Run post check and gather output """
        # Setup a memory buffer to capture logging messages from jsnapy
        jsnapy_log = logging.handlers.MemoryHandler(1024 * 10, logging.DEBUG)
        jsnapy_log.setLevel(logging.DEBUG)
        log = logging.getLogger()
        log.setLevel(logging.DEBUG)
        debug_format = logging.Formatter("%(message)")
        jsnapy_log.setFormatter(debug_format)
        log.addHandler(jsnapy_log)

        # Run jsnapy check on pre and post snapshots
        js = SnapAdmin()
        self.result = js.check(self.template, pre_file="pre", post_file="post")

        # Gather output from buffer
        post_log = []
        for line in jsnapy_log.buffer:
            post_log.append(str(line.getMessage()))
        self.data = format_html.formatting(post_log)
def main():

    js = SnapAdmin()

    config_file = "infra.yaml"
    js.snap(config_file, "module_snap1")
    js.snap(config_file, "module_snap1")
    chk = js.check(config_file, "module_snap1", "module_snap1")

    for check in chk:
        print "Tested on", check.device
        print "Final result: ", check.result
        print "Total passed: ", check.no_passed
        print "Total failed:", check.no_failed
        #pprint(dict(check.test_details))

    if (check.result == "Failed"):
        print("The snapshot verification has failed")

    else:
        print("The snapshot verification was successful")

    pprint(dict(check))
Ejemplo n.º 12
0
### performing function similar to --snapcheck option in command line ######
from jnpr.jsnapy import SnapAdmin
from pprint import pprint
from jnpr.junos import Device

js = SnapAdmin()

config_file = "/etc/jsnapy/testfiles/config_single_snapcheck.yml"
snapvalue = js.snapcheck(config_file, "snap")

for snapcheck in snapvalue:
    print "\n -----------snapcheck----------"
    print "Tested on", snapcheck.device
    print "Final result: ", snapcheck.result
    print "Total passed: ", snapcheck.no_passed
    print "Total failed:", snapcheck.no_failed
    pprint(dict(snapcheck.test_details))
Ejemplo n.º 13
0
def invokeTests(vmanme):

    js = SnapAdmin()

    helpers = Helpers()

    config_file = """
    hosts:
    - device: 192.168.122.9
      username: ubuntu
    tests:
    - test_diff.yml
    """

    #js.snap(config_file, "pre")
    js.snap(config_file, "post")
    chk = js.check(config_file, "pre", "post")

    '''
    file_name = "/home/ubuntu/Evolved_Campus_Core_Automation/Inventory"
    with open(file_name) as f:
        content = f.read()
        campuses = re.findall('\[(.*?)\]',content)

    campuses_info = {}
    for campus in campuses:
        if "children" in campus:
            campus_id = campus.rsplit(":",1)[0]
            campuses_info.update({campus_id: {'leaf': [],'spine':[]}})
    for campus in campuses:
        leaf_count = 0
        spine_count = 0
        campus_id = campus.rsplit("-")[1]
        if not "children"in campus:
            if "leaf" in campus:
                data_loader = DataLoader()
                inventory = InventoryManager(loader = data_loader,
                                            sources=[file_name])
                lst = inventory.get_groups_dict()[campus]
                for ls in lst:
                    campuses_info['campus-' + campus_id]['leaf'].append(ls)
            elif "spine" in campus:
                data_loader = DataLoader()
                inventory = InventoryManager(loader = data_loader,
                                            sources=[file_name])
                lst = inventory.get_groups_dict()[campus]
                for ls in lst:
                    campuses_info['campus-' + campus_id]['spine'].append(ls)

    '''
    spine_leaf_info = {}
    '''
    for campus in campuses_info:
        for leaf_dev in campuses_info[campus]["leaf"]:
            spine_leaf_info.update({leaf_dev: campuses_info[campus]["spine"]})
    '''

    devip = ""
    failed = 0
    test_name = ""
    for check in chk:
        devip = check.device
        failed = check.no_failed
        if not not check.test_results.keys():
            test_name = check.test_results.keys()[0].replace(' ','_')

    ae_id = ""
    dev = helpers.device_connect(devip)
    dev.open()
    data = dev.rpc.get_config(options={'format':'json'})
    for ints in data['configuration']['interfaces']['interface']:
        if "ae" in ints['name']:
            ae_id = ints['name']
    dev.close()

    pre_file = '/home/ubuntu/jsnapy/snapshots/' + devip + '_pre_' + test_name + '.xml'
    post_file = '/home/ubuntu/jsnapy/snapshots/' + devip + '_post_' + test_name + '.xml'

    dict = {}
    if failed != 0:
        with open(pre_file, 'r') as hosts0:
            with open(post_file, 'r') as hosts1:
                diff = difflib.unified_diff(
                    hosts0.readlines(),
                    hosts1.readlines(),
                    fromfile='hosts0',
                    tofile='hosts1',
                    n=0,
                )
                lines = list(diff)[2:]
        print lines
        added = [line[1:] for line in lines if line[0] == '+']
        additions = ""
        for line in added:
            additions += line
        print 'additions'
        vlan_text = ""
        if additions:
            tree = xml.etree.ElementTree.fromstring(additions)
            print tree
            for vlan in tree.findall('l2ng-l2rtb-vlan-name'):
                text = vlan.text
                vlan_text = text.split("vlan")[1]
                print vlan_text

            dict = {'vqfx3': {'vlanid':vlan_text}}
            template_file = '/home/ubuntu/Evolved_Campus_Core_Automation/Config/QFX_vlan_leaf_addition.conf'
            helpers.load_template_config(devip,"spine",dict,template_file)

            '''
            print spine_leaf_info
            for spine_ip in spine_leaf_info[devip]:
                template_file = '/home/ubuntu/Evolved_Campus_Core_Automation/Config/QFX_vlan_spine_addition.conf'
                #helpers.load_template_config(spine_ip,"spine",dict,template_file)
            '''

    if failed != 0:
        with open(post_file) as f:
            lines = f.readlines()
            lines1 = ""
            for line in lines:
                lines1 += line
            with open(pre_file, "w") as f1:
                f1.writelines(lines1)
Ejemplo n.º 14
0
import pprint
from jnpr.jsnapy import SnapAdmin
from jnpr.junos import Device

CONFIG_FILE = 'test_show_default_route.yml'
PRE_SNAP = 'pre_show_route_test.xml'
POST_SNAP = 'post_show_route_test.xml'

TESTS = {'tests': [CONFIG_FILE]}

if __name__ == "__main__":
    device = Device(host='example.com', user='******', passwd='123456')

    snap = SnapAdmin()
    r = snap.check(TESTS, PRE_SNAP, POST_SNAP, device)
    pprint.pprint(r[0].test_results)