Skip to content

Commit 27d54d6

Browse files
orchestrator/app: add ProvisioningStateDir, AppComposeFilePath methods (#517)
1 parent 8fa1420 commit 27d54d6

File tree

4 files changed

+22
-43
lines changed

4 files changed

+22
-43
lines changed

internal/orchestrator/app/app.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func Load(appPath string) (ArduinoApp, error) {
6969
return ArduinoApp{}, errors.New("main python file and sketch file missing from app")
7070
}
7171

72+
// Ensure the .cache folder is created
73+
if err := app.ProvisioningStateDir().MkdirAll(); err != nil {
74+
return ArduinoApp{}, errors.New("cannot create .cache directory for app provisioning state")
75+
}
76+
7277
return app, nil
7378
}
7479

@@ -104,3 +109,11 @@ func (a *ArduinoApp) Save() error {
104109
func (a *ArduinoApp) SketchBuildPath() *paths.Path {
105110
return a.FullPath.Join(".cache", "sketch")
106111
}
112+
113+
func (a *ArduinoApp) ProvisioningStateDir() *paths.Path {
114+
return a.FullPath.Join(".cache")
115+
}
116+
117+
func (a *ArduinoApp) AppComposeFilePath() *paths.Path {
118+
return a.ProvisioningStateDir().Join("app-compose.yaml")
119+
}

internal/orchestrator/logs.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,7 @@ func AppLogs(
4545
return x.EmptyIter[LogMessage](), nil
4646
}
4747

48-
provisioningStateDir, err := getProvisioningStateDir(app)
49-
if err != nil {
50-
return nil, err
51-
}
52-
mainCompose := provisioningStateDir.Join("app-compose.yaml")
48+
mainCompose := app.AppComposeFilePath()
5349
if mainCompose.NotExist() {
5450
return x.EmptyIter[LogMessage](), nil
5551
}
@@ -63,7 +59,7 @@ func AppLogs(
6359
slog.Warn("invalid brick id", slog.String("brick_id", brick.ID))
6460
continue
6561
}
66-
composeFilePath := provisioningStateDir.Join("compose", namespace, brickName, "brick_compose.yaml")
62+
composeFilePath := app.ProvisioningStateDir().Join("compose", namespace, brickName, "brick_compose.yaml")
6763
if composeFilePath.Exist() {
6864
prj, err := loader.LoadWithContext(
6965
ctx,

internal/orchestrator/orchestrator.go

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ func StartApp(
153153
return
154154
}
155155

156-
provisioningStateDir, err := getProvisioningStateDir(app)
157-
if err != nil {
158-
yield(StreamMessage{error: err})
159-
return
160-
}
161-
162156
// Override the compose Variables with the app's variables and model configuration.
163157
envs := []string{}
164158
addMapToEnv := func(m map[string]string) {
@@ -173,10 +167,9 @@ func StartApp(
173167
}
174168
}
175169

176-
mainCompose := provisioningStateDir.Join("app-compose.yaml")
177-
overrideComposeFile := provisioningStateDir.Join("app-compose-overrides.yaml")
170+
overrideComposeFile := app.ProvisioningStateDir().Join("app-compose-overrides.yaml")
178171
commands := []string{}
179-
commands = append(commands, "docker", "compose", "-f", mainCompose.String())
172+
commands = append(commands, "docker", "compose", "-f", app.AppComposeFilePath().String())
180173
if ok, _ := overrideComposeFile.ExistCheck(); ok {
181174
commands = append(commands, "-f", overrideComposeFile.String())
182175
}
@@ -221,12 +214,7 @@ func StopApp(ctx context.Context, app app.ArduinoApp) iter.Seq[StreamMessage] {
221214
}
222215

223216
if app.MainPythonFile != nil {
224-
provisioningStateDir, err := getProvisioningStateDir(app)
225-
if err != nil {
226-
yield(StreamMessage{error: err})
227-
return
228-
}
229-
mainCompose := provisioningStateDir.Join("app-compose.yaml")
217+
mainCompose := app.AppComposeFilePath()
230218
// In case the app was never started
231219
if mainCompose.Exist() {
232220
process, err := paths.NewProcess(nil, "docker", "compose", "-f", mainCompose.String(), "stop", fmt.Sprintf("--timeout=%d", DefaultDockerStopTimeoutSeconds))

internal/orchestrator/provision.go

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ func (p *Provision) App(
9393
return fmt.Errorf("provisioning failed: arduinoApp is nil")
9494
}
9595

96-
provisioningStateDir, err := getProvisioningStateDir(*arduinoApp)
97-
if err != nil {
98-
return err
99-
}
100-
101-
dst := provisioningStateDir.Join("compose")
96+
dst := arduinoApp.ProvisioningStateDir().Join("compose")
10297
if err := dst.RemoveAll(); err != nil {
10398
return fmt.Errorf("failed to remove compose directory: %w", err)
10499
}
@@ -195,14 +190,6 @@ func pullBasePythonContainer(ctx context.Context, pythonImage string) error {
195190
return process.RunWithinContext(ctx)
196191
}
197192

198-
func getProvisioningStateDir(app app.ArduinoApp) (*paths.Path, error) {
199-
cacheDir := app.FullPath.Join(".cache")
200-
if err := cacheDir.MkdirAll(); err != nil {
201-
return nil, err
202-
}
203-
return cacheDir, nil
204-
}
205-
206193
const DockerAppLabel = "cc.arduino.app"
207194
const DockerAppPathLabel = "cc.arduino.app.path"
208195

@@ -211,19 +198,14 @@ func generateMainComposeFile(
211198
bricksIndex *bricksindex.BricksIndex,
212199
pythonImage string,
213200
) error {
214-
provisioningStateDir, err := getProvisioningStateDir(*app)
215-
if err != nil {
216-
return err
217-
}
218-
219201
slog.Debug("Generating main compose file for the App")
220202

221203
var composeFiles paths.PathList
222204
services := []string{}
223205
brickServices := map[string][]string{}
224206
for _, brick := range app.Descriptor.Bricks {
225207
brickPath := filepath.Join(strings.Split(brick.ID, ":")...)
226-
composeFilePath := provisioningStateDir.Join("compose", brickPath, "brick_compose.yaml")
208+
composeFilePath := app.ProvisioningStateDir().Join("compose", brickPath, "brick_compose.yaml")
227209
if composeFilePath.Exist() {
228210
composeFiles.Add(composeFilePath)
229211
svcs, e := extracServicesFromComposeFile(composeFilePath)
@@ -240,9 +222,9 @@ func generateMainComposeFile(
240222
}
241223

242224
// Create a single docker-mainCompose that includes all the required services
243-
mainComposeFile := provisioningStateDir.Join("app-compose.yaml")
225+
mainComposeFile := app.AppComposeFilePath()
244226
// If required, create an override compose file for devices
245-
overrideComposeFile := provisioningStateDir.Join("app-compose-overrides.yaml")
227+
overrideComposeFile := app.ProvisioningStateDir().Join("app-compose-overrides.yaml")
246228

247229
type mainService struct {
248230
Main service `yaml:"main"`

0 commit comments

Comments
 (0)