Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding opm CLI #96

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

MOD_FLAGS := $(shell (go version | grep -q -E "1\.(11|12)") && echo -mod=vendor)
CMDS := $(addprefix bin/, $(shell go list $(MOD_FLAGS) ./cmd/... | xargs -I{} basename {}))
CMDS := $(addprefix bin/, $(shell ls ./cmd))

.PHONY: build test vendor clean

22 changes: 22 additions & 0 deletions cmd/opm/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/operator-framework/operator-registry/cmd/opm/registry"
)

func main() {
rootCmd := &cobra.Command{
Use: "opm",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

Short: "operator package manager",
Long: "CLI to interact with operator-registry and build indexes of operator content",
}

rootCmd.AddCommand(registry.NewOpmRegistryCmd())

if err := rootCmd.Execute(); err != nil {
logrus.Panic(err.Error())
}
}
26 changes: 26 additions & 0 deletions cmd/opm/registry/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package registry

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// NewOpmRegistryCmd returns the appregistry-server command
func NewOpmRegistryCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "registry",
Short: "interact with operator-registry database",
Long: `interact with operator-registry database building, modifying and/or serving the operator-registry database`,

PreRunE: func(cmd *cobra.Command, args []string) error {
if debug, _ := cmd.Flags().GetBool("debug"); debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},
}

rootCmd.AddCommand(newRegistryServeCmd())

return rootCmd
}
100 changes: 100 additions & 0 deletions cmd/opm/registry/serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package registry

import (
"context"
"net"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

"github.com/operator-framework/operator-registry/pkg/api"
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
"github.com/operator-framework/operator-registry/pkg/lib/log"
"github.com/operator-framework/operator-registry/pkg/registry"
"github.com/operator-framework/operator-registry/pkg/server"
"github.com/operator-framework/operator-registry/pkg/sqlite"
)

func newRegistryServeCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "serve",
Short: "serve an operator-registry database",
Long: `serve an operator-registry database that is queriable using grpc`,

PreRunE: func(cmd *cobra.Command, args []string) error {
if debug, _ := cmd.Flags().GetBool("debug"); debug {
logrus.SetLevel(logrus.DebugLevel)
}
return nil
},

RunE: runRegistryServeCmdFunc,
}

rootCmd.Flags().Bool("debug", false, "enable debug logging")
rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to sqlite db")
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")

return rootCmd

}

func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error {
// Immediately set up termination log
terminationLogPath, err := cmd.Flags().GetString("termination-log")
if err != nil {
return err
}
err = log.AddDefaultWriterHooks(terminationLogPath)
if err != nil {
logrus.WithError(err).Warn("unable to set termination log path")
}
dbName, err := cmd.Flags().GetString("database")
if err != nil {
return err
}

port, err := cmd.Flags().GetString("port")
if err != nil {
return err
}

logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port})

var store registry.Query
store, err = sqlite.NewSQLLiteQuerier(dbName)
if err != nil {
logger.WithError(err).Warnf("failed to load db")
}
if store == nil {
store = registry.NewEmptyQuerier()
}

// sanity check that the db is available
tables, err := store.ListTables(context.TODO())
if err != nil {
logger.WithError(err).Warnf("couldn't list tables in db")
}
if len(tables) == 0 {
logger.Warn("no tables found in db")
}

lis, err := net.Listen("tcp", ":"+port)
if err != nil {
logger.Fatalf("failed to listen: %s", err)
}
s := grpc.NewServer()

api.RegisterRegistryServer(s, server.NewRegistryServer(store))
health.RegisterHealthServer(s, server.NewHealthServer())
reflection.Register(s)
logger.Info("serving registry")
if err := s.Serve(lis); err != nil {
logger.Fatalf("failed to serve: %s", err)
}

return nil
}