예제 #1
0
def add_vendors():
    if not request.json:
        abort(400)
    body = request.json
    LOGGER.info('Adding vendor with body {}'.format(body))
    tree_created = False
    resolved_authorization = authorize_for_vendors(request, body)
    if 'passed' != resolved_authorization:
        return resolved_authorization
    try:
        http_request(protocol + '://' + confd_ip + ':' + repr(confdPort) + '/api/config/platforms/',
                     'DELETE', None, credentials, 'application/vnd.yang.collection+json')
    except:
        pass
    path = protocol + '://' + confd_ip + ':' + repr(confdPort) + '/api/config/'
    try:
        http_request(path, 'POST', json.dumps(body), credentials, 'application/vnd.yang.data+json')
    except urllib2.HTTPError as e:
        abort(e.code)
    repo = {}

    direc = 0
    while True:
        if os.path.isdir('../parseAndPopulate/' + repr(direc)):
            direc += 1
        else:
            break
    direc = '../parseAndPopulate/' + repr(direc)

    for platform in body['platforms']:
        capability = platform['module-list-file']
        file_name = capability['path'].split('/')[-1]
        if request.method == 'POST':
            repo_split = capability['repository'].split('.')[0]
            if os.path.isfile('../../api/vendor/' + capability['owner'] + '/' + repo_split + '/' + capability['path']):
                continue

        repo_url = url + capability['owner'] + '/' + capability['repository']

        if repo_url not in repo:
            repo[repo_url] = repoutil.RepoUtil(repo_url)
            repo[repo_url].clone()

        for submodule in repo[repo_url].repo.submodules:
            submodule.update(init=True)

        if capability.get('branch'):
            branch = capability.get('branch')
        else:
            branch = 'master'
        directory = '/'.join(capability['path'].split('/')[:-1])
        save_to = direc + '/temp/' + capability['owner'] + '/' \
                  + capability['repository'].split('.')[0] + '/' + branch + '/' + directory

        try:
            shutil.copytree(repo[repo_url].localdir + '/' + directory, save_to,
                            ignore=shutil.ignore_patterns('*.json', '*.xml', '*.sh', '*.md', '*.txt', '*.bin'))
        except OSError:
            pass
        with open(save_to + '/' + file_name.split('.')[0] + '.json', "w") as plat:
            json.dump(platform, plat)
        shutil.copy(repo[repo_url].localdir + '/' + capability['path'], save_to)
        tree_created = True

    for key in repo:
        repo[key].remove()
    arguments = ["python", "../parseAndPopulate/populate.py", "--port", repr(confdPort), "--dir", direc + "/temp",
                 "--api", "--ip", confd_ip, "--credentials", credentials[0], credentials[1], repr(tree_created),
                 integrity_file_location, protocol]
    job_id = sender.send('#'.join(arguments))
    LOGGER.info('job_id {}'.format(job_id))
    return jsonify({'info': 'Verification successful', 'job-id': job_id})
