Skip to content

Commit 7ad315a

Browse files
authored
Merge pull request #1306 from threefoldtech/development-create-zos4-registrar
create zos4 registerer server
2 parents b9f88b5 + 4a83c48 commit 7ad315a

File tree

20 files changed

+3255
-1
lines changed

20 files changed

+3255
-1
lines changed

.github/workflows/publish-images.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ jobs:
3434
image: "tfgridproxy"
3535
- dir: "tools/relay-cache-warmer"
3636
image: "relay-cache-warmer"
37+
- dir: "node-registrar"
38+
image: "tfregistrar"
3739

3840
steps:
3941
- name: Checkout the repo

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DIRS := "activation-service" "farmerbot" "grid-cli" "grid-client" "grid-proxy" "gridify" "monitoring-bot" "rmb-sdk-go" "user-contracts-mon" "tfrobot"
1+
DIRS := "activation-service" "farmerbot" "grid-cli" "grid-client" "grid-proxy" "gridify" "monitoring-bot" "rmb-sdk-go" "user-contracts-mon" "tfrobot" "node-registrar"
22

33
mainnet-release:
44
cd grid-client && go get github.com/threefoldtech/tfchain/clients/tfchain-client-go@5d6a2dd

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ This repo contains the go clients for Threefold grid.
1717
- [user contracts mon](./user-contracts-mon/README.md)
1818
- [activation service](./activation-service/README.md)
1919
- [farmerbot](./farmerbot/README.md)
20+
- [node-registrar](./node-registrar/README.md)
2021

2122
## Release
2223

node-registrar/Dockerfile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM golang:1.21-alpine as builder
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
7+
RUN go mod download
8+
9+
COPY . .
10+
11+
RUN go build -o server ./cmds/registrar.go
12+
13+
FROM alpine:3.21
14+
15+
WORKDIR /root/
16+
17+
COPY --from=builder /app/server .
18+
19+
EXPOSE 8080
20+
21+
CMD ["./server"]

node-registrar/Makefile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
run:
2+
go run cmds/registrar.go --postgres-host localhost --postgres-port 5432 --postgres-db postgres --postgres-user postgres --postgres-password password --domain localhost --server-port 8080
3+
4+
postgres:
5+
docker run --name postgres -e POSTGRES_USER=postgres POSTGRES_PASSWORD=password POSTGRES_DB=postgres -p 5432:5432 -d postgres
6+
7+
stop-postgres:
8+
docker stop postgres && docker rm postgres
9+
10+
build: ## Bulil the server
11+
go build -o bin/server cmds/registrar.go
12+
13+
server-start:
14+
@go run cmds/registrar.go \
15+
--server-port :8080 \
16+
--log-level debug \
17+
--domain localhost \
18+
--sql-log-level 4 \
19+
--postgres-host localhost \
20+
--postgres-port 5432 \
21+
--postgres-db postgres \
22+
--postgres-user postgres \
23+
--postgres-password password \
24+
25+
getverifiers:
26+
@echo "Installing staticcheck" && go get -u honnef.co/go/tools/cmd/staticcheck && go install honnef.co/go/tools/cmd/staticcheck
27+
@echo "Installing gocyclo" && go get -u github.com/fzipp/gocyclo/cmd/gocyclo && go install github.com/fzipp/gocyclo/cmd/gocyclo
28+
@echo "Installing deadcode" && go get -u github.com/remyoudompheng/go-misc/deadcode && go install github.com/remyoudompheng/go-misc/deadcode
29+
@echo "Installing misspell" && go get -u github.com/client9/misspell/cmd/misspell && go install github.com/client9/misspell/cmd/misspell
30+
@echo "Installing golangci-lint" && go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.50.1
31+
go mod tidy
32+
33+
verifiers: fmt lint cyclo deadcode spelling staticcheck
34+
35+
checks: verifiers
36+
37+
fmt:
38+
@echo "Running $@"
39+
@gofmt -d .
40+
41+
lint:
42+
@echo "Running $@"
43+
@$(shell go env GOPATH)/bin/golangci-lint run
44+
45+
cyclo:
46+
@echo "Running $@"
47+
@$(shell go env GOPATH)/bin/gocyclo -over 100 .
48+
49+
deadcode:
50+
@echo "Running $@"
51+
@$(shell go env GOPATH)/bin/deadcode -test $(shell go list ./...) || true
52+
53+
spelling:
54+
@echo "Running $@"
55+
@$(shell go env GOPATH)/bin/misspell -i monitord -error `find .`
56+
57+
staticcheck:
58+
@echo "Running $@"
59+
@$(shell go env GOPATH)/bin/staticcheck -- ./...
60+

