-
Notifications
You must be signed in to change notification settings - Fork 390
/
main.go
170 lines (138 loc) · 4.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package main
import (
"context"
"fmt"
"math"
"os"
"time"
"github.com/spf13/cobra"
"storj.io/common/fpath"
"storj.io/common/sync2"
)
// Flags contains different flags for commands.
type Flags struct {
Directory string
Host string
SatelliteCount int
StorageNodeCount int
Identities int
NoGateways bool
IsDev bool
FailFast bool
OnlyEnv bool // only do things necessary for loading env vars
// Connection string for the postgres database to use for storj-sim processes
Postgres string
Redis string
// Value of first redis db
RedisStartDB int
}
var printCommands bool
func main() {
cobra.EnableCommandSorting = false
var flags Flags
rootCmd := &cobra.Command{
Use: "storj-sim",
Short: "Storj Network Simulator",
}
defaultConfigDir := fpath.ApplicationDir("storj", "local-network")
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")
rootCmd.PersistentFlags().StringVarP(&flags.Host, "host", "", "127.0.0.1", "host to use for network")
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")
rootCmd.PersistentFlags().BoolVarP(&flags.NoGateways, "no-gateways", "", false, "whether to disable gateway creation for each satellite")
rootCmd.PersistentFlags().BoolVarP(&printCommands, "print-commands", "x", false, "print commands as they are run")
rootCmd.PersistentFlags().BoolVarP(&flags.IsDev, "dev", "", true, "use configuration values tuned for development")
rootCmd.PersistentFlags().BoolVarP(&flags.FailFast, "failfast", "", true, "stop all processes when one of the processes fails")
rootCmd.PersistentFlags().StringVarP(&flags.Postgres, "postgres", "", os.Getenv("STORJ_SIM_POSTGRES"), "connection string for postgres (defaults to STORJ_SIM_POSTGRES)")
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)")
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")
},
}, &cobra.Command{
Use: "env [name]",
Short: "print environment variables",
RunE: func(cmd *cobra.Command, args []string) (err error) {
return networkEnv(&flags, args)
},
}, &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)
},
},
)
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 {
*retries = math.MaxInt32
}
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
}(),
)
rootCmd.AddCommand(
networkCmd,
toolCmd,
)
rootCmd.SilenceUsage = true
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}