예제 #2
0
def add_modules():
    if not request.json:
        abort(400)
    body = request.json
    LOGGER.info('Adding modules with body {}'.format(body))
    tree_created = False

    with open('./prepare-sdo.json', "w") as plat:
        json.dump(body, plat)

    try:
        http_request(protocol + '://' + confd_ip + ':' + repr(confdPort) + '/api/config/modules/',
                     'DELETE', None, credentials, 'application/vnd.yang.collection+json')
    except:
        pass
    path = protocol + '://' + confd_ip + ':' + repr(confdPort) + '/api/config/modules'
    try:
        http_request(path, 'PUT', json.dumps(body), credentials, 'application/vnd.yang.data+json')
    except urllib2.HTTPError as e:
        abort(e.code)
    repo = {}
    warning = []

    direc = 0
    while True:
        if os.path.isdir('../parseAndPopulate/' + repr(direc)):
            direc += 1
        else:
            break
    direc = '../parseAndPopulate/' + repr(direc)
    for mod in body['modules']['module']:
        sdo = mod['source-file']
        orgz = mod['organization']
        if request.method == 'POST':
            try:
                path = protocol + '://' + confd_ip + ':' + repr(confdPort) + '/api/config/catalog/modules/module/' + \
                       mod['name'] + ',' + mod['revision'] + ',' + mod['organization']
                http_request(path, 'GET', None, credentials, 'application/vnd.yang.data+json')
                continue
            except urllib2.HTTPError as e:
                pass
        directory = '/'.join(sdo['path'].split('/')[:-1])

        repo_url = url + sdo['owner'] + '/' + sdo['repository']
        LOGGER.debug('Cloning repository')
        if repo_url not in repo:
            repo[repo_url] = repoutil.RepoUtil(repo_url)
            repo[repo_url].clone()

        for submodule in repo[repo_url].repo.submodules:
            submodule.update(init=True)

        if sdo.get('branch'):
            branch = sdo.get('branch')
        else:
            branch = 'master'
        save_to = direc + '/temp/' + sdo['owner'] + '/' + sdo['repository'].split('.')[0] \
                  + '/' + branch + '/' + directory
        try:
            os.makedirs(save_to)
        except OSError as e:
            # be happy if someone already created the path
            if e.errno != errno.EEXIST:
                raise
        shutil.copy(repo[repo_url].localdir + '/' + sdo['path'], save_to)
        if os.path.isfile('./prepare-sdo.json'):
            shutil.move('./prepare-sdo.json', direc)
        tree_created = True
        organization = ''
        try:
            namespace = yangParser.parse(os.path.abspath(save_to + '/' + sdo['path'].split('/')[-1])) \
                .search('namespace')[0].arg

            for ns, org in NS_MAP.items():
                if ns in namespace:
                    organization = org
            if organization == '':
                if 'urn:' in namespace:
                    organization = namespace.split('urn:')[1].split(':')[0]
        except:
            while True:
                try:
                    belongs_to = yangParser.parse(os.path.abspath(repo[repo_url].localdir + '/' + sdo['path'])) \
                        .search('belongs-to')[0].arg
                except:
                    break
                try:
                    namespace = yangParser.parse(os.path.abspath(repo[repo_url].localdir + '/' + '/'.join(
                        sdo['path'].split('/')[:-1]) + '/' + belongs_to + '.yang')).search('namespace')[0].arg
                    for ns, org in NS_MAP.items():
                        if ns in namespace:
                            organization = org
                    if organization == '':
                        if 'urn:' in namespace:
                            organization = namespace.split('urn:')[1].split(':')[0]
                    break
                except:
                    pass
        resolved_authorization = authorize_for_sdos(request, orgz, organization)
        if not resolved_authorization:
            shutil.rmtree(direc)
            for key in repo:
                repo[key].remove()
            return unauthorized()
        if 'organization' in repr(resolved_authorization):
            warning.append(sdo['path'].split('/')[-1] + ' ' + resolved_authorization)

    for key in repo:
        repo[key].remove()
    LOGGER.debug('Sending a new job')
    arguments = ["python", "../parseAndPopulate/populate.py", "--sdo", "--port", repr(confdPort), "--dir",
                 direc + "/temp", "--api", "--ip", confd_ip, "--credentials", credentials[0], credentials[1],
                 repr(tree_created), protocol]
    job_id = sender.send('#'.join(arguments))
    LOGGER.info('job_id {}'.format(job_id))
    if len(warning) > 0:
        return jsonify({'info': 'Verification successful', 'job-id': job_id, 'warnings': [{'warning': val}
                                                                                          for val in warning]})
    else:
        return jsonify({'info': 'Verification successful', 'job-id': job_id})
