From 24b765e6b98656e44ce506a54179b7c85bfad161 Mon Sep 17 00:00:00 2001 From: Ares <75481906+ice-ares@users.noreply.github.com> Date: Thu, 19 Dec 2024 16:20:28 +0200 Subject: [PATCH] added a basic ion-connect-indexer cmd impl --- .github/workflows/CICD.yaml | 10 +- Makefile | 2 +- .../.testdata/regenerate.sh | 18 ++++ .../subzero_ion_connect_indexer.go | 96 +++++++++++++++++++ 4 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 cmd/subzero-ion-connect-indexer/.testdata/regenerate.sh create mode 100644 cmd/subzero-ion-connect-indexer/subzero_ion_connect_indexer.go diff --git a/.github/workflows/CICD.yaml b/.github/workflows/CICD.yaml index 7aaca39..9c1fcf0 100644 --- a/.github/workflows/CICD.yaml +++ b/.github/workflows/CICD.yaml @@ -340,6 +340,7 @@ jobs: make_latest: true files: | subzero-ion-connect.linux.amd64.bin + subzero-ion-connect-indexer.linux.amd64.bin - name: Slack Notification For Failure/Cancellation if: ${{ github.event_name == 'push' && (failure() || cancelled()) }} uses: rtCamp/action-slack-notify@v2 @@ -376,13 +377,20 @@ jobs: cmd: | cd secret-infrastructure yq e -i '.generic-service-chart.applicationImage.tag = strenv(APP_TAG)' helm/subzero-ion-connect/staging/common-values.yaml + - name: Update [staging] application tag version in helm/subzero-ion-connect-indexer/staging/common-values.yaml + uses: mikefarah/yq@master + with: + cmd: | + cd secret-infrastructure + yq e -i '.generic-service-chart.applicationImage.tag = strenv(APP_TAG)' helm/subzero-ion-connect-indexer/staging/common-values.yaml - name: Commit and Push Changes to Application Tag Version run: | cd secret-infrastructure git config user.name "ice CI/CD Bot" git config user.email ice-cicd-bot@ice.vip git add helm/subzero-ion-connect/staging/common-values.yaml - git commit -m "Updated 'subzero-ion-connect' tag version (${{env.APP_TAG}}) in application helm chart deployment manifests" + git add helm/subzero-ion-connect-indexer/staging/common-values.yaml + git commit -m "Updated 'subzero-ion-connect' & 'subzero-ion-connect-indexer' tag version (${{env.APP_TAG}}) in application helm chart deployment manifests" git push --set-upstream origin master - name: Slack Notification For Success if: ${{ success() }} diff --git a/Makefile b/Makefile index 2ac7d02..144514f 100644 --- a/Makefile +++ b/Makefile @@ -157,7 +157,7 @@ format-imports: buildAllBinaries: set -xe; \ - find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | while read service; do \ + find ./cmd -mindepth 1 -maxdepth 1 -type d -print | grep -v 'fixture' | grep -v 'scripts' | grep -v 'subzero-ion-connect-keygen' | while read service; do \ env SERVICE_NAME=$${service##*/} env GOOS=$(GOOS) env GOARCH=$(GOARCH) $(MAKE) binary-specific-service; \ done; diff --git a/cmd/subzero-ion-connect-indexer/.testdata/regenerate.sh b/cmd/subzero-ion-connect-indexer/.testdata/regenerate.sh new file mode 100644 index 0000000..c2ede30 --- /dev/null +++ b/cmd/subzero-ion-connect-indexer/.testdata/regenerate.sh @@ -0,0 +1,18 @@ +# SPDX-License-Identifier: ice License 1.0 + +# Generate a new ecdsa key/cert for local development of WebTransport +HOST="localhost" +CRT="$HOST.crt" +KEY="$HOST.key" +# Install the system certificate if it's not already +go get filippo.io/mkcert +go run filippo.io/mkcert -ecdsa -install +cp $(go run filippo.io/mkcert -CAROOT)/rootCA.pem ca.pem + +# Generate a new certificate for localhost +NETWORK_IP=$(ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1' | tail -1) +go run filippo.io/mkcert -ecdsa -days 13 -cert-file "$CRT" -key-file "$KEY" localhost $(hostname) 127.0.0.1 ::1 $NETWORK_IP + +# Compute the sha256 fingerprint of the certificate for WebTransport +rm fingerprint.base64 || true +openssl x509 -in localhost.crt | openssl dgst -sha256 -binary | base64 > fingerprint.base64 \ No newline at end of file diff --git a/cmd/subzero-ion-connect-indexer/subzero_ion_connect_indexer.go b/cmd/subzero-ion-connect-indexer/subzero_ion_connect_indexer.go new file mode 100644 index 0000000..30763ac --- /dev/null +++ b/cmd/subzero-ion-connect-indexer/subzero_ion_connect_indexer.go @@ -0,0 +1,96 @@ +// SPDX-License-Identifier: ice License 1.0 + +package main + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" + + "github.com/cockroachdb/errors" + "github.com/spf13/cobra" + + "github.com/ice-blockchain/subzero/cfg" + "github.com/ice-blockchain/subzero/database/command" + "github.com/ice-blockchain/subzero/database/query" + "github.com/ice-blockchain/subzero/dvm" + "github.com/ice-blockchain/subzero/model" + "github.com/ice-blockchain/subzero/server" + wsserver "github.com/ice-blockchain/subzero/server/ws" +) + +var ( + configPath string + ionConnectIndexer = &cobra.Command{ + Use: "subzero-ion-connect-indexer", + Short: "subzero-ion-connect-indexer", + Run: func(cmd *cobra.Command, _ []string) { + cfg.MustInit(configPath) + query.MustInit(cmd.Context()) + dvm.MustInit() + server.MustListenAndServe(cmd.Context()) + }, + } + initFlags = func() { + ionConnectIndexer.Flags().StringVar(&configPath, "config", cfg.DefaultYAMLConfigurationFilePath, "absolute path to the service config yaml file") + } +) + +func init() { + initFlags() + wsserver.RegisterReqMustAuthenticate(func(_ context.Context, sub *model.Subscription) (authRequired bool) { + authRequired = sub != nil + + return authRequired + }) + wsserver.RegisterEventMustAuthenticate(func(_ context.Context, _ ...*model.Event) (authRequired bool) { + authRequired = true + + return authRequired + }) + wsserver.RegisterWSEventListener(func(ctx context.Context, events ...*model.Event) error { + if err := command.AcceptEvents(ctx, events...); err != nil { + return errors.Wrapf(err, "failed to command.AcceptEvent(%#v)", events) + } + if err := query.AcceptEvents(ctx, events...); err != nil { + return errors.Wrapf(err, "failed to query.AcceptEvent(%#v)", events) + } + if err := dvm.AcceptJob(ctx, events[0]); err != nil { + return errors.Wrapf(err, "failed to dvm.AcceptEvent(%#v)", events[0]) + } + + return nil + }) + wsserver.RegisterWSSubscriptionListener(query.GetStoredEvents) +} + +func newContext() context.Context { + ctx, cancel := context.WithCancel(context.Background()) + + c := make(chan os.Signal, 2) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + force := false + for sig := range c { + if force { + log.Println("force shutdown", "signal", sig.String()) + os.Exit(2) + } else { + log.Println("graceful shutdown", "signal", sig.String()) + cancel() + force = true + } + } + }() + + return ctx +} + +func main() { + err := ionConnectIndexer.ExecuteContext(newContext()) + if err != nil { + log.Panic(err) + } +}