2019-07-31 05:32:02 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
|
2019-08-01 04:42:22 +02:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/lbrycrd"
|
|
|
|
|
2019-07-31 05:32:02 +02:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
"github.com/docker/docker/client"
|
2019-08-01 04:42:22 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-07-31 05:32:02 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetBlobsDir() string {
|
|
|
|
blobsDir := os.Getenv("BLOBS_DIRECTORY")
|
|
|
|
if blobsDir == "" {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2019-08-01 04:42:22 +02:00
|
|
|
log.Error(err.Error())
|
2019-07-31 05:32:02 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
blobsDir = usr.HomeDir + "/.lbrynet/blobfiles/"
|
|
|
|
}
|
|
|
|
|
|
|
|
return blobsDir
|
|
|
|
}
|
|
|
|
|
2019-08-01 04:42:22 +02:00
|
|
|
func IsBlobReflectionOff() bool {
|
|
|
|
return os.Getenv("REFLECT_BLOBS") == "false"
|
|
|
|
}
|
|
|
|
|
2019-07-31 05:32:02 +02:00
|
|
|
func GetLBRYNetDir() string {
|
|
|
|
lbrynetDir := os.Getenv("LBRYNET_DIR")
|
|
|
|
if lbrynetDir == "" {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return usr.HomeDir + "/.lbrynet/"
|
|
|
|
}
|
|
|
|
return lbrynetDir
|
|
|
|
}
|
|
|
|
|
2019-08-01 04:42:22 +02:00
|
|
|
const ALL = true
|
|
|
|
const ONLINE = false
|
|
|
|
|
2019-07-31 05:32:02 +02:00
|
|
|
func GetLBRYNetContainer(all bool) (*types.Container, error) {
|
2019-08-01 04:42:22 +02:00
|
|
|
return getDockerContainer("lbrynet", all)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDockerContainer(name string, all bool) (*types.Container, error) {
|
2019-07-31 05:32:02 +02:00
|
|
|
cli, err := client.NewEnvClient()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
filters := filters.NewArgs()
|
2019-08-01 04:42:22 +02:00
|
|
|
filters.Add("name", name)
|
2019-07-31 05:32:02 +02:00
|
|
|
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: all, Filters: filters})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
for _, container := range containers {
|
|
|
|
fmt.Printf("%s %s\n", container.ID[:10], container.Image)
|
|
|
|
}
|
|
|
|
if len(containers) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
if len(containers) > 1 {
|
2019-08-01 04:42:22 +02:00
|
|
|
return nil, errors.Err("more than one %s container found", name)
|
2019-07-31 05:32:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &containers[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func IsUsingDocker() bool {
|
|
|
|
return os.Getenv("LBRYNET_USE_DOCKER") == "true"
|
|
|
|
}
|
2019-08-01 04:42:22 +02:00
|
|
|
|
|
|
|
func IsRegTest() bool {
|
|
|
|
return os.Getenv("REGTEST") == "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLbrycrdClient(lbrycrdString string) (*lbrycrd.Client, error) {
|
|
|
|
var lbrycrdd *lbrycrd.Client
|
|
|
|
var err error
|
|
|
|
if lbrycrdString == "" {
|
|
|
|
lbrycrdd, err = lbrycrd.NewWithDefaultURL()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lbrycrdd, err = lbrycrd.New(lbrycrdString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lbrycrdd, nil
|
|
|
|
}
|