예제 #3
0
파일: draftPull.py 프로젝트: termlen0/yang
                        default='',
                        help='Set name of the repository for automatic push')
    parser.add_argument('--token',
                        type=str,
                        default='',
                        help='Set token of the repository for automatic push')
    args = parser.parse_args()

    github_credentials = ''
    if len(args.username) > 0:
        github_credentials = args.username + ':' + args.token + '@'

    # Fork and clone the repository YangModles/yang
    reponse = requests.post('https://' + github_credentials +
                            'api.github.com/repos/YangModels/yang/forks')
    repo = repoutil.RepoUtil('https://github.com/' + args.username +
                             '/yang.git')

    repo.clone()
    travis = TravisPy.github_auth(args.token)
    # Download all the latest yang modules out of http://www.claise.be/IETFYANGDraft.json and store them in tmp folder
    ietf_draft_json = load_json_from_url(
        'http://www.claise.be/IETFYANGDraft.json')
    try:
        os.makedirs(repo.localdir +
                    '/experimental/ietf-extracted-YANG-modules/')
    except OSError as e:
        # be happy if someone already created the path
        if e.errno != errno.EEXIST:
            raise
    for key in ietf_draft_json:
        yang_file = open(
예제 #4
0
def add_vendors():
    if not request.json:
        abort(400)
    body = request.json
    resolved_authorization = authorize_for_vendors(request, body)
    if 'passed' != resolved_authorization:
        return resolved_authorization

    repo = {}
    for platform in body['platforms']:
        capability = platform['capabilities-file']
        file_name = capability['path'].split('/')[-1]

        repo_url = url + capability['owner'] + '/' + capability['repo']
        if repo_url not in repo:
            repo[repo_url] = repoutil.RepoUtil(repo_url)
            repo[repo_url].clone()

        for submodule in repo[repo_url].repo.submodules:
            submodule.update(init=True)

        directory = '/'.join(capability['path'].split('/')[:-1])
        save_to = 'temp/' + capability['owner'] + '/' + capability[
            'repo'].split('.')[0] + '/' + directory

        try:
            shutil.copytree(repo[repo_url].localdir + '/' + directory,
                            save_to,
                            ignore=shutil.ignore_patterns(
                                '*.json', '*.xml', '*.sh', '*.md', '*.txt',
                                '*.bin'))
        except OSError:
            pass
        with open(save_to + '/' + file_name.split('.')[0] + '.json',
                  "w") as plat:
            json.dump(platform, plat)
        shutil.copy(
            repo[repo_url].localdir + '/' + capability['path'],
            save_to,
        )

    #os.system('python populate.py --port ' + repr(confdPort) + ' --dir temp --api --ip ' + confd_ip + ' --credentials '
    #          + ' '.join(credentials))
    with open("log.txt", "wr") as f:
        try:
            arguments = [
                "python", "../parseAndPopulate/populate.py", "--port",
                repr(confdPort), "--dir", "temp", "--api", "--ip", confd_ip,
                "--credentials", credentials[0], credentials[1]
            ]
            subprocess.check_call(arguments, stderr=f)
            #df = subprocess.Popen(args, stdout=subprocess.PIPE)
            #output, err = df.communicate()
        except subprocess.CalledProcessError as e:
            shutil.rmtree('temp/')
            for key in repo:
                repo[key].remove()
            return jsonify(
                {'error': 'Was not able to write all or partial yang files'})
    try:
        os.makedirs('../../api/vendor/')
    except OSError as e:
        # be happy if someone already created the path
        if e.errno != errno.EEXIST:
            raise
    subprocess.call(["cp", "-r", "temp/.", "../../api/vendor/"])

    shutil.rmtree('temp/')
    for item in os.listdir('./'):
        if 'log' in item and '.txt' in item:
            os.remove(item)
    for key in repo:
        repo[key].remove()
    integrity_file_name = datetime.datetime.utcnow().strftime(
        "%Y-%m-%dT%H:%m:%S.%f")[:-3] + 'Z'

    if integrity_file_name != './':
        shutil.move(
            './integrity.html', integrity_file_location + 'integrity' +
            integrity_file_name + '.html')
    return jsonify({
        'result': {
            'integrity-file':
            'www.yangcatalog.org/integrity/integrity' + integrity_file_name +
            '.html',
            'info':
            'success'
        }
    })
예제 #5
0
def add_modules():
    if not request.json:
        abort(400)
    body = request.json
    resolved_authorization = authorize_for_sdos(request, body)
    if not resolved_authorization:
        return unauthorized()

    with open('./prepare-sdo.json', "w") as plat:
        json.dump(body, plat)

    repo = {}
    for mod in body['modules']['module']:
        sdo = mod['sdo-file']

        directory = '/'.join(sdo['path'].split('/')[:-1])

        repo_url = url + sdo['owner'] + '/' + sdo['repo']
        if repo_url not in repo:
            repo[repo_url] = repoutil.RepoUtil(repo_url)
            repo[repo_url].clone()

        for submodule in repo[repo_url].repo.submodules:
            submodule.update(init=True)

        save_to = 'temp/' + sdo['owner'] + '/' + sdo['repo'].split(
            '.')[0] + '/' + directory
        try:
            os.makedirs(save_to)
        except OSError as e:
            # be happy if someone already created the path
            if e.errno != errno.EEXIST:
                raise
        shutil.copy(repo[repo_url].localdir + '/' + sdo['path'], save_to)

    #os.system('python populate.py --sdo --port ' + repr(confdPort) + ' --dir temp --api --ip ' + confd_ip + ' --credentials '
    #                + ' '.join(credentials))
    with open("log.txt", "wr") as f:
        try:
            arguments = [
                "python", "../parseAndPopulate/populate.py", "--sdo", "--port",
                repr(confdPort), "--dir", "temp", "--api", "--ip", confd_ip,
                "--credentials", credentials[0], credentials[1]
            ]
            subprocess.check_call(arguments, stderr=f)
            #df = subprocess.Popen(args, stdout=subprocess.PIPE)
            #output, err = df.communicate()
        except subprocess.CalledProcessError as e:
            shutil.rmtree('temp/')
            for key in repo:
                repo[key].remove()
            return jsonify(
                {'error': 'Was not able to write all or partial yang files'})

    try:
        os.makedirs('../../api/sdo/')
    except OSError as e:
        # be happy if someone already created the path
        if e.errno != errno.EEXIST:
            raise
    subprocess.call(["cp", "-r", "temp/.", "../../api/sdo/"])

    shutil.rmtree('temp')
    for item in os.listdir('./'):
        if 'log' in item and '.txt' in item:
            os.remove(item)
    for key in repo:
        repo[key].remove()
    return jsonify({'info': 'success'})
예제 #6
0
            platform_metadata['software-version'] = v.group(1)

        pn = re.search(r'^\s+cisco ([^\s]+)\s.*Chassis', version_output, re.M)
        if pn is not None:
            platform_metadata['name'] = pn.group(1)

        pid = re.search(r'PID: ([^,]+),', inventory_output)
        if pid is not None:
            platform_metadata['product-ids'].append(pid.group(1).strip())

    args.git_path = '%s/%s/%s' % (args.git_path, os, ver)

    #
    # Pull down the repo and create the file output directory
    #
    repo = repoutil.RepoUtil(args.git_repo)
    repo.clone()
    targetdir = repo.localdir + '/' + args.git_path
    caps_name = platform_metdata['name'].lower() + '-capabilities.xml'
    caps_file = targetdir + '/' + caps_name
    # TODO This should be revisited when ietf-yang-library support is added.
    platform_metadata['module-list-file']['type'] = 'capabilities'

    platform_metadata['module-list-file'][
        'path'] = args.git_path + '/' + caps_name
    platform_metadata['module-list-file']['owner'] = repo.get_owner()
    platform_metadata['module-list-file']['repository'] = repo.get_repo_dir()
    if not exists(targetdir):
        makedirs(targetdir)

    # testing