diff --git a/README.md b/README.md index 0fc25a5..3bb9f10 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,24 @@ Motion is a service to propel data onto FileCoin network via a simple easy to use API. It aims to create an easy path for independent software vendors to integrate FileCoin as a storage layer. +## Usage + +```text +$ motion --help +NAME: + motion - Propelling data onto FileCoin + +USAGE: + motion [global options] command [command options] [arguments...] + +COMMANDS: + help, h Shows a list of commands or help for one command + +GLOBAL OPTIONS: + --storeDir value The path at which to store Motion data (default: OS Temporary directory) [$MOTION_STORE_DIR] + --help, -h show help +``` + ## Run Server Locally ### Prerequisites @@ -17,7 +35,7 @@ To start the motion API server run: docker run --rm -p 40080:40080 ghcr.io/filecoin-project/motion:main ``` The above starts the Motion HTTP API exposed on default listen address: http://localhost:40080. -It uses a temporary directory to store blobls in a flat file format. +It uses a temporary directory to store blobs in a flat file format. ### Store blobs @@ -46,6 +64,17 @@ fish Alternatively, you can browse the same URL in a web browser, which should prompt you to download the binary file. +### Configure local store directory + +To configure the local storage directory, set `--storeDir` flag as an argument to the container. +This will override the directy path used by Motion to store files locally. +The path can be a [mounted volume](https://docs.docker.com/storage/volumes/), which then allows you to retain the stored files after the container +is restarted and restore them back. For example, the following command uses a directory named `store` in the current working directory mounted as a volume on Motion container at path `/store`: + +```shell +docker run --rm -p 40080:40080 -v $PWD/store:/store ghcr.io/filecoin-project/motion:main --storeDir=/store +``` + ### Check the status of an uploaded blob Not yet implemented. diff --git a/blob/local_store.go b/blob/local_store.go index 0576884..4f96828 100644 --- a/blob/local_store.go +++ b/blob/local_store.go @@ -34,7 +34,7 @@ func (l *LocalStore) Put(_ context.Context, reader io.ReadCloser) (*Descriptor, if err != nil { return nil, err } - dest, err := os.CreateTemp("", "motion_local_store_*.bin") + dest, err := os.CreateTemp(l.dir, "motion_local_store_*.bin.temp") if err != nil { return nil, err } diff --git a/cmd/motion/main.go b/cmd/motion/main.go index 75f7b2b..c366873 100644 --- a/cmd/motion/main.go +++ b/cmd/motion/main.go @@ -1,38 +1,60 @@ package main import ( - "context" "os" "os/signal" "github.com/filecoin-project/motion" + "github.com/filecoin-project/motion/blob" "github.com/ipfs/go-log/v2" + "github.com/urfave/cli/v2" ) var logger = log.Logger("motion/cmd") func main() { - // TODO: add flags, options and all that jazz if _, set := os.LookupEnv("GOLOG_LOG_LEVEL"); !set { _ = log.SetLogLevel("*", "INFO") } - m, err := motion.New() - if err != nil { - logger.Fatalw("Failed to instantiate Motion", "err", err) - } - ctx := context.Background() + app := cli.App{ + Name: "motion", + Usage: "Propelling data onto FileCoin", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "storeDir", + Usage: "The path at which to store Motion data", + DefaultText: "OS Temporary directory", + Value: os.TempDir(), + EnvVars: []string{"MOTION_STORE_DIR"}, + }, + }, + Action: func(cctx *cli.Context) error { + storeDir := cctx.String("storeDir") + logger.Infow("Using local blob store", "storeDir", storeDir) + m, err := motion.New( + motion.WithBlobStore(blob.NewLocalStore(storeDir)), + ) + if err != nil { + logger.Fatalw("Failed to instantiate Motion", "err", err) + } + ctx := cctx.Context - if err := m.Start(ctx); err != nil { - logger.Fatalw("Failed to start Motion", "err", err) + if err := m.Start(ctx); err != nil { + logger.Fatalw("Failed to start Motion", "err", err) + } + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + <-c + logger.Info("Terminating...") + if err := m.Shutdown(ctx); err != nil { + logger.Warnw("Failure occurred while shutting down Motion.", "err", err) + return err + } + logger.Info("Shut down Motion successfully.") + return nil + }, } - - c := make(chan os.Signal, 1) - signal.Notify(c, os.Interrupt) - <-c - logger.Info("Terminating...") - if err := m.Shutdown(ctx); err != nil { - logger.Warnw("Failure occurred while shutting down Motion.", "err", err) - } else { - logger.Info("Shut down Motion successfully.") + if err := app.Run(os.Args); err != nil { + logger.Error(err) } } diff --git a/go.mod b/go.mod index 205f9f0..23ce9c3 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,14 @@ go 1.20 require ( github.com/google/uuid v1.3.0 github.com/ipfs/go-log/v2 v2.5.1 + github.com/urfave/cli/v2 v2.25.7 ) require ( + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect github.com/mattn/go-isatty v0.0.14 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect go.uber.org/zap v1.19.1 // indirect diff --git a/go.sum b/go.sum index d5aa7c4..da39ced 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -16,10 +18,16 @@ github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs= +github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU= +github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= @@ -62,5 +70,5 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=