Skip to content

Commit

Permalink
Prepare stub for Storage Indexer (#811)
Browse files Browse the repository at this point in the history
* Prepare code for storage indexer

* Fix

* Changelog

* Update main.go

Co-authored-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>

* Address PR comments

Co-authored-by: Jaime Soriano Pastor <jaime.soriano@elastic.co>
  • Loading branch information
mtojek and jsoriano authored May 18, 2022
1 parent e0061be commit 8b93e0d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

* Prepare stub for Storage Indexer. Disable fetching packages from Package Storage v1. [#811](https://github.com/elastic/package-registry/pull/811)

### Deprecated

### Known Issues
Expand Down
21 changes: 0 additions & 21 deletions magefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
"github.com/pkg/errors"
)

var (
Expand All @@ -34,29 +33,9 @@ var (
)

func Build() error {
err := FetchPackageStorage()
if err != nil {
return err
}
return sh.Run("go", "build", ".")
}

func FetchPackageStorage() error {
// Remove old storage directory
if err := os.RemoveAll(storageRepoDir); err != nil {
return errors.Wrapf(err, "failed to remove existing storage directory: %s", storageRepoDir)
}

packageStorageRevision := os.Getenv("PACKAGE_STORAGE_REVISION")
if packageStorageRevision == "" {
packageStorageRevision = "production"
}

// Check out fresh storage directory
return sh.Run("git", "clone", "--depth=1", "--single-branch",
"--branch", packageStorageRevision, "https://github.com/elastic/package-storage.git", storageRepoDir)
}

func Check() error {
Format()

Expand Down
22 changes: 16 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
ucfgYAML "github.com/elastic/go-ucfg/yaml"

"github.com/elastic/package-registry/packages"
"github.com/elastic/package-registry/storage"
"github.com/elastic/package-registry/util"
)

Expand All @@ -44,6 +45,8 @@ var (
dryRun bool
configPath string

featureStorageIndexer bool

defaultConfig = Config{
CacheTimeIndex: 10 * time.Second,
CacheTimeSearch: 10 * time.Minute,
Expand All @@ -61,6 +64,8 @@ func init() {
// This flag is experimental and might be removed in the future or renamed
flag.BoolVar(&dryRun, "dry-run", false, "Runs a dry-run of the registry without starting the web service (experimental).")
flag.BoolVar(&packages.ValidationDisabled, "disable-package-validation", false, "Disable package content validation.")
// This flag is a technical preview and might be removed in the future or renamed
flag.BoolVar(&featureStorageIndexer, "feature-storage-indexer", false, "Enable storage indexer to include packages from Package Storage v2 (technical preview).")
}

type Config struct {
Expand Down Expand Up @@ -123,18 +128,23 @@ func initServer(logger *zap.Logger) *http.Server {

config := mustLoadConfig(logger)
packagesBasePaths := getPackagesBasePaths(config)
indexer := NewCombinedIndexer(
packages.NewFileSystemIndexer(packagesBasePaths...),
packages.NewZipFileSystemIndexer(packagesBasePaths...),
)
ensurePackagesAvailable(ctx, logger, indexer)

var indexers []Indexer
if featureStorageIndexer {
indexers = append(indexers, storage.NewIndexer())
} else {
indexers = append(indexers, packages.NewFileSystemIndexer(packagesBasePaths...))
indexers = append(indexers, packages.NewZipFileSystemIndexer(packagesBasePaths...))
}
combinedIndexer := NewCombinedIndexer(indexers...)
ensurePackagesAvailable(ctx, logger, combinedIndexer)

// If -dry-run=true is set, service stops here after validation
if dryRun {
os.Exit(0)
}

router := mustLoadRouter(logger, config, indexer)
router := mustLoadRouter(logger, config, combinedIndexer)
apmgorilla.Instrument(router, apmgorilla.WithTracer(apmTracer))

return &http.Server{Addr: address, Handler: router}
Expand Down
25 changes: 25 additions & 0 deletions storage/indexer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package storage

import (
"context"

"github.com/elastic/package-registry/packages"
)

type Indexer struct{}

func NewIndexer() *Indexer {
return new(Indexer)
}

func (i *Indexer) Init(context.Context) error {
return nil
}

func (i *Indexer) Get(context.Context, *packages.GetOptions) (packages.Packages, error) {
panic("not implemented yet")
}

0 comments on commit 8b93e0d

Please sign in to comment.