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-10-10 16:50:33 +02:00
|
|
|
"github.com/lbryio/lbry.go/v2/extras/errors"
|
2019-07-10 15:46:54 +02:00
|
|
|
"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-12-11 13:13:30 +01:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-07-10 15:46:54 +02:00
|
|
|
|
2020-06-11 18:45:56 +02:00
|
|
|
"github.com/lbryio/ytsync/v5/util"
|
2019-07-10 15:46:54 +02:00
|
|
|
)
|
|
|
|
|
2019-08-28 15:14:32 +02:00
|
|
|
var dbHandle *db.SQL
|
|
|
|
|
2019-07-10 15:46:54 +02:00
|
|
|
func ReflectAndClean() error {
|
|
|
|
err := reflectBlobs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-04 00:34:48 +02:00
|
|
|
return util.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
|
|
|
|
}
|
2020-11-04 18:13:30 +01:00
|
|
|
logrus.Infoln("reflecting blobs...")
|
2019-07-10 15:46:54 +02:00
|
|
|
//make sure lbrynet is off
|
2019-08-04 00:34:48 +02:00
|
|
|
running, err := util.IsLbrynetRunning()
|
2019-07-10 15:46:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if running {
|
|
|
|
return errors.Prefix("cannot reflect blobs as the daemon is running", err)
|
|
|
|
}
|
2019-12-11 13:13:30 +01:00
|
|
|
logrus.SetLevel(logrus.InfoLevel)
|
|
|
|
defer logrus.SetLevel(logrus.DebugLevel)
|
2019-07-10 15:46:54 +02:00
|
|
|
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)
|
|
|
|
}
|
2019-08-28 15:14:32 +02:00
|
|
|
if dbHandle == nil {
|
|
|
|
dbHandle = new(db.SQL)
|
|
|
|
err = dbHandle.Connect(config.DBConn)
|
2019-07-10 22:23:57 +02:00
|
|
|
if err != nil {
|
2019-08-28 15:14:32 +02:00
|
|
|
return errors.Err(err)
|
2019-07-10 22:23:57 +02:00
|
|
|
}
|
2019-08-28 15:14:32 +02:00
|
|
|
}
|
2021-04-12 23:31:37 +02:00
|
|
|
st := store.NewDBBackedStore(store.NewS3Store(config.AwsID, config.AwsSecret, config.BucketRegion, config.BucketName), dbHandle, false)
|
2019-07-10 15:46:54 +02:00
|
|
|
|
|
|
|
uploadWorkers := 10
|
2020-09-01 20:27:39 +02:00
|
|
|
uploader := reflector.NewUploader(dbHandle, st, uploadWorkers, false, false)
|
2019-07-10 15:46:54 +02:00
|
|
|
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
|
|
|
|
}
|