Пример #1
0
    def Run(self, args):
        running_state = (api_util.GetMessagesModule(
            release_track=self.ReleaseTrack()).Environment.
                         StateValueValuesEnum.RUNNING)

        env_ref = args.CONCEPTS.environment.Parse()
        env_obj = environments_api_util.Get(env_ref,
                                            release_track=self.ReleaseTrack())

        if env_obj.state != running_state:
            raise command_util.Error(
                'Cannot execute subcommand for environment in state {}. '
                'Must be RUNNING.'.format(env_obj.state))

        cluster_id = env_obj.config.gkeCluster
        cluster_location_id = command_util.ExtractGkeClusterLocationId(env_obj)

        with command_util.TemporaryKubeconfig(cluster_location_id, cluster_id):
            pod = command_util.GetGkePod(pod_substr=WORKER_POD_SUBSTR)

            self.BypassConfirmationPrompt(args)
            kubectl_args = [
                'exec', pod, '-tic', WORKER_CONTAINER, 'airflow',
                args.subcommand
            ]
            if args.cmd_args:
                # Add '--' to the argument list so kubectl won't eat the command args.
                kubectl_args.extend(['--'] + args.cmd_args)
            command_util.RunKubectlCommand(kubectl_args,
                                           out_func=log.status.Print)
Пример #2
0
  def _ExtractGkeClusterLocationIdHelper(
      self, env_config_location, list_names_locations, expected_location):
    """Verifies zone extraction for an environment config zone and cluster list.

    This helper constructs a fake Environment whose GKE cluster ID is
    self.TEST_GKE_CLUSTER.

    Args:
      env_config_location: str, the compute location to place in the
          environment's node config
      list_names_locations: (str, str), a list of 2-tuples of cluster IDs and
          locations to return in the GKE ListClusters stub.
      expected_location: str, the expected compute location to be extracted
    """
    env_obj = self.MakeEnvironment(
        self.TEST_PROJECT,
        self.TEST_LOCATION,
        self.TEST_ENVIRONMENT_ID,
        config=self.messages.EnvironmentConfig(
            nodeConfig=self.messages.NodeConfig(location=env_config_location),
            gkeCluster=self.TEST_GKE_CLUSTER))

    if not env_config_location:
      properties.VALUES.core.project.Set(self.TEST_PROJECT)
      self.mock_gke_client.projects_locations_clusters.List.Expect(
          self.gke_messages.ContainerProjectsLocationsClustersListRequest(
              parent=gke_api_adapter.ProjectLocation(self.TEST_PROJECT, '-')),
          self.gke_messages.ListClustersResponse(clusters=[
              self.gke_messages.Cluster(name=list_name, location=list_location)
              for list_name, list_location in list_names_locations
          ]))
    self.assertEqual(expected_location,
                     command_util.ExtractGkeClusterLocationId(env_obj))
    def Run(self, args):
        self.DeprecationWarningPrompt(args)
        self.CheckForRequiredCmdArgs(args)

        running_state = (api_util.GetMessagesModule(
            release_track=self.ReleaseTrack()).Environment.
                         StateValueValuesEnum.RUNNING)

        env_ref = args.CONCEPTS.environment.Parse()
        env_obj = environments_api_util.Get(env_ref,
                                            release_track=self.ReleaseTrack())

        if env_obj.state != running_state:
            raise command_util.Error(
                'Cannot execute subcommand for environment in state {}. '
                'Must be RUNNING.'.format(env_obj.state))

        cluster_id = env_obj.config.gkeCluster
        cluster_location_id = command_util.ExtractGkeClusterLocationId(env_obj)

        tty = 'no-tty' not in args

        with command_util.TemporaryKubeconfig(cluster_location_id, cluster_id):
            try:
                image_version = env_obj.config.softwareConfig.imageVersion
                airflow_version = self._ExtractAirflowVersion(image_version)

                self.CheckSubcommandAirflowSupport(args, airflow_version)
                self.CheckSubcommandNestedAirflowSupport(args, airflow_version)

                kubectl_ns = command_util.FetchKubectlNamespace(image_version)
                pod = command_util.GetGkePod(pod_substr=WORKER_POD_SUBSTR,
                                             kubectl_namespace=kubectl_ns)

                log.status.Print(
                    'Executing within the following Kubernetes cluster namespace: '
                    '{}'.format(kubectl_ns))

                self.BypassConfirmationPrompt(args, airflow_version)
                kubectl_args = ['exec', pod, '--stdin']
                if tty:
                    kubectl_args.append('--tty')
                kubectl_args.extend([
                    '--container', WORKER_CONTAINER, '--', 'airflow',
                    args.subcommand
                ])
                if args.subcommand_nested:
                    kubectl_args.append(args.subcommand_nested)
                if args.cmd_args:
                    kubectl_args.extend(args.cmd_args)

                command_util.RunKubectlCommand(
                    command_util.AddKubectlNamespace(kubectl_ns, kubectl_args),
                    out_func=log.out.Print)
            except command_util.KubectlError as e:
                raise self.ConvertKubectlError(e, env_obj)
