Skip to content

Commit 6ab318a

Browse files
Fix app status not doing an exact match
1 parent ff72af0 commit 6ab318a

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

internal/orchestrator/helpers.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/docker/docker/api/types/container"
1212
"github.com/docker/docker/api/types/filters"
1313
dockerClient "github.com/docker/docker/client"
14+
"github.com/gosimple/slug"
1415

1516
"github.com/arduino/arduino-app-cli/pkg/parser"
1617
)
@@ -45,9 +46,12 @@ func dockerComposeAppStatus(ctx context.Context, app parser.App) (DockerComposeA
4546
if err != nil {
4647
return DockerComposeAppStatusResponse{}, err
4748
}
48-
composeName := app.FullPath.Base()
49+
composeProjectName, err := getAppComposeProjectNameFromApp(app)
50+
if err != nil {
51+
return DockerComposeAppStatusResponse{}, err
52+
}
4953

50-
process, err := paths.NewProcess(nil, "docker", "compose", "-f", mainCompose.String(), "ls", "--format", "json", "--all", "--filter", fmt.Sprintf("name=%s", composeName))
54+
process, err := paths.NewProcess(nil, "docker", "compose", "-f", mainCompose.String(), "ls", "--format", "json", "--all", "--filter", fmt.Sprintf("name=%s", composeProjectName))
5155
if err != nil {
5256
return DockerComposeAppStatusResponse{}, err
5357
}
@@ -67,17 +71,27 @@ func dockerComposeAppStatus(ctx context.Context, app parser.App) (DockerComposeA
6771
if len(statusResponse) == 0 {
6872
return DockerComposeAppStatusResponse{}, fmt.Errorf("failed to find app status in docker compose response")
6973
}
70-
// We only want the first response, as we are filtering by name
71-
resp := statusResponse[0]
74+
// It is possible that the --filter returns multiple items as it's not an exact match
75+
var match DockerComposeAppStatusResponse
76+
for _, v := range statusResponse {
77+
if v.Name == composeProjectName {
78+
match = v
79+
break
80+
}
81+
}
82+
83+
if match == (DockerComposeAppStatusResponse{}) {
84+
return DockerComposeAppStatusResponse{}, fmt.Errorf("failed to find app status in docker compose response")
85+
}
7286

7387
// The response from compose is in the form of "state(number_services)". Example: "running(2)"
7488
// We only want the state, so we remove the number of services
75-
idx := strings.Index(resp.Status, "(")
89+
idx := strings.Index(match.Status, "(")
7690
if idx != -1 {
77-
resp.Status = resp.Status[:idx]
91+
match.Status = match.Status[:idx]
7892
}
7993

80-
return resp, nil
94+
return match, nil
8195
}
8296

8397
func getRunningApp(ctx context.Context, docker *dockerClient.Client) (*parser.App, error) {
@@ -128,3 +142,11 @@ func getRunningApp(ctx context.Context, docker *dockerClient.Client) (*parser.Ap
128142
}
129143
return nil, nil
130144
}
145+
146+
func getAppComposeProjectNameFromApp(app parser.App) (string, error) {
147+
composeProjectName, err := app.FullPath.RelFrom(orchestratorConfig.AppsDir())
148+
if err != nil {
149+
return "", fmt.Errorf("failed to get compose project name: %w", err)
150+
}
151+
return slug.Make(composeProjectName.String()), nil
152+
}

internal/orchestrator/provision.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/arduino/go-paths-helper"
1313
"github.com/docker/docker/api/types/container"
1414
dockerClient "github.com/docker/docker/client"
15-
"github.com/gosimple/slug"
1615
"go.bug.st/f"
1716
"gopkg.in/yaml.v3"
1817
)
@@ -144,13 +143,12 @@ func generateMainComposeFile(ctx context.Context, app parser.App, pythonImage st
144143
}
145144

146145
// Merge compose
147-
mainAppCompose.Include = composeFiles.AsStrings()
148-
149-
composeProjectName, err := app.FullPath.RelFrom(orchestratorConfig.AppsDir())
146+
composeProjectName, err := getAppComposeProjectNameFromApp(app)
150147
if err != nil {
151-
return fmt.Errorf("failed to get compose project name: %w", err)
148+
return err
152149
}
153-
mainAppCompose.Name = slug.Make(composeProjectName.String())
150+
mainAppCompose.Name = composeProjectName
151+
mainAppCompose.Include = composeFiles.AsStrings()
154152
if err := writeMainCompose(); err != nil {
155153
return err
156154
}

0 commit comments

Comments
 (0)