def get_nightly_comparisons(date=None): '''load the latest nightly comparisons. :param date: if provided, will load specified date instead of latest. ''' root = os.path.abspath(os.path.join(MEDIA_ROOT, 'trees', 'nightly')) base_name = "%s/container-tree" % (root) if date == None: date = "latest" base_name = "%s-%s.json" % (base_name, date) if os.path.exists(base_name): return read_json(base_name) return None
def read_client_secrets(): '''for private or protected registries, a client secrets file is required to be located at .sregistry. If no secrets are found, we use default of Singularity Hub, and return a dummy secrets. ''' client_secrets = _default_client_secrets() # If token file not provided, check environment secrets = _get_secrets_file() if secrets is not None: client_secrets = read_json(secrets) return client_secrets
def test_write_json(tmp_path): import json from sregistry.utils import write_json, read_json good_json = {"Wakkawakkawakka": [True, "2", 3]} tmpfile = str(tmp_path / 'good_json_file.txt') assert not os.path.exists(tmpfile) write_json(good_json, tmpfile) with open(tmpfile, 'r') as f: content = json.loads(f.read()) assert isinstance(content, dict) assert "Wakkawakkawakka" in content content = read_json(tmpfile) assert "Wakkawakkawakka" in content
def get_nightly_comparisons(date=None): """load the latest nightly comparisons. Parameters ========== date: if provided, will load specified date instead of latest. """ root = os.path.abspath( os.path.join(settings.MEDIA_ROOT, "trees", "nightly")) base_name = "%s/container-tree" % root if date is None: date = "latest" base_name = "%s-%s.json" % (base_name, date) if os.path.exists(base_name): return read_json(base_name) return None
def get_build_template(name="singularity-cloudbuild-local.json"): """get default build template. Parameters ========== name: singularity-cloudbuild-local.json (default) that will build a container interactively, waiting for the build to finish. singularity-cloudbuild-git.json build a recipe from a GitHub repository. """ base = get_installdir() name = "%s/main/templates/build/%s" % (base, name) if os.path.exists(name): bot.debug("Found template %s" % name) return read_json(name) bot.warning("Template %s not found." % name)
def _update_secrets(self): """update secrets will take a secrets credential file either located at .sregistry or the environment variable SREGISTRY_CLIENT_SECRETS and update the current client secrets as well as the associated API base. For the case of using Docker Hub, if we find a .docker secrets file, we update from there. """ # If the user has defined secrets, use them credentials = self._get_setting("SREGISTRY_DOCKERHUB_SECRETS") # First try for SINGULARITY exported, then try sregistry username = self._get_setting("SINGULARITY_DOCKER_USERNAME") password = self._get_setting("SINGULARITY_DOCKER_PASSWORD") username = self._get_setting("SREGISTRY_DOCKERHUB_USERNAME", username) password = self._get_setting("SREGISTRY_DOCKERHUB_PASSWORD", password) # Option 1: the user exports username and password auth = None if username is not None and password is not None: auth = basic_auth_header(username, password) self.headers.update(auth) # Option 2: look in .docker config file if credentials is not None and auth is None: if os.path.exists(credentials): credentials = read_json(credentials) # Find a matching auth in .docker config if "auths" in credentials: for auths, params in credentials["auths"].items(): if self._base in auths: if "auth" in params: auth = "Basic %s" % params["auth"] self.headers["Authorization"] = auth # Also update headers if "HttpHeaders" in credentials: for key, value in credentials["HttpHeaders"].items(): self.headers[key] = value else: bot.warning("Credentials file set to %s, but does not exist.")
def read_client_secrets(): '''for private or protected registries, a client secrets file is required to be located at .sregistry. If no secrets are found, we use default of Singularity Hub, and return a dummy secrets. ''' client_secrets = _default_client_secrets() # If token file not provided, check environment secrets = _get_secrets_file() # If exists, load if secrets is not None: client_secrets = read_json(secrets) # Otherwise, initialize else: from sregistry.defaults import SREGISTRY_CLIENT_SECRETS write_json(client_secrets, SREGISTRY_CLIENT_SECRETS) return client_secrets
def load_build_config(self, config=None): '''load a google compute config, meaning that we have the following cases: 1. the user has not provided a config file directly, we look in env. 2. the environment is not set, so we use a reasonable default 3. if the final string is not found as a file, we look for it in library 4. we load the library name, or the user file, else error Parameters ========== config: the config file the user has provided, or the library URI ''' # If the config is already a dictionary, it's loaded if isinstance(config, dict): bot.debug('Config is already loaded.') return config # if the config is not defined, look in environment, then choose a default if config is None: config = self._get_and_update_setting( 'SREGISTRY_COMPUTE_CONFIG', 'google/compute/ubuntu/securebuild-2.4.3') # If the config is a file, we read it elif os.path.exists(config): return read_json(config) # otherwise, try to look it up in library configs = self._load_templates(config) if configs is None: bot.error('%s is not a valid config. %s' % name) sys.exit(1) bot.info('Found config %s in library!' % config) config = configs[0] return config
######################### # Global Settings ######################### USERHOME = get_userhome() DISABLE_CACHE = convert2boolean(getenv("SINGULARITY_DISABLE_CACHE", False)) DISABLE_DATABASE = convert2boolean(getenv("SREGISTRY_DISABLE", False)) DISABLE_SSL_CHECK = convert2boolean(getenv("SREGISTRY_HTTPS_NOVERIFY", False)) _secrets = os.path.join(USERHOME, ".sregistry") SREGISTRY_CLIENT_SECRETS = getenv('SREGISTRY_CLIENT_SECRETS', _secrets) # If the client secrets exist, use the default as first priority _client = 'hub' if os.path.exists(SREGISTRY_CLIENT_SECRETS): secrets = read_json(SREGISTRY_CLIENT_SECRETS) _client = secrets.get('SREGISTRY_CLIENT', 'hub') SREGISTRY_CLIENT = getenv("SREGISTRY_CLIENT", _client) ######################### # Fun Settings ######################### # None defaults to robot. Path must exist, and ensure image < 2MB and min 220px SREGISTRY_THUMBNAIL = getenv('SREGISTRY_THUMBNAIL') ######################### # Multiprocessing ######################### SREGISTRY_WORKERS = int(getenv("SREGISTRY_PYTHON_THREADS", 9))