1
1
package cloudshellwrapper
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"errors"
6
7
"fmt"
@@ -22,6 +23,9 @@ import (
22
23
cp "github.com/otiai10/copy"
23
24
log "github.com/sirupsen/logrus"
24
25
26
+ "github.com/openconfig/gnmic/pkg/api"
27
+ "google.golang.org/protobuf/encoding/prototext"
28
+
25
29
"github.com/gorilla/mux"
26
30
"github.com/gorilla/websocket"
27
31
@@ -157,43 +161,118 @@ func init() {
157
161
rootCommand .AddCommand (& clabCommand )
158
162
}
159
163
160
- // define a reader which will listen for
161
- // new messages being sent to our WebSocket
162
- // endpoint
163
- func reader (conn * websocket.Conn ) {
164
- defer conn .Close ()
164
+ // test gMNIc
165
+ func SendGnmicToNodeCapabilities (targetName string , targetAddress string , targetUsername string , targetPassword string , skipVerifyFlag bool , insecureFlag bool ) {
166
+ // create a target
167
+ tg , err := api .NewTarget (
168
+ api .Name (targetName ),
169
+ api .Address (targetAddress + ":57400" ),
170
+ api .Username (targetUsername ),
171
+ api .Password (targetPassword ),
172
+ api .SkipVerify (skipVerifyFlag ),
173
+ api .Insecure (insecureFlag ),
174
+ )
175
+ if err != nil {
176
+ log .Fatal (err )
177
+ }
165
178
166
- // Set the maximum allowed idle time for the WebSocket connection
167
- conn . SetReadDeadline ( time . Now (). Add ( 5 * time . Second )) // Adjust the duration as needed
179
+ ctx , cancel := context . WithCancel ( context . Background ())
180
+ defer cancel ()
168
181
169
- for {
170
- // read in a message
171
- messageType , p , err := conn .ReadMessage ()
172
- if err != nil {
173
- // Check for specific close error codes indicating client-initiated closure
174
- if websocket .IsCloseError (err , websocket .CloseNormalClosure , websocket .CloseGoingAway ) {
175
- log .Info ("WebSocket connection closed by the client." )
176
- } else {
177
- log .Info ("Error while reading from WebSocket:" , err )
178
- }
179
- return
180
- }
181
- // print out that message for clarity
182
- log .Info (string (p ))
182
+ // create a gNMI client
183
+ err = tg .CreateGNMIClient (ctx )
184
+ if err != nil {
185
+ log .Fatal (err )
186
+ }
187
+ defer tg .Close ()
183
188
184
- if err := conn .WriteMessage (messageType , p ); err != nil {
185
- log .Info (err )
186
- return
187
- }
188
- conn .SetReadDeadline (time .Now ().Add (5 * time .Second ))
189
+ // send a gNMI capabilities request to the created target
190
+ capResp , err := tg .Capabilities (ctx )
191
+ if err != nil {
192
+ log .Fatal (err )
189
193
}
194
+ fmt .Println (prototext .Format (capResp ))
190
195
}
191
196
197
+ func SendGnmicToNodeGet (targetName string , targetAddress string , targetUsername string , targetPassword string , skipVerifyFlag bool , insecureFlag bool , path string ) {
198
+ // create a target
199
+ tg , err := api .NewTarget (
200
+ api .Name (targetName ),
201
+ api .Address (targetAddress + ":57400" ),
202
+ api .Username (targetUsername ),
203
+ api .Password (targetPassword ),
204
+ api .SkipVerify (skipVerifyFlag ),
205
+ api .Insecure (insecureFlag ),
206
+ )
207
+ if err != nil {
208
+ log .Fatal (err )
209
+ }
210
+
211
+ ctx , cancel := context .WithCancel (context .Background ())
212
+ defer cancel ()
213
+
214
+ // create a gNMI client
215
+ err = tg .CreateGNMIClient (ctx )
216
+ if err != nil {
217
+ log .Fatal (err )
218
+ }
219
+ defer tg .Close ()
220
+
221
+ // create a GetRequest
222
+ getReq , err := api .NewGetRequest (
223
+ api .Path (path ),
224
+ api .Encoding ("json_ietf" ))
225
+ if err != nil {
226
+ log .Fatal (err )
227
+ }
228
+ fmt .Println (prototext .Format (getReq ))
229
+
230
+ // send the created gNMI GetRequest to the created target
231
+ getResp , err := tg .Get (ctx , getReq )
232
+ if err != nil {
233
+ log .Fatal (err )
234
+ }
235
+ fmt .Println (prototext .Format (getResp ))
236
+ }
237
+
238
+ // define a reader which will listen for
239
+ // new messages being sent to our WebSocket
240
+ // endpoint
241
+ // func reader(conn *websocket.Conn) {
242
+ // defer conn.Close()
243
+
244
+ // // Set the maximum allowed idle time for the WebSocket connection
245
+ // conn.SetReadDeadline(time.Now().Add(5 * time.Second)) // Adjust the duration as needed
246
+
247
+ // for {
248
+ // // read in a message
249
+ // messageType, p, err := conn.ReadMessage()
250
+ // if err != nil {
251
+ // // Check for specific close error codes indicating client-initiated closure
252
+ // if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
253
+ // log.Info("WebSocket connection closed by the client.")
254
+ // } else {
255
+ // log.Info("Error while reading from WebSocket:", err)
256
+ // }
257
+ // return
258
+ // }
259
+ // // print out that message for clarity
260
+ // log.Info(string(p))
261
+
262
+ // if err := conn.WriteMessage(messageType, p); err != nil {
263
+ // log.Info(err)
264
+ // return
265
+ // }
266
+ // conn.SetReadDeadline(time.Now().Add(5 * time.Second))
267
+ // }
268
+ // }
269
+
192
270
func checkSudoAccess () {
193
271
euid := syscall .Geteuid ()
194
272
195
273
if euid == 0 {
196
274
log .Infof ("Yo, this app is running with sudo access (as root)." )
275
+
197
276
} else {
198
277
log .Infof ("This app ain't got no sudo powers, bro." )
199
278
os .Exit (1 )
@@ -205,6 +284,11 @@ func Clab(_ *cobra.Command, _ []string) error {
205
284
206
285
//check sudo
207
286
checkSudoAccess ()
287
+ // SendGnmicToNodeCapabilities("srl", "10.2.1.121", "admin", "NokiaSrl1!", true, false)
288
+ // SendGnmicToNodeCapabilities("sros", "10.2.1.101", "admin", "admin", true, true)
289
+
290
+ // SendGnmicToNodeGet("srl", "10.2.1.121", "admin", "NokiaSrl1!", true, false, "/system/name")
291
+ // SendGnmicToNodeGet("sros", "10.2.1.101", "admin", "admin", true, true, "/system/name")
208
292
209
293
// initialise the cloudshellLogger
210
294
// tools.InitCloudShellLog(tools.Format(confClab.GetString("log-format")), tools.Level(confClab.GetString("log-level")))
@@ -633,18 +717,6 @@ func Clab(_ *cobra.Command, _ []string) error {
633
717
err1 := cp .Copy (sourceClabClientFolder , destinationClabClientImageFolder )
634
718
log .Debug ("Copying clab-client folder error: " , err1 )
635
719
636
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "index.tmpl", cyTopo.ClabTopoDataV2.Name+"/"+"index.html", "dataCytoMarshall-"+cyTopo.ClabTopoDataV2.Name+".json")
637
-
638
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "index.tmpl", cyTopo.ClabTopoDataV2.Name+"/"+"index.html", cyTopo.ClabTopoDataV2.Name)
639
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "cy-style.tmpl", cyTopo.ClabTopoDataV2.Name+"/"+"cy-style.json", "")
640
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "cloudshell-index.tmpl", cyTopo.ClabTopoDataV2.Name+"/cloudshell/"+"index.html", "")
641
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "cloudshell-terminal-js.tmpl", cyTopo.ClabTopoDataV2.Name+"/cloudshell/"+"terminal.js", "")
642
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "tools-cloudshell-index.tmpl", cyTopo.ClabTopoDataV2.Name+"/cloudshell-tools/"+"index.html", "")
643
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "tools-cloudshell-terminal-js.tmpl", cyTopo.ClabTopoDataV2.Name+"/cloudshell-tools/"+"terminal.js", "")
644
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "websocket-index.tmpl", cyTopo.ClabTopoDataV2.Name+"/ws/"+"index.html", "")
645
-
646
- // createHtmlPublicFiles(htmlTemplatePath, htmlPublicPrefixPath, "button.tmpl", cyTopo.ClabTopoDataV2.Name+"/"+"button.html", cyTopo.ClabTopoDataV2.Name)
647
-
648
720
indexHtmldata := IndexHtmlStruct {
649
721
LabName : cyTopo .ClabTopoDataV2 .Name ,
650
722
DeploymentType : deploymentType ,
0 commit comments