forked from nitrictech/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
283 lines (232 loc) · 7.89 KB
/
run.go
File metadata and controls
283 lines (232 loc) · 7.89 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// 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 cmd
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
tea "github.com/charmbracelet/bubbletea"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/nitrictech/cli/pkg/cloud"
"github.com/nitrictech/cli/pkg/cloud/gateway"
"github.com/nitrictech/cli/pkg/dashboard"
docker "github.com/nitrictech/cli/pkg/docker"
"github.com/nitrictech/cli/pkg/env"
"github.com/nitrictech/cli/pkg/paths"
"github.com/nitrictech/cli/pkg/project"
"github.com/nitrictech/cli/pkg/system"
"github.com/nitrictech/cli/pkg/view/tui"
"github.com/nitrictech/cli/pkg/view/tui/commands/build"
"github.com/nitrictech/cli/pkg/view/tui/commands/local"
"github.com/nitrictech/cli/pkg/view/tui/commands/services"
"github.com/nitrictech/cli/pkg/view/tui/teax"
)
var runNoBrowser bool
var runCmd = &cobra.Command{
Use: "run",
Short: "Run your project locally for development and testing",
Long: `Run your project locally for development and testing`,
Example: `nitric run`,
Annotations: map[string]string{"commonCommand": "yes"},
RunE: func(cmd *cobra.Command, args []string) error {
err := docker.VerifyDockerIsAvailable()
tui.CheckErr(err)
fs := afero.NewOsFs()
proj, err := project.FromFile(fs, "")
tui.CheckErr(err)
additionalEnvFiles := []string{}
if envFile != "" {
additionalEnvFiles = append(additionalEnvFiles, envFile)
}
loadEnv, err := env.ReadLocalEnv(additionalEnvFiles...)
if err != nil && !os.IsNotExist(err) {
tui.CheckErr(err)
}
var tlsCredentials *gateway.TLSCredentials
if enableHttps {
createTlsCredentialsIfNotPresent(fs, proj.Directory)
tlsCredentials = &gateway.TLSCredentials{
CertFile: paths.NitricTlsCertFile(proj.Directory),
KeyFile: paths.NitricTlsKeyFile(proj.Directory),
}
}
logFilePath, err := paths.NewNitricLogFile(proj.Directory)
tui.CheckErr(err)
logWriter, err := fs.OpenFile(logFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
tui.CheckErr(err)
defer logWriter.Close()
// Initialize the system service log logger
system.InitializeServiceLogger(proj.Directory)
teaOptions := []tea.ProgramOption{}
if isNonInteractive() {
teaOptions = append(teaOptions, tea.WithoutRenderer(), tea.WithInput(nil))
}
runView := teax.NewProgram(local.NewLocalCloudStartModel(isNonInteractive()), teaOptions...)
var localCloud *cloud.LocalCloud
go func() {
// Start the local cloud service analogues
localCloud, err = cloud.New(proj.Name, cloud.LocalCloudOptions{
TLSCredentials: tlsCredentials,
LogWriter: logWriter,
LocalConfig: proj.LocalConfig,
MigrationRunner: project.BuildAndRunMigrations,
LocalCloudMode: cloud.RunMode,
})
tui.CheckErr(err)
runView.Send(local.LocalCloudStartStatusMsg{Status: local.Done})
}()
_, err = runView.Run()
tui.CheckErr(err)
// Start dashboard
dash, err := dashboard.New(startNoBrowser, localCloud, proj)
tui.CheckErr(err)
err = dash.Start()
tui.CheckErr(err)
updates, err := proj.BuildServices(fs, !noBuilder)
tui.CheckErr(err)
batchBuildUpdates, err := proj.BuildBatches(fs, !noBuilder)
tui.CheckErr(err)
allBuildUpdates := lo.FanIn(10, updates, batchBuildUpdates)
if isNonInteractive() {
fmt.Println("building project services")
for _, service := range proj.GetServices() {
fmt.Printf("service matched '%s', auto-naming this service '%s'\n", service.GetFilePath(), service.Name)
}
// non-interactive environment
for update := range allBuildUpdates {
for _, line := range strings.Split(strings.TrimSuffix(update.Message, "\n"), "\n") {
fmt.Printf("%s [%s]: %s\n", update.ServiceName, update.Status, line)
}
}
} else {
prog := teax.NewProgram(build.NewModel(allBuildUpdates, "Building Services"))
// blocks but quits once the above updates channel is closed by the build process
_, err = prog.Run()
tui.CheckErr(err)
}
websiteBuildUpdates, err := proj.BuildWebsites(loadEnv)
tui.CheckErr(err)
if isNonInteractive() {
fmt.Println("building project websites")
for update := range websiteBuildUpdates {
for _, line := range strings.Split(strings.TrimSuffix(update.Message, "\n"), "\n") {
fmt.Printf("%s [%s]: %s\n", update.ServiceName, update.Status, line)
}
}
} else {
prog := teax.NewProgram(build.NewModel(websiteBuildUpdates, "Building Websites"))
// blocks but quits once the above updates channel is closed by the build process
_, err = prog.Run()
tui.CheckErr(err)
}
// Run the app code (project services)
stopChan := make(chan bool)
updatesChan := make(chan project.ServiceRunUpdate)
// panic recovery for local cloud
// gracefully stop the local cloud in the case of a panic
defer func() {
if r := recover(); r != nil {
localCloud.Stop()
}
}()
go func() {
err := proj.RunServices(localCloud, stopChan, updatesChan, loadEnv)
if err != nil {
localCloud.Stop()
tui.CheckErr(err)
}
}()
go func() {
err := proj.RunBatches(localCloud, stopChan, updatesChan, loadEnv)
if err != nil {
localCloud.Stop()
tui.CheckErr(err)
}
}()
go func() {
err := proj.RunWebsites(localCloud)
if err != nil {
localCloud.Stop()
tui.CheckErr(err)
}
}()
tui.CheckErr(err)
// FIXME: This is a hack to get labelled logs into the TUI
// We should refactor the system logs to be more generic
systemChan := make(chan project.ServiceRunUpdate)
system.SubscribeToLogs(func(msg string) {
systemChan <- project.ServiceRunUpdate{
ServiceName: "nitric",
Label: "nitric",
Status: project.ServiceRunStatus_Running,
Message: msg,
}
})
allUpdates := lo.FanIn(10, updatesChan, systemChan)
// non-interactive environment
if isNonInteractive() {
go func() {
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT)
// Wait for a signal
<-sigChan
fmt.Println("Stopping local cloud")
localCloud.Stop()
// Send stop signal to stopChan
close(stopChan)
}()
logger := system.GetServiceLogger()
for {
select {
case update := <-allUpdates:
fmt.Printf("%s [%s]: %s", update.ServiceName, update.Status, update.Message)
// Write log to file
level := logrus.InfoLevel
if update.Status == project.ServiceRunStatus_Error {
level = logrus.ErrorLevel
}
logger.WriteLog(level, update.Message, update.Label)
case <-stopChan:
fmt.Println("Shutting down services - exiting")
return nil
}
}
} else {
runView := teax.NewProgram(services.NewModel(stopChan, allUpdates, localCloud, dash.GetDashboardUrl()))
_, _ = runView.Run()
localCloud.Stop()
}
return nil
},
Args: cobra.ExactArgs(0),
}
func init() {
runCmd.Flags().StringVarP(&envFile, "env-file", "e", "", "--env-file config/.my-env")
runCmd.Flags().BoolVar(&enableHttps, "https-preview", false, "enable https support for local APIs (preview feature)")
runCmd.Flags().BoolVar(&noBuilder, "no-builder", false, "don't create a buildx container")
runCmd.PersistentFlags().BoolVar(
&runNoBrowser,
"no-browser",
false,
"disable browser opening for local dashboard, note: in CI mode the browser opening feature is disabled",
)
rootCmd.AddCommand(tui.AddDependencyCheck(runCmd, tui.RequireContainerBuilder))
}