node-registrar/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
2+
## Project Documentation
3+
4+
### Overview
5+
This project provides an API for registring zos nodes using the Go Gin framework and PostgreSQL database.
6+
The API supports operations like registring, listing, and updating farms and nodes, as well as reporting uptime and consumption data for nodes.
7+
8+
### Endpoint Descriptions
9+
10+
#### Farms Endpoints
11+
1. **GET /farms/** - List all farms, or use FarmFilter to list specific set of farms.
12+
2. **GET /farms/:farm_id** - Get a specific farm by ID.
13+
3. **POST /farms/** - Create a new farm.
14+
4. **PATCH /farms/** - Update an existing farm.
15+
16+
#### Nodes Endpoints
17+
1. **GET /nodes/** - List all nodes, or use NodeFilter to list specific set of nodes.
18+
2. **GET /nodes/:node_id** - Get a specific node by ID.
19+
3. **POST /nodes/** - Register a new node.
20+
4. **POST /nodes/:node_id/uptime** - Report uptime for a specific node.
21+
5. **POST /nodes/:node_id/consumption** - Report consumption for a specific node.
22+
23+
## Setup Instructions
24+
25+
1. **Start PostgreSQL:**
26+
```bash
27+
make postgres
28+
```
29+
2. **Run the Server:**
30+
```bash
31+
make run
32+
```
33+
3. **Stop PostgreSQL:**
34+
```bash
35+
make stop-postgres
36+
```
37+
38+
### Swagger Documentation
39+
Once the server is running, Swagger documentation can be accessed at:
40+
```
41+
http://<domain>:<port>/swagger/index.html
42+
```
43+
Replace `<domain>` and `<port>` with the appropriate values.
44+
45+
### How to Use the Server
46+
1. Use a tool like Postman or cURL to interact with the API.
47+
2. Refer to the Swagger documentation for detailed information about request parameters and response structures.
48+
49+
### How to run the server with docker
50+
1. use the docker file to build the docker image
51+
```
52+
docker build -t registrar:latest .
53+
```
54+
2. run the image
55+
```
56+
docker run -d \
57+
-p 8080:8080 \
58+
--name registrar \
59+
registrar:latest \
60+
./server
61+
--postgres-host=<your-postgres-host> \
62+
--postgres-port=5432 \
63+
--postgres-db=<your-db-name> \
64+
--postgres-user=<your-db-user> \
65+
--postgres-password=<your-db-password> \
66+
--ssl-mode=disable \
67+
--sql-log-level=2 \
68+
--max-open-conn=10 \
69+
--max-idle-conn=5 \
70+
--server-port=8080 \
71+
--<domain=your-domain> \
72+
--network=main\
73+
--debug
74+
```

node-registrar/cmds/registrar.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"net"
7+
"os"
8+
"os/signal"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/pkg/errors"
13+
"github.com/rs/zerolog"
14+
"github.com/rs/zerolog/log"
15+
"github.com/threefoldtech/tfgrid-sdk-go/node-registrar/pkg/db"
16+
"github.com/threefoldtech/tfgrid-sdk-go/node-registrar/pkg/server"
17+
"gorm.io/gorm/logger"
18+
)
19+
20+
type flags struct {
21+
db.Config
22+
debug bool
23+
version bool
24+
domain string
25+
serverPort uint
26+
network string
27+
}
28+
29+
var (
30+
commit string
31+
version string
32+
)
33+
34+
func main() {
35+
if err := Run(); err != nil {
36+
log.Fatal().Err(err).Send()
37+
}
38+
}
39+
40+
func Run() error {
41+
f := flags{}
42+
var sqlLogLevel int
43+
flag.StringVar(&f.PostgresHost, "postgres-host", "", "postgres host")
44+
flag.Uint64Var(&f.Config.PostgresPort, "postgres-port", 5432, "postgres port")
45+
flag.StringVar(&f.DBName, "postgres-db", "", "postgres database")
46+
flag.StringVar(&f.PostgresUser, "postgres-user", "", "postgres username")
47+
flag.StringVar(&f.PostgresPassword, "postgres-password", "", "postgres password")
48+
flag.StringVar(&f.SSLMode, "ssl-mode", "disable", "postgres ssl mode[disable, require, verify-ca, verify-full]")
49+
flag.IntVar(&sqlLogLevel, "sql-log-level", 2, "sql logger level")
50+
flag.Uint64Var(&f.MaxOpenConns, "max-open-conn", 3, "max open sql connections")
51+
flag.Uint64Var(&f.MaxIdleConns, "max-idle-conn", 3, "max idle sql connections")
52+
53+
flag.BoolVar(&f.version, "v", false, "shows the package version")
54+
flag.BoolVar(&f.debug, "debug", false, "allow debug logs")
55+
flag.UintVar(&f.serverPort, "server-port", 8080, "server port")
56+
flag.StringVar(&f.domain, "domain", "", "domain on which the server will be served")
57+
flag.StringVar(&f.network, "network", "dev", "the registrar network")
58+
59+
flag.Parse()
60+
f.SqlLogLevel = logger.LogLevel(sqlLogLevel)
61+
62+
if f.version {
63+
log.Info().Str("version", version).Str("commit", commit).Send()
64+
return nil
65+
}
66+
67+
if err := f.validate(); err != nil {
68+
return err
69+
}
70+
71+
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
72+
zerolog.SetGlobalLevel(zerolog.InfoLevel)
73+
if f.debug {
74+
zerolog.SetGlobalLevel(zerolog.DebugLevel)
75+
}
76+
77+
db, err := db.NewDB(f.Config)
78+
if err != nil {
79+
return errors.Wrap(err, "failed to open database with the specified configurations")
80+
}
81+
82+
defer func() {
83+
err = db.Close()
84+
if err != nil {
85+
log.Error().Err(err).Msg("failed to close database connection")
86+
}
87+
}()
88+
89+
s, err := server.NewServer(db, f.network)
90+
if err != nil {
91+
return errors.Wrap(err, "failed to start gin server")
92+
}
93+
94+
quit := make(chan os.Signal, 1)
95+
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
96+
97+
log.Info().Msg("server is running on port :8080")
98+
err = s.Run(quit, fmt.Sprintf("%s:%d", f.domain, f.serverPort))
99+
if err != nil {
100+
return errors.Wrap(err, "failed to run gin server")
101+
}
102+
103+
return nil
104+
}
105+
106+
func (f flags) validate() error {
107+
if f.serverPort < 1 && f.serverPort > 65535 {
108+
return errors.Errorf("invalid port %d, server port should be in the valid port range 1–65535", f.serverPort)
109+
}
110+
111+
if strings.TrimSpace(f.domain) == "" {
112+
return errors.New("invalid domain name, domain name should not be empty")
113+
}
114+
if _, err := net.LookupHost(f.domain); err != nil {
115+
return errors.Wrapf(err, "invalid domain %s", f.PostgresHost)
116+
}
117+
118+
return f.Config.Validate()
119+
}

0 commit comments

Comments
 (0)