def debug_snippets_in_repo(directory: Path, err_list: list) -> list: sl = SkilletLoader() all_skillets = sl.load_all_skillets_from_dir(directory) for e in sl.skillet_errors: err_detail = dict() err_detail['severity'] = 'error' if type(e) is dict: err_detail['path'] = e.get('path', str(directory.absolute())) err_detail['err_list'] = [ e.get('error', 'Could not load Skillet YAML') ] else: err_detail['path'] = str(directory.absolute()) err_detail['err_list'] = [e] err_list.append(err_detail) for skillet in all_skillets: debug_errors = sl.debug_skillet_structure(skillet.skillet_dict) if debug_errors: err_detail = dict() err_detail['path'] = skillet.path err_detail['severity'] = 'error' err_detail['err_list'] = debug_errors err_list.append(err_detail) return err_list
def load_skillet_by_name(self, skillet_name) -> (dict, None): """ Loads application specific skillet :param skillet_name: :return: """ all_skillets = cnc_utils.get_long_term_cached_value( self.app_dir, 'all_snippets') if not all_skillets: all_skillets = list() application_skillets_dir = Path( os.path.join(settings.SRC_PATH, self.app_dir, 'snippets')) skillet_loader = SkilletLoader() app_skillets = skillet_loader.load_all_skillets_from_dir( application_skillets_dir) for s in app_skillets: all_skillets.append(s.skillet_dict) cnc_utils.set_long_term_cached_value(self.app_dir, 'all_snippets', all_skillets, -1) for skillet in all_skillets: if skillet['name'] == skillet_name: return skillet return None
def refresh_skillets_from_repo(repo_name: str) -> list: all_skillets = list() user_dir = os.path.expanduser('~/.pan_cnc') app_name = get_default_app_name() snippets_dir = os.path.join(user_dir, app_name, 'repositories') repo_dir = os.path.join(snippets_dir, repo_name) if not os.path.exists(repo_dir): print(f'Repository {repo_dir} does not exist!') return all_skillets try: repo_object = RepositoryDetails.objects.get(name=repo_name) sl = SkilletLoader() found_skillets = sl.load_all_skillets_from_dir(repo_dir) for skillet_object in found_skillets: skillet_name = skillet_object.name (skillet_record, created) = Skillet.objects.get_or_create( name=skillet_name, defaults={ 'skillet_json': json.dumps(skillet_object.skillet_dict), 'repository_id': repo_object.id, } ) if not created: if skillet_record.repository_id == repo_object.id: # check if skillet contents have been updated found_skillet_json = json.dumps(skillet_object.skillet_dict) if skillet_record.skillet_json != found_skillet_json: skillet_record.skillet_json = found_skillet_json skillet_record.save() else: print(f'Found existing skillet from another repository: {skillet_name}!!') raise DuplicateSkilletException( f'Refusing to import duplicate Skillet: {skillet_name}' ) for db_skillet in repo_object.skillet_set.all(): found = False for found_skillet in found_skillets: if db_skillet.name == found_skillet.name: found = True continue if not found: db_skillet.delete() update_skillet_cache() return load_skillets_from_repo(repo_name) except ObjectDoesNotExist: return all_skillets
def refresh_skillets_from_repo(repo_name: str) -> list: all_skillets = list() user_dir = os.path.expanduser('~/.pan_cnc') app_name = get_default_app_name() snippets_dir = os.path.join(user_dir, app_name, 'repositories') repo_dir = os.path.join(snippets_dir, repo_name) try: repo_object = RepositoryDetails.objects.get(name=repo_name) sl = SkilletLoader() found_skillets = sl.load_all_skillets_from_dir(repo_dir) for skillet_object in found_skillets: skillet_name = skillet_object.name (skillet_record, created) = Skillet.objects.get_or_create( name=skillet_name, defaults={ 'skillet_json': json.dumps(skillet_object.skillet_dict), 'repository_id': repo_object.id, }) if not created: # check if skillet contents have been updated found_skillet_json = json.dumps(skillet_object.skillet_dict) if skillet_record.skillet_json != found_skillet_json: skillet_record.skillet_json = found_skillet_json skillet_record.save() for db_skillet in repo_object.skillet_set.all(): found = False for found_skillet in found_skillets: if db_skillet.name == found_skillet.name: found = True continue if not found: db_skillet.delete() update_skillet_cache() return load_skillets_from_repo(repo_name) except ObjectDoesNotExist: return all_skillets
repo = os.environ.get( 'REPO', 'https://github.com/PaloAltoNetworks/SkilletBuilder.git') repo_branch = os.environ.get('BRANCH', 'master') repo_name = os.environ.get('NAME', 'appetizer') local_dir = os.path.expanduser('~/.pan_cnc/appetizer/repositories') if not os.path.exists(local_dir): os.makedirs(local_dir) sl = SkilletLoader() repo_full_dir = os.path.join(local_dir, repo_name) if os.path.exists(repo_full_dir): print('Using local dir') all_skillets = sl.load_all_skillets_from_dir(repo_full_dir) else: print(f'Pulling anew into {local_dir}') # do not check for self signed certs here os.environ['GIT_SSL_NO_VERIFY'] = "1" all_skillets = sl.load_from_git(repo, repo_name, repo_branch, local_dir=local_dir) src_dir = os.path.join(repo_full_dir, 'src') if os.path.exists(src_dir): print('Found a CNC APP to build') for d in os.listdir(src_dir): full_path = os.path.join(src_dir, d)