From 96a9de802f40accc27c2cd1c83e4462a0d29ef43 Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 13 Mar 2024 17:20:24 -0500 Subject: [PATCH] feat: move packages to separate files (#117) Fixes #108 --- packages/cardano-node/cardano-node-8.7.3.yaml | 28 +++ .../mithril-client/mithril-client-0.5.17.yaml | 20 ++ .../mithril-client/mithril-client-0.7.0.yaml | 20 ++ packages/ogmios/ogmios-v6.1.0.yaml | 30 +++ .../test-packageA/test-packageA-1.0.2.yaml | 9 + .../test-packageA/test-packageA-1.0.3.yaml | 13 ++ .../test-packageA/test-packageA-2.1.3.yaml | 8 + .../test-packageB/test-packageB-0.1.0.yaml | 25 +++ pkgmgr/config.go | 1 + pkgmgr/error.go | 3 + pkgmgr/package.go | 2 +- pkgmgr/registry.go | 196 +++--------------- 12 files changed, 183 insertions(+), 172 deletions(-) create mode 100755 packages/cardano-node/cardano-node-8.7.3.yaml create mode 100755 packages/mithril-client/mithril-client-0.5.17.yaml create mode 100755 packages/mithril-client/mithril-client-0.7.0.yaml create mode 100755 packages/ogmios/ogmios-v6.1.0.yaml create mode 100755 packages/test-packageA/test-packageA-1.0.2.yaml create mode 100755 packages/test-packageA/test-packageA-1.0.3.yaml create mode 100755 packages/test-packageA/test-packageA-2.1.3.yaml create mode 100755 packages/test-packageB/test-packageB-0.1.0.yaml diff --git a/packages/cardano-node/cardano-node-8.7.3.yaml b/packages/cardano-node/cardano-node-8.7.3.yaml new file mode 100755 index 0000000..20d8e42 --- /dev/null +++ b/packages/cardano-node/cardano-node-8.7.3.yaml @@ -0,0 +1,28 @@ +name: cardano-node +version: 8.7.3 +description: Cardano node software by Input Output Global +installSteps: + - docker: + containerName: cardano-node + image: ghcr.io/blinklabs-io/cardano-node:8.7.3 + env: + CARDANO_NODE_SOCKET_PATH: /ipc/node.socket + NETWORK: '{{ .Context.Network }}' + binds: + - '{{ .Paths.ContextDir }}/node-ipc:/ipc' + - '{{ .Paths.DataDir }}/data:/data' + ports: + - "3001" + pullOnly: false + - file: + binary: true + filename: cardano-cli + content: | + #!/bin/bash + docker exec -ti {{ .Package.Name }}-cardano-node cardano-cli $@ +tags: + - docker + - linux + - darwin + - amd64 + - arm64 diff --git a/packages/mithril-client/mithril-client-0.5.17.yaml b/packages/mithril-client/mithril-client-0.5.17.yaml new file mode 100755 index 0000000..e025614 --- /dev/null +++ b/packages/mithril-client/mithril-client-0.5.17.yaml @@ -0,0 +1,20 @@ +name: mithril-client +version: 0.5.17 +description: Mithril client by Input Output Global +installSteps: + - docker: + containerName: mithril-client + image: ghcr.io/blinklabs-io/mithril-client:0.5.17-1 + pullOnly: true + - file: + binary: false + filename: mithril-client + content: | + #!/bin/bash + docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.5.17 $@ +tags: + - docker + - linux + - darwin + - amd64 + - arm64 diff --git a/packages/mithril-client/mithril-client-0.7.0.yaml b/packages/mithril-client/mithril-client-0.7.0.yaml new file mode 100755 index 0000000..086e6bf --- /dev/null +++ b/packages/mithril-client/mithril-client-0.7.0.yaml @@ -0,0 +1,20 @@ +name: mithril-client +version: 0.7.0 +description: Mithril client by Input Output Global +installSteps: + - docker: + containerName: mithril-client + image: ghcr.io/blinklabs-io/mithril-client:0.7.0-1 + pullOnly: true + - file: + binary: false + filename: mithril-client + content: | + #!/bin/bash + docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.7.0-1 $@ +tags: + - docker + - linux + - darwin + - amd64 + - arm64 diff --git a/packages/ogmios/ogmios-v6.1.0.yaml b/packages/ogmios/ogmios-v6.1.0.yaml new file mode 100755 index 0000000..d98e734 --- /dev/null +++ b/packages/ogmios/ogmios-v6.1.0.yaml @@ -0,0 +1,30 @@ +name: ogmios +version: v6.1.0 +description: Ogmios, a WebSocket & HTTP server for Cardano, providing a bridge between Cardano nodes and clients. +installSteps: + - docker: + containerName: ogmios + image: cardanosolutions/ogmios:v6.1.0 + command: + - ogmios + - --log-level + - info + - --host + - 0.0.0.0 + - --port + - "1337" + - --node-socket + - /ipc/node.socket + - --node-config + - /config/mainnet/cardano-node/config.json + binds: + - '{{ .Paths.ContextDir }}/node-ipc:/ipc' + ports: + - "1337" + pullOnly: false +tags: + - docker + - linux + - darwin + - amd64 + - arm64 diff --git a/packages/test-packageA/test-packageA-1.0.2.yaml b/packages/test-packageA/test-packageA-1.0.2.yaml new file mode 100755 index 0000000..02fd136 --- /dev/null +++ b/packages/test-packageA/test-packageA-1.0.2.yaml @@ -0,0 +1,9 @@ +name: test-packageA +version: 1.0.2 +tags: + - docker + - linux + - darwin + - amd64 + - arm64 +postInstallNotes: Notes for {{ .Package.Name }} diff --git a/packages/test-packageA/test-packageA-1.0.3.yaml b/packages/test-packageA/test-packageA-1.0.3.yaml new file mode 100755 index 0000000..79f16a9 --- /dev/null +++ b/packages/test-packageA/test-packageA-1.0.3.yaml @@ -0,0 +1,13 @@ +name: test-packageA +version: 1.0.3 +tags: + - docker + - linux + - darwin + - amd64 + - arm64 +postInstallNotes: Notes for {{ .Package.Name }} +outputs: + - name: foo + description: the 'foo' description + value: '{{ .Package.Name }}' diff --git a/packages/test-packageA/test-packageA-2.1.3.yaml b/packages/test-packageA/test-packageA-2.1.3.yaml new file mode 100755 index 0000000..3b9f64a --- /dev/null +++ b/packages/test-packageA/test-packageA-2.1.3.yaml @@ -0,0 +1,8 @@ +name: test-packageA +version: 2.1.3 +tags: + - docker + - linux + - darwin + - amd64 + - arm64 diff --git a/packages/test-packageB/test-packageB-0.1.0.yaml b/packages/test-packageB/test-packageB-0.1.0.yaml new file mode 100755 index 0000000..9fa1505 --- /dev/null +++ b/packages/test-packageB/test-packageB-0.1.0.yaml @@ -0,0 +1,25 @@ +name: test-packageB +version: 0.1.0 +installSteps: + - condition: eq .Package.ShortName "test-packageB" + file: + binary: false + filename: test-file1 + content: test1 + - condition: eq .Package.ShortName "test-packageZ" + file: + binary: false + filename: test-file2 + content: test2 +dependencies: + - test-packageA[fooA,-fooB] < 2.0.0, >= 1.0.2 +tags: + - docker + - linux + - darwin + - amd64 + - arm64 +postInstallNotes: |- + Values: + + {{ toPrettyJson . }} diff --git a/pkgmgr/config.go b/pkgmgr/config.go index 52abc87..35a7b6c 100644 --- a/pkgmgr/config.go +++ b/pkgmgr/config.go @@ -79,6 +79,7 @@ func NewDefaultConfig() (Config, error) { runtime.GOOS, runtime.GOARCH, }, + RegistryUrl: "https://github.com/blinklabs-io/cardano-up/archive/refs/heads/main.zip", } return ret, nil } diff --git a/pkgmgr/error.go b/pkgmgr/error.go index da15951..1d6eced 100644 --- a/pkgmgr/error.go +++ b/pkgmgr/error.go @@ -53,6 +53,9 @@ var ErrContainerAlreadyExists = errors.New("specified container already exists") // ErrContainerNotExists is returned when querying a container by name that doesn't exist var ErrContainerNotExists = errors.New("specified container does not exist") +// ErrNoRegistryConfigured is returned when no registry is configured +var ErrNoRegistryConfigured = errors.New("no package registry is configured") + func NewUnknownNetworkError(networkName string) error { return fmt.Errorf( "unknown network %q", diff --git a/pkgmgr/package.go b/pkgmgr/package.go index 807e722..89780b7 100644 --- a/pkgmgr/package.go +++ b/pkgmgr/package.go @@ -461,7 +461,7 @@ func (p Package) services(cfg Config, context string) ([]*DockerService, error) } type PackageInstallStep struct { - Condition string `yaml:"condition"` + Condition string `yaml:"condition,omitempty"` Docker *PackageInstallStepDocker `yaml:"docker,omitempty"` File *PackageInstallStepFile `yaml:"file,omitempty"` } diff --git a/pkgmgr/registry.go b/pkgmgr/registry.go index c7ec8b1..867d327 100644 --- a/pkgmgr/registry.go +++ b/pkgmgr/registry.go @@ -17,6 +17,7 @@ package pkgmgr import ( "archive/zip" "bytes" + "errors" "fmt" "io" "io/fs" @@ -29,190 +30,36 @@ import ( "gopkg.in/yaml.v3" ) -var registryBuiltinPackages = []Package{ - { - Name: "cardano-node", - Version: "8.7.3", - Description: "Cardano node software by Input Output Global", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - InstallSteps: []PackageInstallStep{ - { - Docker: &PackageInstallStepDocker{ - ContainerName: "cardano-node", - Image: "ghcr.io/blinklabs-io/cardano-node:8.7.3", - Env: map[string]string{ - "NETWORK": "{{ .Context.Network }}", - "CARDANO_NODE_SOCKET_PATH": "/ipc/node.socket", - }, - Binds: []string{ - "{{ .Paths.ContextDir }}/node-ipc:/ipc", - "{{ .Paths.DataDir }}/data:/data", - }, - Ports: []string{ - "3001", - }, - }, - }, - { - File: &PackageInstallStepFile{ - Binary: true, - Filename: "cardano-cli", - // TODO: figure out how to get network magic for named network - Content: `#!/bin/bash -docker exec -ti {{ .Package.Name }}-cardano-node cardano-cli $@ -`, - }, - }, - }, - }, - { - Name: "mithril-client", - Version: "0.5.17", - Description: "Mithril client by Input Output Global", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - InstallSteps: []PackageInstallStep{ - { - Docker: &PackageInstallStepDocker{ - ContainerName: "mithril-client", - Image: "ghcr.io/blinklabs-io/mithril-client:0.5.17-1", - PullOnly: true, - }, - }, - { - File: &PackageInstallStepFile{ - Filename: "mithril-client", - Content: `#!/bin/bash -docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.5.17 $@ -`, - }, - }, - }, - }, - { - Name: "mithril-client", - Version: "0.7.0", - Description: "Mithril client by Input Output Global", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - InstallSteps: []PackageInstallStep{ - { - Docker: &PackageInstallStepDocker{ - ContainerName: "mithril-client", - Image: "ghcr.io/blinklabs-io/mithril-client:0.7.0-1", - PullOnly: true, - }, - }, - { - File: &PackageInstallStepFile{ - Filename: "mithril-client", - Content: `#!/bin/bash -docker run --rm -ti ghcr.io/blinklabs-io/mithril-client:0.7.0-1 $@ -`, - }, - }, - }, - }, - { - Name: "ogmios", - Version: "v6.1.0", - Description: "Ogmios, a WebSocket & HTTP server for Cardano, providing a bridge between Cardano nodes and clients.", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - InstallSteps: []PackageInstallStep{ - { - Docker: &PackageInstallStepDocker{ - ContainerName: "ogmios", - Image: "cardanosolutions/ogmios:v6.1.0", - Binds: []string{ - "{{ .Paths.ContextDir }}/node-ipc:/ipc", - }, - Ports: []string{ - "1337", - }, - Command: []string{ - "ogmios", - "--log-level", "info", - "--host", "0.0.0.0", - "--port", "1337", - "--node-socket", "/ipc/node.socket", - "--node-config", "/config/mainnet/cardano-node/config.json", - }, - }, - }, - }, - }, - - // Test packages - { - Name: "test-packageA", - Version: "1.0.2", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - PostInstallNotes: "Notes for {{ .Package.Name }}", - }, - { - Name: "test-packageA", - Version: "1.0.3", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - PostInstallNotes: "Notes for {{ .Package.Name }}", - Outputs: []PackageOutput{ - { - Name: "foo", - Description: "the 'foo' description", - Value: `{{ .Package.Name }}`, - }, - }, - }, - { - Name: "test-packageA", - Version: "2.1.3", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - }, - { - Name: "test-packageB", - Version: "0.1.0", - Tags: []string{"docker", "linux", "darwin", "amd64", "arm64"}, - Dependencies: []string{ - "test-packageA[fooA,-fooB] < 2.0.0, >= 1.0.2", - }, - PostInstallNotes: "Values:\n\n{{ toPrettyJson . }}", - InstallSteps: []PackageInstallStep{ - { - Condition: `eq .Package.ShortName "test-packageB"`, - File: &PackageInstallStepFile{ - Filename: "test-file1", - Content: `test1`, - }, - }, - { - Condition: `eq .Package.ShortName "test-packageZ"`, - File: &PackageInstallStepFile{ - Filename: "test-file2", - Content: `test2`, - }, - }, - }, - }, -} - func registryPackages(cfg Config) ([]Package, error) { if cfg.RegistryDir != "" { return registryPackagesDir(cfg) } else if cfg.RegistryUrl != "" { return registryPackagesUrl(cfg) } else { - return registryBuiltinPackages[:], nil + return nil, ErrNoRegistryConfigured } } func registryPackagesDir(cfg Config) ([]Package, error) { - tmpFs := os.DirFS("/").(fs.ReadFileFS) + tmpFs := os.DirFS(cfg.RegistryDir).(fs.ReadFileFS) return registryPackagesFs(cfg, tmpFs) } func registryPackagesFs(cfg Config, filesystem fs.ReadFileFS) ([]Package, error) { var ret []Package - err := fs.WalkDir( + absRegistryDir, err := filepath.Abs(cfg.RegistryDir) + if err != nil { + return nil, err + } + err = fs.WalkDir( filesystem, - cfg.RegistryDir, + `.`, func(path string, d fs.DirEntry, err error) error { + // Replacing leading dot with registry dir + fullPath := filepath.Join( + absRegistryDir, + path, + ) if err != nil { return err } @@ -238,12 +85,15 @@ func registryPackagesFs(cfg Config, filesystem fs.ReadFileFS) ([]Package, error) cfg.Logger.Warn( fmt.Sprintf( "failed to load %q as package: %s", - path, + fullPath, err, ), ) return nil } + if tmpPkg.Name == "" || tmpPkg.Version == "" { + return nil + } ret = append(ret, tmpPkg) return nil }, @@ -262,12 +112,12 @@ func registryPackagesUrl(cfg Config) ([]Package, error) { // Check age of existing cache stat, err := os.Stat(cachePath) if err != nil { - if err != fs.ErrNotExist { + if !errors.Is(err, fs.ErrNotExist) { return nil, err } } // Fetch and extract registry ZIP into cache if it doesn't exist or is too old - if err == fs.ErrNotExist || + if errors.Is(err, fs.ErrNotExist) || stat.ModTime().Before(time.Now().Add(-24*time.Hour)) { // Fetch registry ZIP resp, err := http.Get(cfg.RegistryUrl) @@ -296,11 +146,15 @@ func registryPackagesUrl(cfg Config) ([]Package, error) { } // Extract files from ZIP into cache path for _, zipFile := range zipReader.File { + // Skip directory entries + if (zipFile.Mode() & fs.ModeDir) > 0 { + continue + } outPath := filepath.Join( cachePath, zipFile.Name, ) - // Create parent dir + // Create parent dir(s) if err := os.MkdirAll(filepath.Dir(outPath), fs.ModePerm); err != nil { return nil, err }