def main(argv): del argv # Unused. utils.clear_screen() utils.write('Welcome to the Grab n Go management script!\n') try: manager = _Manager.new(FLAGS.config_file_path, FLAGS.project) exit_code = manager.run() except KeyboardInterrupt as err: logging.error('Manager received CTRL-C, exiting: %s', err) exit_code = 1 sys.exit(exit_code)
def main(argv): del argv # Unused. utils.clear_screen() utils.write('Welcome to the Grab n Go management script!\n') try: _Manager.new( FLAGS.config_file_path, FLAGS.prefer_gcs, project_key=FLAGS.project, version=FLAGS.app_version, ).run() except KeyboardInterrupt as err: logging.error('Manager received CTRL-C, exiting: %s', err) exit_code = 1 else: exit_code = 0 sys.exit(exit_code)
def run(self): """Runs the Grab n Go manager. Returns: An integer representing the exit code. Zero is success and any other number is a failure. """ exit_code = 0 try: while True: utils.clear_screen() utils.write( 'Which of the following actions would you like to take?\n') for opt, desc in six.iteritems(_OPTIONS): utils.write('Action: {!r}\nDescription: {!r}'.format( opt, desc)) action = utils.prompt_enum( '', accepted_values=_OPTIONS.keys(), case_sensitive=False).strip().lower() if action == _QUIT: break elif action == _CHANGE_PROJECT: self = self._change_project() elif action == _CONFIGURE: self._configure() finally: utils.write( 'Done managing Grab n Go for Cloud Project {!r}.'.format( self._config.project)) return exit_code
def _configure(self): """Prompts the user for project wide constants. Returns: An updated instance of _Manager. """ opts = sorted(self._constants.keys()) opts.append(_QUIT) try: while True: utils.clear_screen() utils.write( 'Here are the project wide constants for {!r}:\n'.format( self._config.project)) configured, unconfigured = [], [] for name in sorted(self._constants.keys()): if self._constants[name].valid: configured.append(name) else: unconfigured.append(name) utils.write( 'Constant: {!r}\nDescription: {}\nCurrent Value: {}\n'. format(name, self._constants[name].message, self._constants[name].value)) choice = utils.prompt_enum( 'Which constant would you like to configure?\n' 'Currently configured constants include: {}\n' 'Currently unconfigured constants include: {}\n'.format( ', '.join(configured) or 'None', ', '.join(unconfigured) or 'None'), accepted_values=opts, case_sensitive=False).strip().lower() if choice == _QUIT: break else: self._constants[choice].prompt() self._save_constants() finally: utils.write( 'Exiting configuration menu, to return enter {!r} in the main menu.\n' 'Returning to the main menu.'.format(_CONFIGURE)) return self
def run(self): """Runs the Grab n Go manager.""" try: while True: utils.clear_screen() utils.write( 'Which of the following actions would you like to take?\n') for opt in self._options.values(): utils.write('Action: {!r}\nDescription: {}\n'.format( opt.name, opt.description)) action = utils.prompt_enum( '', accepted_values=list(self._options.keys()), case_sensitive=False).strip().lower() callback = self._options[action].callback if callback is None: break self = callback() finally: utils.write( 'Done managing Grab n Go for Cloud Project {!r}.'.format( self._config.project))
def test_write(self, wrap, text, expected_output): with mock.patch.object(flags, 'get_help_width', return_value=wrap): utils.write(text) self.assertEqual(sys.stdout.getvalue(), expected_output)
def new(cls, config_file_path, project_key=None): """Creates a new instance of a Grab n Go Manager. Args: config_file_path: str, the name or path to the config file. project_key: Optional[str], the project friendly name, used as the top-level key in the config file. Returns: A new instance of a Grab n Go Manager. Raises: _AuthError: when unable to generate valid credentials. """ if project_key is None: project_key = utils.prompt_string( 'A project name was not provided, which project would you like to ' 'configure?\nNOTE: If you are unsure, or if you plan to use only one ' 'Google Cloud Project, you can use the default.\n' 'The following projects are currently defined in {!r}:\n {}'. format( config_file_path, ', '.join(common.get_available_configs(config_file_path))), default=common.DEFAULT).lower() logging.debug( 'Project key %r was provided, attempting to load from yaml.', project_key) try: config = common.ProjectConfig.from_yaml(project_key, config_file_path) except (common.ConfigError, KeyError) as err: logging.debug( 'Failed to initialize project with key %r: %s\n' 'Attempting to load new config...', project_key, err) utils.write( 'There does not appear to be a saved configuration for this project: ' '{!r}. Before we can get started, we need to gather some information.' '\n'.format(project_key)) config = common.ProjectConfig.from_prompts(project_key, config_file_path) config.write() try: cloud_creds = auth.CloudCredentials(config, _get_oauth_scopes()) except auth.InvalidCredentials as err: utils.write( 'We were unable to create credentials for this configuration, please ' 'verify the Project ID, OAuth2 Client ID and Client Secret are ' 'correct and then re-enter them in the following prompts.') config = common.ProjectConfig.from_prompts(project_key, config_file_path) config.write() return cls.new(config_file_path, project_key) gae_admin_api = app_engine.AdminAPI.from_config(config, cloud_creds) datastore_api = datastore.DatastoreAPI.from_config(config, cloud_creds) directory_api = directory.DirectoryAPI.from_config(config, cloud_creds) storage_api = storage.CloudStorageAPI.from_config(config, cloud_creds) constants = app_constants.get_constants_from_flags() return cls(config, constants, cloud_creds, gae_admin_api, datastore_api, directory_api, storage_api)