github.com/apache/beam/sdks/v2@v2.48.2/python/expansion-service-container/boot.go (about)

     1  // Licensed to the Apache Software Foundation (ASF) under one or more
     2  // contributor license agreements.  See the NOTICE file distributed with
     3  // this work for additional information regarding copyright ownership.
     4  // The ASF licenses this file to You under the Apache License, Version 2.0
     5  // (the "License"); you may not use this file except in compliance with
     6  // the License.  You may obtain a copy of the License at
     7  //
     8  //    http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // boot is the boot code for the Python SDK harness container. It is responsible
    17  // for retrieving and install staged files and invoking python correctly.
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"fmt"
    23  	"log"
    24  	"os"
    25  	"path/filepath"
    26  	"strconv"
    27  	"strings"
    28  
    29  	"github.com/apache/beam/sdks/v2/go/pkg/beam/util/execx"
    30  )
    31  
    32  var (
    33  	id   = flag.String("id", "", "Local identifier (required)")
    34  	port = flag.Int("port", 0, "Port for the expansion service (required)")
    35  )
    36  
    37  const (
    38  	expansionServiceEntrypoint = "apache_beam.runners.portability.expansion_service_main"
    39  	venvDirectory              = "beam_venv" // This should match the venv directory name used in the Dockerfile.
    40  	requirementsFile           = "requirements.txt"
    41  	beamSDKArtifact            = "apache-beam-sdk.tar.gz"
    42  	beamSDKOptions             = "[gcp,dataframe]"
    43  )
    44  
    45  func main() {
    46  	flag.Parse()
    47  
    48  	if *id == "" {
    49  		log.Fatalf("The flag 'id' was not specified")
    50  	}
    51  	if *port == 0 {
    52  		log.Fatalf("The flag 'port' was not specified")
    53  	}
    54  
    55  	if err := launchExpansionServiceProcess(); err != nil {
    56  		log.Fatal(err)
    57  	}
    58  }
    59  
    60  func launchExpansionServiceProcess() error {
    61  	log.Printf("Starting Python expansion service ...")
    62  
    63  	dir := filepath.Join("/opt/apache/beam", venvDirectory)
    64  	os.Setenv("VIRTUAL_ENV", dir)
    65  	os.Setenv("PATH", strings.Join([]string{filepath.Join(dir, "bin"), os.Getenv("PATH")}, ":"))
    66  
    67  	args := []string{"-m", expansionServiceEntrypoint, "-p", strconv.Itoa(*port), "--fully_qualified_name_glob", "*"}
    68  	if err := execx.Execute("python", args...); err != nil {
    69  		return fmt.Errorf("Could not start the expansion service: %s", err)
    70  	}
    71  
    72  	return nil
    73  }