Skip to content

Commit

Permalink
merge uplink and storj (#323)
Browse files Browse the repository at this point in the history
also remove testing package from import dependency chain
(testing defines some flags)
  • Loading branch information
jtolio committed Sep 7, 2018
1 parent 1ea3f9f commit 8f3398b
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 169 deletions.
13 changes: 0 additions & 13 deletions cmd/storj/main.go

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions cmd/storj/cmd/root.go → cmd/uplink/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
minio "github.com/minio/minio/cmd"
"github.com/minio/minio/pkg/auth"
"github.com/spf13/cobra"

"storj.io/storj/pkg/miniogw"
)

const defaultConfDir = "$HOME/.storj/cli"
const defaultConfDir = "$HOME/.storj/uplink"

// Config is miniogw.Config configuration
type Config struct {
Expand Down Expand Up @@ -45,6 +46,6 @@ func getStorjObjects(ctx context.Context, cfg Config) (minio.ObjectLayer, error)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "storj",
Short: "A brief description of your application",
Use: "uplink",
Short: "The Storj client-side S3 gateway and CLI",
}
37 changes: 37 additions & 0 deletions cmd/uplink/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.

package cmd

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"

"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/process"
)

var (
runCfg Config
runCmd = &cobra.Command{
Use: "run",
Short: "Run the S3 gateway",
RunE: cmdRun,
}
)

func init() {
RootCmd.AddCommand(runCmd)
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
runCmd.Flags().String("config", filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
}

func cmdRun(cmd *cobra.Command, args []string) (err error) {
for _, flagname := range args {
return fmt.Errorf("%s - Invalid flag. Try 'uplink run'", flagname)
}

return runCfg.Run(process.Ctx(cmd))
}
54 changes: 46 additions & 8 deletions cmd/storj/cmd/setup.go → cmd/uplink/cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
package cmd

import (
"crypto/rand"
"fmt"
"os"
"path/filepath"

base58 "github.com/jbenet/go-base58"
"github.com/spf13/cobra"

"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/provider"
Expand All @@ -21,10 +24,12 @@ var (
RunE: cmdSetup,
}
setupCfg struct {
CA provider.CASetupConfig
Identity provider.IdentitySetupConfig
BasePath string `default:"$CONFDIR" help:"base path for setup"`
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
CA provider.CASetupConfig
Identity provider.IdentitySetupConfig
BasePath string `default:"$CONFDIR" help:"base path for setup"`
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
SatelliteAddr string `default:"localhost:7778" help:"the address to use for the satellite"`
APIKey string `default:"" help:"the api key to use for the satellite"`
}
)

Expand All @@ -34,9 +39,18 @@ func init() {
}

func cmdSetup(cmd *cobra.Command, args []string) (err error) {
setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath)
if err != nil {
return err
}

for _, flagname := range args {
return fmt.Errorf("%s - Invalid flag. Pleas see --help", flagname)
}

_, err = os.Stat(setupCfg.BasePath)
if !setupCfg.Overwrite && err == nil {
fmt.Println("A cli configuration already exists. Rerun with --overwrite")
fmt.Println("An uplink configuration already exists. Rerun with --overwrite")
return nil
}

Expand All @@ -58,11 +72,35 @@ func cmdSetup(cmd *cobra.Command, args []string) (err error) {
return err
}

accessKey, err := generateAWSKey()
if err != nil {
return err
}

secretKey, err := generateAWSKey()
if err != nil {
return err
}

o := map[string]interface{}{
"cert-path": setupCfg.Identity.CertPath,
"key-path": setupCfg.Identity.KeyPath,
"cert-path": setupCfg.Identity.CertPath,
"key-path": setupCfg.Identity.KeyPath,
"api-key": setupCfg.APIKey,
"pointer-db-addr": setupCfg.SatelliteAddr,
"overlay-addr": setupCfg.SatelliteAddr,
"access-key": accessKey,
"secret-key": secretKey,
}

return process.SaveConfig(cpCmd.Flags(),
return process.SaveConfig(runCmd.Flags(),
filepath.Join(setupCfg.BasePath, "config.yaml"), o)
}

func generateAWSKey() (key string, err error) {
var buf [20]byte
_, err = rand.Read(buf[:])
if err != nil {
return "", err
}
return base58.Encode(buf[:]), nil
}
140 changes: 2 additions & 138 deletions cmd/uplink/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,146 +4,10 @@
package main

import (
"crypto/rand"
"fmt"
"os"
"path/filepath"

base58 "github.com/jbenet/go-base58"
"github.com/spf13/cobra"

"storj.io/storj/pkg/cfgstruct"
"storj.io/storj/pkg/miniogw"
"storj.io/storj/cmd/uplink/cmd"
"storj.io/storj/pkg/process"
"storj.io/storj/pkg/provider"
)

var (
rootCmd = &cobra.Command{
Use: "uplink",
Short: "Uplink",
}
runCmd = &cobra.Command{
Use: "run",
Short: "Run the uplink",
RunE: cmdRun,
}
setupCmd = &cobra.Command{
Use: "setup",
Short: "Create config files",
RunE: cmdSetup,
}

runCfg miniogw.Config
setupCfg struct {
CA provider.CASetupConfig
Identity provider.IdentitySetupConfig
BasePath string `default:"$CONFDIR" help:"base path for setup"`
// Concurrency uint `default:"4" help:"number of concurrent workers for certificate authority generation"`
Overwrite bool `default:"false" help:"whether to overwrite pre-existing configuration files"`
SatelliteAddr string `default:"localhost:7778" help:"the address to use for the satellite"`
APIKey string `default:"" help:"the api key to use for the satellite"`
}

defaultConfDir = "$HOME/.storj/uplink"
)

func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(setupCmd)
cfgstruct.Bind(runCmd.Flags(), &runCfg, cfgstruct.ConfDir(defaultConfDir))
cfgstruct.Bind(setupCmd.Flags(), &setupCfg, cfgstruct.ConfDir(defaultConfDir))
}

