/
run.go
252 lines (209 loc) · 5.69 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright Nitric Pty Ltd.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package run
import (
"fmt"
"net"
"os"
"path/filepath"
"time"
"github.com/nitrictech/cli/pkg/dashboard"
"github.com/nitrictech/cli/pkg/project"
"github.com/nitrictech/cli/pkg/utils"
"github.com/nitrictech/nitric/core/pkg/membrane"
"github.com/nitrictech/nitric/core/pkg/worker/pool"
)
type LocalServices interface {
Start(pool pool.WorkerPool, suppressLogs bool) error
Stop() error
Running() bool
Status() *LocalServicesStatus
Refresh() error
Apis() map[string]string
HttpWorkers() map[int]string
Websockets() map[string]string
TriggerAddress() string
GetWorkerPool() pool.WorkerPool
GetStorageService() *RunStorageService
GetDashboard() *dashboard.Dashboard
}
type LocalServicesStatus struct {
RunDir string `yaml:"runDir"`
// GatewayAddress string `yaml:"gatewayAddress"`
MembraneAddress string `yaml:"membraneAddress"`
StorageEndpoint string `yaml:"storageEndpoint"`
}
type localServices struct {
project *project.Project
storage *SeaweedServer
membrane *membrane.Membrane
status *LocalServicesStatus
gateway *BaseHttpGateway
storageService *RunStorageService
dashboard *dashboard.Dashboard
isStart bool
}
func NewLocalServices(project *project.Project, isStart bool, dashboard *dashboard.Dashboard) LocalServices {
return &localServices{
project: project,
isStart: isStart,
status: &LocalServicesStatus{
RunDir: filepath.Join(utils.NitricRunDir(), project.Name),
MembraneAddress: net.JoinHostPort("localhost", "50051"),
},
dashboard: dashboard,
}
}
func (l *localServices) TriggerAddress() string {
if l.gateway != nil {
return l.gateway.GetTriggerAddress()
}
return ""
}
func (l *localServices) Refresh() error {
if l.gateway != nil {
err := l.gateway.Refresh()
if err != nil {
return err
}
}
err := l.dashboard.Refresh(&dashboard.RefreshOptions{
Pool: l.GetWorkerPool(),
TriggerAddress: l.TriggerAddress(),
ApiAddresses: l.Apis(),
WebSocketAddresses: l.Websockets(),
StorageAddress: l.Status().StorageEndpoint,
ServiceListener: l.gateway.serviceListener,
})
if err != nil {
return err
}
return nil
}
func (l *localServices) Stop() error {
l.membrane.Stop()
return l.storage.Stop()
}
func (l *localServices) Running() bool {
conn, err := net.DialTimeout("tcp", net.JoinHostPort("0.0.0.0", "50051"), time.Second)
if err == nil && conn != nil {
defer conn.Close()
return true
}
return false
}
func (l *localServices) Apis() map[string]string {
if l.gateway != nil {
return l.gateway.GetApiAddresses()
}
return nil
}
func (l *localServices) HttpWorkers() map[int]string {
if l.gateway != nil {
return l.gateway.GetHttpWorkerAddresses()
}
return nil
}
func (l *localServices) Websockets() map[string]string {
if l.gateway != nil {
return l.gateway.GetWebsocketAddresses()
}
return nil
}
func (l *localServices) Status() *LocalServicesStatus {
return l.status
}
func (l *localServices) Start(pool pool.WorkerPool, suppressLogs bool) error {
var err error
l.storage, err = NewSeaweed(l.status.RunDir)
if err != nil {
return err
}
// start seaweed server
err = l.storage.Start()
if err != nil {
return err
}
l.status.StorageEndpoint = fmt.Sprintf("http://localhost:%d", l.storage.GetApiPort())
l.storageService, err = NewStorage(StorageOptions{
AccessKey: "dummykey",
SecretKey: "dummysecret",
Endpoint: l.status.StorageEndpoint,
}, pool)
if err != nil {
return err
}
// Connect dev documents
os.Setenv("LOCAL_DB_DIR", l.status.RunDir)
dp, err := NewBoltService()
if err != nil {
return err
}
// Connect secrets plugin
os.Setenv("LOCAL_SEC_DIR", l.status.RunDir)
secp, err := NewSecretService()
if err != nil {
return err
}
// Connect queue plugin
os.Setenv("LOCAL_QUEUE_DIR", l.status.RunDir)
qp, err := NewQueueService()
if err != nil {
return err
}
ev, err := NewEvents(pool, l.project)
if err != nil {
return err
}
wsPlugin, _ := NewRunWebsocketService(l.dashboard)
// Start a new gateway plugin
l.gateway, err = NewGateway(wsPlugin)
if err != nil {
return err
}
l.gateway.project = l.project
l.gateway.dash = l.dashboard
// Prepare development membrane to start
// This will start a single membrane that all
// running functions will connect to
l.membrane, err = membrane.New(&membrane.MembraneOptions{
ServiceAddress: "0.0.0.0:50051",
SecretPlugin: secp,
QueuePlugin: qp,
StoragePlugin: l.storageService,
DocumentPlugin: dp,
GatewayPlugin: l.gateway,
EventsPlugin: ev,
ResourcesPlugin: NewResources(l, l.isStart),
WebsocketPlugin: wsPlugin,
Pool: pool,
TolerateMissingServices: false,
SuppressLogs: suppressLogs,
})
if err != nil {
return err
}
return l.membrane.Start()
}
func (l *localServices) GetWorkerPool() pool.WorkerPool {
return l.gateway.pool
}
func (l *localServices) GetStorageService() *RunStorageService {
return l.storageService
}
func (l *localServices) GetDashboard() *dashboard.Dashboard {
return l.dashboard
}