-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1219 from criblio/feat/1108-ipc-mechanism
IPC mechanism #1108
- Loading branch information
Showing
41 changed files
with
3,898 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/criblio/scope/inspect" | ||
"github.com/criblio/scope/internal" | ||
"github.com/criblio/scope/ipc" | ||
"github.com/criblio/scope/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var pidCtx *ipc.IpcPidCtx = &ipc.IpcPidCtx{} | ||
|
||
// inspectCmd represents the inspect command | ||
var inspectCmd = &cobra.Command{ | ||
Use: "inspect", | ||
Short: "Return information on scoped process", | ||
Long: `Return information on scoped process identified by PID.`, | ||
Example: `scope inspect 1000`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
internal.InitConfig() | ||
// Nice message for non-adminstrators | ||
err := util.UserVerifyRootPerm() | ||
if errors.Is(err, util.ErrGetCurrentUser) { | ||
util.ErrAndExit("Unable to get current user: %v", err) | ||
} | ||
if errors.Is(err, util.ErrMissingAdmPriv) { | ||
fmt.Println("INFO: Run as root (or via sudo) to get info from all processes") | ||
} | ||
|
||
pid, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
util.ErrAndExit("Convert PID fails: %v", err) | ||
} | ||
pidCtx.Pid = pid | ||
cfg, err := inspect.InspectScopeCfg(*pidCtx) | ||
if err != nil { | ||
util.ErrAndExit("Inspect PID fails: %v", err) | ||
} | ||
fmt.Println(cfg) | ||
}, | ||
} | ||
|
||
func init() { | ||
ipcCmdFlags(inspectCmd, pidCtx) | ||
RootCmd.AddCommand(inspectCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/criblio/scope/internal" | ||
"github.com/criblio/scope/update" | ||
"github.com/criblio/scope/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var cfgPath string | ||
|
||
// updateCmd represents the info command | ||
var updateCmd = &cobra.Command{ | ||
Use: "update", | ||
Short: "Updates configuration of scoped process", | ||
Long: `Updates configuration of scoped process identified by PID.`, | ||
Example: `scope update 1000 --config test_cfg.yml`, | ||
Args: cobra.ExactArgs(1), | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if !util.CheckFileExists(cfgPath) { | ||
util.ErrAndExit("Configuration file: %s does not exist", cfgPath) | ||
} | ||
internal.InitConfig() | ||
// Nice message for non-adminstrators | ||
err := util.UserVerifyRootPerm() | ||
if errors.Is(err, util.ErrGetCurrentUser) { | ||
util.ErrAndExit("Unable to get current user: %v", err) | ||
} | ||
if errors.Is(err, util.ErrMissingAdmPriv) { | ||
fmt.Println("INFO: Run as root (or via sudo) to get info from all processes") | ||
} | ||
|
||
pid, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
util.ErrAndExit("Convert PID fails: %v", err) | ||
} | ||
|
||
pidCtx.Pid = pid | ||
|
||
err = update.UpdateScopeCfg(*pidCtx, cfgPath) | ||
if err != nil { | ||
util.ErrAndExit("Update Scope configuration fails: %v", err) | ||
} | ||
fmt.Println("Update Scope configuration success.") | ||
}, | ||
} | ||
|
||
func init() { | ||
ipcCmdFlags(updateCmd, pidCtx) | ||
updateCmd.Flags().StringVarP(&cfgPath, "config", "c", "", "Path to configuration file") | ||
updateCmd.MarkFlagRequired("config") | ||
RootCmd.AddCommand(updateCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package inspect | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
|
||
"github.com/criblio/scope/ipc" | ||
) | ||
|
||
var errInspectCfg = errors.New("error inspect cfg") | ||
|
||
// InspectScopeCfg returns the configuration of scoped process | ||
func InspectScopeCfg(pidCtx ipc.IpcPidCtx) (string, error) { | ||
|
||
cmd := ipc.CmdGetScopeCfg{} | ||
resp, err := cmd.Request(pidCtx) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
err = cmd.UnmarshalResp(resp.ResponseScopeMsgData) | ||
if err != nil { | ||
return "", err | ||
} | ||
if resp.MetaMsgStatus != ipc.ResponseOK || *cmd.Response.Status != ipc.ResponseOK { | ||
return "", errInspectCfg | ||
} | ||
marshalToPrint, err := json.MarshalIndent(cmd.Response.Cfg.Current, "", " ") | ||
if err != nil { | ||
return "", err | ||
} | ||
return string(marshalToPrint), nil | ||
} |
Oops, something went wrong.