in gslib/commands/config.py [0:0]
def RunCommand(self):
"""Command entry point for the config command."""
cred_type = CredTypes.OAUTH2_USER_ACCOUNT
output_file_name = None
has_a = False
has_e = False
configure_auth = True
for opt, opt_arg in self.sub_opts:
if opt == '-a':
cred_type = CredTypes.HMAC
has_a = True
elif opt == '-e':
cred_type = CredTypes.OAUTH2_SERVICE_ACCOUNT
has_e = True
elif opt == '-n':
configure_auth = False
elif opt == '-o':
output_file_name = opt_arg
else:
self.RaiseInvalidArgumentException()
if has_e and has_a:
raise CommandException('Both -a and -e cannot be specified. Please see '
'"gsutil help config" for more information.')
if not configure_auth and (has_a or has_e):
raise CommandException(
'The -a and -e flags cannot be specified with the -n flag. Please '
'see "gsutil help config" for more information.')
# Don't allow users to configure Oauth2 (any option other than -a and -n)
# when running in the Cloud SDK, unless they have the Cloud SDK configured
# not to pass credentials to gsutil.
if (system_util.InvokedViaCloudSdk() and
system_util.CloudSdkCredPassingEnabled() and not has_a and
configure_auth):
raise CommandException('\n'.join([
'OAuth2 is the preferred authentication mechanism with the Cloud '
'SDK.',
'Run "gcloud auth login" to configure authentication, unless:',
'\n'.join(
textwrap.wrap(
'You don\'t want gsutil to use OAuth2 credentials from the Cloud '
'SDK, but instead want to manage credentials with .boto files '
'generated by running "gsutil config"; in which case run '
'"gcloud config set pass_credentials_to_gsutil false".',
initial_indent='- ',
subsequent_indent=' ')),
'\n'.join(
textwrap.wrap(
'You want to authenticate with an HMAC access key and secret, in '
'which case run "gsutil config -a".',
initial_indent='- ',
subsequent_indent=' ')),
]))
if system_util.InvokedViaCloudSdk() and has_a:
sys.stderr.write('\n'.join(
textwrap.wrap(
'This command will configure HMAC credentials, but gsutil will use '
'OAuth2 credentials from the Cloud SDK by default. To make sure '
'the HMAC credentials are used, run: "gcloud config set '
'pass_credentials_to_gsutil false".')) + '\n\n')
default_config_path_bak = None
if not output_file_name:
# Check to see if a default config file name is requested via
# environment variable. If so, use it, otherwise use the hard-coded
# default file. Then use the default config file name, if it doesn't
# exist or can be moved out of the way without clobbering an existing
# backup file.
boto_config_from_env = os.environ.get('BOTO_CONFIG', None)
if boto_config_from_env:
default_config_path = boto_config_from_env
else:
default_config_path = os.path.expanduser(os.path.join('~', '.boto'))
if not os.path.exists(default_config_path):
output_file_name = default_config_path
else:
default_config_path_bak = default_config_path + '.bak'
if os.path.exists(default_config_path_bak):
raise CommandException('Cannot back up existing config '
'file "%s": backup file exists ("%s").' %
(default_config_path, default_config_path_bak))
else:
try:
sys.stderr.write(
'Backing up existing config file "%s" to "%s"...\n' %
(default_config_path, default_config_path_bak))
os.rename(default_config_path, default_config_path_bak)
except Exception as e:
raise CommandException(
'Failed to back up existing config '
'file ("%s" -> "%s"): %s.' %
(default_config_path, default_config_path_bak, e))
output_file_name = default_config_path
if output_file_name == '-':
output_file = sys.stdout
else:
output_file = self._OpenConfigFile(output_file_name)
sys.stderr.write('\n'.join(
textwrap.wrap(
'This command will create a boto config file at %s containing your '
'credentials, based on your responses to the following questions.'
% output_file_name)) + '\n')
# Catch ^C so we can restore the backup.
RegisterSignalHandler(signal.SIGINT, _CleanupHandler)
try:
self._WriteBotoConfigFile(output_file,
cred_type=cred_type,
configure_auth=configure_auth)
except Exception as e:
user_aborted = isinstance(e, AbortException)
if user_aborted:
sys.stderr.write('\nCaught ^C; cleaning up\n')
# If an error occurred during config file creation, remove the invalid
# config file and restore the backup file.
if output_file_name != '-':
output_file.close()
os.unlink(output_file_name)
try:
if default_config_path_bak:
sys.stderr.write('Restoring previous backed up file (%s)\n' %
default_config_path_bak)
os.rename(default_config_path_bak, output_file_name)
except Exception as e:
# Raise the original exception so that we can see what actually went
# wrong, rather than just finding out that we died before assigning
# a value to default_config_path_bak.
raise e
raise
if output_file_name != '-':
output_file.close()
if not boto.config.has_option('Boto', 'proxy'):
sys.stderr.write('\n' + '\n'.join(
textwrap.wrap(
'Boto config file "%s" created.\nIf you need to use a proxy to '
'access the Internet please see the instructions in that file.'
% output_file_name)) + '\n')
return 0