Skip to content

Commit

Permalink
Change ENV VAR names, Refactor server.go
Browse files Browse the repository at this point in the history
  • Loading branch information
re7eal committed Dec 5, 2018
1 parent 726ddac commit 0076a51
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
name: Start Tendermint with ABCI
command: |
go run abci/server.go --home ./config/tendermint/IdP unsafe_reset_all
CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" DB_NAME=IdP_DB \
CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" ABCI_DB_DIR_PATH=IdP_DB \
go run \
-ldflags "-X github.com/ndidplatform/smart-contract/abci/version.GitCommit=`git rev-parse --short=8 HEAD`" \
-tags "gcc" \
Expand Down
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ TENDERMINT_ADDRESS=http://localhost:45000 go test -v

**Environment variable options**

- `DB_NAME`: Directory path for persistence data files [Default: `__dirname/DID` (`DID` directory in repository's directory)]
- `LOG_LEVEL`: Log level. Allowed values are `error`, `warn`, `info` and `debug` [Default: `debug`]
- `LOG_TARGET`: Where should logger writes logs to. Allowed values are `console` or `file path` (eg. `ABCI.log`) [Default: `console`]
- `ABCI_DB_DIR_PATH`: Directory path for ABCI app persistence data files [Default: `./DID`]
- `ABCI_LOG_LEVEL`: Log level. Allowed values are `error`, `warn`, `info` and `debug` [Default: `debug`]
- `ABCI_LOG_TARGET`: Where should logger writes logs to. Allowed values are `console` or `file` (eg. `ABCI.log`) [Default: `console`]
- `ABCI_LOG_FILE_PATH`: File path for log file (use when `ABCI_LOG_TARGET` is set to `file`) [Default: `./abci.log`]

### Run IdP node

Expand All @@ -84,7 +85,7 @@ TENDERMINT_ADDRESS=http://localhost:45000 go test -v
mkdir -p IdP_DB
go run ./abci --home ./config/tendermint/IdP unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" DB_NAME=IdP_DB go run -tags "gcc" ./abci --home ./config/tendermint/IdP node
go run ./abci --home ./config/tendermint/IdP unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" ABCI_DB_DIR_PATH=IdP_DB go run -tags "gcc" ./abci --home ./config/tendermint/IdP node
```

### Run RP node
Expand All @@ -96,7 +97,7 @@ TENDERMINT_ADDRESS=http://localhost:45000 go test -v
mkdir -p RP_DB
go run ./abci --home ./config/tendermint/RP unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" DB_NAME=RP_DB go run -tags "gcc" ./abci --home ./config/tendermint/RP node
go run ./abci --home ./config/tendermint/RP unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" ABCI_DB_DIR_PATH=RP_DB go run -tags "gcc" ./abci --home ./config/tendermint/RP node
```

### Run AS node
Expand All @@ -108,7 +109,7 @@ TENDERMINT_ADDRESS=http://localhost:45000 go test -v
mkdir -p AS_DB
go run ./abci --home ./config/tendermint/AS unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" DB_NAME=AS_DB go run -tags "gcc" ./abci --home ./config/tendermint/AS node
go run ./abci --home ./config/tendermint/AS unsafe_reset_all && CGO_ENABLED=1 CGO_LDFLAGS="-lsnappy" ABCI_DB_DIR_PATH=AS_DB go run -tags "gcc" ./abci --home ./config/tendermint/AS node
```

## Run in Docker
Expand Down
94 changes: 94 additions & 0 deletions abci/commands.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact [email protected] for any further questions
*
*/

package main

import (
"fmt"
// "os"
// "strconv"
// "time"

"github.com/spf13/cobra"
// "github.com/spf13/viper"

// "github.com/tendermint/tendermint/cmd/tendermint/commands"
// cfg "github.com/tendermint/tendermint/config"
// tmflags "github.com/tendermint/tendermint/libs/cli/flags"
// "github.com/tendermint/tendermint/libs/log"
// "github.com/tendermint/tmlibs/cli"

"github.com/ndidplatform/smart-contract/abci/version"
)

var abciVersionCmd = &cobra.Command{
Use: "abci_app_version",
Short: "Show DID ABCI app version info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.Version)
},
}

// func customTMRootCmdPersistentPreRunE(cmd *cobra.Command, args []string) (err error) {
// if cmd.Name() == commands.VersionCmd.Name() {
// return nil
// }
// config, err := commands.ParseConfig()
// if err != nil {
// return err
// }

// var logTarget = getEnv("TENDERMINT_LOG_TARGET", logTargetConsole)
// var logger log.Logger

// if logTarget == logTargetConsole {
// if config.LogFormat == cfg.LogFormatJSON {
// logger = log.NewTMJSONLogger(log.NewSyncWriter(os.Stdout))
// } else {
// logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout))
// }
// } else if logTarget == logTargetFile {
// currentTime := time.Now()
// currentTimeStr := currentTime.Format(fileDatetimeFormat)

// var logFilePath = getEnv("TENDERMINT_LOG_FILE_PATH", "./tm-"+strconv.Itoa(os.Getpid())+"-"+currentTimeStr+".log")
// logFile, _ := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)

// if config.LogFormat == cfg.LogFormatJSON {
// logger = log.NewTMJSONLogger(log.NewSyncWriter(logFile))
// } else {
// logger = log.NewTMLogger(log.NewSyncWriter(logFile))
// }
// } else {
// panic(fmt.Errorf("Unknown log target: \"%s\". Only \"console\" and \"file\" are allowed", logTarget))
// }

// logger, err = tmflags.ParseLogLevel(config.LogLevel, logger, cfg.DefaultLogLevel())
// if err != nil {
// return err
// }
// if viper.GetBool(cli.TraceFlag) {
// logger = log.NewTracingLogger(logger)
// }
// logger = logger.With("module", "main")
// return nil
// }
2 changes: 1 addition & 1 deletion abci/did/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type DIDApplicationInterface struct {

func NewDIDApplicationInterface() *DIDApplicationInterface {
logger := logrus.WithFields(logrus.Fields{"module": "abci-app"})
var dbDir = getEnv("DB_NAME", "DID")
var dbDir = getEnv("ABCI_DB_DIR_PATH", "./DID")
if err := cmn.EnsureDir(dbDir, 0700); err != nil {
panic(fmt.Errorf("Could not create DB directory: %v", err.Error()))
}
Expand Down
34 changes: 19 additions & 15 deletions abci/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
"fmt"
"os"
"path/filepath"
// "strings"
"strconv"
"time"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

cmd "github.com/tendermint/tendermint/cmd/tendermint/commands"
cfg "github.com/tendermint/tendermint/config"
Expand All @@ -43,32 +43,36 @@ import (
"github.com/tendermint/tendermint/abci/types"

"github.com/ndidplatform/smart-contract/abci/did"
"github.com/ndidplatform/smart-contract/abci/version"
)

type loggerWriter struct{}

// var mainLogger *logrus.Entry

var abciVersionCmd = &cobra.Command{
Use: "abci_app_version",
Short: "Show DID ABCI app version info",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(version.Version)
},
}
const (
fileDatetimeFormat = "01-02-2006_15:04:05"
logTargetConsole = "console"
logTargetFile = "file"
)

func init() {
// Set default logrus

var logLevel = getEnv("LOG_LEVEL", "debug")
var logTarget = getEnv("LOG_TARGET", "console")
var logLevel = getEnv("ABCI_LOG_LEVEL", "debug")
var logTarget = getEnv("ABCI_LOG_TARGET", logTargetConsole)

currentTime := time.Now()
currentTimeStr := currentTime.Format(fileDatetimeFormat)

if logTarget != "console" {
logFile, _ := os.OpenFile(logTarget, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
var logFilePath = getEnv("ABCI_LOG_FILE_PATH", "./abci-"+strconv.Itoa(os.Getpid())+"-"+currentTimeStr+".log")

if logTarget == logTargetConsole {
logrus.SetOutput(os.Stdout)
} else if logTarget == logTargetFile {
logFile, _ := os.OpenFile(logFilePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
logrus.SetOutput(logFile)
} else {
logrus.SetOutput(os.Stdout)
panic(fmt.Errorf("Unknown log target: \"%s\". Only \"console\" and \"file\" are allowed", logTarget))
}

switch logLevel {
Expand Down
8 changes: 4 additions & 4 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ RUN go install \
FROM alpine:3.8
LABEL maintainer="NDID IT Team <[email protected]>"
ENV TERM=xterm-256color
ENV DB_NAME=/DID
ENV ABCI_DB_DIR_PATH=/DID

# Tendermint will be looking for genesis file in /tendermint (unless you change
# `genesis_file` in config.toml). You can put your config.toml and private
Expand All @@ -74,11 +74,11 @@ COPY --from=builder /usr/local/lib /usr/local/lib

COPY --from=builder /go/bin/abci /usr/bin/did-tendermint
COPY docker/start-node.sh /usr/bin/
RUN mkdir -p ${DB_NAME} ${TMHOME}
RUN mkdir -p ${TMHOME} ${ABCI_DB_DIR_PATH}

# Change owner to nobodoy:nogroup and permission to 640
RUN chown -R nobody:nogroup /usr/bin/start-node.sh /usr/bin/did-tendermint ${TMHOME} ${DB_NAME}
RUN chmod -R 740 /usr/bin/start-node.sh /usr/bin/did-tendermint ${TMHOME} ${DB_NAME}
RUN chown -R nobody:nogroup /usr/bin/start-node.sh /usr/bin/did-tendermint ${TMHOME} ${ABCI_DB_DIR_PATH}
RUN chmod -R 740 /usr/bin/start-node.sh /usr/bin/did-tendermint ${TMHOME} ${ABCI_DB_DIR_PATH}

USER nobody
ENTRYPOINT ["start-node.sh"]
Expand Down

0 comments on commit 0076a51

Please sign in to comment.