def completion_cache(self, cache_type, obj_class, mode): """ The completion cache store items that can be used for bash autocompletion, like UUIDs or human-friendly IDs. A resource listing will clear and repopulate the cache. A resource create will append to the cache. Delete is not handled because listings are assumed to be performed often enough to keep the cache reasonably up-to-date. """ # NOTE(wryan): This lock protects read and write access to the # completion caches with self.cache_lock: base_dir = cliutils.env('patronclient_UUID_CACHE_DIR', default="~/.patronclient") # NOTE(sirp): Keep separate UUID caches for each username + # endpoint pair username = cliutils.env('OS_USERNAME', 'PATRON_USERNAME') url = cliutils.env('OS_URL', 'PATRON_URL') uniqifier = hashlib.md5( username.encode('utf-8') + url.encode('utf-8')).hexdigest() cache_dir = os.path.expanduser(os.path.join(base_dir, uniqifier)) try: os.makedirs(cache_dir, 0o755) except OSError: # NOTE(kiall): This is typically either permission denied while # attempting to create the directory, or the # directory already exists. Either way, don't # fail. pass resource = obj_class.__name__.lower() filename = "%s-%s-cache" % (resource, cache_type.replace('_', '-')) path = os.path.join(cache_dir, filename) cache_attr = "_%s_cache" % cache_type try: setattr(self, cache_attr, open(path, mode)) except IOError: # NOTE(kiall): This is typically a permission denied while # attempting to write the cache file. pass try: yield finally: cache = getattr(self, cache_attr, None) if cache: cache.close() delattr(self, cache_attr)
def completion_cache(self, cache_type, obj_class, mode): """ The completion cache store items that can be used for bash autocompletion, like UUIDs or human-friendly IDs. A resource listing will clear and repopulate the cache. A resource create will append to the cache. Delete is not handled because listings are assumed to be performed often enough to keep the cache reasonably up-to-date. """ # NOTE(wryan): This lock protects read and write access to the # completion caches with self.cache_lock: base_dir = cliutils.env('patronclient_UUID_CACHE_DIR', default="~/.patronclient") # NOTE(sirp): Keep separate UUID caches for each username + # endpoint pair username = cliutils.env('OS_USERNAME', 'PATRON_USERNAME') url = cliutils.env('OS_URL', 'PATRON_URL') uniqifier = hashlib.md5(username.encode('utf-8') + url.encode('utf-8')).hexdigest() cache_dir = os.path.expanduser(os.path.join(base_dir, uniqifier)) try: os.makedirs(cache_dir, 0o755) except OSError: # NOTE(kiall): This is typically either permission denied while # attempting to create the directory, or the # directory already exists. Either way, don't # fail. pass resource = obj_class.__name__.lower() filename = "%s-%s-cache" % (resource, cache_type.replace('_', '-')) path = os.path.join(cache_dir, filename) cache_attr = "_%s_cache" % cache_type try: setattr(self, cache_attr, open(path, mode)) except IOError: # NOTE(kiall): This is typically a permission denied while # attempting to write the cache file. pass try: yield finally: cache = getattr(self, cache_attr, None) if cache: cache.close() delattr(self, cache_attr)
def _append_global_identity_args(self, parser): # Register the CLI arguments that have moved to the session object. ksession.Session.register_cli_options(parser) parser.set_defaults( insecure=cliutils.env('patronclient_INSECURE', default=False)) identity.Password.register_argparse_arguments(parser) parser.set_defaults( os_username=cliutils.env('OS_USERNAME', 'PATRON_USERNAME')) parser.set_defaults( os_password=cliutils.env('OS_PASSWORD', 'PATRON_PASSWORD')) parser.set_defaults( os_auth_url=cliutils.env('OS_AUTH_URL', 'PATRON_URL'))
def _append_global_identity_args(self, parser): # Register the CLI arguments that have moved to the session object. ksession.Session.register_cli_options(parser) parser.set_defaults(insecure=cliutils.env('patronclient_INSECURE', default=False)) identity.Password.register_argparse_arguments(parser) parser.set_defaults(os_username=cliutils.env('OS_USERNAME', 'PATRON_USERNAME')) parser.set_defaults(os_password=cliutils.env('OS_PASSWORD', 'PATRON_PASSWORD')) parser.set_defaults(os_auth_url=cliutils.env('OS_AUTH_URL', 'PATRON_URL'))
def password(self): # Cache password so we prompt user at most once if self._password: pass elif self._validate_string(self.args.os_password): self._password = self.args.os_password else: verify_pass = strutils.bool_from_string( cliutils.env("OS_VERIFY_PASSWORD", default=False), True) self._password = self._prompt_password(verify_pass) if not self._password: raise exc.CommandError('Expecting a password provided via either ' '--os-password, env[OS_PASSWORD], or ' 'prompted response') return self._password
def password(self): # Cache password so we prompt user at most once if self._password: pass elif self._validate_string(self.args.os_password): self._password = self.args.os_password else: verify_pass = strutils.bool_from_string( cliutils.env("OS_VERIFY_PASSWORD", default=False), True) self._password = self._prompt_password(verify_pass) if not self._password: raise exc.CommandError( 'Expecting a password provided via either ' '--os-password, env[OS_PASSWORD], or ' 'prompted response') return self._password
def get_base_parser(self): parser = patronclientArgumentParser( prog='patron', description=__doc__.strip(), epilog='See "patron help COMMAND" ' 'for help on a specific command.', add_help=False, formatter_class=OpenStackHelpFormatter, ) # Global arguments parser.add_argument( '-h', '--help', action='store_true', help=argparse.SUPPRESS, ) parser.add_argument('--version', action='version', version=patronclient.__version__) parser.add_argument('--debug', default=False, action='store_true', help=_("Print debugging output")) parser.add_argument( '--os-cache', default=strutils.bool_from_string( cliutils.env('OS_CACHE', default=False), True), action='store_true', help=_("Use the auth token cache. Defaults to False if " "env[OS_CACHE] is not set.")) parser.add_argument('--timings', default=False, action='store_true', help=_("Print call timing info")) parser.add_argument('--os-auth-token', default=cliutils.env('OS_AUTH_TOKEN'), help='Defaults to env[OS_AUTH_TOKEN]') parser.add_argument('--os_username', help=argparse.SUPPRESS) parser.add_argument('--os_password', help=argparse.SUPPRESS) parser.add_argument('--os-tenant-name', metavar='<auth-tenant-name>', default=cliutils.env('OS_TENANT_NAME', 'PATRON_PROJECT_ID'), help=_('Defaults to env[OS_TENANT_NAME].')) parser.add_argument('--os_tenant_name', help=argparse.SUPPRESS) parser.add_argument('--os-tenant-id', metavar='<auth-tenant-id>', default=cliutils.env('OS_TENANT_ID'), help=_('Defaults to env[OS_TENANT_ID].')) parser.add_argument('--os_auth_url', help=argparse.SUPPRESS) parser.add_argument('--os-region-name', metavar='<region-name>', default=cliutils.env('OS_REGION_NAME', 'PATRON_REGION_NAME'), help=_('Defaults to env[OS_REGION_NAME].')) parser.add_argument('--os_region_name', help=argparse.SUPPRESS) parser.add_argument('--os-auth-system', metavar='<auth-system>', default=cliutils.env('OS_AUTH_SYSTEM'), help='Defaults to env[OS_AUTH_SYSTEM].') parser.add_argument('--os_auth_system', help=argparse.SUPPRESS) parser.add_argument('--service-type', metavar='<service-type>', help=_('Defaults to compute for most actions')) parser.add_argument('--service_type', help=argparse.SUPPRESS) parser.add_argument('--service-name', metavar='<service-name>', default=cliutils.env('PATRON_SERVICE_NAME'), help=_('Defaults to env[PATRON_SERVICE_NAME]')) parser.add_argument('--service_name', help=argparse.SUPPRESS) parser.add_argument( '--volume-service-name', metavar='<volume-service-name>', default=cliutils.env('PATRON_VOLUME_SERVICE_NAME'), help=_('Defaults to env[PATRON_VOLUME_SERVICE_NAME]')) parser.add_argument('--volume_service_name', help=argparse.SUPPRESS) parser.add_argument('--os-endpoint-type', metavar='<endpoint-type>', dest='endpoint_type', default=cliutils.env( 'PATRON_ENDPOINT_TYPE', default=cliutils.env( 'OS_ENDPOINT_TYPE', default=DEFAULT_PATRON_ENDPOINT_TYPE)), help=_('Defaults to env[PATRON_ENDPOINT_TYPE], ' 'env[OS_ENDPOINT_TYPE] or ') + DEFAULT_PATRON_ENDPOINT_TYPE + '.') parser.add_argument('--endpoint-type', help=argparse.SUPPRESS) # NOTE(dtroyer): We can't add --endpoint_type here due to argparse # thinking usage-list --end is ambiguous; but it # works fine with only --endpoint-type present # Go figure. I'm leaving this here for doc purposes. # parser.add_argument('--endpoint_type', # help=argparse.SUPPRESS) parser.add_argument('--os-compute-api-version', metavar='<compute-api-ver>', default=cliutils.env( 'OS_COMPUTE_API_VERSION', default=DEFAULT_OS_COMPUTE_API_VERSION), help=_('Accepts 1.1 or 3, ' 'defaults to env[OS_COMPUTE_API_VERSION].')) parser.add_argument('--os_compute_api_version', help=argparse.SUPPRESS) parser.add_argument( '--bypass-url', metavar='<bypass-url>', dest='bypass_url', default=cliutils.env('patronclient_BYPASS_URL'), help="Use this API endpoint instead of the Service Catalog. " "Defaults to env[patronclient_BYPASS_URL]") parser.add_argument('--bypass_url', help=argparse.SUPPRESS) # The auth-system-plugins might require some extra options patronclient.auth_plugin.load_auth_system_opts(parser) self._append_global_identity_args(parser) return parser
def get_base_parser(self): parser = patronclientArgumentParser( prog='patron', description=__doc__.strip(), epilog='See "patron help COMMAND" ' 'for help on a specific command.', add_help=False, formatter_class=OpenStackHelpFormatter, ) # Global arguments parser.add_argument( '-h', '--help', action='store_true', help=argparse.SUPPRESS, ) parser.add_argument('--version', action='version', version=patronclient.__version__) parser.add_argument( '--debug', default=False, action='store_true', help=_("Print debugging output")) parser.add_argument( '--os-cache', default=strutils.bool_from_string( cliutils.env('OS_CACHE', default=False), True), action='store_true', help=_("Use the auth token cache. Defaults to False if " "env[OS_CACHE] is not set.")) parser.add_argument( '--timings', default=False, action='store_true', help=_("Print call timing info")) parser.add_argument( '--os-auth-token', default=cliutils.env('OS_AUTH_TOKEN'), help='Defaults to env[OS_AUTH_TOKEN]') parser.add_argument( '--os_username', help=argparse.SUPPRESS) parser.add_argument( '--os_password', help=argparse.SUPPRESS) parser.add_argument( '--os-tenant-name', metavar='<auth-tenant-name>', default=cliutils.env('OS_TENANT_NAME', 'PATRON_PROJECT_ID'), help=_('Defaults to env[OS_TENANT_NAME].')) parser.add_argument( '--os_tenant_name', help=argparse.SUPPRESS) parser.add_argument( '--os-tenant-id', metavar='<auth-tenant-id>', default=cliutils.env('OS_TENANT_ID'), help=_('Defaults to env[OS_TENANT_ID].')) parser.add_argument( '--os_auth_url', help=argparse.SUPPRESS) parser.add_argument( '--os-region-name', metavar='<region-name>', default=cliutils.env('OS_REGION_NAME', 'PATRON_REGION_NAME'), help=_('Defaults to env[OS_REGION_NAME].')) parser.add_argument( '--os_region_name', help=argparse.SUPPRESS) parser.add_argument( '--os-auth-system', metavar='<auth-system>', default=cliutils.env('OS_AUTH_SYSTEM'), help='Defaults to env[OS_AUTH_SYSTEM].') parser.add_argument( '--os_auth_system', help=argparse.SUPPRESS) parser.add_argument( '--service-type', metavar='<service-type>', help=_('Defaults to compute for most actions')) parser.add_argument( '--service_type', help=argparse.SUPPRESS) parser.add_argument( '--service-name', metavar='<service-name>', default=cliutils.env('PATRON_SERVICE_NAME'), help=_('Defaults to env[PATRON_SERVICE_NAME]')) parser.add_argument( '--service_name', help=argparse.SUPPRESS) parser.add_argument( '--volume-service-name', metavar='<volume-service-name>', default=cliutils.env('PATRON_VOLUME_SERVICE_NAME'), help=_('Defaults to env[PATRON_VOLUME_SERVICE_NAME]')) parser.add_argument( '--volume_service_name', help=argparse.SUPPRESS) parser.add_argument( '--os-endpoint-type', metavar='<endpoint-type>', dest='endpoint_type', default=cliutils.env( 'PATRON_ENDPOINT_TYPE', default=cliutils.env( 'OS_ENDPOINT_TYPE', default=DEFAULT_PATRON_ENDPOINT_TYPE)), help=_('Defaults to env[PATRON_ENDPOINT_TYPE], ' 'env[OS_ENDPOINT_TYPE] or ') + DEFAULT_PATRON_ENDPOINT_TYPE + '.') parser.add_argument( '--endpoint-type', help=argparse.SUPPRESS) # NOTE(dtroyer): We can't add --endpoint_type here due to argparse # thinking usage-list --end is ambiguous; but it # works fine with only --endpoint-type present # Go figure. I'm leaving this here for doc purposes. # parser.add_argument('--endpoint_type', # help=argparse.SUPPRESS) parser.add_argument( '--os-compute-api-version', metavar='<compute-api-ver>', default=cliutils.env('OS_COMPUTE_API_VERSION', default=DEFAULT_OS_COMPUTE_API_VERSION), help=_('Accepts 1.1 or 3, ' 'defaults to env[OS_COMPUTE_API_VERSION].')) parser.add_argument( '--os_compute_api_version', help=argparse.SUPPRESS) parser.add_argument( '--bypass-url', metavar='<bypass-url>', dest='bypass_url', default=cliutils.env('patronclient_BYPASS_URL'), help="Use this API endpoint instead of the Service Catalog. " "Defaults to env[patronclient_BYPASS_URL]") parser.add_argument('--bypass_url', help=argparse.SUPPRESS) # The auth-system-plugins might require some extra options patronclient.auth_plugin.load_auth_system_opts(parser) self._append_global_identity_args(parser) return parser