-
Notifications
You must be signed in to change notification settings - Fork 0
/
topology.go
140 lines (121 loc) · 3.84 KB
/
topology.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2016 Red Hat, 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 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 client
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/skydive-project/skydive/common"
"github.com/skydive-project/skydive/config"
"github.com/skydive-project/skydive/graffiti/graph"
"github.com/skydive-project/skydive/graffiti/hub"
gws "github.com/skydive-project/skydive/graffiti/websocket"
"github.com/skydive-project/skydive/websocket"
"github.com/spf13/cobra"
)
var (
gremlinQuery string
outputFormat string
filename string
)
// TopologyCmd skydive topology root command
var TopologyCmd = &cobra.Command{
Use: "topology",
Short: "Request on topology",
Long: "Request on topology",
SilenceUsage: false,
}
// TopologyRequest skydive topology query command
var TopologyRequest = &cobra.Command{
Use: "query",
Short: "query topology [deprecated: use 'client query' instead]",
Long: "query topology [deprecated: use 'client query' instead]",
Run: func(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "The 'client topology query' command is deprecated. Please use 'client query' instead")
QueryCmd.Run(cmd, []string{gremlinQuery})
},
}
// TopologyImport skydive topology import command
var TopologyImport = &cobra.Command{
Use: "import",
Short: "import topology",
Long: "import topology",
Run: func(cmd *cobra.Command, args []string) {
sa, err := config.GetOneAnalyzerServiceAddress()
if err != nil {
exitOnError(err)
}
url := config.GetURL("ws", sa.Addr, sa.Port, "/ws/publisher")
opts := websocket.ClientOpts{AuthOpts: &AuthenticationOpts, Headers: http.Header{}}
opts.Headers.Add("X-Persistence-Policy", string(hub.Persistent))
client, err := config.NewWSClient(common.UnknownService, url, opts)
if err != nil {
exitOnError(err)
}
if err := client.Connect(); err != nil {
exitOnError(err)
}
go client.Run()
defer func() {
client.Flush()
client.StopAndWait()
}()
content, err := ioutil.ReadFile(filename)
if err != nil {
exitOnError(err)
}
els := []*graph.Elements{}
if err := json.Unmarshal(content, &els); err != nil {
exitOnError(err)
}
if len(els) != 1 {
exitOnError(errors.New("Invalid graph format"))
}
for _, node := range els[0].Nodes {
msg := gws.NewStructMessage(gws.NodeAddedMsgType, node)
if err := client.SendMessage(msg); err != nil {
exitOnError(fmt.Errorf("Failed to send message: %s", err))
}
}
for _, edge := range els[0].Edges {
msg := gws.NewStructMessage(gws.EdgeAddedMsgType, edge)
if err := client.SendMessage(msg); err != nil {
exitOnError(fmt.Errorf("Failed to send message: %s", err))
}
}
},
}
// TopologyExport skydive topology export command
var TopologyExport = &cobra.Command{
Use: "export",
Short: "export topology",
Long: "export topology",
Run: func(cmd *cobra.Command, args []string) {
QueryCmd.Run(cmd, []string{"G"})
},
}
func init() {
TopologyCmd.AddCommand(TopologyExport)
TopologyImport.Flags().StringVarP(&filename, "file", "", "graph.json", "Input file")
TopologyCmd.AddCommand(TopologyImport)
TopologyRequest.Flags().StringVarP(&gremlinQuery, "gremlin", "", "G", "Gremlin Query")
TopologyRequest.Flags().StringVarP(&outputFormat, "format", "", "json", "Output format (json, dot or pcap)")
TopologyCmd.AddCommand(TopologyRequest)
}