Skip to content

Commit

Permalink
Enhancements (#39)
Browse files Browse the repository at this point in the history
* change paths

* add --project-directory to docker-compose command

* fix passing .env to compose

* add clean script

* remove comment

* fix get env path

* update README

* fix change paths
  • Loading branch information
altafan authored and tiero committed May 16, 2019
1 parent 36bc3a9 commit 85da5c7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $ git clone https://github.com/vulpemventures/nigiri.git
$ bash scripts/install
```

This will create `~/.nigiri` copying there the `cli/resources/` directory.
This will create `~/.nigiri` copying there the `resources/` directory.

* Build binary
```
Expand All @@ -62,13 +62,13 @@ $ bash scripts/build darwin amd64
$ bash scripts/build linux amd64
```

* Go in `build` folder, rename and move the binary and give permissions to it
* Remove nigiri
```
$ cd build
$ mv nigiri-darwin-amd64 nigiri
$ chmod +x nigiri
$ bash scripts/clean
```

Note: Remeber to always `clean` Nigiri before running `install` after a pull.

## Tasting

At the moment bitcoind, liquidd and electrs are started on *regtest* network. *testnet* and *mainnet* compose files will be released soon.
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var RootCmd = &cobra.Command{
Use: "nigiri",
Short: "Nigiri lets you manage a full dockerized bitcoin environment",
Long: "Nigiri lets you create your dockerized environment with a bitcoin and optionally a liquid node + block explorer powered by an electrum server for every network",
Version: "0.1.0",
Version: "0.0.2",
}

func init() {
Expand Down
64 changes: 47 additions & 17 deletions cli/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"strconv"
"strings"

"github.com/vulpemventures/nigiri/cli/config"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/spf13/cobra"
"github.com/vulpemventures/nigiri/cli/config"
)

const listAll = true
Expand Down Expand Up @@ -73,13 +73,13 @@ func startChecks(cmd *cobra.Command, args []string) error {
return err
}
if !exists {
filedir := filepath.Join(datadir, "nigiri.config.json")
filedir := getPath(datadir, "config")
if err := config.WriteConfig(filedir); err != nil {
return err
}
// .env must be in the directory where docker-compose is run from, not where YAML files are placed
// https://docs.docker.com/compose/env-file/
filedir = filepath.Join(".", ".env")
filedir = getPath(datadir, "env")
if err := writeComposeEnvFile(filedir, ports); err != nil {
return err
}
Expand All @@ -92,17 +92,18 @@ func startChecks(cmd *cobra.Command, args []string) error {
}

func start(cmd *cobra.Command, args []string) error {
bashCmd, err := getStartBashCmd()
datadir, _ := cmd.Flags().GetString("datadir")

bashCmd, err := getStartBashCmd(datadir)
if err != nil {
return err
}

err = bashCmd.Run()
if err != nil {
if err := bashCmd.Run(); err != nil {
return err
}

path := filepath.Join(".", ".env")
path := getPath(datadir, "env")
ports, err := readComposeEnvFile(path)
if err != nil {
return err
Expand All @@ -111,7 +112,8 @@ func start(cmd *cobra.Command, args []string) error {
for chain, services := range ports {
fmt.Printf("%s services:\n", chain)
for name, port := range services {
fmt.Printf("\t%s: localhost:%d\n", name, port)
formatName := fmt.Sprintf("%s:", name)
fmt.Printf(" %-14s localhost:%d\n", formatName, port)
}
}

Expand Down Expand Up @@ -201,16 +203,27 @@ func isEnvOk(stringifiedJSON string) bool {
return true
}

func getComposePath() string {
func getPath(datadir, t string) string {
viper := config.Viper()
datadir := viper.GetString("datadir")
network := viper.GetString("network")
attachLiquid := viper.GetBool("attachLiquid")
if attachLiquid {
network += "-liquid"

if t == "compose" {
network := viper.GetString("network")
attachLiquid := viper.GetBool("attachLiquid")
if attachLiquid {
network += "-liquid"
}
return filepath.Join(datadir, "resources", fmt.Sprintf("docker-compose-%s.yml", network))
}

if t == "env" {
return filepath.Join(datadir, ".env")
}

return filepath.Join(datadir, "resources", fmt.Sprintf("docker-compose-%s.yml", network))
if t == "config" {
return filepath.Join(datadir, "nigiri.config.json")
}

return ""
}

func nigiriIsRunning() (bool, error) {
Expand All @@ -222,8 +235,11 @@ func nigiriExistsAndNotRunning() (bool, error) {
return nigiriExists(listAll)
}

func getStartBashCmd() (*exec.Cmd, error) {
composePath := getComposePath()
func getStartBashCmd(datadir string) (*exec.Cmd, error) {
composePath := getPath(datadir, "compose")
envPath := getPath(datadir, "env")
env := loadEnv(envPath)

bashCmd := exec.Command("docker-compose", "-f", composePath, "up", "-d")

isStopped, err := nigiriExistsAndNotRunning()
Expand All @@ -235,6 +251,7 @@ func getStartBashCmd() (*exec.Cmd, error) {
}
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr
bashCmd.Env = env

return bashCmd, nil
}
Expand Down Expand Up @@ -312,3 +329,16 @@ func mergeComposeEnvFiles(rawJSON []byte) map[string]map[string]int {

return mergedPorts
}

func loadEnv(path string) []string {
content, _ := ioutil.ReadFile(path)
lines := strings.Split(string(content), "\n")
env := os.Environ()
for _, line := range lines {
if line != "" {
env = append(env, line)
}
}

return env
}
61 changes: 35 additions & 26 deletions cli/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os/exec"
"path/filepath"

"github.com/spf13/cobra"
"github.com/vulpemventures/nigiri/cli/config"
"github.com/spf13/cobra"
)

var StopCmd = &cobra.Command{
Expand Down Expand Up @@ -45,30 +45,57 @@ func stopChecks(cmd *cobra.Command, args []string) error {

func stop(cmd *cobra.Command, args []string) error {
delete, _ := cmd.Flags().GetBool("delete")
datadir, _ := cmd.Flags().GetString("datadir")

bashCmd := getStopBashCmd(delete)
bashCmd := getStopBashCmd(datadir, delete)
if err := bashCmd.Run(); err != nil {
return err
}

if delete {
fmt.Println("Removing volume(s) data")
if err := cleanVolumes(); err != nil {
fmt.Println("Removing data from volumes...")
if err := cleanVolumes(datadir); err != nil {
return err
}

configFile := getPath(datadir, "config")
envFile := getPath(datadir, "env")

fmt.Println("Removing configuration file...")
if err := os.Remove(configFile); err != nil {
return err
}
fmt.Println("Removing configuration file")
if err := deleteConfig(); err != nil {

fmt.Println("Removing environmet file...")
if err := os.Remove(envFile); err != nil {
return err
}
}

fmt.Println("Nigiri has been cleaned up successfully.")

return nil
}

func getStopBashCmd(datadir string, delete bool) *exec.Cmd {
composePath := getPath(datadir, "compose")
envPath := getPath(datadir, "env")
env := loadEnv(envPath)

bashCmd := exec.Command("docker-compose", "-f", composePath, "stop")
if delete {
bashCmd = exec.Command("docker-compose", "-f", composePath, "down")
}
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr
bashCmd.Env = env

return bashCmd
}

// cleanVolumes navigates into <datadir>/resources/volumes/<network>
// and deletes all files and directories but the *.conf config files.
func cleanVolumes() error {
datadir := config.GetString(config.Datadir)
func cleanVolumes(datadir string) error {
network := config.GetString(config.Network)
attachLiquid := config.GetBool(config.AttachLiquid)
if attachLiquid {
Expand All @@ -95,21 +122,3 @@ func cleanVolumes() error {

return nil
}

func deleteConfig() error {
datadir := config.GetString(config.Datadir)
configFile := filepath.Join(datadir, config.Filename)
return os.Remove(configFile)
}

func getStopBashCmd(delete bool) *exec.Cmd {
composePath := getComposePath()
bashCmd := exec.Command("docker-compose", "-f", composePath, "stop")
if delete {
bashCmd = exec.Command("docker-compose", "-f", composePath, "down")
}
bashCmd.Stdout = os.Stdout
bashCmd.Stderr = os.Stderr

return bashCmd
}
24 changes: 24 additions & 0 deletions scripts/clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -ex

PARENT_PATH=$(dirname $(cd $(dirname $0); pwd -P))

pushd $PARENT_PATH

case $OSTYPE in
darwin*) OS="darwin";;
linux-gnu*) OS="linux";;
*) echo "OS $OS not supported"; exit 1;;
esac

case $(uname -m) in
amd64) ARCH="amd64";;
x86_64) ARCH="amd64";;
*) echo "Architecture $ARCH not supported"; exit 1;;
esac

./build/nigiri-$OS-$ARCH stop --delete &>/dev/null
rm -rf build vendor ~/.nigiri

popd

0 comments on commit 85da5c7

Please sign in to comment.