2019-07-10 15:46:54 +02:00
|
|
|
package blobs_reflector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-07-31 05:32:02 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-07-10 15:46:54 +02:00
|
|
|
"github.com/lbryio/lbry.go/extras/errors"
|
|
|
|
"github.com/lbryio/reflector.go/cmd"
|
|
|
|
"github.com/lbryio/reflector.go/db"
|
|
|
|
"github.com/lbryio/reflector.go/reflector"
|
|
|
|
"github.com/lbryio/reflector.go/store"
|
2019-07-31 05:32:02 +02:00
|
|
|
"github.com/lbryio/ytsync/util"
|
2019-07-10 15:46:54 +02:00
|
|
|
"github.com/mitchellh/go-ps"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ReflectAndClean() error {
|
|
|
|
err := reflectBlobs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-10 17:27:46 +02:00
|
|
|
return cleanupLbrynet()
|
2019-07-10 15:46:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig(path string) (cmd.Config, error) {
|
|
|
|
var c cmd.Config
|
|
|
|
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return c, errors.Err("config file not found")
|
|
|
|
}
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(raw, &c)
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func reflectBlobs() error {
|
2019-08-01 04:42:22 +02:00
|
|
|
if util.IsBlobReflectionOff() {
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-10 15:46:54 +02:00
|
|
|
//make sure lbrynet is off
|
|
|
|
running, err := isLbrynetRunning()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if running {
|
|
|
|
return errors.Prefix("cannot reflect blobs as the daemon is running", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbHandle := new(db.SQL)
|
|
|
|
ex, err := os.Executable()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
exPath := filepath.Dir(ex)
|
2019-07-10 18:04:05 +02:00
|
|
|
config, err := loadConfig(exPath + "/prism_config.json")
|
2019-07-10 15:46:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
err = dbHandle.Connect(config.DBConn)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2019-07-10 22:23:57 +02:00
|
|
|
defer func() {
|
|
|
|
err := dbHandle.CloseDB()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed to close db handle: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
}()
|
2019-07-10 15:46:54 +02:00
|
|
|
st := store.NewDBBackedS3Store(
|
|
|
|
store.NewS3BlobStore(config.AwsID, config.AwsSecret, config.BucketRegion, config.BucketName),
|
|
|
|
dbHandle)
|
|
|
|
|
|
|
|
uploadWorkers := 10
|
|
|
|
uploader := reflector.NewUploader(dbHandle, st, uploadWorkers, false)
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
blobsDir := usr.HomeDir + "/.lbrynet/blobfiles/"
|
|
|
|
err = uploader.Upload(blobsDir)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2019-07-10 17:39:58 +02:00
|
|
|
if uploader.GetSummary().Err > 0 {
|
|
|
|
return errors.Err("not al blobs were reflected. Errors: %d", uploader.GetSummary().Err)
|
|
|
|
}
|
2019-07-10 15:46:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanupLbrynet() error {
|
|
|
|
//make sure lbrynet is off
|
|
|
|
running, err := isLbrynetRunning()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if running {
|
|
|
|
return errors.Prefix("cannot cleanup lbrynet as the daemon is running", err)
|
|
|
|
}
|
2019-07-31 05:32:02 +02:00
|
|
|
lbrynetDir := util.GetLBRYNetDir()
|
2019-07-10 15:46:54 +02:00
|
|
|
files, err := filepath.Glob(lbrynetDir + "lbrynet.sqlite*")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
err = os.Remove(f)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 05:32:02 +02:00
|
|
|
blobsDir := util.GetBlobsDir()
|
2019-07-10 17:42:28 +02:00
|
|
|
err = os.RemoveAll(blobsDir)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
|
|
|
err = os.Mkdir(blobsDir, 0755)
|
2019-07-10 17:27:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.Err(err)
|
|
|
|
}
|
2019-07-10 15:46:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isLbrynetRunning() (bool, error) {
|
2019-07-31 05:32:02 +02:00
|
|
|
if util.IsUsingDocker() {
|
2019-08-01 04:42:22 +02:00
|
|
|
container, err := util.GetLBRYNetContainer(util.ONLINE)
|
2019-07-31 05:32:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return container != nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-10 15:46:54 +02:00
|
|
|
processes, err := ps.Processes()
|
|
|
|
if err != nil {
|
|
|
|
return true, errors.Err(err)
|
|
|
|
}
|
|
|
|
var daemonProcessId = -1
|
|
|
|
for _, p := range processes {
|
|
|
|
if p.Executable() == "lbrynet" {
|
|
|
|
daemonProcessId = p.Pid()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
running := daemonProcessId != -1
|
|
|
|
return running, nil
|
|
|
|
}
|