Esempio n. 1
0
  def Run(self, cli, args):
    """Run this command with the given arguments.

    Args:
      cli: The cli.CLI object for this command line tool.
      args: The arguments for this command as a namespace.

    Returns:
      The object returned by the module's Run() function.

    Raises:
      exceptions.Error: if thrown by the Run() function.
      exceptions.ExitCodeNoError: if the command is returning with a non-zero
        exit code.
    """
    metrics.Loaded()

    tool_context = {}
    if self._parent_group:
      self._parent_group.RunGroupFilter(tool_context, args)

    command_instance = self._common_type(cli=cli, context=tool_context)

    base.LogCommand(self.dotted_name, args)
    resources = command_instance.Run(args)
    resources = display.Displayer(command_instance, args, resources,
                                  display_info=self.ai.display_info).Display()
    metrics.Ran()

    if command_instance.exit_code != 0:
      raise exceptions.ExitCodeNoError(exit_code=command_instance.exit_code)

    return resources
  def Run(self, args):
    ve_dir = config.Paths().virtualenv_dir
    if not util.VirtualEnvExists(ve_dir):
      log.error('Virtual env does not exist at {}.'.format(ve_dir))
      raise exceptions.ExitCodeNoError(exit_code=1)

    log.status.Print('Updating modules...')
    update_modules = [
        '{}/bin/pip3'.format(ve_dir), 'install', '--log',
        '{}/update_module.log'.format(ve_dir), '-q',
        '--disable-pip-version-check'
    ]
    update_modules.extend(util.MODULES)
    ec = execution_utils.Exec(update_modules, no_exit=True)
    if ec != 0:
      log.error('Failed to update modules.')
      raise exceptions.ExitCodeNoError(exit_code=1)
    log.status.Print('Modules updated.')
Esempio n. 3
0
 def Run(self, args):
   ve_dir = config.Paths().virtualenv_dir
   if util.VirtualEnvExists(ve_dir):
     if not util.EnableFileExists(ve_dir):
       util.CreateEnableFile(ve_dir)
     log.status.Print('Virtual env enabled.')
   else:
     log.error('Virtual env does not exist at {}.'.format(ve_dir))
     raise exceptions.ExitCodeNoError(exit_code=1)
Esempio n. 4
0
 def Run(self, args):
     ve_dir = config.Paths().virtualenv_dir
     if not util.VirtualEnvExists(ve_dir):
         log.status.Print(
             'Virtual env does not exist at {}.'.format(ve_dir))
         raise exceptions.ExitCodeNoError(exit_code=1)
     console_io.PromptContinue(
         message='Delete virtual env setup at {}'.format(ve_dir),
         cancel_on_no=True)
     files.RmTree(ve_dir)
