Skip to content

Commit

Permalink
Adds update worker
Browse files Browse the repository at this point in the history
Signed-off-by: Ashraf Fouda <[email protected]>
  • Loading branch information
ashraffouda committed Feb 18, 2025
1 parent ff6e3dd commit 994322c
Show file tree
Hide file tree
Showing 11 changed files with 1,293 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tools/zos-update-worker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

tf-autobuilder/
tf-zos/
bin/
coverage/

dist/
34 changes: 34 additions & 0 deletions tools/zos-update-worker/.goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This is an example .goreleaser.yml file with some sensible defaults.
# Make sure to check the documentation at https://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
goos:
- linux
- darwin
archives:
- replacements:
darwin: Darwin
linux: Linux
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'

# modelines, feel free to remove those if you don't want/use them:
# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj
40 changes: 40 additions & 0 deletions tools/zos-update-worker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
OUT=$(shell realpath -m bin)
GOPATH=$(shell go env GOPATH)
branch=$(shell git symbolic-ref -q --short HEAD || git describe --tags --exact-match)
revision=$(shell git rev-parse HEAD)
dirty=$(shell test -n "`git diff --shortstat 2> /dev/null | tail -n1`" && echo "*")
ldflags='-w -s -X $(version).Branch=$(branch) -X $(version).Revision=$(revision) -X $(version).Dirty=$(dirty)'

all: getdeps test

getdeps:
@echo "Installing golangci-lint" && go get github.com/golangci/golangci-lint/cmd/golangci-lint && go install github.com/golangci/golangci-lint/cmd/golangci-lint
go mod tidy

lint:
@echo "Running $@"
golangci-lint run -c ../../.golangci.yml

test: lint
go test -v -vet=off ./...

benchmarks:
go test -v -vet=off ./... -bench=. -count 1 -benchtime=10s -benchmem -run=^#

coverage: clean
mkdir coverage
go test -v -vet=off ./... -coverprofile=coverage/coverage.out
go tool cover -html=coverage/coverage.out -o coverage/coverage.html

testrace: lint
go test -v -race -vet=off ./...

run:
go run main.go

build:
go build -o bin/zos-update-worker main.go

clean:
rm ./coverage -rf
rm ./bin -rf
46 changes: 46 additions & 0 deletions tools/zos-update-worker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# zos-update-worker

A worker to get the version set on the chain with the substrate-client with a specific interval (for example: 10 mins) for mainnet, testnet, and qanet

## How to use

- Get the binary

