From 038ba8edaaf12a41361352f2bfc8f2cc385c0911 Mon Sep 17 00:00:00 2001 From: Paul Lorenz Date: Thu, 10 Mar 2022 12:57:05 -0500 Subject: [PATCH] Update deps and change. Add fabric db ops. Add ziti agent command --- CHANGELOG.md | 16 ++ go.mod | 6 +- go.sum | 12 +- ziti-controller/subcmd/run.go | 19 ++- ziti/cmd/ziti/cmd/agent.go | 88 +++++++++++ ...add_route.go => agent_router_add_route.go} | 17 +-- .../{ps_router_basic.go => agent_simple.go} | 18 +-- ziti/cmd/ziti/cmd/cmd.go | 4 +- ziti/cmd/ziti/cmd/common.go | 5 - ziti/cmd/ziti/cmd/edge/traceroute.go | 12 +- ziti/cmd/ziti/cmd/fabric/db.go | 24 +++ .../cmd/ziti/cmd/fabric/db_check_integrity.go | 140 ++++++++++++++++++ ziti/cmd/ziti/cmd/fabric/db_snapshot.go | 60 ++++++++ ziti/cmd/ziti/cmd/fabric/root.go | 1 + ziti/cmd/ziti/cmd/ps.go | 58 ++------ .../ziti/cmd/ps_clear_channel_log_level.go | 13 +- ziti/cmd/ziti/cmd/ps_gc.go | 13 +- ziti/cmd/ziti/cmd/ps_get.go | 11 +- ziti/cmd/ziti/cmd/ps_goversion.go | 13 +- ziti/cmd/ziti/cmd/ps_memstats.go | 13 +- ziti/cmd/ziti/cmd/ps_pprof_cpu.go | 13 +- ziti/cmd/ziti/cmd/ps_pprof_heap.go | 11 +- ziti/cmd/ziti/cmd/ps_router_reconnect.go | 73 --------- ziti/cmd/ziti/cmd/ps_set_channel_log_level.go | 13 +- ziti/cmd/ziti/cmd/ps_set_log_level.go | 13 +- ziti/cmd/ziti/cmd/ps_setgc.go | 13 +- ziti/cmd/ziti/cmd/ps_stack.go | 13 +- ziti/cmd/ziti/cmd/ps_stats.go | 13 +- ziti/cmd/ziti/cmd/ps_trace.go | 11 +- ziti/cmd/ziti/cmd/pscmd.go | 113 -------------- ziti/cmd/ziti/cmd/version.go | 22 +-- 31 files changed, 481 insertions(+), 370 deletions(-) create mode 100644 ziti/cmd/ziti/cmd/agent.go rename ziti/cmd/ziti/cmd/{ps_router_add_route.go => agent_router_add_route.go} (86%) rename ziti/cmd/ziti/cmd/{ps_router_basic.go => agent_simple.go} (75%) create mode 100644 ziti/cmd/ziti/cmd/fabric/db.go create mode 100644 ziti/cmd/ziti/cmd/fabric/db_check_integrity.go create mode 100644 ziti/cmd/ziti/cmd/fabric/db_snapshot.go delete mode 100644 ziti/cmd/ziti/cmd/ps_router_reconnect.go delete mode 100644 ziti/cmd/ziti/cmd/pscmd.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f88fe601..ac382a583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,19 @@ +# Release 0.25.2 + +## Deprecations +The Ziti Edge management REST `/database` and `/terminators` endpoints are being deprecated. They belong in the +fabric management API, but there was no fabric REST api at the time when they were added. Now that they are +available under fabric, they will be removed from the edge APIs in a future release, v0.26 or later. + +## What's New + +* Enhancement: Only translate router ids -> names in `ziti edge traceroute` when requested to with flag +* Enhancement: Add the /database rest API from edge to fabric, where they below + * `ziti fabric db` now as the same commands as `ziti edge db` +* Enhancement: Add `ziti agent` command for sending IPC commands. Contains copy of what was under `ziti ps`. +* Enhancement: Add `ziti agent controller snapshot-db ` IPC command + + # Release 0.25.1 * Bug fix: Fix panic caused by race condition at router start up diff --git a/go.mod b/go.mod index ecc92df7e..d5f31b50d 100644 --- a/go.mod +++ b/go.mod @@ -158,7 +158,7 @@ require ( golang.org/x/image v0.0.0-20191206065243-da761ea9ff43 // indirect golang.org/x/mod v0.5.1 // indirect golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect - golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 // indirect + golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/tools v0.1.7 // indirect @@ -173,7 +173,7 @@ require ( require ( github.com/jedib0t/go-pretty/v6 v6.2.4 - github.com/openziti/edge v0.21.124 - github.com/openziti/fabric v0.17.61 + github.com/openziti/edge v0.21.127 + github.com/openziti/fabric v0.17.62 github.com/rivo/uniseg v0.2.0 // indirect ) diff --git a/go.sum b/go.sum index edd2503d5..308145830 100644 --- a/go.sum +++ b/go.sum @@ -824,10 +824,10 @@ github.com/openziti/channel v0.18.14 h1:MS6Wfo0BklKI/IQ/HQl+wKJBw9rPimTSSqFIeBcs github.com/openziti/channel v0.18.14/go.mod h1:F370DYbKEKpIEhRaZVbEK6xN+pirSZx4qnwbObQAV2I= github.com/openziti/dilithium v0.3.3 h1:PLgQ6PMNLSTzCFbX/h98cmudgz/cU6TmjdSv5NAPD8k= github.com/openziti/dilithium v0.3.3/go.mod h1:vsCjI2AU/hon9e+dLhUFbCNGesJDj2ASgkySOcpmvjo= -github.com/openziti/edge v0.21.124 h1:+GmkJ/4HFXNeOVv4zn2gmZEURGDBuDi3N8lMzwsw6sI= -github.com/openziti/edge v0.21.124/go.mod h1:qZ4Jn3Y0POBdIQR03maD+GAzNI7C5W57aiBiFE5c7is= -github.com/openziti/fabric v0.17.61 h1:3sWTJdZPfHOsyxGWA0e5D7fQhjaZWRoB8dj9JHfqaPQ= -github.com/openziti/fabric v0.17.61/go.mod h1:8oBcmWriG/nTGF/+8pkv1oAKQY97XXG4ptns8dUTMr4= +github.com/openziti/edge v0.21.127 h1:4Z2AlmSjMgw/a0PQt4aYuVIURW0nQVwnR0mSvgyVZcc= +github.com/openziti/edge v0.21.127/go.mod h1:NIOrmA5yEhdTbmARyPm4DOrdpWiaB33OVpJVj4PkEDY= +github.com/openziti/fabric v0.17.62 h1:V1Eavso4AvMSCMXJYtEATu5NQ9Dtpu3s3OYZt+SuVwY= +github.com/openziti/fabric v0.17.62/go.mod h1:8oBcmWriG/nTGF/+8pkv1oAKQY97XXG4ptns8dUTMr4= github.com/openziti/foundation v0.17.8 h1:vlX6yQHrs5Tcp39xHylJhzohTDOZ6d/VrVlQahw7GBo= github.com/openziti/foundation v0.17.8/go.mod h1:dTaJgdO5OqnKpV+0OPHpnTnqfPpqNGbhJv8O6pMot6o= github.com/openziti/sdk-golang v0.16.20 h1:AtSaD0Do5HgkDWFR0dLUVpiOpky2WAh6cf78hcG30jk= @@ -1382,8 +1382,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7 h1:8IVLkfbr2cLhv0a/vKq4UFUcJym8RmDoDboxCFWEjYE= -golang.org/x/sys v0.0.0-20220307203707-22a9840ba4d7/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng= +golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY= diff --git a/ziti-controller/subcmd/run.go b/ziti-controller/subcmd/run.go index c7878abed..aa25cb8cb 100644 --- a/ziti-controller/subcmd/run.go +++ b/ziti-controller/subcmd/run.go @@ -25,6 +25,7 @@ import ( "github.com/openziti/ziti/common/version" "github.com/sirupsen/logrus" "github.com/spf13/cobra" + "io" "os" "os/signal" "syscall" @@ -59,12 +60,6 @@ func run(cmd *cobra.Command, args []string) { startLogger = startLogger.WithField("nodeId", config.Id.Token) startLogger.Info("starting ziti-controller") - if cliAgentEnabled { - if err := agent.Listen(agent.Options{Addr: cliAgentAddr}); err != nil { - pfxlog.Logger().WithError(err).Error("unable to start CLI agent") - } - } - var fabricController *controller.Controller if fabricController, err = controller.NewController(config, version.GetCmdBuildInfo()); err != nil { fmt.Printf("unable to create fabric controller %+v\n", err) @@ -78,6 +73,18 @@ func run(cmd *cobra.Command, args []string) { } edgeController.Initialize() + + if cliAgentEnabled { + options := agent.Options{Addr: cliAgentAddr} + fabricController.RegisterDefaultAgentOps() + options.CustomOps = map[byte]func(conn io.ReadWriter) error{ + agent.CustomOp: fabricController.HandleCustomAgentOp, + } + if err := agent.Listen(options); err != nil { + pfxlog.Logger().WithError(err).Error("unable to start CLI agent") + } + } + go waitForShutdown(fabricController, edgeController) edgeController.Run() if err := fabricController.Run(); err != nil { diff --git a/ziti/cmd/ziti/cmd/agent.go b/ziti/cmd/ziti/cmd/agent.go new file mode 100644 index 000000000..38dd96693 --- /dev/null +++ b/ziti/cmd/ziti/cmd/agent.go @@ -0,0 +1,88 @@ +/* + Copyright NetFoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package cmd + +import ( + "github.com/openziti/edge/router/debugops" + "github.com/openziti/fabric/controller" + "github.com/openziti/fabric/router" + cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" + "github.com/spf13/cobra" + "io" +) + +type AgentAppId byte + +const ( + AgentAppController = AgentAppId(controller.AgentAppId) + AgentAppRouter = AgentAppId(router.AgentAppId) +) + +func NewAgentCmd(out io.Writer, errOut io.Writer) *cobra.Command { + agentCmd := &cobra.Command{ + Use: "agent", + Short: "Interact with ziti processes using the the IPC agent", + Run: func(cmd *cobra.Command, args []string) { + cmdhelper.CheckErr(cmd.Help()) + }, + } + + ctrlCmd := &cobra.Command{ + Use: "controller", + Aliases: []string{"c"}, + Short: "Interact with a ziti-controller process using the IPC agent", + Run: func(cmd *cobra.Command, args []string) { + cmdhelper.CheckErr(cmd.Help()) + }, + } + + agentCmd.AddCommand(ctrlCmd) + ctrlCmd.AddCommand(NewSimpleAgentCustomCmd("snapshot-db", AgentAppController, controller.AgentOpSnapshotDbSnaps, out, errOut)) + + routerCmd := &cobra.Command{ + Use: "router", + Aliases: []string{"r"}, + Short: "Interact with a ziti-router process using the IPC agent", + Run: func(cmd *cobra.Command, args []string) { + cmdhelper.CheckErr(cmd.Help()) + }, + } + + agentCmd.AddCommand(routerCmd) + + routerCmd.AddCommand(NewCmdPsRoute(out, errOut)) + routerCmd.AddCommand(NewSimpleAgentCustomCmd("dump-routes", AgentAppRouter, router.DumpForwarderTables, out, errOut)) + routerCmd.AddCommand(NewSimpleAgentCustomCmd("disconnect", AgentAppRouter, router.CloseControlChannel, out, errOut)) + routerCmd.AddCommand(NewSimpleAgentCustomCmd("reconnect", AgentAppRouter, router.OpenControlChannel, out, errOut)) + routerCmd.AddCommand(NewSimpleAgentCustomCmd("dump-api-sessions", AgentAppRouter, debugops.DumpApiSessions, out, errOut)) + routerCmd.AddCommand(NewSimpleAgentCustomCmd("dump-links", AgentAppRouter, router.DumpLinks, out, errOut)) + + agentCmd.AddCommand(NewCmdPsGoversion(out, errOut)) + agentCmd.AddCommand(NewCmdPsGc(out, errOut)) + agentCmd.AddCommand(NewCmdPsSetgc(out, errOut)) + agentCmd.AddCommand(NewCmdPsStack(out, errOut)) + agentCmd.AddCommand(NewCmdPsMemstats(out, errOut)) + agentCmd.AddCommand(NewCmdPsStats(out, errOut)) + agentCmd.AddCommand(NewCmdPsPprofHeap(out, errOut)) + agentCmd.AddCommand(NewCmdPsPprofCpu(out, errOut)) + agentCmd.AddCommand(NewCmdPsTrace(out, errOut)) + agentCmd.AddCommand(NewCmdPsSetLogLevel(out, errOut)) + agentCmd.AddCommand(NewCmdPsSetChannelLogLevel(out, errOut)) + agentCmd.AddCommand(NewCmdPsClearChannelLogLevel(out, errOut)) + + return agentCmd +} diff --git a/ziti/cmd/ziti/cmd/ps_router_add_route.go b/ziti/cmd/ziti/cmd/agent_router_add_route.go similarity index 86% rename from ziti/cmd/ziti/cmd/ps_router_add_route.go rename to ziti/cmd/ziti/cmd/agent_router_add_route.go index ef4b677da..19cb804a8 100644 --- a/ziti/cmd/ziti/cmd/ps_router_add_route.go +++ b/ziti/cmd/ziti/cmd/agent_router_add_route.go @@ -22,7 +22,6 @@ import ( "github.com/openziti/fabric/pb/ctrl_pb" "github.com/openziti/fabric/router" "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -36,13 +35,12 @@ type PsRouteOptions struct { } // NewCmdPsRoute creates a command object for the "create" command -func NewCmdPsRoute(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsRoute(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsRouteOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } @@ -92,14 +90,15 @@ func (o *PsRouteOptions) Run() error { return err } - fullBuf := make([]byte, len(buf)+5) - fullBuf[0] = router.UpdateRoute + fullBuf := make([]byte, len(buf)+6) + fullBuf[0] = byte(AgentAppRouter) + fullBuf[1] = router.UpdateRoute sizeBuf := make([]byte, 4) binary.LittleEndian.PutUint32(sizeBuf, uint32(len(buf))) - copy(fullBuf[1:], sizeBuf) - copy(fullBuf[5:], buf) + copy(fullBuf[2:], sizeBuf) + copy(fullBuf[6:], buf) return agent.MakeRequest(addr, agent.CustomOp, fullBuf, os.Stdout) } diff --git a/ziti/cmd/ziti/cmd/ps_router_basic.go b/ziti/cmd/ziti/cmd/agent_simple.go similarity index 75% rename from ziti/cmd/ziti/cmd/ps_router_basic.go rename to ziti/cmd/ziti/cmd/agent_simple.go index 93cb5f7de..f5257f473 100644 --- a/ziti/cmd/ziti/cmd/ps_router_basic.go +++ b/ziti/cmd/ziti/cmd/agent_simple.go @@ -18,24 +18,22 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" "os" ) -type PsBasicRouterCmdOptions struct { +type SimpleAgentCmdOptions struct { PsOptions } -func NewCmdPsBasicRouterCmd(name string, op byte, f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { - options := &PsBasicRouterCmdOptions{ +func NewSimpleAgentCustomCmd(name string, appId AgentAppId, op byte, out io.Writer, errOut io.Writer) *cobra.Command { + options := &SimpleAgentCmdOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } @@ -46,7 +44,7 @@ func NewCmdPsBasicRouterCmd(name string, op byte, f cmdutil.Factory, out io.Writ Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args - err := options.Run(op) + err := options.Run(appId, op) cmdhelper.CheckErr(err) }, } @@ -57,13 +55,13 @@ func NewCmdPsBasicRouterCmd(name string, op byte, f cmdutil.Factory, out io.Writ } // Run implements the command -func (o *PsBasicRouterCmdOptions) Run(op byte) error { +func (o *SimpleAgentCmdOptions) Run(appId AgentAppId, op byte) error { addr, err := agent.ParseGopsAddress(o.Args) if err != nil { return err } - buf := []byte{op} + buf := []byte{byte(appId), op} return agent.MakeRequest(addr, agent.CustomOp, buf, os.Stdout) } diff --git a/ziti/cmd/ziti/cmd/cmd.go b/ziti/cmd/ziti/cmd/cmd.go index c8fb557f6..d8e361f5d 100644 --- a/ziti/cmd/ziti/cmd/cmd.go +++ b/ziti/cmd/ziti/cmd/cmd.go @@ -131,7 +131,8 @@ func NewCmdRoot(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Comm createCommands := NewCmdCreate(f, out, err) updateCommands := NewCmdUpdate(f, out, err) executeCommands := NewCmdExecute(f, out, err) - psCommands := NewCmdPs(f, out, err) + agentCommands := NewAgentCmd(out, err) + psCommands := NewCmdPs(out, err) pkiCommands := NewCmdPKI(f, out, err) fabricCommand := fabric.NewFabricCmd(p) edgeCommand := edge.NewCmdEdge(f, out, err) @@ -161,6 +162,7 @@ func NewCmdRoot(f cmdutil.Factory, in io.Reader, out, err io.Writer) *cobra.Comm Message: "Executing Ziti components:", Commands: []*cobra.Command{ executeCommands, + agentCommands, psCommands, pkiCommands, unwrapIdentityFileCommand, diff --git a/ziti/cmd/ziti/cmd/common.go b/ziti/cmd/ziti/cmd/common.go index 3f675dd09..a83fdbd3a 100644 --- a/ziti/cmd/ziti/cmd/common.go +++ b/ziti/cmd/ziti/cmd/common.go @@ -19,7 +19,6 @@ package cmd import ( "fmt" cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" - "github.com/openziti/ziti/ziti/cmd/ziti/cmd/table" "github.com/openziti/ziti/ziti/cmd/ziti/internal/log" "github.com/spf13/cobra" "io" @@ -57,10 +56,6 @@ func (c *CommonOptions) Stdout() io.Writer { return os.Stdout } -func (c *CommonOptions) CreateTable() table.Table { - return c.Factory.CreateTable(c.Stdout()) -} - // Debugf outputs the given text to the console if verbose mode is enabled func (c *CommonOptions) Debugf(format string, a ...interface{}) { if c.Verbose { diff --git a/ziti/cmd/ziti/cmd/edge/traceroute.go b/ziti/cmd/ziti/cmd/edge/traceroute.go index 3d370d3bf..8c49a10e4 100644 --- a/ziti/cmd/ziti/cmd/edge/traceroute.go +++ b/ziti/cmd/ziti/cmd/edge/traceroute.go @@ -20,6 +20,7 @@ type traceRouteOptions struct { hops uint8 configFile string timeout time.Duration + lookupRouterName bool } func newTraceRouteCmd(out io.Writer, errOut io.Writer) *cobra.Command { @@ -50,6 +51,7 @@ func newTraceRouteCmd(out io.Writer, errOut io.Writer) *cobra.Command { cmd.Flags().Uint8Var(&options.hops, "hops", 0, "Maximum number of hops") cmd.Flags().DurationVarP(&options.timeout, "timeout", "t", 5*time.Second, "Trace route response timeout") cmd.Flags().BoolVarP(&options.Verbose, "verbose", "", false, "Enable verbose logging") + cmd.Flags().BoolVarP(&options.lookupRouterName, "get-router-names", "n", false, "Lookup and output router names instead of ids. Requires admin privileges") return cmd } @@ -99,10 +101,12 @@ func (o *traceRouteOptions) Run() error { hopLabel := result.HopId if result.HopType == "forwarder" { - hopLabel, err = mapIdToName("transit-routers", result.HopId, o.Options) - if err != nil { - hopLabel = result.HopId - routerNameLookupsFailed = true + if o.lookupRouterName { + hopLabel, err = mapIdToName("transit-routers", result.HopId, o.Options) + if err != nil { + hopLabel = result.HopId + routerNameLookupsFailed = true + } } } diff --git a/ziti/cmd/ziti/cmd/fabric/db.go b/ziti/cmd/ziti/cmd/fabric/db.go new file mode 100644 index 000000000..849db59ff --- /dev/null +++ b/ziti/cmd/ziti/cmd/fabric/db.go @@ -0,0 +1,24 @@ +package fabric + +import ( + "github.com/openziti/ziti/ziti/cmd/ziti/cmd/common" + cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" + "github.com/spf13/cobra" +) + +func newDbCmd(p common.OptionsProvider) *cobra.Command { + cmd := &cobra.Command{ + Use: "db", + Short: "Database management operations for the Ziti Edge Controller", + Run: func(cmd *cobra.Command, args []string) { + err := cmd.Help() + cmdhelper.CheckErr(err) + }, + } + + cmd.AddCommand(newDbSnapshotCmd(p)) + cmd.AddCommand(newDbCheckIntegrityCmd(p)) + cmd.AddCommand(newDbCheckIntegrityStatusCmd(p)) + + return cmd +} diff --git a/ziti/cmd/ziti/cmd/fabric/db_check_integrity.go b/ziti/cmd/ziti/cmd/fabric/db_check_integrity.go new file mode 100644 index 000000000..46d9fb153 --- /dev/null +++ b/ziti/cmd/ziti/cmd/fabric/db_check_integrity.go @@ -0,0 +1,140 @@ +package fabric + +import ( + "fmt" + "github.com/Jeffail/gabs" + "github.com/openziti/ziti/ziti/cmd/ziti/cmd/api" + "github.com/openziti/ziti/ziti/cmd/ziti/cmd/common" + cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" + "github.com/openziti/ziti/ziti/cmd/ziti/util" + "github.com/pkg/errors" + "github.com/spf13/cobra" + "net/http" +) + +type dbCheckIntegrityOptions struct { + api.Options + fix bool +} + +func newDbCheckIntegrityCmd(p common.OptionsProvider) *cobra.Command { + options := &dbCheckIntegrityOptions{ + Options: api.Options{CommonOptions: p()}, + } + + cmd := &cobra.Command{ + Use: "start-check-integrity", + Short: "starts background operation checking integrity of database references and constraints", + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + options.Cmd = cmd + options.Args = args + err := runCheckIntegrityDb(options) + cmdhelper.CheckErr(err) + }, + SuggestFor: []string{}, + } + + // allow interspersing positional args and flags + cmd.Flags().SetInterspersed(true) + cmd.Flags().BoolVarP(&options.fix, "fix-errors", "f", false, "Attempt to fix any detected errors") + options.AddCommonFlags(cmd) + + return cmd +} + +func runCheckIntegrityDb(o *dbCheckIntegrityOptions) error { + var target string + if o.fix { + target = "database/fix-data-integrity" + } else { + target = "database/check-data-integrity" + } + + if _, err := util.ControllerUpdate("edge", target, "", o.Out, http.MethodPost, o.OutputJSONRequest, o.OutputJSONResponse, o.Options.Timeout, o.Options.Verbose); err != nil { + return err + } + + _, err := fmt.Fprint(o.Out, "check integrity operation started\n") + return err +} + +func newDbCheckIntegrityStatusCmd(p common.OptionsProvider) *cobra.Command { + options := &dbCheckIntegrityOptions{ + Options: api.Options{CommonOptions: p()}, + } + + cmd := &cobra.Command{ + Use: "check-integrity-status", + Short: "shows current results from background operation checking integrity of database references and constraints", + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + options.Cmd = cmd + options.Args = args + err := runCheckIntegrityStatus(options) + cmdhelper.CheckErr(err) + }, + SuggestFor: []string{}, + } + + // allow interspersing positional args and flags + cmd.Flags().SetInterspersed(true) + options.AddCommonFlags(cmd) + + return cmd +} + +func runCheckIntegrityStatus(o *dbCheckIntegrityOptions) error { + body, err := util.EdgeControllerList("database/data-integrity-results", nil, o.OutputJSONResponse, o.Out, o.Options.Timeout, o.Options.Verbose) + if err != nil { + return err + } + + data := body.S("data") + inProgress := data.S("inProgress").Data() + if _, err = fmt.Fprintf(o.Out, "In Progress: %v\n", inProgress); err != nil { + return err + } + + fixingErrors := data.S("fixingErrors").Data() + if _, err = fmt.Fprintf(o.Out, "Fixing Errors: %v\n", fixingErrors); err != nil { + return err + } + + tooManyErrors := data.S("tooManyErrors").Data() + if _, err = fmt.Fprintf(o.Out, "Too Many Errors: %v (if true, additional errors can be found in controller log)\n", tooManyErrors); err != nil { + return err + } + + startTime := data.S("startTime").Data() + if _, err = fmt.Fprintf(o.Out, "Started At: %v\n", startTime); err != nil { + return err + } + + endTime := data.S("endTime").Data() + if _, err = fmt.Fprintf(o.Out, "Finished At: %v\n", endTime); err != nil { + return err + } + + opError := data.S("error").Data() + if _, err = fmt.Fprintf(o.Out, "Operation Error: %v\n", opError); err != nil { + return err + } + + results := data.S("results") + children, err := results.Children() + if len(children) == 0 || errors.Is(gabs.ErrNotObjOrArray, err) { + _, err = fmt.Fprintln(o.Out, "no data integrity errors found") + } + + for _, child := range children { + desc := child.S("description").Data().(string) + fixed := child.S("fixed").Data().(bool) + + if _, err = fmt.Fprintf(o.Out, "Issue: %v. Fixed: %v\n", desc, fixed); err != nil { + return err + } + } + + return err +} diff --git a/ziti/cmd/ziti/cmd/fabric/db_snapshot.go b/ziti/cmd/ziti/cmd/fabric/db_snapshot.go new file mode 100644 index 000000000..3c82124ee --- /dev/null +++ b/ziti/cmd/ziti/cmd/fabric/db_snapshot.go @@ -0,0 +1,60 @@ +/* + Copyright NetFoundry, Inc. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package fabric + +import ( + "github.com/openziti/ziti/ziti/cmd/ziti/cmd/api" + "github.com/openziti/ziti/ziti/cmd/ziti/cmd/common" + cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" + "github.com/openziti/ziti/ziti/cmd/ziti/util" + "github.com/spf13/cobra" + "net/http" +) + +type dbSnapshotOptions struct { + api.Options +} + +func newDbSnapshotCmd(p common.OptionsProvider) *cobra.Command { + options := &dbSnapshotOptions{ + Options: api.Options{CommonOptions: p()}, + } + + cmd := &cobra.Command{ + Use: "snapshot", + Short: "creates a database snapshot", + Args: cobra.ExactArgs(0), + Run: func(cmd *cobra.Command, args []string) { + options.Cmd = cmd + options.Args = args + err := runSnapshotDb(options) + cmdhelper.CheckErr(err) + }, + SuggestFor: []string{}, + } + + // allow interspersing positional args and flags + cmd.Flags().SetInterspersed(true) + options.AddCommonFlags(cmd) + + return cmd +} + +func runSnapshotDb(o *dbSnapshotOptions) error { + _, err := util.ControllerUpdate("edge", "database/snapshot", "", o.Out, http.MethodPost, false, false, o.Options.Timeout, o.Options.Verbose) + return err +} diff --git a/ziti/cmd/ziti/cmd/fabric/root.go b/ziti/cmd/ziti/cmd/fabric/root.go index 1061f9ebd..2266757c4 100644 --- a/ziti/cmd/ziti/cmd/fabric/root.go +++ b/ziti/cmd/ziti/cmd/fabric/root.go @@ -33,6 +33,7 @@ func NewFabricCmd(p common.OptionsProvider) *cobra.Command { fabricCmd.AddCommand(newAddIdentityCmd(p), newRemoveIdentityCmd(p)) fabricCmd.AddCommand(newCreateCommand(p), newListCmd(p), newUpdateCommand(p), newDeleteCmd(p)) fabricCmd.AddCommand(newInspectCmd(p)) + fabricCmd.AddCommand(newDbCmd(p)) return fabricCmd } diff --git a/ziti/cmd/ziti/cmd/ps.go b/ziti/cmd/ziti/cmd/ps.go index 3aec9b609..f4a75a29a 100644 --- a/ziti/cmd/ziti/cmd/ps.go +++ b/ziti/cmd/ziti/cmd/ps.go @@ -19,8 +19,6 @@ package cmd import ( "bytes" "fmt" - "github.com/openziti/edge/router/debugops" - "github.com/openziti/fabric/router" "github.com/spf13/cobra" "io" "os" @@ -28,7 +26,6 @@ import ( "strconv" "strings" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/openziti/ziti/ziti/cmd/ziti/cmd/templates" "github.com/openziti/ziti/ziti/goprocess" @@ -52,12 +49,11 @@ var ( ) // NewCmdPs Pss a command object for the "Ps" command -func NewCmdPs(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPs(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, } @@ -73,45 +69,23 @@ func NewCmdPs(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command }, } - routerCmd := &cobra.Command{ - Use: "router", - Short: "Show Ziti router process info", - Long: psLong, - Run: func(cmd *cobra.Command, args []string) { - cmdhelper.CheckErr(cmd.Help()) - }, - } + cmd.AddCommand(NewCmdPsGet(out, errOut)) + cmd.AddCommand(NewCmdPsGoversion(out, errOut)) + cmd.AddCommand(NewCmdPsGc(out, errOut)) + cmd.AddCommand(NewCmdPsSetgc(out, errOut)) + cmd.AddCommand(NewCmdPsStack(out, errOut)) + cmd.AddCommand(NewCmdPsMemstats(out, errOut)) + cmd.AddCommand(NewCmdPsStats(out, errOut)) + cmd.AddCommand(NewCmdPsPprofHeap(out, errOut)) + cmd.AddCommand(NewCmdPsPprofCpu(out, errOut)) + cmd.AddCommand(NewCmdPsTrace(out, errOut)) + cmd.AddCommand(NewCmdPsSetLogLevel(out, errOut)) + cmd.AddCommand(NewCmdPsSetChannelLogLevel(out, errOut)) + cmd.AddCommand(NewCmdPsClearChannelLogLevel(out, errOut)) - cmd.AddCommand(routerCmd) - cmd.AddCommand(NewCmdPsGet(f, out, errOut)) - cmd.AddCommand(NewCmdPsGoversion(f, out, errOut)) - cmd.AddCommand(NewCmdPsGc(f, out, errOut)) - cmd.AddCommand(NewCmdPsSetgc(f, out, errOut)) - cmd.AddCommand(NewCmdPsStack(f, out, errOut)) - cmd.AddCommand(NewCmdPsMemstats(f, out, errOut)) - cmd.AddCommand(NewCmdPsStats(f, out, errOut)) - cmd.AddCommand(NewCmdPsPprofHeap(f, out, errOut)) - cmd.AddCommand(NewCmdPsPprofCpu(f, out, errOut)) - cmd.AddCommand(NewCmdPsTrace(f, out, errOut)) - cmd.AddCommand(NewCmdPsSetLogLevel(f, out, errOut)) - cmd.AddCommand(NewCmdPsSetChannelLogLevel(f, out, errOut)) - cmd.AddCommand(NewCmdPsClearChannelLogLevel(f, out, errOut)) - - routerCmd.AddCommand(NewCmdPsRoute(f, out, errOut)) - routerCmd.AddCommand(NewCmdPsBasicRouterCmd("dump-routes", router.DumpForwarderTables, f, out, errOut)) - routerCmd.AddCommand(NewCmdPsBasicRouterCmd("disconnect", router.CloseControlChannel, f, out, errOut)) - routerCmd.AddCommand(NewCmdPsBasicRouterCmd("reconnect", router.OpenControlChannel, f, out, errOut)) - routerCmd.AddCommand(NewCmdPsBasicRouterCmd("dump-api-sessions", debugops.DumpApiSessions, f, out, errOut)) - routerCmd.AddCommand(NewCmdPsBasicRouterCmd("dump-links", router.DumpLinks, f, out, errOut)) - - options.addPsFlags(cmd) return cmd } -func (options *PsOptions) addPsFlags(cmd *cobra.Command) { - // cmd.Flags().StringVarP(&options.Flags.Identity, "identity", "", "", "Which identity to use.") -} - // Run implements this command func (o *PsOptions) Run() error { fmt.Println("ps is running") diff --git a/ziti/cmd/ziti/cmd/ps_clear_channel_log_level.go b/ziti/cmd/ziti/cmd/ps_clear_channel_log_level.go index ade37810c..391dc1076 100644 --- a/ziti/cmd/ziti/cmd/ps_clear_channel_log_level.go +++ b/ziti/cmd/ziti/cmd/ps_clear_channel_log_level.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -33,20 +32,20 @@ type PsClearChannelLogLevelOptions struct { } // NewCmdPsClearChannelLogLevel creates a command object for the "create" command -func NewCmdPsClearChannelLogLevel(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsClearChannelLogLevel(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsClearChannelLogLevelOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "clear-channel-log-level target channel", - Args: cobra.MinimumNArgs(1), + Use: "clear-channel-log-level target channel", + Short: "Clears a channel-specific log level in the target application", + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_gc.go b/ziti/cmd/ziti/cmd/ps_gc.go index 6eb66f8ba..3b00584b4 100644 --- a/ziti/cmd/ziti/cmd/ps_gc.go +++ b/ziti/cmd/ziti/cmd/ps_gc.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -32,20 +31,20 @@ type PsGcOptions struct { } // NewCmdPsGc creates a command object for the "create" command -func NewCmdPsGc(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsGc(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsGcOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "gc ", - Args: cobra.MaximumNArgs(1), + Use: "gc ", + Short: "Run garbage collection in the target application", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_get.go b/ziti/cmd/ziti/cmd/ps_get.go index d5178b540..546415686 100644 --- a/ziti/cmd/ziti/cmd/ps_get.go +++ b/ziti/cmd/ziti/cmd/ps_get.go @@ -23,7 +23,6 @@ import ( "io" "strconv" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/openziti/ziti/ziti/cmd/ziti/internal/log" ) @@ -36,19 +35,19 @@ type PsGetOptions struct { } // NewCmdPsGet creates a command object for the "create" command -func NewCmdPsGet(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsGet(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsGetOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "get", + Use: "get", + Short: "Returns information about the target process", Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_goversion.go b/ziti/cmd/ziti/cmd/ps_goversion.go index e7ab5dcf5..c846d8e64 100644 --- a/ziti/cmd/ziti/cmd/ps_goversion.go +++ b/ziti/cmd/ziti/cmd/ps_goversion.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -32,20 +31,20 @@ type PsGoversionOptions struct { } // NewCmdPsGoversion creates a command object for the "create" command -func NewCmdPsGoversion(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsGoversion(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsGoversionOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "goversion ", - Args: cobra.MaximumNArgs(1), + Use: "goversion ", + Short: "Returns the golang version of the target application", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_memstats.go b/ziti/cmd/ziti/cmd/ps_memstats.go index a9fbb9df3..733e5e3fc 100644 --- a/ziti/cmd/ziti/cmd/ps_memstats.go +++ b/ziti/cmd/ziti/cmd/ps_memstats.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -32,20 +31,20 @@ type PsMemstatsOptions struct { } // NewCmdPsMemstats creates a command object for the "create" command -func NewCmdPsMemstats(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsMemstats(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsMemstatsOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "memstats ", - Args: cobra.MaximumNArgs(1), + Use: "memstats ", + Short: "Returns memory use summary of the target application", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_pprof_cpu.go b/ziti/cmd/ziti/cmd/ps_pprof_cpu.go index 0f6273bfb..6abb8e9c7 100644 --- a/ziti/cmd/ziti/cmd/ps_pprof_cpu.go +++ b/ziti/cmd/ziti/cmd/ps_pprof_cpu.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -32,20 +31,20 @@ type PsPprofCpuOptions struct { } // NewCmdPsPprofCpu creates a command object for the "create" command -func NewCmdPsPprofCpu(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsPprofCpu(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsPprofCpuOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "pprof-cpu", - Args: cobra.MaximumNArgs(2), + Use: "pprof-cpu", + Short: "Runs and emits a 30 second pprof from the target application", + Args: cobra.MaximumNArgs(2), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_pprof_heap.go b/ziti/cmd/ziti/cmd/ps_pprof_heap.go index 371c38ae3..ca7a383f3 100644 --- a/ziti/cmd/ziti/cmd/ps_pprof_heap.go +++ b/ziti/cmd/ziti/cmd/ps_pprof_heap.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -31,19 +30,19 @@ type PsPprofHeapOptions struct { } // NewCmdPsPprofHeap creates a command object for the "create" command -func NewCmdPsPprofHeap(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsPprofHeap(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsPprofHeapOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "pprof-heap", + Use: "pprof-heap", + Short: "Returns a memory heap pprof of the target application", Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_router_reconnect.go b/ziti/cmd/ziti/cmd/ps_router_reconnect.go deleted file mode 100644 index c4f1a9ce1..000000000 --- a/ziti/cmd/ziti/cmd/ps_router_reconnect.go +++ /dev/null @@ -1,73 +0,0 @@ -/* - Copyright NetFoundry, Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package cmd - -import ( - "github.com/openziti/fabric/router" - "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" - cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" - "github.com/spf13/cobra" - "io" - "os" -) - -// PsRouteOptions the options for the create spring command -type PsRouterReconnectOptions struct { - PsOptions - CtrlListener string -} - -// NewCmdPsRouterReconnect creates a command object for the "router-disconnect" command -func NewCmdPsRouterReconnect(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { - options := &PsRouterReconnectOptions{ - PsOptions: PsOptions{ - CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, - }, - }, - } - - cmd := &cobra.Command{ - Args: cobra.MaximumNArgs(1), - Use: "router-reconnect ", - Run: func(cmd *cobra.Command, args []string) { - options.Cmd = cmd - options.Args = args - err := options.Run() - cmdhelper.CheckErr(err) - }, - } - - options.addCommonFlags(cmd) - - return cmd -} - -// Run implements the command -func (o *PsRouterReconnectOptions) Run() error { - addr, err := agent.ParseGopsAddress(o.Args) - if err != nil { - return err - } - - buf := []byte{router.OpenControlChannel} - - return agent.MakeRequest(addr, agent.CustomOp, buf, os.Stdout) -} diff --git a/ziti/cmd/ziti/cmd/ps_set_channel_log_level.go b/ziti/cmd/ziti/cmd/ps_set_channel_log_level.go index 4a1bd5a54..d3ec563fa 100644 --- a/ziti/cmd/ziti/cmd/ps_set_channel_log_level.go +++ b/ziti/cmd/ziti/cmd/ps_set_channel_log_level.go @@ -20,7 +20,6 @@ import ( "bytes" "encoding/binary" "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -36,20 +35,20 @@ type PsSetChannelLogLevelOptions struct { } // NewCmdPsSetChannelLogLevel creates a command object for the "create" command -func NewCmdPsSetChannelLogLevel(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsSetChannelLogLevel(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsSetChannelLogLevelOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "set-channel-log-level target channel log-level (panic, fatal, error, warn, info, debug, trace)", - Args: cobra.MinimumNArgs(2), + Use: "set-channel-log-level target channel log-level (panic, fatal, error, warn, info, debug, trace)", + Short: "Sets a channel-specific log level in the target application", + Args: cobra.MinimumNArgs(2), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_set_log_level.go b/ziti/cmd/ziti/cmd/ps_set_log_level.go index bf0d473df..347fdd4bc 100644 --- a/ziti/cmd/ziti/cmd/ps_set_log_level.go +++ b/ziti/cmd/ziti/cmd/ps_set_log_level.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -35,20 +34,20 @@ type PsSetLogLevelOptions struct { } // NewCmdPsLogLevel creates a command object for the "create" command -func NewCmdPsSetLogLevel(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsSetLogLevel(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsSetLogLevelOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "set-log-level target log-level (panic, fatal, error, warn, info, debug, trace)", - Args: cobra.MinimumNArgs(1), + Use: "set-log-level target log-level (panic, fatal, error, warn, info, debug, trace)", + Short: "Sets the global logrus logging level in the target application", + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_setgc.go b/ziti/cmd/ziti/cmd/ps_setgc.go index 4acb78e97..3101cb9f8 100644 --- a/ziti/cmd/ziti/cmd/ps_setgc.go +++ b/ziti/cmd/ziti/cmd/ps_setgc.go @@ -19,7 +19,6 @@ package cmd import ( "encoding/binary" "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -34,20 +33,20 @@ type PsSetgcOptions struct { } // NewCmdPsSetgc creates a command object for the "create" command -func NewCmdPsSetgc(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsSetgc(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsSetgcOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "setgc target gc-percentage", - Args: cobra.MinimumNArgs(1), + Use: "setgc target gc-percentage", + Short: "Sets the GC percentage in the target application", + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_stack.go b/ziti/cmd/ziti/cmd/ps_stack.go index 856a9edf2..d55ca6a36 100644 --- a/ziti/cmd/ziti/cmd/ps_stack.go +++ b/ziti/cmd/ziti/cmd/ps_stack.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -34,20 +33,20 @@ type PsStackOptions struct { } // NewCmdPsStack creates a command object for the "create" command -func NewCmdPsStack(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsStack(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsStackOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Args: cobra.MaximumNArgs(1), - Use: "stack []", + Args: cobra.MaximumNArgs(1), + Use: "stack []", + Short: "Emits a go-routine stack dump from the target application", Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_stats.go b/ziti/cmd/ziti/cmd/ps_stats.go index 3b617baf1..485dc1a78 100644 --- a/ziti/cmd/ziti/cmd/ps_stats.go +++ b/ziti/cmd/ziti/cmd/ps_stats.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -32,20 +31,20 @@ type PsStatsOptions struct { } // NewCmdPsStats creates a command object for the "create" command -func NewCmdPsStats(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsStats(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsStatsOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "stats ", - Args: cobra.MaximumNArgs(1), + Use: "stats ", + Short: "Emits some runtime information (# go-routines, threads, cpus, etc) from the target application", + Args: cobra.MaximumNArgs(1), Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/ps_trace.go b/ziti/cmd/ziti/cmd/ps_trace.go index 6d27d863b..885f8d5b8 100644 --- a/ziti/cmd/ziti/cmd/ps_trace.go +++ b/ziti/cmd/ziti/cmd/ps_trace.go @@ -18,7 +18,6 @@ package cmd import ( "github.com/openziti/foundation/agent" - cmdutil "github.com/openziti/ziti/ziti/cmd/ziti/cmd/factory" cmdhelper "github.com/openziti/ziti/ziti/cmd/ziti/cmd/helpers" "github.com/spf13/cobra" "io" @@ -31,19 +30,19 @@ type PsTraceOptions struct { } // NewCmdPsTrace creates a command object for the "create" command -func NewCmdPsTrace(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command { +func NewCmdPsTrace(out io.Writer, errOut io.Writer) *cobra.Command { options := &PsTraceOptions{ PsOptions: PsOptions{ CommonOptions: CommonOptions{ - Factory: f, - Out: out, - Err: errOut, + Out: out, + Err: errOut, }, }, } cmd := &cobra.Command{ - Use: "trace", + Use: "trace", + Short: "Turn tracing on for 5 seconds in the target application", Run: func(cmd *cobra.Command, args []string) { options.Cmd = cmd options.Args = args diff --git a/ziti/cmd/ziti/cmd/pscmd.go b/ziti/cmd/ziti/cmd/pscmd.go deleted file mode 100644 index 497a7b025..000000000 --- a/ziti/cmd/ziti/cmd/pscmd.go +++ /dev/null @@ -1,113 +0,0 @@ -/* - Copyright NetFoundry, Inc. - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package cmd - -import ( - "errors" - "fmt" - "github.com/openziti/foundation/agent" - "io" - "io/ioutil" - "net" - "os" - "os/exec" - "strconv" - "strings" - - "github.com/openziti/ziti/ziti/util" -) - -func trace(addr net.TCPAddr, _ []string) error { - fmt.Println("Tracing now, will take 5 secs...") - out, err := cmd(addr, agent.Trace) - if err != nil { - return err - } - if len(out) == 0 { - return errors.New("nothing has traced") - } - tmpfile, err := ioutil.TempFile("", "trace") - if err != nil { - return err - } - if err := ioutil.WriteFile(tmpfile.Name(), out, 0); err != nil { - return err - } - fmt.Printf("Trace dump saved to: %s\n", tmpfile.Name()) - // If go tool chain not found, stopping here and keep trace file. - if _, err := exec.LookPath("go"); err != nil { - return nil - } - defer os.Remove(tmpfile.Name()) - cmd := exec.Command("go", "tool", "trace", tmpfile.Name()) - cmd.Env = os.Environ() - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - -// targetToAddr tries to parse the target string, be it remote host:port -// or local process's PID. -func targetToAddr(target string) (*net.TCPAddr, error) { - if strings.Contains(target, ":") { - // addr host:port passed - var err error - addr, err := net.ResolveTCPAddr("tcp", target) - if err != nil { - return nil, fmt.Errorf("couldn't parse dst address: %v", err) - } - return addr, nil - } - // try to find port by pid then, connect to local - pid, err := strconv.Atoi(target) - if err != nil { - return nil, fmt.Errorf("couldn't parse PID: %v", err) - } - port, err := util.GetPort(pid) - if err != nil { - return nil, fmt.Errorf("couldn't get port for PID %v: %v", pid, err) - } - addr, _ := net.ResolveTCPAddr("tcp", "127.0.0.1:"+port) - return addr, nil -} - -func cmd(addr net.TCPAddr, c byte, params ...byte) ([]byte, error) { - conn, err := cmdLazy(addr, c, params...) - if err != nil { - return nil, fmt.Errorf("couldn't get port by PID: %v", err) - } - - all, err := ioutil.ReadAll(conn) - if err != nil { - return nil, err - } - return all, nil -} - -func cmdLazy(addr net.TCPAddr, c byte, params ...byte) (io.Reader, error) { - conn, err := net.DialTCP("tcp", nil, &addr) - if err != nil { - return nil, err - } - buf := []byte{c} - buf = append(buf, params...) - if _, err := conn.Write(buf); err != nil { - return nil, err - } - return conn, nil -} diff --git a/ziti/cmd/ziti/cmd/version.go b/ziti/cmd/ziti/cmd/version.go index 5792c176b..c8ce5a858 100644 --- a/ziti/cmd/ziti/cmd/version.go +++ b/ziti/cmd/ziti/cmd/version.go @@ -22,6 +22,8 @@ import ( "github.com/openziti/ziti/ziti/cmd/ziti/cmd/table" "github.com/openziti/ziti/ziti/cmd/ziti/internal/log" "io" + "os" + // "strings" "github.com/openziti/ziti/common/version" @@ -77,19 +79,19 @@ func (o *VersionOptions) Run() error { info := util.ColorInfo - table := o.CreateTable() - table.AddRow("NAME", "VERSION") + t := table.CreateTable(os.Stdout) + t.AddRow("NAME", "VERSION") - table.AddRow(c.ZITI, info(version.GetBuildMetadata(o.Verbose))) + t.AddRow(c.ZITI, info(version.GetBuildMetadata(o.Verbose))) - o.versionPrintZitiApp(c.ZITI_CONTROLLER, &table) - o.versionPrintZitiApp(c.ZITI_FABRIC, &table) - o.versionPrintZitiApp(c.ZITI_PROX_C, &table) - o.versionPrintZitiApp(c.ZITI_ROUTER, &table) - o.versionPrintZitiApp(c.ZITI_TUNNEL, &table) - o.versionPrintZitiApp(c.ZITI_EDGE_TUNNEL, &table) + o.versionPrintZitiApp(c.ZITI_CONTROLLER, &t) + o.versionPrintZitiApp(c.ZITI_FABRIC, &t) + o.versionPrintZitiApp(c.ZITI_PROX_C, &t) + o.versionPrintZitiApp(c.ZITI_ROUTER, &t) + o.versionPrintZitiApp(c.ZITI_TUNNEL, &t) + o.versionPrintZitiApp(c.ZITI_EDGE_TUNNEL, &t) - table.Render() + t.Render() if !o.NoVersionCheck && !o.Verbose { return o.versionCheck()