Esempio n. 5
0
  def Run(self, cli, args):
    """Run this command with the given arguments.

    Args:
      cli: The cli.CLI object for this command line tool.
      args: The arguments for this command as a namespace.

    Returns:
      The object returned by the module's Run() function.

    Raises:
      exceptions.Error: if thrown by the Run() function.
      exceptions.ExitCodeNoError: if the command is returning with a non-zero
        exit code.
    """
    def Http(**kwargs):
      # Possibly override timeout, making sure to leave kwargs[timeout]
      # undefined (as opposed to None) if args.http_timout is not set.
      if args.http_timeout:
        kwargs['timeout'] = args.http_timeout

      return core_cli.Http(cmd_path=self.dotted_name,
                           trace_token=args.trace_token,
                           log_http=properties.VALUES.core.log_http.GetBool(),
                           **kwargs)

    tool_context = self._config_hooks.load_context()
    last_group = None
    for context_filter in self._config_hooks.context_filters:
      last_group = context_filter(tool_context, Http, args)

    command_instance = self._common_type(
        cli=cli,
        context=tool_context,
        group=last_group,
        http_func=Http,
        format_string=args.format or 'yaml')

    if args.format:
      output_formatter = command_instance.format
    else:
      output_formatter = lambda obj: command_instance.Display(args, obj)

    log.debug('Running %s with %s.', self.dotted_name, args)
    metrics.Loaded()
    result = command_instance.Run(args)
    if log.IsUserOutputEnabled():
      output_formatter(result)
    metrics.Ran()

    if command_instance.exit_code != 0:
      raise exceptions.ExitCodeNoError(exit_code=command_instance.exit_code)

    return result
    def Run(self, args):
        if util.IsPy2() and not args.IsSpecified('python_to_use'):
            log.error('Virtual env support requires Python 3.')
            raise exceptions.ExitCodeNoError(exit_code=3)
        if util.IsWindows():
            log.error('Virtual env support not enabled on Windows.')
            raise exceptions.ExitCodeNoError(exit_code=4)
        if args.IsSpecified('python_to_use'):
            python = args.python_to_use
        else:
            try:
                python = execution_utils.GetPythonExecutable()
            except ValueError:
                log.error('Failed to resolve python to use for virtual env.')
                raise exceptions.ExitCodeNoError(exit_code=5)

        ve_dir = config.Paths().virtualenv_dir
        if util.VirtualEnvExists(ve_dir):
            log.error('Virtual env setup {} already exists.'.format(ve_dir))
            raise exceptions.ExitCodeNoError(exit_code=5)

        succeeded_making_venv = False
        try:
            log.status.Print('Creating virtualenv...')
            # python -m venv is preferred as it aligns the python used with
            # the current in used Python.
            ec = execution_utils.Exec([python, '-m', 'venv', ve_dir],
                                      no_exit=True,
                                      err_func=log.file_only_logger.debug,
                                      out_func=log.file_only_logger.debug)
            if ec != 0:
                # Many linux vendors havea a history of having a broken python-venv
                # package that will not work correctly, debian for example. If -m venv
                # failed above we will attempt to use the virtualenv tool if it is
                # installed and exists in $PATH.
                ec = execution_utils.Exec(
                    ['virtualenv', '-q', '-p', python, ve_dir], no_exit=True)
                if ec != 0:
                    log.error('Virtual env setup failed.')
                    raise exceptions.ExitCodeNoError(exit_code=ec)
            log.status.Print('Installing modules...')
            install_modules = [
                '{}/bin/pip3'.format(ve_dir), 'install', '--log',
                '{}/install_module.log'.format(ve_dir), '-q',
                '--disable-pip-version-check'
            ]
            install_modules.extend(util.MODULES)
            ec = execution_utils.Exec(install_modules, no_exit=True)
            if ec == 0:
                # prevent the cleanup that occurs in finally block
                succeeded_making_venv = True
            else:
                log.error('Virtual env setup failed.')
                raise exceptions.ExitCodeNoError(exit_code=ec)
        finally:
            # If something went wrong we clean up any partial created ve_dir
            if not succeeded_making_venv:
                if util.VirtualEnvExists(ve_dir):
                    files.RmTree(ve_dir)
    def Run(self, args):
        """Executes the given docker command, after refreshing our credentials.

    Args:
      args: An argparse.Namespace that contains the values for
         the arguments specified in the .Args() method.

    Raises:
      exceptions.ExitCodeNoError: The docker command execution failed.
    """
        if args.account:
            # Since the docker binary invokes `gcloud auth docker-helper` through
            # `docker-credential-gcloud`, it cannot forward the command line
            # arguments. Subsequently, we are unable to set the account (or any
            # flag for that matter) used by `docker-credential-gcloud` with
            # the global `--account` flag.
            log.warning('Docker uses the account from the gcloud config.'
                        'To set the account in the gcloud config, run '
                        '`gcloud config set account <account_name>`.')

        with base.WithLegacyQuota():
            force_refresh = True
            for server in args.server:
                if server not in _DEFAULT_REGISTRIES:
                    log.warning(
                        'Authenticating to a non-default server: {server}.'.
                        format(server=server))
                docker.UpdateDockerCredentials(server, refresh=force_refresh)
                # Only force a refresh for the first server we authorize
                force_refresh = False

            if args.authorize_only:
                # NOTE: We don't know at this point how long the access token we have
                # placed in the docker configuration will last.  More information needs
                # to be exposed from all credential kinds in order for us to have an
                # accurate awareness of lifetime here.
                log.err.Print(
                    'Short-lived access for {server} configured.'.format(
                        server=args.server))
                return

            docker_args = args.docker_args or []
            docker_args = (docker_args if not args.docker_host else
                           ['-H', args.docker_host] + docker_args)

            result = docker_client_utils.Execute(docker_args)
            # Explicitly avoid displaying an error message that might
            # distract from the docker error message already displayed.
            if result:
                raise exceptions.ExitCodeNoError(exit_code=result)
            return
Esempio n. 8
0
    def Run(self, cli, args):
        """Run this command with the given arguments.

    Args:
      cli: The cli.CLI object for this command line tool.
      args: The arguments for this command as a namespace.

    Returns:
      The object returned by the module's Run() function.

    Raises:
      exceptions.Error: if thrown by the Run() function.
    """
        def Http(**kwargs):
            return core_cli.Http(cmd_path=self.dotted_name,
                                 trace_token=args.trace_token,
                                 log_http=args.log_http,
                                 timeout=args.http_timeout,
                                 **kwargs)

        tool_context = self._config_hooks.load_context()
        last_group = None
        for context_filter in self._config_hooks.context_filters:
            last_group = context_filter(tool_context, Http, args)

        command_instance = self._common_type(cli=cli,
                                             context=tool_context,
                                             group=last_group,
                                             http_func=Http,
                                             format_string=args.format
                                             or 'yaml')

        if args.format:
            output_formatter = command_instance.format
        else:
            output_formatter = lambda obj: command_instance.Display(args, obj)

        log.debug('Running %s with %s.', self.dotted_name, args)
        metrics.Loaded()
        result = command_instance.Run(args)
        if log.IsUserOutputEnabled():
            output_formatter(result)
        metrics.Ran()

        if command_instance.exit_code != 0:
            raise exceptions.ExitCodeNoError(
                exit_code=command_instance.exit_code)

        return result
