def add_repos(ctx, filename): """ Add repositories to Augur's database """ app = ctx.obj df = app.database.execute( s.sql.text("SELECT repo_group_id FROM augur_data.repo_groups")) repo_group_IDs = [group[0] for group in df.fetchall()] insertSQL = s.sql.text(""" INSERT INTO augur_data.repo(repo_group_id, repo_git, repo_status, tool_source, tool_version, data_source, data_collection_date) VALUES (:repo_group_id, :repo_git, 'New', 'CLI', 1.0, 'Git', CURRENT_TIMESTAMP) """) with open(filename) as upload_repos_file: data = csv.reader(upload_repos_file, delimiter=',') for row in data: logger.info( f"Inserting repo with Git URL `{row[1]}` into repo group {row[0]}" ) if int(row[0]) in repo_group_IDs: result = app.database.execute(insertSQL, repo_group_id=int(row[0]), repo_git=row[1]) else: logger.warn( f"Invalid repo group id specified for {row[1]}, skipping.")
def check_for_upgrade(ctx): """ Upgrade the configured database to the latest version """ app = ctx.obj check_pgpass_credentials(app.config) current_db_version = get_db_version(app) update_scripts_filenames = [] for (_, _, filenames) in walk('schema/generate'): update_scripts_filenames.extend( [file for file in filenames if 'update' in file]) # files_temp.extend([file.split("-")[1][14:].split(".")[0] for file in filenames if 'update' in file]) break target_version_script_map = {} for script in update_scripts_filenames: upgrades_to = int(script.split("-")[1][14:].split(".")[0]) target_version_script_map[upgrades_to] = str(script) target_version_script_map = OrderedDict( sorted(target_version_script_map.items())) most_recent_version = list(target_version_script_map.keys())[-1] if current_db_version == most_recent_version: logger.info("Database is already up to date.") elif current_db_version < most_recent_version: logger.info( f"Current database version: v{current_db_version}\nPlease upgrade to the most recent version (v{most_recent_version}) with augur db upgrade-db-version." ) elif current_db_version > most_recent_version: logger.warn( f"Unrecognized version: {current_db_version}\nThe most recent version is {most_recent_version}. Please contact your system administrator to resolve this error." )
def __init__(self): """ Reads config, creates DB session, and initializes cache """ self.config_file_name = 'augur.config.json' self.__shell_config = None self.__export_file = None self.__env_file = None self.config = default_config self.env_config = {} self.root_augur_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) default_config_path = self.root_augur_dir + '/' + self.config_file_name using_config_file = False config_locations = [self.config_file_name, default_config_path, f"/opt/augur/{self.config_file_name}"] if os.getenv('AUGUR_CONFIG_FILE') is not None: config_file_path = os.getenv('AUGUR_CONFIG_FILE') using_config_file = True else: for index, location in enumerate(config_locations): try: f = open(location, "r+") config_file_path = os.path.abspath(location) using_config_file = True f.close() break except FileNotFoundError: pass if using_config_file: try: with open(config_file_path, 'r+') as config_file_handle: self.config = json.loads(config_file_handle.read()) except json.decoder.JSONDecodeError as e: logger.warn('%s could not be parsed, using defaults. Fix that file, or delete it and run this again to regenerate it. Error: %s', config_file_path, str(e)) else: logger.warn('%s could not be parsed, using defaults.') self.load_env_configuration() # List of data sources that can do periodic updates self.cache_config = { 'cache.type': 'file', 'cache.data_dir': 'runtime/cache/', 'cache.lock_dir': 'runtime/cache/' } if not os.path.exists(self.cache_config['cache.data_dir']): os.makedirs(self.cache_config['cache.data_dir']) if not os.path.exists(self.cache_config['cache.lock_dir']): os.makedirs(self.cache_config['cache.lock_dir']) cache_parsed = parse_cache_config_options(self.cache_config) self.cache = CacheManager(**cache_parsed) self.metrics = MetricDefinitions(self)
def get_api_key(ctx): app = ctx.obj get_api_key_sql = s.sql.text(""" SELECT value FROM augur_operations.augur_settings WHERE setting='augur_api_key'; """) try: print(app.database.execute(get_api_key_sql).fetchone()[0]) except TypeError: logger.warn("No Augur API key found.")
def import_plugins(cls): """ Imports all plugins and datasources """ if not hasattr(cls, 'plugins'): setattr(cls, 'plugins', {}) plugins = [augur.plugins, augur.datasources] for module in plugins: for importer, modname, ispkg in pkgutil.iter_modules(module.__path__): if ispkg: try: importer.find_module(modname).load_module(modname) except Exception as e: logger.warn('Error when loading plugin {module.__name__}.{modname}:') logger.exception(e)