> Download the latest from the [releases page](https://github.com/threefoldtech/zos/releases)
- Run the worker

After downloading the binary

```bash
sudo cp zos-update-worker /usr/local/bin
zos-update-worker
```

- you can run the command with:

```bash
zos-update-worker --src=tf-autobuilder --dst=tf-zos --interval=10 --main-url=wss://tfchain.grid.tf/ws --main-url=wss://tfchain.grid.tf/ws --test-url=wss://tfchain.test.grid.tf/ws --test-url=wss://tfchain.test.grid.tf/ws --qa-url=wss://tfchain.qa.grid.tf/ws --qa-url=wss://tfchain.qa.grid.tf/ws
```

## Test

```bash
make test
```

## Coverage

```bash
make coverage
```

## Substrate URLs

```go
SUBSTRATE_URLS := map[string][]string{
"qa": {"wss://tfchain.qa.grid.tf/ws"},
"testing": {"wss://tfchain.test.grid.tf/ws"},
"production": {"wss://tfchain.grid.tf/ws"},
}
```
54 changes: 54 additions & 0 deletions tools/zos-update-worker/Taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
version: "3"

tasks:
build:
desc: Build the app
cmds:
- GOFLAGS=-mod=mod go build -o bin/zos-update-worker main.go

run:
desc: Run the app
cmds:
- GOFLAGS=-mod=mod go run main.go

test:
desc: Test the app
cmds:
- go test -v -vet=off ./...

benchmarks:
desc: Test the app benchmarks
cmds:
- go test -v -vet=off ./... -bench=. -count 1 -benchtime=10s -benchmem -run=^#

coverage:
desc: Test the app coverages
cmds:
- rm ./coverage -rf
- mkdir coverage
- go test -v -vet=off ./... -coverprofile=coverage/coverage.out
- go tool cover -html=coverage/coverage.out -o coverage/coverage.html

deps:
desc: install deps
cmds:
- go get -u golang.org/x/lint/golint
- go get -u github.com/fzipp/gocyclo/cmd/gocyclo
- go get -u github.com/remyoudompheng/go-misc/deadcode
- go get -u github.com/client9/misspell/cmd/misspell

verifiers:
desc: Run verifiers
cmds:
- gofmt -d .
- $(go env GOPATH)/bin/golangci-lint run
- $(go env GOPATH)/bin/gocyclo -over 100 .
- $(go env GOPATH)/bin/deadcode -test $(shell go list ./...) || true
- $(go env GOPATH)/bin/misspell -i monitord -error `find .`
- go run honnef.co/go/tools/cmd/staticcheck -- ./...

clean:
desc: Remove all coverage and bin files
cmds:
- rm ./coverage -rf
- rm ./bin -rf
110 changes: 110 additions & 0 deletions tools/zos-update-worker/cmd/worker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd

import (
"fmt"
"os"
"time"

"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/spf13/cobra"
"github.com/threefoldtech/zosbase/tools/zos-update-version/internal"
)

var rootCmd = &cobra.Command{
Use: "zos-update-version",
Short: "A worker to update the version of zos",
RunE: func(cmd *cobra.Command, args []string) error {
if ok, _ := cmd.Flags().GetBool("debug"); ok {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
} else {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}

src, err := cmd.Flags().GetString("src")
if err != nil {
return err
}

dst, err := cmd.Flags().GetString("dst")
if err != nil {
return err
}

params := internal.Params{}
interval, err := cmd.Flags().GetInt("interval")
if err != nil {
return err
}
params.Interval = time.Duration(interval) * time.Minute

production, err := cmd.Flags().GetStringSlice("main-url")
if err != nil {
return err
}
if len(production) > 0 {
params.MainUrls = production
}

test, err := cmd.Flags().GetStringSlice("test-url")
if err != nil {
return err
}
if len(test) > 0 {
params.TestUrls = test
}

qa, err := cmd.Flags().GetStringSlice("qa-url")
if err != nil {
return err
}
if len(qa) > 0 {
params.QAUrls = qa
}

worker, err := internal.NewWorker(src, dst, params)
if err != nil {
return err
}
worker.UpdateWithInterval(cmd.Context())
return nil
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

func init() {

log.Logger = log.Output(zerolog.NewConsoleWriter())

cobra.OnInitialize()

rootCmd.Flags().StringP("src", "s", "tf-autobuilder", "Enter your source directory")
rootCmd.Flags().StringP("dst", "d", "tf-zos", "Enter your destination directory")
rootCmd.Flags().IntP("interval", "i", 10, "Enter the interval between each update")
rootCmd.Flags().Bool("debug", false, "enable debug logging")
rootCmd.Flags().StringSliceP("main-url", "m", []string{}, "Enter your mainnet substrate urls")
rootCmd.Flags().StringSliceP("test-url", "t", []string{}, "Enter your testnet substrate urls")
rootCmd.Flags().StringSliceP("qa-url", "q", []string{}, "Enter your qanet substrate urls")
}
39 changes: 39 additions & 0 deletions tools/zos-update-worker/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module github.com/threefoldtech/zosbase/tools/zos-update-version

go 1.21

require (
github.com/rs/zerolog v1.28.0
github.com/spf13/cobra v1.6.1
github.com/threefoldtech/substrate-client v0.1.5
)

require (
github.com/ChainSafe/go-schnorrkel v1.0.0 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.5 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/base58 v1.0.3 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/ethereum/go-ethereum v1.10.17 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jbenet/go-base58 v0.0.0-20150317085156-6237cf65f3a6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/pierrec/xxHash v0.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/vedhavyas/go-subkey v1.0.3 // indirect
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
)

replace github.com/centrifuge/go-substrate-rpc-client/v4 v4.0.5 => github.com/threefoldtech/go-substrate-rpc-client/v4 v4.0.6-0.20220927094755-0f0d22c73cc7
Loading

0 comments on commit 994322c

Please sign in to comment.