Skip to content

Commit

Permalink
Move workflow and shell cli to graffiti
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Dec 16, 2020
1 parent fd04037 commit d08870b
Show file tree
Hide file tree
Showing 22 changed files with 507 additions and 86 deletions.
6 changes: 2 additions & 4 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ statics/js/dev/*.js linguist-vendored=true
*_easyjson.go linguist-generated=true
*_gendecoder.go -diff -merge
*_gendecoder.go linguist-generated=true
ebpf/statics/bindata.go -diff -merge
ebpf/statics/bindata.go linguist-generated=true
statics/bindata.go -diff -merge
statics/bindata.go linguist-generated=true
*/bindata.go -diff -merge
*/bindata.go linguist-generated=true
topology/probes/vpp/bin_api/interfaces/* -diff -merge
topology/probes/vpp/bin_api/interfaces/* linguist-generated=true
topology/probes/vpp/bin_api/vpe/* -diff -merge
Expand Down
4 changes: 2 additions & 2 deletions .mk/api.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.typescript: statics/js/bundle.js js/browser.js js/api.js
.typescript: statics/js/bundle.js js/browser.js js/api.js graffiti/js/api.js

graffiti/js/api.js: graffiti/js/api.ts
cd graffiti/js && npm ci && PATH=`npm bin`:$$PATH tsc --module commonjs --target ES5 api.ts

js/api.js: js/api.ts
js/api.js: js/api.ts graffiti/js/api.ts
cd js && npm ci && PATH=`npm bin`:$$PATH tsc --module commonjs --target ES5 api.ts

js/browser.js: js/browser.ts js/api.ts
Expand Down
1 change: 1 addition & 0 deletions analyzer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func NewServerFromConfig() (*Server, error) {
Peers: peers,
EtcdClient: etcdClient,
TopologyMarshallers: api.TopologyMarshallers,
Assets: &statics.Assets,
}

if config.GetBool("etcd.embedded") {
Expand Down
6 changes: 3 additions & 3 deletions cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
gclient "github.com/skydive-project/skydive/graffiti/cmd/client"
"github.com/skydive-project/skydive/graffiti/http"
"github.com/skydive-project/skydive/graffiti/logging"
"github.com/skydive-project/skydive/statics"
)

var (
Expand Down Expand Up @@ -56,15 +57,12 @@ func printJSON(obj interface{}) {

// RegisterClientCommands registers the 'client' CLI subcommands
func RegisterClientCommands(cmd *cobra.Command) {
cmd.AddCommand(AlertCmd)
cmd.AddCommand(CaptureCmd)
cmd.AddCommand(EdgeRuleCmd)
cmd.AddCommand(NodeRuleCmd)
cmd.AddCommand(PacketInjectorCmd)
cmd.AddCommand(PcapCmd)
cmd.AddCommand(ShellCmd)
cmd.AddCommand(StatusCmd)
cmd.AddCommand(WorkflowCmd)
}

func init() {
Expand Down Expand Up @@ -92,5 +90,7 @@ func init() {
config.BindPFlag("tls.client_cert", ClientCmd.PersistentFlags().Lookup("cert"))
config.BindPFlag("tls.client_key", ClientCmd.PersistentFlags().Lookup("key"))

gclient.ShellAssets = &statics.Assets

RegisterClientCommands(ClientCmd)
}
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ require (
github.com/lunixbochs/struc v0.0.0-20180408203800-02e4c2afbb2a
github.com/lxc/lxd v0.0.0-20200330183600-518f06676866
github.com/mailru/easyjson v0.7.6
github.com/mattn/go-runewidth v0.0.0-20160315040712-d6bea18f7897 // indirect
github.com/mattn/goveralls v0.0.2
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.3
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/networkservicemesh/networkservicemesh/controlplane/api v0.2.0
Expand All @@ -63,11 +61,9 @@ require (
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/olivere/elastic/v7 v7.0.21
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 // indirect
github.com/peterh/liner v0.0.0-20160615113019-8975875355a8
github.com/pierrec/xxHash v0.1.5
github.com/pkg/errors v0.9.1
github.com/pmylund/go-cache v2.1.0+incompatible
github.com/robertkrimen/otto v0.0.0-20200922221731-ef014fd054ac
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8
github.com/safchain/insanelock v0.0.0-20200217234559-cfbf166e05b3
github.com/shirou/gopsutil v2.18.12+incompatible
Expand Down
5 changes: 3 additions & 2 deletions graffiti/alert/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/skydive-project/skydive/graffiti/api/server"
api "github.com/skydive-project/skydive/graffiti/api/server"
"github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/assets"
etcd "github.com/skydive-project/skydive/graffiti/etcd/client"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/graffiti/graph/traversal"
Expand Down Expand Up @@ -394,10 +395,10 @@ func (a *Server) Stop() {
}

// NewServer creates a new alerting server
func NewServer(apiServer *api.Server, pool ws.StructSpeakerPool, graph *graph.Graph, parser *traversal.GremlinTraversalParser, etcdClient *etcd.Client) (*Server, error) {
func NewServer(apiServer *api.Server, pool ws.StructSpeakerPool, graph *graph.Graph, parser *traversal.GremlinTraversalParser, etcdClient *etcd.Client, assets assets.Assets) (*Server, error) {
election := etcdClient.NewElection("/elections/alert-server")

runtime, err := server.NewWorkflowRuntime(graph, parser, apiServer)
runtime, err := server.NewWorkflowRuntime(graph, parser, apiServer, assets)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions graffiti/api/server/workflow_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (

"github.com/robertkrimen/otto"
"github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/assets"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/graffiti/graph/traversal"
"github.com/skydive-project/skydive/graffiti/js"
)

// NewWorkflowRuntime returns a new Workflow runtime
func NewWorkflowRuntime(g *graph.Graph, tr *traversal.GremlinTraversalParser, server *Server) (*js.Runtime, error) {
runtime, err := js.NewRuntime()
// NewWorkflowRuntime returns a new workflow runtime
func NewWorkflowRuntime(g *graph.Graph, tr *traversal.GremlinTraversalParser, server *Server, assets assets.Assets) (*js.Runtime, error) {
runtime, err := js.NewRuntime(assets)
if err != nil {
return nil, err
}
Expand Down
23 changes: 23 additions & 0 deletions graffiti/assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2020 Sylvain Baubeau
*
* 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 ofthe License at
*
* http://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 specificlanguage governing permissions and
* limitations under the License.
*
*/

