Ejemplo n.º 1
0
def add(backend, variable, value, force=False):
    """add the variable to the config
    """
    print("[add]")
    settings = read_client_secrets()

    # If the variable begins with the SREGISTRY_<CLIENT> don't add it
    prefix = "SREGISTRY_%s_" % backend.upper()
    if not variable.startswith(prefix):
        variable = "%s%s" % (prefix, variable)

    # All must be uppercase
    variable = variable.upper()
    bot.info("%s %s" % (variable, value))

    # Does the setting already exist?

    if backend in settings:
        if variable in settings[backend] and force is False:
            previous = settings[backend][variable]
            bot.exit(
                "%s is already set as %s. Use --force to override."
                % (variable, previous)
            )

    if backend not in settings:
        settings[backend] = {}

    settings[backend][variable] = value
    update_secrets(settings)
Ejemplo n.º 2
0
    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. This is where you
           should do any customization of the secrets flie, or using
           it to update your client, if needed.
        """
        # Get a setting for client myclient and some variable name VAR.
        # returns None if not set
        # setting = self._get_setting('SREGISTRY_MYCLIENT_VAR')

        # Get (and if found in environment (1) settings (2) update the variable
        # It will still return None if not set
        # setting = self._get_and_update_setting('SREGISTRY_MYCLIENT_VAR')

        # If you have a setting that is required and not found, you should exit.

        # Here is how to read all client secrets
        self.secrets = read_client_secrets()

        # If you don't want to use the shared settings file, you have your own.
        # Here is how to get if the user has a cache for you enabled, this
        # returns a path (enabled) or None (disabled) that you should honor
        # You can use this as a file path or folder and for both cases, you
        # need to create the file or folder
        if self._credential_cache is not None:
            bot.info("credential cache set to %s" % self._credential_cache)
Ejemplo n.º 3
0
def activate(backend):
    """activate a backend by adding it to the .sregistry configuration file.
    """
    settings = read_client_secrets()
    if backend is not None:
        settings["SREGISTRY_CLIENT"] = backend
        update_secrets(settings)
        print("[activate] %s" % backend)
Ejemplo n.º 4
0
def activate(backend):
    '''activate a backend by adding it to the .sregistry configuration file.
    '''
    settings = read_client_secrets()
    if backend is not None:
        settings['SREGISTRY_CLIENT'] = backend
        update_secrets(settings)
        print('[activate] %s' % backend)
Ejemplo n.º 5
0
def status(backend):
    """print the status for all or one of the backends.
    """
    print("[backend status]")
    settings = read_client_secrets()
    print("There are %s clients found in secrets." % len(settings))
    if "SREGISTRY_CLIENT" in settings:
        print("active: %s" % settings["SREGISTRY_CLIENT"])
        update_secrets(settings)
    else:
        print("There is no active client.")
Ejemplo n.º 6
0
def deactivate():
    """deactivate any active backend by removing it from the .sregistry
       configuration file
    """
    settings = read_client_secrets()
    if "SREGISTRY_CLIENT" in settings:
        del settings["SREGISTRY_CLIENT"]
        update_secrets(settings)
        print("[deactivated]")
    else:
        print("There is no active client")
Ejemplo n.º 7
0
def status(backend):
    '''print the status for all or one of the backends.
    '''
    print('[backend status]')
    settings = read_client_secrets()
    print('There are %s clients found in secrets.' % len(settings))
    if 'SREGISTRY_CLIENT' in settings:
        print('active: %s' % settings['SREGISTRY_CLIENT'])
        update_secrets(settings)
    else:
        print('There is no active client.')
Ejemplo n.º 8
0
def deactivate():
    '''deactivate any active backend by removing it from the .sregistry
       configuration file
    '''
    settings = read_client_secrets()
    if "SREGISTRY_CLIENT" in settings:
        del settings['SREGISTRY_CLIENT']
        update_secrets(settings)
        print('[deactivated]')
    else:
        print('There is no active client')
Ejemplo n.º 9
0
 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.
     '''
     self.secrets = read_client_secrets()
     if self.secrets is not None:
         if "registry" in self.secrets:
             if "base" in self.secrets['registry']:
                 self.base = self.secrets['registry']['base']
                 self._update_base()
Ejemplo n.º 10
0
def get_settings(self, client_name=None):
    '''get all settings, either for a particular client if a name is provided,
       or across clients.

       Parameters
       ==========
       client_name: the client name to return settings for (optional)

    '''
    settings = read_client_secrets()
    if client_name is not None and client_name in settings:
        return settings[client_name]
    return settings
Ejemplo n.º 11
0
def delete_backend(backend):
    """delete a backend, and update the secrets file
    """
    settings = read_client_secrets()
    if backend in settings:
        del settings[backend]

        # If the backend was the active client, remove too
        if "SREGISTRY_CLIENT" in settings:
            if settings["SREGISTRY_CLIENT"] == backend:
                del settings["SREGISTRY_CLIENT"]

        update_secrets(settings)
        print("[delete] %s" % backend)
    else:
        if backend is not None:
            print("%s is not a known client." % backend)
        else:
            print("Please specify a backend to delete.")
Ejemplo n.º 12
0
def delete_backend(backend):
    '''delete a backend, and update the secrets file
    '''
    settings = read_client_secrets()
    if backend in settings:
        del settings[backend]

        # If the backend was the active client, remove too
        if 'SREGISTRY_CLIENT' in settings:
            if settings['SREGISTRY_CLIENT'] == backend:
                del settings['SREGISTRY_CLIENT']

        update_secrets(settings)
        print('[delete] %s' % backend)
    else:
        if backend is not None:
            print('%s is not a known client.' % backend)
        else:
            print('Please specify a backend to delete.')
Ejemplo n.º 13
0
def list_backends(backend=None):
    """return a list of backends installed for the user, which is based on
       the config file keys found present
 
       Parameters
       ==========
       backend: a specific backend to list. If defined, just list parameters.

    """
    settings = read_client_secrets()

    # Backend names are the keys
    backends = list(settings.keys())
    backends = [b for b in backends if b != "SREGISTRY_CLIENT"]

    if backend in backends:
        bot.info(backend)
        print(json.dumps(settings[backend], indent=4, sort_keys=True))
    else:
        if backend is not None:
            print("%s is not a known client." % backend)
        bot.info("Backends Installed")
        print("\n".join(backends))
Ejemplo n.º 14
0
def remove(backend, variable):
    """remove a variable from the config, if found.
    """
    print("[remove]")
    settings = read_client_secrets()

    # If the variable begins with the SREGISTRY_<CLIENT> don't add it
    prefixed = variable
    prefix = "SREGISTRY_%s_" % backend.upper()
    if not variable.startswith(prefix):
        prefixed = "%s%s" % (prefix, variable)

    # All must be uppercase
    variable = variable.upper()
    bot.info(variable)

    # Does the setting already exist?
    if backend in settings:
        if variable in settings[backend]:
            del settings[backend][variable]
        if prefixed in settings[backend]:
            del settings[backend][prefixed]
        update_secrets(settings)
Ejemplo n.º 15
0
def get_setting(self, name, default=None):
    '''return a setting from the environment (first priority) and then
       secrets (second priority) if one can be found. If not, return None.

       Parameters
       ==========
       name: they key (index) of the setting to look up
       default: (optional) if not found, return default instead.
    '''

    # First priority is the environment
    setting = os.environ.get(name)

    # Second priority is the secrets file
    if setting is None:
        secrets = read_client_secrets()
        if self.client_name in secrets:
            secrets = secrets[self.client_name]
            if name in secrets:
                setting = secrets[name]

    if setting is None and default is not None:
        setting = default
    return setting