Skip to content

Commit b01c438

Browse files
refactor config (#555)
1 parent b0ad4bc commit b01c438

File tree

7 files changed

+57
-63
lines changed

7 files changed

+57
-63
lines changed

Taskfile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ tasks:
348348
mv "$OUTPUT_DIR"/arduino/app_bricks/static "$DST_DIR"
349349
rm -r "$OUTPUT_DIR" "$ZIP_NAME"
350350
# Bumps the default pinned latest base python image tag
351-
sed -i "s#runnerVersion = \".*#runnerVersion = \"${semver_tag}\"#" cmd/arduino-app-cli/internal/servicelocator/servicelocator.go
351+
sed -i "s#runnerVersion = \".*#runnerVersion = \"${semver_tag}\"#" internal/orchestrator/config/config.go
352352
353353
update-deb-copyright:
354354
desc: Extract project and dependency licenses into asd copyright

cmd/arduino-app-cli/daemon/daemon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewDaemonCmd(cfg config.Configuration, version string) *cobra.Command {
4949
slog.Info("Try to pull latest docker images in background...")
5050
err = orchestrator.SystemInit(
5151
cmd.Context(),
52-
servicelocator.GetUsedPythonImageTag(),
52+
cfg.UsedPythonImageTag,
5353
servicelocator.GetStaticStore(),
5454
)
5555
if err != nil {

cmd/arduino-app-cli/internal/servicelocator/servicelocator.go

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
package servicelocator
44

55
import (
6-
"fmt"
7-
"log/slog"
8-
"os"
9-
"path"
10-
"strings"
116
"sync"
127

138
dockerCommand "github.com/docker/cli/cli/command"
@@ -31,9 +26,6 @@ func Init(cfg config.Configuration) {
3126
}
3227

3328
var (
34-
// Do not manually modify this, we keep it updated with the `task generate:bricks-and-models-index`
35-
runnerVersion = "0.1.16"
36-
3729
GetBricksIndex = sync.OnceValue(func() *bricksindex.BricksIndex {
3830
return f.Must(bricksindex.GenerateBricksIndexFromFile(GetStaticStore().GetAssetsFolder()))
3931
})
@@ -43,14 +35,8 @@ var (
4335
})
4436

4537
GetProvisioner = sync.OnceValue(func() *orchestrator.Provision {
46-
pythonImage, usedPythonImageTag := getPythonImageAndTag()
47-
slog.Debug("Using pythonImage", slog.String("image", pythonImage))
48-
4938
return f.Must(orchestrator.NewProvision(
5039
GetDockerClient(),
51-
pythonImage,
52-
usedPythonImageTag,
53-
runnerVersion,
5440
globalConfig,
5541
))
5642
})
@@ -79,13 +65,8 @@ var (
7965
return nil
8066
}
8167

82-
GetUsedPythonImageTag = sync.OnceValue(func() string {
83-
_, usedPythonImageTag := getPythonImageAndTag()
84-
return usedPythonImageTag
85-
})
86-
8768
GetStaticStore = sync.OnceValue(func() *store.StaticStore {
88-
return store.NewStaticStore(globalConfig.AssetsDir().Join(GetUsedPythonImageTag()).String())
69+
return store.NewStaticStore(globalConfig.AssetsDir().Join(globalConfig.UsedPythonImageTag).String())
8970
})
9071

9172
GetBrickService = sync.OnceValue(func() *bricks.Service {
@@ -100,22 +81,3 @@ var (
10081
return app.NewAppIDProvider(globalConfig)
10182
})
10283
)
103-
104-
func getPythonImageAndTag() (string, string) {
105-
registryBase := os.Getenv("DOCKER_REGISTRY_BASE")
106-
if registryBase == "" {
107-
registryBase = "ghcr.io/bcmi-labs/"
108-
}
109-
110-
// Python image: image name (repository) and optionally a tag.
111-
pythonImageAndTag := os.Getenv("DOCKER_PYTHON_BASE_IMAGE")
112-
if pythonImageAndTag == "" {
113-
pythonImageAndTag = fmt.Sprintf("arduino/appslab-python-apps-base:%s", runnerVersion)
114-
}
115-
pythonImage := path.Join(registryBase, pythonImageAndTag)
116-
var usedPythonImageTag string
117-
if idx := strings.LastIndex(pythonImage, ":"); idx != -1 {
118-
usedPythonImageTag = pythonImage[idx+1:]
119-
}
120-
return pythonImage, usedPythonImageTag
121-
}

cmd/arduino-app-cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func run(configuration cfg.Configuration) error {
6262
daemon.NewDaemonCmd(configuration, Version),
6363
properties.NewPropertiesCmd(configuration),
6464
config.NewConfigCmd(configuration),
65-
system.NewSystemCmd(),
65+
system.NewSystemCmd(configuration),
6666
board.NewBoardCmd(),
6767
version.NewVersionCmd(Version),
6868
)

cmd/arduino-app-cli/system/system.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@ import (
55

66
"github.com/bcmi-labs/orchestrator/cmd/arduino-app-cli/internal/servicelocator"
77
"github.com/bcmi-labs/orchestrator/internal/orchestrator"
8+
"github.com/bcmi-labs/orchestrator/internal/orchestrator/config"
89
)
910

10-
func NewSystemCmd() *cobra.Command {
11+
func NewSystemCmd(cfg config.Configuration) *cobra.Command {
1112
cmd := &cobra.Command{
1213
Use: "system",
1314
Hidden: true,
1415
}
1516

16-
cmd.AddCommand(newDownloadImage())
17+
cmd.AddCommand(newDownloadImage(cfg))
1718

1819
return cmd
1920
}
2021

21-
func newDownloadImage() *cobra.Command {
22+
func newDownloadImage(cfg config.Configuration) *cobra.Command {
2223
cmd := &cobra.Command{
2324
Use: "init",
2425
Args: cobra.ExactArgs(0),
2526
RunE: func(cmd *cobra.Command, _ []string) error {
26-
return orchestrator.SystemInit(cmd.Context(), servicelocator.GetUsedPythonImageTag(), servicelocator.GetStaticStore())
27+
return orchestrator.SystemInit(cmd.Context(), cfg.UsedPythonImageTag, servicelocator.GetStaticStore())
2728
},
2829
}
2930

internal/orchestrator/config/config.go

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
11
package config
22

33
import (
4+
"fmt"
45
"log/slog"
56
"os"
7+
"path"
8+
"strings"
69

710
"github.com/arduino/go-paths-helper"
811
)
912

13+
// Do not manually modify this, we keep it updated with the `task generate:bricks-and-models-index`
14+
var runnerVersion = "0.1.16"
15+
1016
type Configuration struct {
11-
appsDir *paths.Path
12-
configDir *paths.Path
13-
dataDir *paths.Path
14-
routerSocketPath *paths.Path
15-
customEIModelsDir *paths.Path
17+
appsDir *paths.Path
18+
configDir *paths.Path
19+
dataDir *paths.Path
20+
routerSocketPath *paths.Path
21+
customEIModelsDir *paths.Path
22+
PythonImage string
23+
UsedPythonImageTag string
24+
RunnerVersion string
1625
}
1726

1827
func NewFromEnv() (Configuration, error) {
@@ -78,12 +87,18 @@ func NewFromEnv() (Configuration, error) {
7887
}
7988
}
8089

90+
pythonImage, usedPythonImageTag := getPythonImageAndTag()
91+
slog.Debug("Using pythonImage", slog.String("image", pythonImage))
92+
8193
c := Configuration{
82-
appsDir: appsDir,
83-
configDir: configDir,
84-
dataDir: dataDir,
85-
routerSocketPath: routerSocket,
86-
customEIModelsDir: customEIModelsDir,
94+
appsDir: appsDir,
95+
configDir: configDir,
96+
dataDir: dataDir,
97+
routerSocketPath: routerSocket,
98+
customEIModelsDir: customEIModelsDir,
99+
PythonImage: pythonImage,
100+
UsedPythonImageTag: usedPythonImageTag,
101+
RunnerVersion: runnerVersion,
87102
}
88103
if err := c.init(); err != nil {
89104
return Configuration{}, err
@@ -127,3 +142,22 @@ func (c *Configuration) RouterSocketPath() *paths.Path {
127142
func (c *Configuration) AssetsDir() *paths.Path {
128143
return c.dataDir.Join("assets")
129144
}
145+
146+
func getPythonImageAndTag() (string, string) {
147+
registryBase := os.Getenv("DOCKER_REGISTRY_BASE")
148+
if registryBase == "" {
149+
registryBase = "ghcr.io/bcmi-labs/"
150+
}
151+
152+
// Python image: image name (repository) and optionally a tag.
153+
pythonImageAndTag := os.Getenv("DOCKER_PYTHON_BASE_IMAGE")
154+
if pythonImageAndTag == "" {
155+
pythonImageAndTag = fmt.Sprintf("arduino/appslab-python-apps-base:%s", runnerVersion)
156+
}
157+
pythonImage := path.Join(registryBase, pythonImageAndTag)
158+
var usedPythonImageTag string
159+
if idx := strings.LastIndex(pythonImage, ":"); idx != -1 {
160+
usedPythonImageTag = pythonImage[idx+1:]
161+
}
162+
return pythonImage, usedPythonImageTag
163+
}

internal/orchestrator/provision.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,17 @@ type Provision struct {
5151

5252
func NewProvision(
5353
docker command.Cli,
54-
pythonImage string,
55-
usedPythonImageTag string,
56-
pinnedPythonImagTag string,
5754
cfg config.Configuration,
5855
) (*Provision, error) {
59-
isDevelopmentMode := usedPythonImageTag != pinnedPythonImagTag
56+
isDevelopmentMode := cfg.UsedPythonImageTag != cfg.RunnerVersion
6057
if isDevelopmentMode {
61-
dynamicProvisionDir := cfg.AssetsDir().Join(usedPythonImageTag)
58+
dynamicProvisionDir := cfg.AssetsDir().Join(cfg.UsedPythonImageTag)
6259
_ = dynamicProvisionDir.RemoveAll()
6360
tmpProvisionDir, err := cfg.AssetsDir().MkTempDir("dynamic-provisioning")
6461
if err != nil {
6562
return nil, fmt.Errorf("failed to perform creation of dynamic provisioning dir: %w", err)
6663
}
67-
if err := dynamicProvisioning(context.Background(), docker.Client(), pythonImage, tmpProvisionDir.String()); err != nil {
64+
if err := dynamicProvisioning(context.Background(), docker.Client(), cfg.PythonImage, tmpProvisionDir.String()); err != nil {
6865
return nil, fmt.Errorf("failed to perform dynamic provisioning: %w", err)
6966
}
7067
if err := tmpProvisionDir.Rename(dynamicProvisionDir); err != nil {
@@ -74,7 +71,7 @@ func NewProvision(
7471

7572
return &Provision{
7673
docker: docker,
77-
pythonImage: pythonImage,
74+
pythonImage: cfg.PythonImage,
7875
}, nil
7976
}
8077

0 commit comments

Comments
 (0)