2018-05-15 02:55:45 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/lbryio/reflector.go/db"
|
2019-07-02 18:13:20 +02:00
|
|
|
"github.com/lbryio/reflector.go/reflector"
|
2018-05-15 02:55:45 +02:00
|
|
|
"github.com/lbryio/reflector.go/store"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-08-08 02:19:04 +02:00
|
|
|
var uploadWorkers int
|
|
|
|
var uploadSkipExistsCheck bool
|
2020-05-02 20:31:10 +02:00
|
|
|
var uploadDeleteBlobsAfterUpload bool
|
2018-05-15 19:24:13 +02:00
|
|
|
|
2018-05-15 02:55:45 +02:00
|
|
|
func init() {
|
|
|
|
var cmd = &cobra.Command{
|
2018-08-08 02:19:04 +02:00
|
|
|
Use: "upload PATH",
|
2018-05-15 02:55:45 +02:00
|
|
|
Short: "Upload blobs to S3",
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
Run: uploadCmd,
|
|
|
|
}
|
2018-08-08 02:19:04 +02:00
|
|
|
cmd.PersistentFlags().IntVar(&uploadWorkers, "workers", 1, "How many worker threads to run at once")
|
|
|
|
cmd.PersistentFlags().BoolVar(&uploadSkipExistsCheck, "skipExistsCheck", false, "Dont check if blobs exist before uploading")
|
2020-05-02 20:31:10 +02:00
|
|
|
cmd.PersistentFlags().BoolVar(&uploadDeleteBlobsAfterUpload, "deleteBlobsAfterUpload", false, "Delete blobs after uploading them")
|
2018-05-30 03:38:55 +02:00
|
|
|
rootCmd.AddCommand(cmd)
|
2018-05-15 02:55:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func uploadCmd(cmd *cobra.Command, args []string) {
|
|
|
|
db := new(db.SQL)
|
2018-05-30 03:38:55 +02:00
|
|
|
err := db.Connect(globalConfig.DBConn)
|
2018-05-15 02:55:45 +02:00
|
|
|
checkErr(err)
|
|
|
|
|
2019-10-03 22:58:17 +02:00
|
|
|
st := store.NewDBBackedStore(
|
2019-07-02 18:13:20 +02:00
|
|
|
store.NewS3BlobStore(globalConfig.AwsID, globalConfig.AwsSecret, globalConfig.BucketRegion, globalConfig.BucketName),
|
|
|
|
db)
|
2018-05-15 02:55:45 +02:00
|
|
|
|
2020-05-02 20:31:10 +02:00
|
|
|
uploader := reflector.NewUploader(db, st, uploadWorkers, uploadSkipExistsCheck, uploadDeleteBlobsAfterUpload)
|
2018-05-15 02:55:45 +02:00
|
|
|
|
2018-05-30 03:38:55 +02:00
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-interruptChan
|
2019-07-02 18:13:20 +02:00
|
|
|
uploader.Stop()
|
2018-05-30 03:38:55 +02:00
|
|
|
}()
|
|
|
|
|
2019-07-02 18:13:20 +02:00
|
|
|
err = uploader.Upload(args[0])
|
|
|
|
checkErr(err)
|
2018-05-30 03:38:55 +02:00
|
|
|
}
|