func cmdRun(cmd *cobra.Command, args []string) (err error) {
/* check for the supported flags */
flagsupported := cmd.Flags()
for _, flagname := range args {
f := flagsupported.Lookup(flagname)
if f == nil {
fmt.Println(flagname + " - Invalid flag. try 'uplink run'")
return nil
}
}

return runCfg.Run(process.Ctx(cmd))
}

func cmdSetup(cmd *cobra.Command, args []string) (err error) {
setupCfg.BasePath, err = filepath.Abs(setupCfg.BasePath)
if err != nil {
return err
}

/* check for the supported flags */
flagsupported := cmd.Flags()
for _, flagname := range args {
f := flagsupported.Lookup(flagname)
if f == nil {
fmt.Println(flagname + " - Invalid flag. Supported list use --help")
return nil
}
}

_, err = os.Stat(setupCfg.BasePath)
if !setupCfg.Overwrite && err == nil {
fmt.Println("An uplink configuration already exists. Rerun with --overwrite")
return nil
}

err = os.MkdirAll(setupCfg.BasePath, 0700)
if err != nil {
return err
}

// TODO: handle setting base path *and* identity file paths via args
// NB: if base path is set this overrides identity and CA path options
if setupCfg.BasePath != defaultConfDir {
setupCfg.CA.CertPath = filepath.Join(setupCfg.BasePath, "ca.cert")
setupCfg.CA.KeyPath = filepath.Join(setupCfg.BasePath, "ca.key")
setupCfg.Identity.CertPath = filepath.Join(setupCfg.BasePath, "identity.cert")
setupCfg.Identity.KeyPath = filepath.Join(setupCfg.BasePath, "identity.key")
}
err = provider.SetupIdentity(process.Ctx(cmd), setupCfg.CA, setupCfg.Identity)
if err != nil {
return err
}

accessKey, err := generateAWSKey()
if err != nil {
return err
}

secretKey, err := generateAWSKey()
if err != nil {
return err
}

o := map[string]interface{}{
"cert-path": setupCfg.Identity.CertPath,
"key-path": setupCfg.Identity.KeyPath,
"api-key": setupCfg.APIKey,
"pointer-db-addr": setupCfg.SatelliteAddr,
"overlay-addr": setupCfg.SatelliteAddr,
"access-key": accessKey,
"secret-key": secretKey,
}

return process.SaveConfig(runCmd.Flags(),
filepath.Join(setupCfg.BasePath, "config.yaml"), o)
}

func generateAWSKey() (key string, err error) {
var buf [20]byte
_, err = rand.Read(buf[:])
if err != nil {
return "", err
}
return base58.Encode(buf[:]), nil
}

func main() {
runCmd.Flags().String("config",
filepath.Join(defaultConfDir, "config.yaml"), "path to configuration")
process.Exec(rootCmd)
process.Exec(cmd.RootCmd)
}
7 changes: 0 additions & 7 deletions storage/storelogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ package storelogger
import (
"strconv"
"sync/atomic"
"testing"

"storj.io/storj/storage"

"go.uber.org/zap"
"go.uber.org/zap/zaptest"
)

var id int64
Expand All @@ -29,11 +27,6 @@ func New(log *zap.Logger, store storage.KeyValueStore) *Logger {
return &Logger{log.Named(name), store}
}

// NewTest creates a logger for testing
func NewTest(t *testing.T, store storage.KeyValueStore) *Logger {
return New(zaptest.NewLogger(t), store)
}

// Put adds a value to store
func (store *Logger) Put(key storage.Key, value storage.Value) error {
store.log.Debug("Put", zap.String("key", string(key)), zap.Binary("value", []byte(value)))
Expand Down

0 comments on commit 8f3398b

Please sign in to comment.