Пример #4
0
  def Run(self, args):
    self.DeprecationWarningPrompt(args)

    running_state = (
        api_util.GetMessagesModule(release_track=self.ReleaseTrack())
        .Environment.StateValueValuesEnum.RUNNING)

    env_ref = args.CONCEPTS.environment.Parse()
    env_obj = environments_api_util.Get(
        env_ref, release_track=self.ReleaseTrack())

    if env_obj.state != running_state:
      raise command_util.Error(
          'Cannot execute subcommand for environment in state {}. '
          'Must be RUNNING.'.format(env_obj.state))

    cluster_id = env_obj.config.gkeCluster
    cluster_location_id = command_util.ExtractGkeClusterLocationId(env_obj)

    with command_util.TemporaryKubeconfig(cluster_location_id, cluster_id):
      try:
        kubectl_ns = command_util.FetchKubectlNamespace(
            env_obj.config.softwareConfig.imageVersion)
        pod = command_util.GetGkePod(
            pod_substr=WORKER_POD_SUBSTR, kubectl_namespace=kubectl_ns)

        log.status.Print(
            'Executing within the following kubectl namespace: {}'.format(
                kubectl_ns))

        self.BypassConfirmationPrompt(args)
        kubectl_args = [
            'exec', pod, '-tic', WORKER_CONTAINER, 'airflow', args.subcommand
        ]
        if args.cmd_args:
          # Add '--' to the argument list so kubectl won't eat the command args.
          kubectl_args.extend(['--'] + args.cmd_args)

        command_util.RunKubectlCommand(
            command_util.AddKubectlNamespace(kubectl_ns, kubectl_args),
            out_func=log.status.Print)
      except command_util.KubectlError as e:
        raise self.ConvertKubectlError(e, env_obj)
Пример #5
0
    def Run(self, args):
        env_ref = args.CONCEPTS.environment.Parse()
        env_obj = environments_api_util.Get(env_ref,
                                            release_track=self.ReleaseTrack())

        cluster_id = env_obj.config.gkeCluster
        cluster_location_id = command_util.ExtractGkeClusterLocationId(env_obj)

        tty = 'no-tty' not in args

        with command_util.TemporaryKubeconfig(cluster_location_id, cluster_id):
            try:
                image_version = env_obj.config.softwareConfig.imageVersion

                kubectl_ns = command_util.FetchKubectlNamespace(image_version)
                pod = command_util.GetGkePod(pod_substr=WORKER_POD_SUBSTR,
                                             kubectl_namespace=kubectl_ns)

                log.status.Print(
                    'Executing within the following Kubernetes cluster namespace: '
                    '{}'.format(kubectl_ns))

                kubectl_args = ['exec', pod, '--stdin']
                if tty:
                    kubectl_args.append('--tty')
                kubectl_args.extend(['--container', WORKER_CONTAINER, '--'])
                if args.tree:
                    kubectl_args.extend(
                        ['python', '-m', 'pipdeptree', '--warn'])
                else:
                    kubectl_args.extend(['python', '-m', 'pip', 'list'])

                command_util.RunKubectlCommand(
                    command_util.AddKubectlNamespace(kubectl_ns, kubectl_args),
                    out_func=log.out.Print)
            except command_util.KubectlError as e:
                raise self.ConvertKubectlError(e, env_obj)