Skip to content

Commit

Permalink
cli + infra updates
Browse files Browse the repository at this point in the history
- cli for p2pmq (wip)
- dockerfile
- makefile
- default config files
- protoc gen script
  • Loading branch information
amirylm committed Sep 17, 2023
1 parent 99e7c61 commit 679fe13
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 10 deletions.
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Build

FROM golang:1.20 AS builder

RUN apt-get update && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
git make g++ gcc-aarch64-linux-gnu wget \
&& rm -rf /var/lib/apt/lists/*

ARG APP_VERSION
ARG APP_NAME
ARG BUILD_TARGET

WORKDIR /p2pmq

COPY go.mod go.sum ./
RUN go mod download
COPY . .

RUN GOOS=linux CGO_ENABLED=0 go build -tags netgo -a -v -o ./bin/${BUILD_TARGET} ./cmd/${BUILD_TARGET}

# Runtime

FROM alpine:latest as runner

ARG BUILD_TARGET

RUN apk --no-cache --upgrade add ca-certificates bash

WORKDIR /p2pmq

COPY --from=builder /p2pmq/.env* ./
COPY --from=builder /p2pmq/bin/${BUILD_TARGET} ./app
20 changes: 17 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
APP_NAME?=p2pmq
BUILD_TARGET?=${APP_NAME}
BUILD_IMG?=${APP_NAME}
APP_VERSION?=$(git describe --tags $(git rev-list --tags --max-count=1) 2> /dev/null || echo "nightly")
CFG_PATH?=/route-p2p/router.json
TEST_PKG?=./...
TEST_TIMEOUT?=2m

Expand All @@ -8,18 +13,27 @@ fmt:
@go fmt ./...

test:
@go test -v -race -timeout=${TEST_TIMEOUT} `go list ./... | grep -v -E "cmd"`
@go test -v -race -timeout=${TEST_TIMEOUT} `go list ./... | grep -v -E "cmd|scripts"`

test-pkg:
@go test -v -race -timeout=${TEST_TIMEOUT} ${TEST_PKG}

test-cov:
@go test -v -race -timeout=${TEST_TIMEOUT} -coverprofile cover.out `go list ./... | grep -v -E "cmd"`
@go test -v -race -timeout=${TEST_TIMEOUT} -coverprofile cover.out `go list ./... | grep -v -E "cmd|scripts"`

test-open-cov:
@make test-cov
@go tool cover -html cover.out -o cover.html
open cover.html

keygen:
@go run ./cmd/keygen/main.go
@go run ./cmd/keygen/main.go

build:
@go build -o "./bin/${BUILD_TARGET}" "./cmd/${BUILD_TARGET}"

docker-build:
@docker build -t "${APP_NAME}" --build-arg APP_VERSION="${APP_VERSION}" --build-arg APP_NAME="${APP_NAME}" --build-arg BUILD_TARGET="${BUILD_TARGET}" .

docker-run:
@docker run -d --restart unless-stopped --name "${APP_NAME}" -v "${PWD}/data/${APP_NAME}/:/p2pmq/.data" -p "${TCP_PORT}":"${TCP_PORT}" -p "${GRPC_PORT}":"${GRPC_PORT}" -e "GRPC_PORT=${GRPC_PORT}" -it "${BUILD_IMG}" /p2pmq/app -config=${CFG_PATH}
111 changes: 111 additions & 0 deletions cmd/p2pmq/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"context"
"fmt"
"os"

"github.com/amirylm/p2pmq/commons"
"github.com/amirylm/p2pmq/core"
logging "github.com/ipfs/go-log/v2"
"github.com/urfave/cli"
)

func main() {
app := &cli.App{
Name: "route-p2p",
Usage: "p2p router",
Flags: []cli.Flag{
// cli.IntFlag{
// Name: "grpc-port",
// EnvVar: "GRPC_PORT",
// Value: 12001,
// },
// cli.IntFlag{
// Name: "monitor-port",
// EnvVar: "MONITOR_PORT",
// },
cli.StringFlag{
Name: "config",
EnvVar: "P2PMQ_CONFIG",
Value: "/p2pmq/p2pmq.json",
},
cli.StringFlag{
Name: "loglevel",
EnvVar: "P2PMQ_LOGLEVEL",
Value: "info",
},
cli.StringFlag{
Name: "libp2p-loglevel",
EnvVar: "LIBP2P_LOGLEVEL",
Value: "info",
},
},
Action: func(c *cli.Context) (err error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

_ = logging.SetLogLevelRegex("p2p:.*", c.String("libp2p-loglevel"))
_ = logging.SetLogLevelRegex("p2pmq.*", c.String("loglevel"))

var d *core.Daemon
if cfgPath := c.String("config"); len(cfgPath) > 0 {
cfg, err := commons.ReadConfig(cfgPath)
if err != nil {
return err
}
d, err = core.NewDaemon(ctx, *cfg, nil, "node")
if err != nil {
return err
}
if err := d.Start(ctx); err != nil {
return err
}
defer func() {
_ = d.Close()
}()
}

if d == nil {
return fmt.Errorf("could not create daemon instance, please provide a config file")
}

<-ctx.Done()

logging.Logger("p2pmq/cli").Info("closing node")

return nil

// svc := service.NewGrpc(ctx, c.String("name"))
// defer svc.Close()

// if monitorPort := c.Int("monitor-port"); monitorPort > 0 {
// mux := http.NewServeMux()
// monitoring.WithMetrics(mux)
// monitoring.WithProfiling(mux)
// monitoring.WithHealthCheck(mux, func() []error {
// err := ctx.Err()
// if err != nil {
// return []error{err}
// }
// return nil
// })
// go func() {
// err := http.ListenAndServe(fmt.Sprintf(":%d", monitorPort), mux)
// if err != nil {
// panic(err)
// }
// }()
// }

// s := svc.GrpcServer()
// return service.ListenGrpc(s, c.Int("grpc-port"))
},
Commands: []cli.Command{},
}

err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
5 changes: 0 additions & 5 deletions cmd/pq/main.go

This file was deleted.

23 changes: 23 additions & 0 deletions commons/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package commons

import (
"encoding/json"
"fmt"
"os"
"strings"
"time"

"gopkg.in/yaml.v3"
)

type DiscMode string
Expand Down Expand Up @@ -129,3 +135,20 @@ type PProf struct {
Enabled bool
Port uint
}

func ReadConfig(cfgAddr string) (*Config, error) {
raw, err := os.ReadFile(cfgAddr)
if err != nil {
return nil, fmt.Errorf("could not read config file: %w", err)
}
cfg := Config{}
if strings.Contains(cfgAddr, ".yaml") || strings.Contains(cfgAddr, ".yml") {
err = yaml.Unmarshal(raw, &cfg)
} else {
err = json.Unmarshal(raw, &cfg)
}
if err != nil {
return nil, fmt.Errorf("could not unmarshal config file: %w", err)
}
return &cfg, nil
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ require (
github.com/libp2p/go-libp2p-pubsub v0.9.3
github.com/multiformats/go-multiaddr v0.11.0
go.uber.org/zap v1.25.0
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
)

require (
Expand Down Expand Up @@ -48,7 +50,7 @@ require (
github.com/ipfs/boxo v0.10.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipfs/go-log/v2 v2.5.1
github.com/ipld/go-ipld-prime v0.20.0 // indirect
github.com/jackpal/go-nat-pmp v1.0.2 // indirect
github.com/jbenet/go-temp-err-catcher v0.1.0 // indirect
Expand Down Expand Up @@ -103,6 +105,7 @@ require (
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1
dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand All @@ -33,6 +34,8 @@ github.com/coreos/go-systemd/v22 v22.5.0 h1:RrqgGjYQKalulkV8NGVIfkXQf6YYmOyiJKk8
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -318,6 +321,8 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
Expand Down Expand Up @@ -367,6 +372,8 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ=
Expand Down Expand Up @@ -569,6 +576,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
7 changes: 7 additions & 0 deletions resources/config/bootstrapper.p2pmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
listenAddrs:
- /ip4/0.0.0.0/tcp/5001
discovery:
serviceTag: p2pmq/kad
mode: bootstrapper
bootstrappers:
# pubsub:
8 changes: 8 additions & 0 deletions resources/config/default.p2pmq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
listenAddrs:
- /ip4/0.0.0.0/tcp/0
discovery:
serviceTag: p2pmq/kad
mode: server
bootstrappers:
- /ip4/0.0.0.0/tcp/5001
pubsub:
7 changes: 7 additions & 0 deletions scripts/proto-gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/*.proto

protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/**/*.proto

0 comments on commit 679fe13

Please sign in to comment.