package assets

// Assets is the interface by an asset provider
type Assets interface {
Asset(name string) ([]byte, error)
}
9 changes: 2 additions & 7 deletions cmd/client/alert.go → graffiti/cmd/client/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ package client
import (
"os"

"github.com/spf13/cobra"

gapi "github.com/skydive-project/skydive/graffiti/api/client"
"github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/logging"
"github.com/skydive-project/skydive/validator"

"github.com/spf13/cobra"
)

var (
Expand Down Expand Up @@ -57,10 +56,6 @@ var AlertCreate = &cobra.Command{
alert.Trigger = alertTrigger
alert.Action = alertAction

if err := validator.Validate("alert", alert); err != nil {
exitOnError(err)
}

if err := CrudClient.Create("alert", &alert, nil); err != nil {
exitOnError(err)
}
Expand Down
5 changes: 4 additions & 1 deletion graffiti/cmd/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ func init() {
ClientCmd.PersistentFlags().StringVarP(&keyfile, "key", "", "", "private key file name")
ClientCmd.PersistentFlags().BoolVarP(&skipVerify, "insecure", "", false, "do not check server's certificate")

ClientCmd.AddCommand(AlertCmd)
ClientCmd.AddCommand(QueryCmd)
ClientCmd.AddCommand(NodeCmd)
ClientCmd.AddCommand(EdgeCmd)
ClientCmd.AddCommand(NodeCmd)
ClientCmd.AddCommand(ShellCmd)
ClientCmd.AddCommand(TopologyCmd)
ClientCmd.AddCommand(WorkflowCmd)
}
2 changes: 1 addition & 1 deletion cmd/client/liner.go → graffiti/cmd/client/liner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
)

const (
promptDefault = "skydive> "
promptDefault = "shell> "
promptContinue = "..... "
indent = " "
)
Expand Down
42 changes: 17 additions & 25 deletions cmd/client/shell.go → graffiti/cmd/client/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ import (
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"

"github.com/skydive-project/skydive/graffiti/assets"
"github.com/skydive-project/skydive/graffiti/js"
"github.com/skydive-project/skydive/graffiti/logging"
)

var (
shellScript string
historyFile string

// ShellAssets holds the extra assets for the JavaScript engine
ShellAssets assets.Assets

// ErrContinue parser error continue input
ErrContinue = errors.New("<continue input>")
Expand Down Expand Up @@ -68,13 +73,6 @@ func (s *Session) completeWord(line string, pos int) (string, []string, string)
}

func (s *Session) loadHistory() error {
historyFile := ""
home, err := homeDir()
if err != nil {
return fmt.Errorf("Failed to retrieve home directory: %s", err)
}

historyFile = filepath.Join(home, "history")
f, err := os.Open(historyFile)
if err != nil {
if !os.IsNotExist(err) {
Expand Down Expand Up @@ -139,24 +137,9 @@ func (s *Session) prompt() error {
return nil
}

func homeDir() (home string, err error) {
home = os.Getenv("SKYDIVE_HOME")
if home != "" {
return
}

home, err = homedir.Dir()
if err != nil {
return
}

home = filepath.Join(home, ".skydive")
return
}

// NewSession creates a new shell session
func NewSession() (*Session, error) {
runtime, err := js.NewRuntime()
runtime, err := js.NewRuntime(ShellAssets)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -195,8 +178,8 @@ func (s *Session) Close() error {
// ShellCmd skydive shell root command
var ShellCmd = &cobra.Command{
Use: "shell",
Short: "Shell Command Line Interface",
Long: "Skydive Shell Command Line Interface, yet another shell",
Short: "Gremlin shell",
Long: "Gremlin shell",
SilenceUsage: false,
Run: func(cmd *cobra.Command, args []string) {
s, err := NewSession()
Expand All @@ -222,5 +205,14 @@ var ShellCmd = &cobra.Command{
}

func init() {
var history string
home, err := homedir.Dir()
if err == nil {
if executable, err := os.Executable(); err == nil {
history = filepath.Join(home, fmt.Sprintf(".%s_history", filepath.Base(executable)))
}
}

ShellCmd.Flags().StringVarP(&historyFile, "history", "", history, "path to the command history")
ShellCmd.Flags().StringVarP(&shellScript, "script", "", "", "path to a JavaScript to execute")
}
File renamed without changes.
8 changes: 1 addition & 7 deletions cmd/client/workflow.go → graffiti/cmd/client/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@ import (
"io/ioutil"
"os"

"github.com/spf13/cobra"
"gopkg.in/yaml.v2"

"github.com/skydive-project/skydive/graffiti/api/types"
"github.com/skydive-project/skydive/graffiti/logging"
"github.com/skydive-project/skydive/validator"

"github.com/spf13/cobra"
)

var (
Expand Down Expand Up @@ -60,10 +58,6 @@ func loadWorklow(path string) (*types.Workflow, error) {
return nil, err
}

if err := validator.Validate("workflow", workflow); err != nil {
return nil, err
}

return workflow, nil
}

Expand Down
2 changes: 2 additions & 0 deletions graffiti/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ require (
github.com/gorilla/websocket v1.4.2
github.com/hashicorp/go-version v1.2.1
github.com/mailru/easyjson v0.7.6
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/hashstructure v1.0.0
github.com/mitchellh/mapstructure v1.3.3
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
github.com/olivere/elastic/v7 v7.0.21
github.com/peterh/liner v1.2.1
github.com/pierrec/xxHash v0.1.5
github.com/pkg/errors v0.9.1
github.com/pmylund/go-cache v2.1.0+incompatible
Expand Down
8 changes: 2 additions & 6 deletions graffiti/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

"github.com/skydive-project/skydive/graffiti/alert"
api "github.com/skydive-project/skydive/graffiti/api/server"
"github.com/skydive-project/skydive/graffiti/assets"
gc "github.com/skydive-project/skydive/graffiti/common"
etcdclient "github.com/skydive-project/skydive/graffiti/etcd/client"
etcdserver "github.com/skydive-project/skydive/graffiti/etcd/server"
Expand Down Expand Up @@ -62,6 +63,7 @@ type Opts struct {
EtcdClient *etcdclient.Client
EtcdServerOpts *etcdserver.EmbeddedServerOpts
Logger logging.Logger
Assets assets.Assets
}

type podOrigin struct {
Expand Down Expand Up @@ -323,14 +325,8 @@ func NewHub(id string, serviceType service.Type, listen string, g *graph.Graph,
return nil, err
}

alertServer, err := alert.NewServer(apiServer, subscriberWSServer, g, tr, opts.EtcdClient)
if err != nil {
return nil, err
}

hub.httpServer = httpServer
hub.apiServer = apiServer
hub.alertServer = alertServer
hub.podWSServer = podWSServer
hub.replicationEndpoint = replicationEndpoint
hub.replicationWSServer = replicationWSServer
Expand Down
Loading

0 comments on commit d08870b

Please sign in to comment.