Skip to content

Commit

Permalink
add remote-build command with instructions on how to use it
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard-Voiculescu committed Jun 3, 2024
1 parent eedefc0 commit beea86d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
38 changes: 29 additions & 9 deletions cmd/substreams/remote-build.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,41 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/streamingfast/cli"
"github.com/streamingfast/cli/sflags"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

pbbuild "github.com/streamingfast/substreams/remotebuild/pb/sf/remotebuild/v1"
)

func init() {
remoteBuildCmd.Flags().StringArray("env", nil, "Environment variables to pass to the build server")
remoteBuildCmd.Flags().StringP("output-name", "o", "substreams.spkg", "Spkg name to collect")

rootCmd.AddCommand(remoteBuildCmd)
}

var remoteBuildCmd = &cobra.Command{
Use: "remote-build <remote-build-url> <zipped_source_code>",
Short: "Send request to remote build server to build and package a substreams",
Long: cli.Dedent(`
Call the remote build server to build and package a substream.
Examples: substreams remote-build https://substreams-remotebuild-endpoint:443 my-substream.zip
Call the remote build server to build and package a substream. You can also add in environment variables to pass to the build server.
Which will be injected in your substreams build. Make sure you align the make package output command with the collect pattern. By default
it will collect the substreams.spkg file, but if you gave it a different name with the -o or --output-file flag, you should pass it here too.
Example of valide make package commands: substreams pack:
.PHONE: package
pack: build
substreams pack -o my-substreams.spkg substreams.yaml
Then you will need to pass in the --output-name="my-substreams.spkg" flag to the remote-build command.
Examples: substreams remote-build https://substreams-remotebuild-endpoint:443 my-substream.zip --env="ENV0=test0,ENV1=test1" --env "ENV2=test2" --env "ENV3=test3" --output-name="my-substream.spkg"
`),
RunE: remoteBuildE,
Args: cobra.ExactArgs(2),
Expand All @@ -32,15 +47,19 @@ var remoteBuildCmd = &cobra.Command{

func remoteBuildE(cmd *cobra.Command, args []string) error {
remoteBuildURL := args[0]
filepath := args[2]
filepath := args[1]
envs := sflags.MustGetStringArray(cmd, "env")
spkgName := sflags.MustGetString(cmd, "output-name")

fmt.Println("Connecting to remote build server...")
fmt.Printf("Connecting to remote build server at %s ...\n", remoteBuildURL)
remoteBuildURL = strings.Replace(remoteBuildURL, "https://", "", 1)
fmt.Println(remoteBuildURL)
conn, err := grpc.NewClient(remoteBuildURL, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("failed to connect: %w", err)
}

fmt.Println("Reading file...")
fmt.Printf("Reading file: %s ...\n", filepath)
b, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("failed to read file: %w", err)
Expand All @@ -49,20 +68,21 @@ func remoteBuildE(cmd *cobra.Command, args []string) error {
client := pbbuild.NewBuildServiceClient(conn)
buildResponse, err := client.Build(context.Background(), &pbbuild.BuildRequest{
SourceCode: b,
Env: []string{"ENV1=test", ""},
CollectPattern: "substreams.spkg",
Env: envs,
CollectPattern: spkgName,
})
if err != nil {
return fmt.Errorf("failed to build: %w", err)
}

for _, artifact := range buildResponse.Artifacts {
fmt.Printf("Writing file %s...\n", artifact.Filename)
fmt.Printf("Writing file %s ...\n", artifact.Filename)
err = os.WriteFile(artifact.Filename, artifact.Content, 0644)
if err != nil {
return fmt.Errorf("failed to write file: %w", err)
}
}

fmt.Println("All done!")
return nil
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ require (
connectrpc.com/grpchealth v1.3.0 // indirect
connectrpc.com/otelconnect v0.7.0 // indirect
github.com/bits-and-blooms/bitset v1.12.0 // indirect
github.com/bobg/go-generics/v2 v2.1.1 // indirect
github.com/bobg/go-generics/v2 v2.2.2 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ github.com/blendle/zapdriver v1.3.2-0.20200203083823-9200777f8a3d h1:fSlGu5ePbkj
github.com/blendle/zapdriver v1.3.2-0.20200203083823-9200777f8a3d/go.mod h1:yCBkgASmKHgUOFjK9h1sOytUVgA+JkQjqj3xYP4AdWY=
github.com/bobg/go-generics/v2 v2.1.1 h1:4rN9upY6Xm4TASSMeH+NzUghgO4h/SbNrQphIjRd/R0=
github.com/bobg/go-generics/v2 v2.1.1/go.mod h1:iPMSRVFlzkJSYOCXQ0n92RA3Vxw0RBv2E8j9ZODXgHk=
github.com/bobg/go-generics/v2 v2.2.2 h1:cHTV51Vr/wSlwiNWvncz66E4QtoRw9qXZeEiLAmwqW8=
github.com/bobg/go-generics/v2 v2.2.2/go.mod h1:ieOJ1ARFvk+HfMKbW1DT5UzJ/CJPKoiRm17QKK82bRE=
github.com/btcsuite/btcd/btcec/v2 v2.2.0 h1:fzn1qaOt32TuLjFlkzYSsBC35Q3KUjT1SwPxiMSCF5k=
github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
Expand Down Expand Up @@ -454,6 +456,7 @@ github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWV
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/microcosm-cc/bluemonday v1.0.21 h1:dNH3e4PSyE4vNX+KlRGHT5KrSvjeUkoNPwEORjffHJg=
Expand Down
1 change: 1 addition & 0 deletions remotebuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (s *Server) Build(
cmd := exec.Command("/bin/sh", "-c", "./run.sh")

// Setup environmental variables for the command run
// Also add in any environmental variables passed in the request
cmd.Env = append(req.Msg.Env, os.Environ()...)

cmd.Dir = workingDir
Expand Down

0 comments on commit beea86d

Please sign in to comment.