-
Notifications
You must be signed in to change notification settings - Fork 77
/
run.go
144 lines (123 loc) · 3.21 KB
/
run.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
package cmd
import (
"crypto/tls"
log "github.com/Sirupsen/logrus"
"github.com/kelseyhightower/envconfig"
"github.com/luizalabs/teresa/pkg/server"
"github.com/luizalabs/teresa/pkg/server/auth"
"github.com/luizalabs/teresa/pkg/server/deploy"
"github.com/luizalabs/teresa/pkg/server/k8s"
"github.com/luizalabs/teresa/pkg/server/secrets"
"github.com/luizalabs/teresa/pkg/server/storage"
"github.com/spf13/cobra"
)
var runCmd = &cobra.Command{
Use: "run",
Short: "Start Teresa gRPC server",
Run: runServer,
}
func init() {
RootCmd.AddCommand(runCmd)
runCmd.Flags().String("port", "50051", "TCP port to create a listener")
runCmd.Flags().Bool("tls", false, "enable TLS")
runCmd.Flags().Bool("debug", false, "enable debug mode")
}
func runServer(cmd *cobra.Command, args []string) {
port, err := cmd.Flags().GetString("port")
if err != nil {
log.WithError(err).Fatal("invalid port parameter")
}
useTLS, err := cmd.Flags().GetBool("tls")
if err != nil {
log.WithError(err).Fatal("invalid tls parameter")
}
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
log.WithError(err).Fatal("invalid debug parameter")
}
db, err := getDB()
if err != nil {
log.WithError(err).Fatal("failed to connect to database")
}
st, err := getStorage()
if err != nil {
log.WithError(err).Fatal("failed to configure storage")
}
kc, err := getK8s()
if err != nil {
log.WithError(err).Fatal("failed to configure k8s client")
}
sec, err := getSecrets()
if err != nil {
log.WithError(err).Fatal("failed to get secrets data")
}
a, err := getAuth(sec)
if err != nil {
log.WithError(err).Fatal("failed to get auth data")
}
var tlsCert *tls.Certificate
if useTLS {
tlsCert, err = sec.TLSCertificate()
if err != nil {
log.WithError(err).Fatal("failed to get TLS cert")
}
}
deployOpt, err := getDeployOpt()
if err != nil {
log.Fatal("Error getting deploy configuration:", err)
}
s, err := server.New(server.Options{
Port: port,
Auth: a,
DB: db,
TLSCert: tlsCert,
Storage: st,
K8s: kc,
DeployOpt: deployOpt,
Debug: debug,
})
if err != nil {
log.WithError(err).Fatal("failed to create server")
}
log.Info("starting teresa-server on port ", port)
log.Info(s.Run())
}
func getSecrets() (secrets.Secrets, error) {
conf := new(secrets.FileSystemSecretsConfig)
if err := envconfig.Process("teresa_secrets", conf); err != nil {
return nil, err
}
return secrets.NewFileSystemSecrets(conf)
}
func getAuth(s secrets.Secrets) (auth.Auth, error) {
private, err := s.PrivateKey()
if err != nil {
return nil, err
}
public, err := s.PublicKey()
if err != nil {
return nil, err
}
return auth.New(private, public), nil
}
func getStorage() (storage.Storage, error) {
conf := new(storage.Config)
if err := envconfig.Process("teresa_storage", conf); err != nil {
return nil, err
}
return storage.New(conf)
}
func getK8s() (*k8s.Client, error) {
conf := new(k8s.Config)
if err := envconfig.Process("teresa_k8s", conf); err != nil {
return nil, err
}
return k8s.New(conf)
}
func getDeployOpt() (*deploy.Options, error) {
conf := new(deploy.Options)
if err := envconfig.Process("teresa_deploy", conf); err != nil {
return nil, err
}
return conf, nil
}