Skip to content

Commit

Permalink
Move zos-update-worker to zos/tools (#2088)
Browse files Browse the repository at this point in the history
* move zos-update-worker to zos/tools

* add workdir to linter

* fix staticcheck and lint

* merge .gitignore, license and code owners into zos files

* integrate worker Makefile within zos

* update the worker to use the new link style

* remove unnecessary issue template

* move worker .gitignore and Makefile to worker dir

* fix zosCurrent link format

* fix broken imports
  • Loading branch information
Eslam-Nawara authored Nov 13, 2023
1 parent 89999e3 commit 824d08d
Show file tree
Hide file tree
Showing 12 changed files with 1,339 additions and 0 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/zos-update-worker-main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Zos Update Worker

defaults:
run:
working-directory: tools/zos-update-worker

on:
push:
paths:
- tools/zos-update-worker/**
pull_request:
paths:
- tools/zos-update-worker/**

jobs:
Explore-Packge:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v3

- name: Install GO
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
args: --timeout 3m --verbose
working-directory: tools/zos-update-worker

- name: staticcheck
uses: dominikh/[email protected]
with:
version: "2022.1.3"
working-directory: tools/zos-update-worker
env:
GO111MODULE: on

- name: gofmt
uses: Jerome1337/[email protected]
with:
gofmt-flags: "-l -d"
gofmt-path: "tools/zos-update-worker"

- name: Test
run: go test -v ./...


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
67 changes: 67 additions & 0 deletions tools/zos-update-worker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 staticcheck" && go get -u honnef.co/go/tools/cmd/staticcheck && go install honnef.co/go/tools/cmd/staticcheck
@echo "Installing gocyclo" && go get -u github.com/fzipp/gocyclo/cmd/gocyclo && go install github.com/fzipp/gocyclo/cmd/gocyclo
@echo "Installing deadcode" && go get -u github.com/remyoudompheng/go-misc/deadcode && go install github.com/remyoudompheng/go-misc/deadcode
@echo "Installing misspell" && go get -u github.com/client9/misspell/cmd/misspell && go install github.com/client9/misspell/cmd/misspell
@echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/[email protected]
go mod tidy


verifiers: fmt lint cyclo deadcode spelling staticcheck

fmt:
@echo "Running $@"
@gofmt -d .

lint:
@echo "Running $@"
@${GOPATH}/bin/golangci-lint run

cyclo:
@echo "Running $@"
@${GOPATH}/bin/gocyclo -over 100 .

deadcode:
@echo "Running $@"
@${GOPATH}/bin/deadcode -test $(shell go list ./...) || true

spelling:
@echo "Running $@"
@${GOPATH}/bin/misspell -i monitord -error `find .`

staticcheck:
@echo "Running $@"
@${GOPATH}/bin/staticcheck -- ./...

test: verifiers
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: verifiers
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-update-worker/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/zos/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")
}
Loading

0 comments on commit 824d08d

Please sign in to comment.