forked from nitrictech/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug.go
More file actions
189 lines (156 loc) · 5.79 KB
/
debug.go
File metadata and controls
189 lines (156 loc) · 5.79 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
// 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"
"strings"
"github.com/samber/lo"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"google.golang.org/protobuf/encoding/protojson"
"github.com/nitrictech/cli/pkg/collector"
"github.com/nitrictech/cli/pkg/env"
"github.com/nitrictech/cli/pkg/project"
"github.com/nitrictech/cli/pkg/view/tui"
"github.com/nitrictech/cli/pkg/view/tui/commands/build"
"github.com/nitrictech/cli/pkg/view/tui/teax"
)
var (
debugEnvFile string
debugFile string
)
var debugCmd = &cobra.Command{
Use: "debug",
Short: "Debug Operations (utilities for debugging nitric applications)",
Long: `Debug Operations (utilities for debugging nitric applications).`,
Example: `nitric debug spec`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if cmd.Root().PersistentPreRun != nil {
cmd.Root().PersistentPreRun(cmd, args)
}
},
}
var specCmd = &cobra.Command{
Use: "spec",
Short: "Output the nitric application cloud spec.",
Long: `Output the nitric application cloud spec.`,
Run: func(cmd *cobra.Command, args []string) {
fs := afero.NewOsFs()
proj, err := project.FromFile(fs, "")
tui.CheckErr(err)
// Build the Project's Services (Containers)
buildUpdates, err := proj.BuildServices(fs, !noBuilder)
tui.CheckErr(err)
batchBuildUpdates, err := proj.BuildBatches(fs, !noBuilder)
tui.CheckErr(err)
allBuildUpdates := lo.FanIn(10, buildUpdates, 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 buildUpdates {
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
buildModel, err := prog.Run()
tui.CheckErr(err)
if buildModel.(build.Model).Err != nil {
tui.CheckErr(fmt.Errorf("error building services"))
}
}
// Step 2. Start the collectors and containers (respectively in pairs)
// Step 3. Merge requirements from collectors into a specification
serviceRequirements, err := proj.CollectServicesRequirements()
tui.CheckErr(err)
batchRequirements, err := proj.CollectBatchRequirements()
tui.CheckErr(err)
websiteRequirements, err := proj.CollectWebsiteRequirements()
tui.CheckErr(err)
additionalEnvFiles := []string{}
if debugEnvFile != "" {
additionalEnvFiles = append(additionalEnvFiles, envFile)
}
envVariables, err := env.ReadLocalEnv(additionalEnvFiles...)
if err != nil && os.IsNotExist(err) {
if !os.IsNotExist(err) {
tui.CheckErr(err)
}
// If it doesn't exist set blank
envVariables = map[string]string{}
}
spec, err := collector.ServiceRequirementsToSpec(proj.Name, envVariables, serviceRequirements, batchRequirements, websiteRequirements)
tui.CheckErr(err)
migrationImageContexts, err := collector.GetMigrationImageBuildContexts(serviceRequirements, batchRequirements, fs)
tui.CheckErr(err)
// Build images from contexts and provide updates on the builds
if len(migrationImageContexts) > 0 {
migrationBuildUpdates, err := project.BuildMigrationImages(fs, migrationImageContexts, !noBuilder)
tui.CheckErr(err)
if isNonInteractive() {
fmt.Println("building project migration images")
// non-interactive environment
for update := range migrationBuildUpdates {
for _, line := range strings.Split(strings.TrimSuffix(update.Message, "\n"), "\n") {
if update.Status == project.ServiceBuildStatus_Error {
tui.CheckErr(fmt.Errorf("error building migration images %s", update.Message))
}
fmt.Printf("%s [%s]: %s\n", update.ServiceName, update.Status, line)
}
}
} else {
prog := teax.NewProgram(build.NewModel(migrationBuildUpdates, "Building Database Migrations"))
// blocks but quits once the above updates channel is closed by the build process
buildModel, err := prog.Run()
tui.CheckErr(err)
if buildModel.(build.Model).Err != nil {
tui.CheckErr(fmt.Errorf("error building migration images"))
}
}
}
outputFile := debugFile
if outputFile == "" {
outputFile = "./nitric-spec.json"
}
marshaler := protojson.MarshalOptions{
Multiline: true,
Indent: " ",
}
specJson, err := marshaler.Marshal(spec)
tui.CheckErr(err)
// output the spec
err = os.WriteFile(outputFile, specJson, 0o644)
tui.CheckErr(err)
fmt.Printf("Successfully outputted deployment spec to %s\n", outputFile)
},
Aliases: []string{"spec"},
}
func init() {
specCmd.Flags().StringVarP(&debugEnvFile, "env-file", "e", "", "--env-file config/.my-env")
specCmd.Flags().StringVarP(&debugFile, "output", "o", "", "--file my-example-spec.json")
specCmd.Flags().BoolVar(&noBuilder, "no-builder", false, "don't create a buildx container")
// Debug spec
debugCmd.AddCommand(specCmd)
// Add Stack Commands
rootCmd.AddCommand(debugCmd)
addAlias("debug spec", "spec", true)
}