Skip to content

Commit

Permalink
plugin installation and update (#187)
Browse files Browse the repository at this point in the history
uploader CLI options and named uploaders
  • Loading branch information
mandelsoft authored Nov 21, 2022
1 parent c32a6ec commit beb2097
Show file tree
Hide file tree
Showing 123 changed files with 3,832 additions and 675 deletions.
15 changes: 13 additions & 2 deletions cmds/demoplugin/accessmethods/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"strings"

"github.com/mandelsoft/filepath/pkg/filepath"
"github.com/open-component-model/ocm/cmds/demoplugin/common"

"github.com/open-component-model/ocm/cmds/common"
"github.com/open-component-model/ocm/cmds/demoplugin/config"
"github.com/open-component-model/ocm/pkg/cobrautils/flagsets"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci/identity"
Expand Down Expand Up @@ -105,5 +106,15 @@ func (a *AccessMethod) ComposeAccessSpecification(p ppi.Plugin, opts ppi.Config,
func (a *AccessMethod) Reader(p ppi.Plugin, spec ppi.AccessSpec, creds credentials.Credentials) (io.ReadCloser, error) {
my := spec.(*AccessSpec)

return os.Open(filepath.Join(os.TempDir(), my.Path))
cfg, _ := p.GetConfig()
root := os.TempDir()
if cfg != nil && cfg.(*config.Config).AccessMethods.Path != "" {
root = cfg.(*config.Config).Uploaders.Path
err := os.MkdirAll(root, 0o700)
if err != nil {
return nil, errors.Wrapf(err, "cannot create root dir")
}
}

return os.Open(filepath.Join(root, my.Path))
}
File renamed without changes.
28 changes: 28 additions & 0 deletions cmds/demoplugin/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package config

import (
"encoding/json"
)

type Config struct {
AccessMethods Values `json:"accessMethods"`
Uploaders Values `json:"uploaders"`
}

type Values struct {
Path string `json:"path"`
}

func GetConfig(raw json.RawMessage) (interface{}, error) {
var cfg Config

err := json.Unmarshal(raw, &cfg)
if err != nil {
return nil, err
}
return &cfg, nil
}
2 changes: 2 additions & 0 deletions cmds/demoplugin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/open-component-model/ocm/cmds/demoplugin/accessmethods"
"github.com/open-component-model/ocm/cmds/demoplugin/config"
"github.com/open-component-model/ocm/cmds/demoplugin/uploaders"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi/cmds"
Expand All @@ -19,6 +20,7 @@ func main() {

p.SetShort("demo plugin")
p.SetLong("plugin providing access to temp files.")
p.SetConfigParser(config.GetConfig)

p.RegisterAccessMethod(accessmethods.New())
u := uploaders.New()
Expand Down
16 changes: 13 additions & 3 deletions cmds/demoplugin/uploaders/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"path/filepath"
"strings"

"github.com/open-component-model/ocm/cmds/common"
"github.com/open-component-model/ocm/cmds/demoplugin/accessmethods"
"github.com/open-component-model/ocm/cmds/demoplugin/common"
"github.com/open-component-model/ocm/cmds/demoplugin/config"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/oci/identity"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)

Expand Down Expand Up @@ -76,10 +78,18 @@ func (a *Uploader) Writer(p ppi.Plugin, arttype, mediatype, hint string, repo pp
var file *os.File
var err error

my := repo.(*TargetSpec)
cfg, _ := p.GetConfig()
root := os.TempDir()
if cfg != nil && cfg.(*config.Config).Uploaders.Path != "" {
root = cfg.(*config.Config).Uploaders.Path
err := os.MkdirAll(root, 0o700)
if err != nil {
return nil, nil, errors.Wrapf(err, "cannot create root dir")
}
}

path := hint
root := os.TempDir()
my := repo.(*TargetSpec)
dir := root
if my.Path != "" {
root = filepath.Join(root, my.Path)
Expand Down
2 changes: 1 addition & 1 deletion cmds/helminstaller/app/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Execute(d driver.Driver, action string, ctx ocm.Context, octx out.Context,
file.Close()
os.Remove(path)

_, path, err = download.For(ctx).Download(octx, acc, path, osfs.New())
_, path, err = download.For(ctx).Download(common.NewPrinter(octx.StdOut()), acc, path, osfs.New())
if err != nil {
return errors.Wrapf(err, "downloading helm chart")
}
Expand Down
9 changes: 7 additions & 2 deletions cmds/ocm/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/describe"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/download"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/get"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/install"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/show"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/sign"
"github.com/open-component-model/ocm/cmds/ocm/commands/verbs/transfer"
Expand All @@ -47,6 +48,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/topics/common/attributes"
topicconfig "github.com/open-component-model/ocm/cmds/ocm/topics/common/config"
topicocirefs "github.com/open-component-model/ocm/cmds/ocm/topics/oci/refs"
topicocmaccessmethods "github.com/open-component-model/ocm/cmds/ocm/topics/ocm/accessmethods"
topicocmrefs "github.com/open-component-model/ocm/cmds/ocm/topics/ocm/refs"
topicbootstrap "github.com/open-component-model/ocm/cmds/ocm/topics/toi/bootstrapping"
"github.com/open-component-model/ocm/pkg/cobrautils"
Expand All @@ -56,7 +58,7 @@ import (
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/contexts/datacontext/attrs/vfsattr"
datacfg "github.com/open-component-model/ocm/pkg/contexts/datacontext/config/attrs"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/registration"
"github.com/open-component-model/ocm/pkg/contexts/ocm/utils"
"github.com/open-component-model/ocm/pkg/errors"
ocmlog "github.com/open-component-model/ocm/pkg/logging"
Expand Down Expand Up @@ -191,6 +193,7 @@ func newCliCommand(opts *CLIOptions, mod ...func(clictx.Context, *cobra.Command)
cmd.AddCommand(download.NewCommand(opts.Context))
cmd.AddCommand(bootstrap.NewCommand(opts.Context))
cmd.AddCommand(clean.NewCommand(opts.Context))
cmd.AddCommand(install.NewCommand(opts.Context))

cmd.AddCommand(cmdutils.HideCommand(componentarchive.NewCommand(opts.Context)))
cmd.AddCommand(cmdutils.HideCommand(resources.NewCommand(opts.Context)))
Expand Down Expand Up @@ -220,12 +223,14 @@ func newCliCommand(opts *CLIOptions, mod ...func(clictx.Context, *cobra.Command)
cmd.AddCommand(topicconfig.New(ctx))
cmd.AddCommand(topicocirefs.New(ctx))
cmd.AddCommand(topicocmrefs.New(ctx))
cmd.AddCommand(topicocmaccessmethods.New(ctx))
cmd.AddCommand(attributes.New(ctx))
cmd.AddCommand(topicbootstrap.New(ctx, "toi-bootstrapping"))

help.AddCommand(topicconfig.New(ctx))
help.AddCommand(topicocirefs.New(ctx))
help.AddCommand(topicocmrefs.New(ctx))
help.AddCommand(topicocmaccessmethods.New(ctx))
help.AddCommand(topicbootstrap.New(ctx, "toi-bootstrapping"))

for _, m := range mod {
Expand Down Expand Up @@ -347,7 +352,7 @@ func (o *CLIOptions) Complete() error {
}
_ = ctx.ApplyConfig(spec, "cli")
}
return plugincacheattr.Get(o.Context.OCMContext()).RegisterExtensions()
return registration.RegisterExtensions(o.Context.OCMContext())
}

func NewVersionCommand(ctx clictx.Context) *cobra.Command {
Expand Down
3 changes: 3 additions & 0 deletions cmds/ocm/commands/ocmcmds/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/sources"
"github.com/open-component-model/ocm/cmds/ocm/commands/ocmcmds/versions"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
topicocmaccessmethods "github.com/open-component-model/ocm/cmds/ocm/topics/ocm/accessmethods"
topicocmrefs "github.com/open-component-model/ocm/cmds/ocm/topics/ocm/refs"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
)
Expand All @@ -39,5 +40,7 @@ func NewCommand(ctx clictx.Context) *cobra.Command {
cmd.AddCommand(plugins.NewCommand(ctx))

cmd.AddCommand(topicocmrefs.New(ctx))
cmd.AddCommand(topicocmaccessmethods.New(ctx))

return cmd
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
package pluginhdlr

import (
"strings"

"github.com/open-component-model/ocm/cmds/ocm/pkg/output"
"github.com/open-component-model/ocm/cmds/ocm/pkg/utils"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm/attrs/plugincacheattr"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/plugins"
"github.com/open-component-model/ocm/pkg/contexts/ocm/plugin/ppi"
"github.com/open-component-model/ocm/pkg/errors"
)
Expand Down Expand Up @@ -65,7 +68,22 @@ func (h *TypeHandler) Get(elemspec utils.ElemSpec) ([]output.Object, error) {

p := cache.Get(elemspec.String())
if p == nil {
return nil, errors.ErrNotFound(ppi.KIND_PLUGIN, elemspec.String())
objs := Lookup(elemspec.String(), cache)
if len(objs) == 0 {
return nil, errors.ErrNotFound(ppi.KIND_PLUGIN, elemspec.String())
}
return objs, nil
}
return []output.Object{&Object{p}}, nil
}

func Lookup(prefix string, cache plugins.Set) []output.Object {
var objs []output.Object
prefix = prefix + "."
for _, n := range cache.PluginNames() {
if strings.HasPrefix(n, prefix) {
objs = append(objs, &Object{cache.Get(n)})
}
}
return []output.Object{p}, nil
return objs
}
124 changes: 124 additions & 0 deletions cmds/ocm/commands/ocmcmds/common/options/uploaderoption/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package uploaderoption

import (
"encoding/json"
"fmt"
"strings"

"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/spf13/pflag"
"sigs.k8s.io/yaml"

"github.com/open-component-model/ocm/cmds/ocm/pkg/options"
"github.com/open-component-model/ocm/pkg/cobrautils/flag"
"github.com/open-component-model/ocm/pkg/contexts/clictx"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/contexts/ocm/registration"
"github.com/open-component-model/ocm/pkg/errors"
)

func From(o options.OptionSetProvider) *Option {
var opt *Option
o.AsOptionSet().Get(&opt)
return opt
}

type Registration struct {
Name string
ArtefactType string
MediaType string
Config json.RawMessage
}

func New() *Option {
return &Option{}
}

type Option struct {
spec map[string]string
Registrations []*Registration
}

func (o *Option) AddFlags(fs *pflag.FlagSet) {
flag.StringToStringVarP(fs, &o.spec, "uploader", "", nil, "repository uploader (<name>:<artefact type>:<media type>=<JSON target config)")
}

func (o *Option) Complete(ctx clictx.Context) error {
desc := "<name>[:<artefact type>[:<media type>]]"
for n, v := range o.spec {
nam := n
art := ""
med := ""
i := strings.Index(nam, ":")
if i >= 0 {
art = nam[i+1:]
nam = nam[:i]
i = strings.Index(art, ":")
if i >= 0 {
med = art[i+1:]
art = art[:i]
i = strings.Index(med, ":")
if i >= 0 {
return fmt.Errorf("invalid uploader registration %s must be of %s", n, desc)
}
}
}

var data json.RawMessage
var err error
if strings.HasPrefix(v, "@") {
data, err = vfs.ReadFile(ctx.FileSystem(), v[1:])
if err != nil {
return errors.Wrapf(err, "cannot read upload target specification from %q", v[1:])
}
} else {
var values interface{}
if err = yaml.Unmarshal([]byte(v), &values); err != nil {
return errors.Wrapf(err, "invalid target specification %q", v)
}
data, err = json.Marshal(values)
if err != nil {
return errors.Wrapf(err, "cannot marshal target specification")
}
}
o.Registrations = append(o.Registrations, &Registration{
Name: nam,
ArtefactType: art,
MediaType: med,
Config: data,
})
}
return nil
}

func (o *Option) Register(ctx ocm.ContextProvider) error {
for _, s := range o.Registrations {
err := registration.RegisterBlobHandlerByName(ctx.OCMContext(), s.Name, s.Config,
registration.ForArtefactType(s.ArtefactType), registration.ForMimeType(s.MediaType))
if err != nil {
return err
}
}
return nil
}

func (o *Option) Usage() string {
s := `
If the <code>--uploader</code> option is specified, appropriate uploaders
are configured for the transport target. It has the following format
<center>
<pre>&lt;name>:&lt;artefact type>:&lt;media type>=&lt;yaml target config></pre>
</center>
The uploader name may be a path expression with the following possibilities:
- <code>ocm/ociRegistry</code>: oci Registry upload for local OCI artefact blobs.
The media type is optional. If given ist must be an OCI artefact media type.
- <code>plugin/<plugin name>[/<uploader name]</code>: uploader provided by plugin.
`
return s
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0

package uploaderoption

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Uploader Option Suite")
}
Loading

0 comments on commit beb2097

Please sign in to comment.