Skip to content

Commit 98b9e3a

Browse files
committed
move device check before compilation
1 parent beee333 commit 98b9e3a

File tree

2 files changed

+76
-74
lines changed

2 files changed

+76
-74
lines changed

internal/orchestrator/app/validator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,17 @@ func ValidateBricks(a AppDescriptor, index *bricksindex.BricksIndex) error {
4343
}
4444
return allErrors
4545
}
46+
47+
func Validate(app ArduinoApp, index *bricksindex.BricksIndex) error {
48+
var allErrors error
49+
50+
if app.MainPythonFile == nil || !app.MainPythonFile.Exist() {
51+
allErrors = errors.Join(allErrors, fmt.Errorf("main python file is missing"))
52+
}
53+
54+
if err := ValidateBricks(app.Descriptor, index); err != nil {
55+
allErrors = errors.Join(allErrors, err)
56+
}
57+
58+
return allErrors
59+
}

internal/orchestrator/orchestrator.go

Lines changed: 62 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func StartApp(
122122
ctx, cancel := context.WithCancel(ctx)
123123
defer cancel()
124124

125-
err := app.ValidateBricks(appToStart.Descriptor, bricksIndex)
125+
err := app.Validate(appToStart, bricksIndex)
126126
if err != nil {
127127
yield(StreamMessage{error: err})
128128
return
@@ -145,108 +145,96 @@ func StartApp(
145145
slog.Debug("unable to set status leds", slog.String("error", err.Error()))
146146
}
147147

148-
sketchCallbackWriter := NewCallbackWriter(func(line string) {
149-
if !yield(StreamMessage{data: line}) {
150-
cancel()
151-
return
152-
}
153-
})
154-
if !yield(StreamMessage{progress: &Progress{Name: "preparing", Progress: 0.0}}) {
148+
envs := getAppEnvironmentVariables(appToStart, bricksIndex, modelsIndex)
149+
if !yield(StreamMessage{data: "app provisioning"}) {
150+
return
151+
}
152+
153+
if !yield(StreamMessage{progress: &Progress{Name: "provisioning", Progress: 0.0}}) {
154+
return
155+
}
156+
157+
if err := provisioner.App(ctx, bricksIndex, &appToStart, cfg, envs, staticStore); err != nil {
158+
yield(StreamMessage{error: err})
155159
return
156160
}
157161

158162
if _, ok := appToStart.GetSketchPath(); ok {
159-
if !yield(StreamMessage{progress: &Progress{Name: "sketch compiling and uploading", Progress: 0.0}}) {
163+
if !yield(StreamMessage{progress: &Progress{Name: "sketch compiling and uploading", Progress: 10.0}}) {
160164
return
161165
}
166+
sketchCallbackWriter := NewCallbackWriter(func(line string) {
167+
if !yield(StreamMessage{data: line}) {
168+
cancel()
169+
return
170+
}
171+
})
162172
if err := compileUploadSketch(ctx, &appToStart, sketchCallbackWriter); err != nil {
163173
yield(StreamMessage{error: err})
164174
return
165175
}
166-
if !yield(StreamMessage{progress: &Progress{Name: "sketch updated", Progress: 10.0}}) {
176+
if !yield(StreamMessage{progress: &Progress{Name: "sketch updated", Progress: 20.0}}) {
167177
return
168178
}
169179
}
170180

171-
if appToStart.MainPythonFile != nil {
172-
envs := getAppEnvironmentVariables(appToStart, bricksIndex, modelsIndex)
173-
174-
if !yield(StreamMessage{data: "python provisioning"}) {
175-
cancel()
176-
return
177-
}
178-
provisionStartProgress := float32(0.0)
179-
if _, ok := appToStart.GetSketchPath(); ok {
180-
provisionStartProgress = 10.0
181-
}
181+
if !yield(StreamMessage{data: "python downloading"}) {
182+
return
183+
}
182184

183-
if !yield(StreamMessage{progress: &Progress{Name: "python provisioning", Progress: provisionStartProgress}}) {
184-
return
185-
}
185+
// Launch the docker compose command to start the app
186+
overrideComposeFile := appToStart.AppComposeOverrideFilePath()
186187

187-
if err := provisioner.App(ctx, bricksIndex, &appToStart, cfg, envs, staticStore); err != nil {
188-
yield(StreamMessage{error: err})
189-
return
190-
}
188+
commands := []string{}
189+
commands = append(commands, "docker", "compose", "-f", appToStart.AppComposeFilePath().String())
190+
if ok, _ := overrideComposeFile.ExistCheck(); ok {
191+
commands = append(commands, "-f", overrideComposeFile.String())
192+
}
193+
commands = append(commands, "up", "-d", "--remove-orphans", "--pull", "missing")
191194

192-
if !yield(StreamMessage{data: "python downloading"}) {
193-
cancel()
194-
return
195-
}
195+
dockerParser := NewDockerProgressParser(200)
196196

197-
// Launch the docker compose command to start the app
198-
overrideComposeFile := appToStart.AppComposeOverrideFilePath()
197+
var customError error
198+
callbackDockerWriter := NewCallbackWriter(func(line string) {
199+
// docker compose sometimes returns errors as info lines, we try to parse them here and return a proper error
199200

200-
commands := []string{}
201-
commands = append(commands, "docker", "compose", "-f", appToStart.AppComposeFilePath().String())
202-
if ok, _ := overrideComposeFile.ExistCheck(); ok {
203-
commands = append(commands, "-f", overrideComposeFile.String())
201+
if e := GetCustomErrorFomDockerEvent(line); e != nil {
202+
customError = e
204203
}
205-
commands = append(commands, "up", "-d", "--remove-orphans", "--pull", "missing")
206-
207-
dockerParser := NewDockerProgressParser(200)
208-
209-
var customError error
210-
callbackDockerWriter := NewCallbackWriter(func(line string) {
211-
// docker compose sometimes returns errors as info lines, we try to parse them here and return a proper error
212-
213-
if e := GetCustomErrorFomDockerEvent(line); e != nil {
214-
customError = e
215-
}
216-
if percentage, ok := dockerParser.Parse(line); ok {
204+
if percentage, ok := dockerParser.Parse(line); ok {
217205

218-
// assumption: docker pull progress goes from 0 to 80% of the total app start progress
219-
totalProgress := 20.0 + (percentage/100.0)*80.0
206+
// assumption: docker pull progress goes from 0 to 80% of the total app start progress
207+
totalProgress := 20.0 + (percentage/100.0)*80.0
220208

221-
if !yield(StreamMessage{progress: &Progress{Name: "python starting", Progress: float32(totalProgress)}}) {
222-
cancel()
223-
return
224-
}
225-
return
226-
} else if !yield(StreamMessage{data: line}) {
209+
if !yield(StreamMessage{progress: &Progress{Name: "python starting", Progress: float32(totalProgress)}}) {
227210
cancel()
228211
return
229212
}
230-
})
231-
232-
slog.Debug("starting app", slog.String("command", strings.Join(commands, " ")), slog.Any("envs", envs))
233-
process, err := paths.NewProcess(envs.AsList(), commands...)
234-
if err != nil {
235-
yield(StreamMessage{error: err})
213+
return
214+
} else if !yield(StreamMessage{data: line}) {
215+
cancel()
236216
return
237217
}
238-
process.RedirectStderrTo(callbackDockerWriter)
239-
process.RedirectStdoutTo(callbackDockerWriter)
240-
if err := process.RunWithinContext(ctx); err != nil {
241-
// custom error could have been set while reading the output. Not detected by the process exit code
242-
if customError != nil {
243-
err = customError
244-
}
218+
})
245219

246-
yield(StreamMessage{error: err})
247-
return
220+
slog.Debug("starting app", slog.String("command", strings.Join(commands, " ")), slog.Any("envs", envs))
221+
process, err := paths.NewProcess(envs.AsList(), commands...)
222+
if err != nil {
223+
yield(StreamMessage{error: err})
224+
return
225+
}
226+
process.RedirectStderrTo(callbackDockerWriter)
227+
process.RedirectStdoutTo(callbackDockerWriter)
228+
if err := process.RunWithinContext(ctx); err != nil {
229+
// custom error could have been set while reading the output. Not detected by the process exit code
230+
if customError != nil {
231+
err = customError
248232
}
233+
234+
yield(StreamMessage{error: err})
235+
return
249236
}
237+
250238
_ = yield(StreamMessage{progress: &Progress{Name: "", Progress: 100.0}})
251239
}
252240
}
@@ -1138,7 +1126,7 @@ func compileUploadSketch(
11381126
arduinoApp *app.ArduinoApp,
11391127
w io.Writer,
11401128
) error {
1141-
logrus.SetLevel(logrus.ErrorLevel) // Reduce the log level of arduino-cli
1129+
logrus.SetLevel(logrus.FatalLevel) // Reduce the log level of arduino-cli
11421130
srv := commands.NewArduinoCoreServer()
11431131

11441132
var inst *rpc.Instance

0 commit comments

Comments
 (0)