Esempio n. 9
0
    def Run(self, args):
        ve_dir = config.Paths().virtualenv_dir
        if not util.VirtualEnvExists(ve_dir):
            log.error('Virtual env does not exist at {}.'.format(ve_dir))
            raise exceptions.ExitCodeNoError(exit_code=1)

        # The Python version being used.
        python_version = 'NOT AVAILABLE'

        def _ver(output):
            self._version_output = output

        ec = execution_utils.Exec(
            ['{}/bin/python3'.format(ve_dir), '--version'],
            no_exit=True,
            out_func=_ver)
        if ec == 0:
            version_parts = self._version_output.split(' ')
            if len(version_parts) == 2:
                python_version = version_parts[1]

        # The modules installed in the environment.
        modules = []

        def _mod_output(output):
            self._modules_stdout = output

        execution_utils.Exec(['{}/bin/pip3'.format(ve_dir), 'freeze'],
                             no_exit=True,
                             out_func=_mod_output)
        for l in self._modules_stdout.split('\n'):
            if '==' in l:
                mn, mv = l.split('==')
                modules.append(Module(mn, mv))

        # The enable|disable state of the virtual env environment.
        ve_enabled = False
        if util.EnableFileExists(ve_dir):
            ve_enabled = True

        return VirtualEnvInfo(python_version, modules, ve_enabled)
Esempio n. 10
0
    def Run(self, cli, args):
        """Run this command with the given arguments.

    Args:
      cli: The cli.CLI object for this command line tool.
      args: The arguments for this command as a namespace.

    Returns:
      The object returned by the module's Run() function.

    Raises:
      exceptions.Error: if thrown by the Run() function.
      exceptions.ExitCodeNoError: if the command is returning with a non-zero
        exit code.
    """
        metrics.Loaded()

        tool_context = {}
        if self._parent_group:
            self._parent_group.RunGroupFilter(tool_context, args)

        command_instance = self._common_type(cli=cli, context=tool_context)

        log.debug(u'Running [{cmd}] with arguments: [{args}]'.format(
            cmd=self.dotted_name,
            args=u', '.join(u'{arg}: "{value}"'.format(arg=arg, value=value)
                            for arg, value in sorted(
                                args.GetSpecifiedArgs().iteritems()))))
        resources = command_instance.Run(args)
        resources = display.Displayer(
            command_instance,
            args,
            resources,
            display_info=self.ai.display_info).Display()
        metrics.Ran()

        if command_instance.exit_code != 0:
            raise exceptions.ExitCodeNoError(
                exit_code=command_instance.exit_code)

        return resources
Esempio n. 11
0
    def Run(self, args):
        """Executes the given docker command, after refreshing our credentials.

    Args:
      args: An argparse.Namespace that contains the values for
         the arguments specified in the .Args() method.

    Raises:
      exceptions.ExitCodeNoError: The docker command execution failed.
    """
        force_refresh = True
        for server in args.server:
            if server not in _DEFAULT_REGISTRIES:
                log.warn(
                    'Authenticating to a non-default server: {server}.'.format(
                        server=server))
            docker.UpdateDockerCredentials(server, refresh=force_refresh)
            # Only force a refresh for the first server we authorize
            force_refresh = False

        if args.authorize_only:
            # NOTE: We don't know at this point how long the access token we have
            # placed in the docker configuration will last.  More information needs
            # to be exposed from all credential kinds in order for us to have an
            # accurate awareness of lifetime here.
            log.err.Print('Short-lived access for {server} configured.'.format(
                server=args.server))
            return

        # TODO(user): reconcile with the 'gcloud app' docker stuff,
        # which should be using a gcloud config property.
        docker_args = args.docker_args or []
        docker_args = (docker_args if not args.docker_host else
                       ['-H', args.docker_host] + docker_args)

        result = docker.Execute(docker_args)
        # Explicitly avoid displaying an error message that might
        # distract from the docker error message already displayed.
        if result:
            raise exceptions.ExitCodeNoError(exit_code=result)
        return
Esempio n. 12
0
 def _Handler(self, unused_signal, unused_frame):
     log.status.write('\n\nCancelling test [{id}]...\n\n'.format(
         id=self._matrix_monitor.matrix_id))
     self._matrix_monitor.CancelTestMatrix()
     log.status.write('\nTest matrix has been cancelled.\n')
     raise exceptions.ExitCodeNoError(exit_code=exit_code.MATRIX_CANCELLED)
Esempio n. 13
0
 def testConvertNoPrint(self):
     err = exceptions.ExitCodeNoError('Error')
     new_err, print_exc = exceptions.ConvertKnownError(err)
     self.assertIsInstance(new_err, exceptions.ExitCodeNoError)
     self.assertFalse(print_exc)
Esempio n. 14
0
 def _Handler(self, unused_signal, unused_frame):
   log.status.write('\n\nCancelling test [{id}]...\n\n'
                    .format(id=self._matrix_id))
   self._testing_api_helper.CancelTestMatrix(self._matrix_id)
   raise exceptions.ExitCodeNoError(exit_code=exit_code.MATRIX_CANCELLED)