-
Notifications
You must be signed in to change notification settings - Fork 23
/
commands.go
218 lines (178 loc) · 6.05 KB
/
commands.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package cmd
import (
"context"
"github.com/synapsecns/sanguine/agents/agents/notary/db"
"github.com/synapsecns/sanguine/agents/agents/notary/db/sql/mysql"
"github.com/synapsecns/sanguine/agents/agents/notary/db/sql/sqlite"
"github.com/synapsecns/sanguine/agents/agents/notary/metadata"
"github.com/synapsecns/sanguine/core/dbcommon"
"github.com/synapsecns/sanguine/core/metrics"
omnirpcClient "github.com/synapsecns/sanguine/services/omnirpc/client"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"gorm.io/gorm/schema"
"os"
"sync/atomic"
"time"
markdown "github.com/MichaelMure/go-term-markdown"
"github.com/hedzr/log"
"github.com/jftuga/termsize"
"github.com/phayes/freeport"
"github.com/synapsecns/sanguine/agents/agents/notary"
"github.com/synapsecns/sanguine/agents/agents/notary/api"
"golang.org/x/sync/errgroup"
// used to embed markdown.
_ "embed"
"fmt"
"github.com/synapsecns/sanguine/agents/config"
"github.com/synapsecns/sanguine/core"
"github.com/urfave/cli/v2"
)
//go:embed cmd.md
var help string
// NotaryInfoCommand gets info about using the notary agent.
var NotaryInfoCommand = &cli.Command{
Name: "notary-info",
Description: "learn how to use notary cli",
Action: func(c *cli.Context) error {
fmt.Println(string(markdown.Render(help, termsize.Width(), 6)))
return nil
},
}
var metricsPortFlag = &cli.UintFlag{
Name: "metrics-port",
Usage: "--port 5121",
Value: 0,
}
var configFlag = &cli.StringFlag{
Name: "config",
Usage: "--config /Users/synapsecns/notary_config.yaml",
TakesFile: true,
Required: true,
}
var debugFlag = &cli.BoolFlag{
Name: "debug",
Usage: "--debug",
}
func createNotaryParameters(ctx context.Context, c *cli.Context, metrics metrics.Handler) (notaryConfig config.AgentConfig, notaryDB db.NotaryDB, err error) {
notaryConfig, err = config.DecodeAgentConfig(core.ExpandOrReturnPath(c.String(configFlag.Name)))
if err != nil {
return notaryConfig, nil, fmt.Errorf("failed to decode config: %w", err)
}
if notaryConfig.DBPrefix == "" && notaryConfig.DBConfig.Type == dbcommon.Mysql.String() {
notaryConfig.DBPrefix = "notary"
}
if notaryConfig.DBConfig.Type == dbcommon.Sqlite.String() {
notaryConfig.DBPrefix = ""
}
notaryDB, err = InitNotaryDB(
ctx,
notaryConfig.DBConfig.Type,
notaryConfig.DBConfig.Source,
notaryConfig.DBPrefix,
metrics,
)
if err != nil {
return notaryConfig, nil, fmt.Errorf("failed to init db: %w", err)
}
return notaryConfig, notaryDB, nil
}
// NotaryRunCommand runs the notary.
var NotaryRunCommand = &cli.Command{
Name: "notary-run",
Description: "runs the notary service",
Flags: []cli.Flag{configFlag, metricsPortFlag, debugFlag},
Action: func(c *cli.Context) error {
metricsProvider, err := metrics.NewFromEnv(c.Context, metadata.BuildInfo())
if err != nil {
return fmt.Errorf("failed to create metrics handler: %w", err)
}
notaryConfig, notaryDB, err := createNotaryParameters(c.Context, c, metricsProvider)
if err != nil {
return fmt.Errorf("failed to create notary parameters: %w", err)
}
var baseOmniRPCClient omnirpcClient.RPCClient
if debugFlag.IsSet() {
baseOmniRPCClient = omnirpcClient.NewOmnirpcClient(notaryConfig.BaseOmnirpcURL, metricsProvider, omnirpcClient.WithCaptureReqRes())
} else {
baseOmniRPCClient = omnirpcClient.NewOmnirpcClient(notaryConfig.BaseOmnirpcURL, metricsProvider)
}
var shouldRetryAtomic atomic.Bool
shouldRetryAtomic.Store(true)
for shouldRetryAtomic.Load() {
shouldRetryAtomic.Store(false)
g, _ := errgroup.WithContext(c.Context)
notary, err := notary.NewNotary(c.Context, notaryConfig, baseOmniRPCClient, notaryDB, metricsProvider)
if err != nil {
return fmt.Errorf("failed to create notary: %w", err)
}
g.Go(func() error {
err = notary.Start(c.Context)
if err != nil {
shouldRetryAtomic.Store(true)
log.Errorf("Error running notary, will sleep for a minute and retry: %v", err)
time.Sleep(60 * time.Second)
return fmt.Errorf("failed to create notary: %w", err)
}
return nil
})
g.Go(func() error {
err := api.Start(c.Context, uint16(c.Uint(metricsPortFlag.Name)))
if err != nil {
return fmt.Errorf("failed to start api: %w", err)
}
return nil
})
if err := g.Wait(); err != nil {
return fmt.Errorf("failed to run notary: %w", err)
}
}
return nil
},
}
func init() {
metricsPortFlag.Value = uint(freeport.GetPort())
}
// InitNotaryDB initializes a database given a database type and path.
//
//nolint:cyclop
func InitNotaryDB(parentCtx context.Context, database string, path string, tablePrefix string, handler metrics.Handler) (_ db.NotaryDB, err error) {
ctx, span := handler.Tracer().Start(parentCtx, "InitNotaryDB", trace.WithAttributes(
attribute.String("database", database),
attribute.String("path", path),
attribute.String("tablePrefix", tablePrefix),
))
defer func() {
metrics.EndSpanWithErr(span, err)
}()
switch {
case database == dbcommon.Sqlite.String():
sqliteStore, err := sqlite.NewSqliteStore(ctx, path, handler, false)
if err != nil {
return nil, fmt.Errorf("failed to create sqlite store: %w", err)
}
return sqliteStore, nil
case database == dbcommon.Mysql.String():
if os.Getenv("OVERRIDE_MYSQL") != "" {
dbname := os.Getenv("MYSQL_DATABASE")
connString := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?parseTime=true", core.GetEnv("MYSQL_USER", "root"), os.Getenv("MYSQL_PASSWORD"), core.GetEnv("MYSQL_HOST", "127.0.0.1"), core.GetEnvInt("MYSQL_PORT", 3306), dbname)
mysqlStore, err := mysql.NewMysqlStore(ctx, connString, handler)
if err != nil {
return nil, fmt.Errorf("failed to create mysql store: %w", err)
}
return mysqlStore, nil
}
if tablePrefix != "" {
mysql.NamingStrategy = schema.NamingStrategy{
TablePrefix: fmt.Sprintf("%s_", tablePrefix),
}
}
mysqlStore, err := mysql.NewMysqlStore(ctx, path, handler)
if err != nil {
return nil, fmt.Errorf("failed to create mysql store: %w", err)
}
return mysqlStore, nil
default:
return nil, fmt.Errorf("invalid database type: %s", database)
}
}