Skip to content

Commit b81f428

Browse files
authoredOct 17, 2019
Merge pull request #96 from gallettilance/opm-cli
Adding opm CLI
2 parents b58d8a6 + 958896d commit b81f428

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed
 

‎Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
12
MOD_FLAGS := $(shell (go version | grep -q -E "1\.(11|12)") && echo -mod=vendor)
2-
CMDS := $(addprefix bin/, $(shell go list $(MOD_FLAGS) ./cmd/... | xargs -I{} basename {}))
3+
CMDS := $(addprefix bin/, $(shell ls ./cmd))
34

45
.PHONY: build test vendor clean
56

‎cmd/opm/main.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
7+
"github.com/operator-framework/operator-registry/cmd/opm/registry"
8+
)
9+
10+
func main() {
11+
rootCmd := &cobra.Command{
12+
Use: "opm",
13+
Short: "operator package manager",
14+
Long: "CLI to interact with operator-registry and build indexes of operator content",
15+
}
16+
17+
rootCmd.AddCommand(registry.NewOpmRegistryCmd())
18+
19+
if err := rootCmd.Execute(); err != nil {
20+
logrus.Panic(err.Error())
21+
}
22+
}

‎cmd/opm/registry/cmd.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package registry
2+
3+
import (
4+
"github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
// NewOpmRegistryCmd returns the appregistry-server command
9+
func NewOpmRegistryCmd() *cobra.Command {
10+
rootCmd := &cobra.Command{
11+
Use: "registry",
12+
Short: "interact with operator-registry database",
13+
Long: `interact with operator-registry database building, modifying and/or serving the operator-registry database`,
14+
15+
PreRunE: func(cmd *cobra.Command, args []string) error {
16+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
17+
logrus.SetLevel(logrus.DebugLevel)
18+
}
19+
return nil
20+
},
21+
}
22+
23+
rootCmd.AddCommand(newRegistryServeCmd())
24+
25+
return rootCmd
26+
}

‎cmd/opm/registry/serve.go

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package registry
2+
3+
import (
4+
"context"
5+
"net"
6+
7+
"github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
9+
"google.golang.org/grpc"
10+
"google.golang.org/grpc/reflection"
11+
12+
"github.com/operator-framework/operator-registry/pkg/api"
13+
health "github.com/operator-framework/operator-registry/pkg/api/grpc_health_v1"
14+
"github.com/operator-framework/operator-registry/pkg/lib/log"
15+
"github.com/operator-framework/operator-registry/pkg/registry"
16+
"github.com/operator-framework/operator-registry/pkg/server"
17+
"github.com/operator-framework/operator-registry/pkg/sqlite"
18+
)
19+
20+
func newRegistryServeCmd() *cobra.Command {
21+
rootCmd := &cobra.Command{
22+
Use: "serve",
23+
Short: "serve an operator-registry database",
24+
Long: `serve an operator-registry database that is queriable using grpc`,
25+
26+
PreRunE: func(cmd *cobra.Command, args []string) error {
27+
if debug, _ := cmd.Flags().GetBool("debug"); debug {
28+
logrus.SetLevel(logrus.DebugLevel)
29+
}
30+
return nil
31+
},
32+
33+
RunE: runRegistryServeCmdFunc,
34+
}
35+
36+
rootCmd.Flags().Bool("debug", false, "enable debug logging")
37+
rootCmd.Flags().StringP("database", "d", "bundles.db", "relative path to sqlite db")
38+
rootCmd.Flags().StringP("port", "p", "50051", "port number to serve on")
39+
rootCmd.Flags().StringP("termination-log", "t", "/dev/termination-log", "path to a container termination log file")
40+
41+
return rootCmd
42+
43+
}
44+
45+
func runRegistryServeCmdFunc(cmd *cobra.Command, args []string) error {
46+
// Immediately set up termination log
47+
terminationLogPath, err := cmd.Flags().GetString("termination-log")
48+
if err != nil {
49+
return err
50+
}
51+
err = log.AddDefaultWriterHooks(terminationLogPath)
52+
if err != nil {
53+
logrus.WithError(err).Warn("unable to set termination log path")
54+
}
55+
dbName, err := cmd.Flags().GetString("database")
56+
if err != nil {
57+
return err
58+
}
59+
60+
port, err := cmd.Flags().GetString("port")
61+
if err != nil {
62+
return err
63+
}
64+
65+
logger := logrus.WithFields(logrus.Fields{"database": dbName, "port": port})
66+
67+
var store registry.Query
68+
store, err = sqlite.NewSQLLiteQuerier(dbName)
69+
if err != nil {
70+
logger.WithError(err).Warnf("failed to load db")
71+
}
72+
if store == nil {
73+
store = registry.NewEmptyQuerier()
74+
}
75+
76+
// sanity check that the db is available
77+
tables, err := store.ListTables(context.TODO())
78+
if err != nil {
79+
logger.WithError(err).Warnf("couldn't list tables in db")
80+
}
81+
if len(tables) == 0 {
82+
logger.Warn("no tables found in db")
83+
}
84+
85+
lis, err := net.Listen("tcp", ":"+port)
86+
if err != nil {
87+
logger.Fatalf("failed to listen: %s", err)
88+
}
89+
s := grpc.NewServer()
90+
91+
api.RegisterRegistryServer(s, server.NewRegistryServer(store))
92+
health.RegisterHealthServer(s, server.NewHealthServer())
93+
reflection.Register(s)
94+
logger.Info("serving registry")
95+
if err := s.Serve(lis); err != nil {
96+
logger.Fatalf("failed to serve: %s", err)
97+
}
98+
99+
return nil
100+
}

0 commit comments

Comments
 (0)
Please sign in to comment.