Files
nigiri/cli/controller/docker.go
Pietralberto Mazza e02c2fdd0d Changes for new version of esplora image (#62)
* changes for new version of esplora image

* add electrs port and esplora url env vars in compose yaml files

* wrap viper methods into Config type and use constants package

* add controller to interact with nigiri resources:
 * .env for docker-compose
 * docker daemon
 * json config file

* add use of constants and config packages and change start flag from --port to --env

* add package for global constants and variables

* add use of controller and constants packages instead of local methods and vars

* bump version

* use contants in logs command tests
2019-12-09 15:58:32 +01:00

102 lines
2.0 KiB
Go

package controller
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/vulpemventures/nigiri/cli/constants"
)
// Docker type handles interfaction with containers via docker and docker-compose
type Docker struct {
cli *client.Client
}
// New initialize a new Docker handler
func (d *Docker) New() error {
cli, err := client.NewEnvClient()
if err != nil {
return err
}
d.cli = cli
return nil
}
func (d *Docker) isDockerRunning() bool {
_, err := d.cli.ContainerList(context.Background(), types.ContainerListOptions{All: false})
if err != nil {
return false
}
return true
}
func (d *Docker) findNigiriContainers(listAllContainers bool) bool {
containers, _ := d.cli.ContainerList(context.Background(), types.ContainerListOptions{All: listAllContainers})
if len(containers) <= 0 {
return false
}
images := []string{}
for _, c := range containers {
images = append(images, c.Image)
}
for _, nigiriImage := range constants.NigiriImages {
// just check if services for bitcoin chain are up and running
if !strings.Contains(nigiriImage, "liquid") {
if !contains(images, nigiriImage) {
return false
}
}
}
return true
}
func (d *Docker) isNigiriRunning() bool {
return d.findNigiriContainers(false)
}
func (d *Docker) isNigiriStopped() bool {
isRunning := d.isNigiriRunning()
if !isRunning {
return d.findNigiriContainers(true)
}
return false
}
func (d *Docker) cleanVolumes(path string) error {
subdirs, err := ioutil.ReadDir(path)
if err != nil {
return err
}
for _, d := range subdirs {
path := filepath.Join(path, d.Name())
subsubdirs, _ := ioutil.ReadDir(path)
for _, sd := range subsubdirs {
if sd.IsDir() {
if err := os.RemoveAll(filepath.Join(path, sd.Name())); err != nil {
return err
}
}
}
}
return nil
}
func contains(list []string, elem string) bool {
for _, l := range list {
if l == elem {
return true
}
}
return false
}