/
profile.go
190 lines (169 loc) · 5.5 KB
/
profile.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
// Package profile is for specific profiles
// @todo this package is the definition of cruft and
// should be rewritten in a more elegant way
package profile
import (
"fmt"
"os"
"github.com/micro/go-micro/v3/auth/jwt"
"github.com/micro/go-micro/v3/auth/noop"
"github.com/micro/go-micro/v3/broker"
"github.com/micro/go-micro/v3/broker/http"
"github.com/micro/go-micro/v3/client"
"github.com/micro/go-micro/v3/config"
memStream "github.com/micro/go-micro/v3/events/stream/memory"
"github.com/micro/go-micro/v3/registry"
"github.com/micro/go-micro/v3/registry/mdns"
"github.com/micro/go-micro/v3/registry/memory"
"github.com/micro/go-micro/v3/router"
regRouter "github.com/micro/go-micro/v3/router/registry"
"github.com/micro/go-micro/v3/router/static"
"github.com/micro/go-micro/v3/runtime/kubernetes"
"github.com/micro/go-micro/v3/runtime/local"
"github.com/micro/go-micro/v3/server"
"github.com/micro/go-micro/v3/store/file"
mem "github.com/micro/go-micro/v3/store/memory"
"github.com/micro/micro/v3/service/logger"
"github.com/urfave/cli/v2"
inAuth "github.com/micro/micro/v3/internal/auth"
"github.com/micro/micro/v3/internal/user"
microAuth "github.com/micro/micro/v3/service/auth"
microBroker "github.com/micro/micro/v3/service/broker"
microClient "github.com/micro/micro/v3/service/client"
microConfig "github.com/micro/micro/v3/service/config"
microEvents "github.com/micro/micro/v3/service/events"
microRegistry "github.com/micro/micro/v3/service/registry"
microRouter "github.com/micro/micro/v3/service/router"
microRuntime "github.com/micro/micro/v3/service/runtime"
microServer "github.com/micro/micro/v3/service/server"
microStore "github.com/micro/micro/v3/service/store"
)
// profiles which when called will configure micro to run in that environment
var profiles = map[string]*Profile{
// built in profiles
"client": Client,
"service": Service,
"test": Test,
"local": Local,
"kubernetes": Kubernetes,
}
// Profile configures an environment
type Profile struct {
// name of the profile
Name string
// function used for setup
Setup func(*cli.Context) error
// TODO: presetup dependencies
// e.g start resources
}
// Register a profile
func Register(name string, p *Profile) error {
if _, ok := profiles[name]; ok {
return fmt.Errorf("profile %s already exists", name)
}
profiles[name] = p
return nil
}
// Load a profile
func Load(name string) (*Profile, error) {
v, ok := profiles[name]
if !ok {
return nil, fmt.Errorf("profile %s does not exist", name)
}
return v, nil
}
// Client profile is for any entrypoint that behaves as a client
var Client = &Profile{
Name: "client",
Setup: func(ctx *cli.Context) error { return nil },
}
// Local profile to run locally
var Local = &Profile{
Name: "local",
Setup: func(ctx *cli.Context) error {
microAuth.DefaultAuth = jwt.NewAuth()
microRuntime.DefaultRuntime = local.NewRuntime()
microStore.DefaultStore = file.NewStore()
microConfig.DefaultConfig, _ = config.NewConfig()
SetupBroker(http.NewBroker())
SetupRegistry(mdns.NewRegistry())
SetupJWT(ctx)
var err error
microEvents.DefaultStream, err = memStream.NewStream()
if err != nil {
logger.Fatalf("Error configuring stream: %v", err)
}
microStore.DefaultBlobStore, err = file.NewBlobStore()
if err != nil {
logger.Fatalf("Error configuring file blob store: %v", err)
}
return nil
},
}
// Kubernetes profile to run on kubernetes
var Kubernetes = &Profile{
Name: "kubernetes",
Setup: func(ctx *cli.Context) error {
// TODO: implement
// using a static router so queries are routed based on service name
microRouter.DefaultRouter = static.NewRouter()
// Using the kubernetes runtime
microRuntime.DefaultRuntime = kubernetes.NewRuntime()
// registry kubernetes
// config configmap
// store ...
microAuth.DefaultAuth = jwt.NewAuth()
SetupJWT(ctx)
return nil
},
}
// Service is the default for any services run
var Service = &Profile{
Name: "service",
Setup: func(ctx *cli.Context) error { return nil },
}
// Test profile is used for the go test suite
var Test = &Profile{
Name: "test",
Setup: func(ctx *cli.Context) error {
microAuth.DefaultAuth = noop.NewAuth()
microStore.DefaultStore = mem.NewStore()
microConfig.DefaultConfig, _ = config.NewConfig()
SetupRegistry(memory.NewRegistry())
return nil
},
}
// SetupRegistry configures the registry
func SetupRegistry(reg registry.Registry) {
microRegistry.DefaultRegistry = reg
microRouter.DefaultRouter = regRouter.NewRouter(router.Registry(reg))
microServer.DefaultServer.Init(server.Registry(reg))
microClient.DefaultClient.Init(client.Registry(reg))
}
// SetupBroker configures the broker
func SetupBroker(b broker.Broker) {
microBroker.DefaultBroker = b
microClient.DefaultClient.Init(client.Broker(b))
microServer.DefaultServer.Init(server.Broker(b))
}
// SetupJWTRules configures the default internal system rules
func SetupJWT(ctx *cli.Context) {
for _, rule := range inAuth.SystemRules {
if err := microAuth.DefaultAuth.Grant(rule); err != nil {
logger.Fatal("Error creating default rule: %v", err)
}
}
// Only set this up for core services
// Won't work for multi node environments, could use
// the file store for that.
pubKey := ctx.String("auth_public_key")
privKey := ctx.String("auth_private_key")
if len(privKey) == 0 || len(pubKey) == 0 {
privB, pubB, err := user.GetJWTCerts()
if err != nil {
logger.Fatalf("Error getting keys; %v", err)
}
os.Setenv("MICRO_AUTH_PRIVATE_KEY", string(privB))
os.Setenv("MICRO_AUTH_PUBLIC_KEY", string(pubB))
}
}