2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2019-01-02 18:07:49 +00:00
// See LICENSE for copying information.
package main
import (
2020-11-16 20:09:54 +00:00
"context"
"fmt"
2020-12-09 07:36:35 +00:00
"math"
2019-01-02 18:07:49 +00:00
"os"
2020-11-16 20:09:54 +00:00
"time"
2019-01-02 18:07:49 +00:00
"github.com/spf13/cobra"
2019-12-27 11:48:47 +00:00
"storj.io/common/fpath"
2020-11-16 20:09:54 +00:00
"storj.io/common/sync2"
2019-01-02 18:07:49 +00:00
)
2020-07-16 15:18:02 +01:00
// Flags contains different flags for commands.
2019-01-02 18:07:49 +00:00
type Flags struct {
Directory string
2019-01-08 15:24:15 +00:00
Host string
2019-01-02 18:07:49 +00:00
SatelliteCount int
StorageNodeCount int
Identities int
2019-03-12 12:51:06 +00:00
IsDev bool
2019-03-23 21:53:03 +00:00
OnlyEnv bool // only do things necessary for loading env vars
2019-05-14 16:13:18 +01:00
// Connection string for the postgres database to use for storj-sim processes
Postgres string
2019-11-01 19:48:37 +00:00
Redis string
2019-11-04 12:55:02 +00:00
// Value of first redis db
RedisStartDB int
2019-01-02 18:07:49 +00:00
}
var printCommands bool
func main ( ) {
cobra . EnableCommandSorting = false
var flags Flags
rootCmd := & cobra . Command {
2019-01-16 23:09:57 +00:00
Use : "storj-sim" ,
Short : "Storj Network Simulator" ,
2019-01-02 18:07:49 +00:00
}
defaultConfigDir := fpath . ApplicationDir ( "storj" , "local-network" )
2019-10-18 17:12:30 +01:00
2019-01-02 18:07:49 +00:00
configDir := defaultConfigDir
if os . Getenv ( "STORJ_NETWORK_DIR" ) != "" {
configDir = os . Getenv ( "STORJ_NETWORK_DIR" )
}
rootCmd . PersistentFlags ( ) . StringVarP ( & flags . Directory , "config-dir" , "" , configDir , "base project directory" )
2019-01-08 15:24:15 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & flags . Host , "host" , "" , "127.0.0.1" , "host to use for network" )
2019-01-02 18:07:49 +00:00
rootCmd . PersistentFlags ( ) . IntVarP ( & flags . SatelliteCount , "satellites" , "" , 1 , "number of satellites to start" )
rootCmd . PersistentFlags ( ) . IntVarP ( & flags . StorageNodeCount , "storage-nodes" , "" , 10 , "number of storage nodes to start" )
rootCmd . PersistentFlags ( ) . IntVarP ( & flags . Identities , "identities" , "" , 10 , "number of identities to create" )
2019-01-08 15:24:15 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & printCommands , "print-commands" , "x" , false , "print commands as they are run" )
2021-01-05 11:13:27 +00:00
rootCmd . PersistentFlags ( ) . BoolVarP ( & flags . IsDev , "dev" , "" , true , "use configuration values tuned for development" )
2019-01-02 18:07:49 +00:00
2019-10-18 17:12:30 +01:00
rootCmd . PersistentFlags ( ) . StringVarP ( & flags . Postgres , "postgres" , "" , os . Getenv ( "STORJ_SIM_POSTGRES" ) , "connection string for postgres (defaults to STORJ_SIM_POSTGRES)" )
2019-11-04 12:55:02 +00:00
rootCmd . PersistentFlags ( ) . StringVarP ( & flags . Redis , "redis" , "" , os . Getenv ( "STORJ_SIM_REDIS" ) , "connection string for redis e.g. 127.0.0.1:6379 (defaults to STORJ_SIM_REDIS)" )
rootCmd . PersistentFlags ( ) . IntVarP ( & flags . RedisStartDB , "redis-startdb" , "" , 0 , "value of first redis db (defaults to 0)" )
2019-05-14 16:13:18 +01:00
2019-01-02 18:07:49 +00:00
networkCmd := & cobra . Command {
Use : "network" ,
Short : "local network for testing" ,
}
networkCmd . AddCommand (
& cobra . Command {
Use : "run" ,
Short : "run network" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
return networkExec ( & flags , args , "run" )
} ,
2019-03-23 21:53:03 +00:00
} , & cobra . Command {
2019-04-10 02:16:12 +01:00
Use : "env [name]" ,
2019-03-23 21:53:03 +00:00
Short : "print environment variables" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
return networkEnv ( & flags , args )
} ,
2019-01-02 18:07:49 +00:00
} , & cobra . Command {
Use : "setup" ,
Short : "setup network" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
return networkExec ( & flags , args , "setup" )
} ,
} , & cobra . Command {
Use : "test <command>" ,
Short : "run command with an actual network" ,
Args : cobra . MinimumNArgs ( 1 ) ,
RunE : func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
return networkTest ( & flags , args [ 0 ] , args [ 1 : ] )
} ,
} , & cobra . Command {
Use : "destroy" ,
Short : "destroys network if it exists" ,
RunE : func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
return networkDestroy ( & flags , args )
} ,
} ,
)
2020-11-16 20:09:54 +00:00
toolCmd := & cobra . Command {
Use : "tool" ,
Short : "tools for working with storj-sim" ,
}
toolCmd . AddCommand (
func ( ) * cobra . Command {
cmd := & cobra . Command {
Use : "wait-for <address>" ,
Short : "waits for an address to accept connections" ,
Args : cobra . ExactArgs ( 1 ) ,
}
retries := cmd . Flags ( ) . Int ( "retry" , - 1 , "maximum retry count" )
interval := cmd . Flags ( ) . Duration ( "interval" , 50 * time . Millisecond , "how long to wait after each retry" )
cmd . RunE = func ( cmd * cobra . Command , args [ ] string ) ( err error ) {
ctx , cancel := NewCLIContext ( context . Background ( ) )
defer cancel ( )
defer fmt . Println ( )
target := args [ 0 ]
if * retries <= 0 {
2020-12-09 07:36:35 +00:00
* retries = math . MaxInt32
2020-11-16 20:09:54 +00:00
}
for try := 0 ; try < * retries ; try ++ {
if tryConnect ( target ) {
return nil
}
fmt . Print ( "." )
if ! sync2 . Sleep ( ctx , * interval ) {
return ctx . Err ( )
}
}
return fmt . Errorf ( "failed to connect to %q" , target )
}
return cmd
} ( ) ,
)
2019-01-02 18:07:49 +00:00
rootCmd . AddCommand (
networkCmd ,
2020-11-16 20:09:54 +00:00
toolCmd ,
2019-01-02 18:07:49 +00:00
)
rootCmd . SilenceUsage = true
err := rootCmd . Execute ( )
if err != nil {
os . Exit ( 1 )
}
}