Skip to content

Commit

Permalink
dev
Browse files Browse the repository at this point in the history
  • Loading branch information
asadarafat committed Feb 3, 2024
1 parent 2eeaa61 commit 5c4fa10
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 120 deletions.
75 changes: 55 additions & 20 deletions go_cloudshellwrapper/cmdClab.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,30 +533,65 @@ func Clab(_ *cobra.Command, _ []string) error {
clabPass := confClab.GetString("clab-pass")
log.Infof("clabPass: '%s'", clabPass)

// call function to run SSH commnd
cyTopo.RunSSHCommand(clabUser, clabHost[0], clabPass, command)
if deploymentType == "colocated" {

// Create a response JSON object
responseData := map[string]interface{}{
"result": "Netem command received",
}
log.Infof("executing exec command, since deployment type is colocated")

// Marshal the response JSON object into a JSON string
jsonResponse, err := json.Marshal(responseData)
if err != nil {
http.Error(w, "Failed to marshal response data", http.StatusInternalServerError)
return
}
returnData, err := cyTopo.RunExecCommand(clabUser, clabHost[0], command)

// Create a response JSON object
responseData := map[string]interface{}{
"result": "Netem command received",
"return data": returnData,
"error": err,
}

// Set the response Content-Type header
w.Header().Set("Content-Type", "application/json")
// Marshal the response JSON object into a JSON string
jsonResponse, err := json.Marshal(responseData)
if err != nil {
http.Error(w, "Failed to marshal response data", http.StatusInternalServerError)
return
}

// Write the JSON response to the client
_, err = w.Write(jsonResponse)
if err != nil {
// Handle the error (e.g., log it)
http.Error(w, "Failed to write response", http.StatusInternalServerError)
return
// Set the response Content-Type header
w.Header().Set("Content-Type", "application/json")

// Write the JSON response to the client
_, err = w.Write(jsonResponse)
if err != nil {
// Handle the error (e.g., log it)
http.Error(w, "Failed to write response", http.StatusInternalServerError)
return
}

} else {
// call function to run SSH commnd
returnData, err := cyTopo.RunSSHCommand(clabUser, clabHost[0], clabPass, command)

// Create a response JSON object
responseData := map[string]interface{}{
"result": "Netem command received",
"return data": returnData,
"error": err,
}

// Marshal the response JSON object into a JSON string
jsonResponse, err := json.Marshal(responseData)
if err != nil {
http.Error(w, "Failed to marshal response data", http.StatusInternalServerError)
return
}

// Set the response Content-Type header
w.Header().Set("Content-Type", "application/json")

// Write the JSON response to the client
_, err = w.Write(jsonResponse)
if err != nil {
// Handle the error (e.g., log it)
http.Error(w, "Failed to write response", http.StatusInternalServerError)
return
}
}
}).Methods("POST")

Expand Down
25 changes: 25 additions & 0 deletions go_topoengine/adaptorClabv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"os/exec"
"strings"
"time"

Expand Down Expand Up @@ -351,6 +352,30 @@ func (cyTopo *CytoTopology) RunSSHCommand(clabUser string, clabHost string, clab
return b.Bytes(), nil
}

func (cyTopo *CytoTopology) RunExecCommand(clabUser string, clabHost string, command string) ([]byte, error) {

log.Infof("RunExecCommand Function: '%s'", command)

// Split the command into individual arguments
args := strings.Fields(command)
cmd := exec.Command(args[0], args[1:]...)

output, err := cmd.Output()

if err != nil {
if err, ok := err.(*exec.ExitError); ok {
// The command exited with a non-zero status code
return nil, err
}
return nil, err
}

log.Infof("Output of RunExecCommand: %s", output)
log.Errorf("Error of RunExecCommand: %s", err)

return output, err
}

func (cyTopo *CytoTopology) GetDockerNodeStatus(clabNodeName string, clabUser string, clabHost string, clabPassword string) ([]byte, error) {
command := "docker ps --all --format json"
output, err := cyTopo.RunSSHCommand(clabUser, clabHost, clabPassword, command)
Expand Down
133 changes: 53 additions & 80 deletions html-static/template/clab/button.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@

{{rawJSComment " // WebSocket for uptime"}}
const socketUptime = initializeWebSocket('/uptime', (msgUptime) => {
const string01 = 'Containerlab Topology: ' + "{{.LabName}}";
const string01 = 'Containerlab Topology: ' + "{{.LabName}}";
const string02 = ' ::: Uptime: ' + msgUptime.data;

const ClabSubtitle = document.getElementById("ClabSubtitle");
Expand Down Expand Up @@ -362,11 +362,11 @@
}

{{rawJSComment " // Fetch and load element data from a JSON file"}}
console.log("Lab Name: {{.LabName}}")

var deploymentType = {{.DeploymentType}}

console.log ("DeploymentType: ", deploymentType)
console.log ("Lab-Name: {{.LabName}}")
console.log ("DeploymentType: {{.DeploymentType}}")


fetch('dataCytoMarshall-{{.LabName}}.json')

Expand Down Expand Up @@ -473,7 +473,7 @@
console.log(defaultEdgeColor)

{{rawJSComment " // Change the color of the clicked edge (for example, to red)"}}
clickedEdge.style('line-color', '#457800');
clickedEdge.style('line-color', '#0043BF');

{{rawJSComment " // Revert the color of other edges that were not clicked (e.g., back to their default color)"}}
cy.edges().forEach(function(edge) {
Expand Down Expand Up @@ -1343,11 +1343,10 @@
name: 'blue',
iconClass: 'fas fa-play',
hoverMessage: 'Click to START the link impairment.',
hrefFunction: 'linkImpairmentSsh',
hrefLink: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefLink02: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefFunction: 'linkImpairmentManagerExec',
hrefLink: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefLink02: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefFunctionArg: 'start-source',
typeOfTopoviewerDeployment: deploymentType,
},
{
name: 'green',
Expand All @@ -1362,11 +1361,10 @@
name: 'blue',
iconClass: 'fas fa-stop',
hoverMessage: 'Click to STOP the link impairment.',
hrefFunction: 'linkImpairmentSsh',
hrefLink: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefLink02: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefFunctionArg: 'stop-source',
typeOfTopoviewerDeployment: deploymentType,
hrefFunction: 'linkImpairmentManagerExec',
hrefLink: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefLink02: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabSourceLongName} -i ${edge.data("sourceEndpoint")}`,
hrefFunctionArg: 'stop-source'
},
{
name: 'green',
Expand Down Expand Up @@ -1511,11 +1509,10 @@
name: 'blue',
iconClass: 'fas fa-play',
hoverMessage: 'Click to START the link impairment.',
hrefFunction: 'linkImpairmentSsh',
hrefLink: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefLink02: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefFunction: 'linkImpairmentManagerExec',
hrefLink: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefLink02: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefFunctionArg: 'start-target',
typeOfTopoviewerDeployment: deploymentType,
},
{
name: 'green',
Expand All @@ -1530,12 +1527,10 @@
name: 'blue',
iconClass: 'fas fa-stop',
hoverMessage: 'Click to STOP the link impairment.',
hrefFunction: 'linkImpairmentSsh',
hrefLink: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefLink02: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefFunctionArg: 'stop-target',
typeOfTopoviewerDeployment: deploymentType,

hrefFunction: 'linkImpairmentManagerExec',
hrefLink: `ssh ${edge.data("extraData").clabServerUsername}@${location.hostname} sudo /usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefLink02: `/usr/bin/containerlab tools netem set -n ${edge.data('extraData').clabTargetLongName} -i ${edge.data("targetEndpoint")}`,
hrefFunctionArg: 'stop-target'
},
{
name: 'green',
Expand Down Expand Up @@ -2381,7 +2376,7 @@
{{rawJSComment " // { idSuffix: 'labelFindRouteSource', columnLabelTextContent: '', columnLabelClass: 'column is-0 p-0', columnInputType: 'field', columnInputContent: '', columnInputPlaceholder: 'name', columnInputAttribute: 'enabled', columnInputIsInvisible: 'yes',"}}
{{rawJSComment " // addonsContent: []"}}
{{rawJSComment " // } "}}
{{rawJSComment " // { idSuffix: 'labellinkImpairmentSsh', columnLabelTextContent: 'Link Impairment', columnLabelClass: 'column is-4 p-1', columnInputType: 'field', columnInputContent: 'Apply', columntInputAttribute: 'enabled', // columntInputAttribute default is disabled"}}
{{rawJSComment " // { idSuffix: 'labellinkImpairmentManagerExec', columnLabelTextContent: 'Link Impairment', columnLabelClass: 'column is-4 p-1', columnInputType: 'field', columnInputContent: 'Apply', columntInputAttribute: 'enabled', // columntInputAttribute default is disabled"}}
{{rawJSComment " // addonsContent: ["}}
{{rawJSComment " // {"}}
{{rawJSComment " // name: 'blue',"}}
Expand Down Expand Up @@ -2719,60 +2714,60 @@
appendMessage(`SSH command is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`)
});
}
else if (addon.hrefFunction == 'linkImpairmentSsh') {
else if (addon.hrefFunction == 'linkImpairmentManagerExec') {
if (addon.hrefFunctionArg == 'start-source') {
button.classList.add('impairment-start-source-ssh')
button.classList.add('impairment-start-ssh')
button.addEventListener('click', () => {
console.log('impairmentStartSshButton is clicked')
linkImpairmentManagerSsh('start-source', addon.hrefLink, addon.hrefLink02, addon.typeOfTopoviewerDeployment)
linkImpairmentManagerExec('start-source', addon.hrefLink, addon.hrefLink02)
});
}
else if (addon.hrefFunctionArg == 'stop-source') {
button.classList.add('impairment-stop-source-ssh')
button.classList.add('impairment-stop-ssh')
button.addEventListener('click', () => {
console.log('impairmentStopSshButton is clicked')
linkImpairmentManagerSsh('stop-source', addon.hrefLink, addon.hrefLink02, addon.typeOfTopoviewerDeployment)
linkImpairmentManagerExec('stop-source', addon.hrefLink, addon.hrefLink02)
});
}
else if (addon.hrefFunctionArg == 'start-target') {
button.classList.add('impairment-start-target-ssh')
button.classList.add('impairment-stop-ssh')
button.addEventListener('click', () => {
console.log('impairmentStopSshButton is clicked')
linkImpairmentManagerSsh('start-target', addon.hrefLink, addon.hrefLink02, addon.typeOfTopoviewerDeployment)
linkImpairmentManagerExec('start-target', addon.hrefLink, addon.hrefLink02)
});
}
else if (addon.hrefFunctionArg == 'stop-target') {
button.classList.add('impairment-stop-target-ssh')
button.classList.add('impairment-stop-ssh')
button.addEventListener('click', () => {
console.log('impairmentStopSshButton is clicked')
linkImpairmentManagerSsh('stop-target', addon.hrefLink, addon.hrefLink02, addon.typeOfTopoviewerDeployment)
linkImpairmentManagerExec('stop-target', addon.hrefLink, addon.hrefLink02)
});
}
}
else if (addon.hrefFunction == 'linkImpairmentCopy') {
if (addon.hrefFunctionArg == 'start-source') {
button.classList.add('impairment-start-source-copy')
button.classList.add('impairment-start-copy')
button.addEventListener('click', () => {
console.log('impairmentStartCopyButton is clicked')
linkImpairmentManagerCopy('start-source', addon.hrefLink)
});
}
else if (addon.hrefFunctionArg == 'stop-source') {
button.classList.add('impairment-stop-source-copy')
button.classList.add('impairment-stop-copy')
button.addEventListener('click', () => {
console.log('impairmentStopCopyButton is clicked')
linkImpairmentManagerCopy('stop-source', addon.hrefLink)
});
}
else if (addon.hrefFunctionArg == 'start-target') {
button.classList.add('impairment-stop-target-copy')
button.classList.add('impairment-stop-copy')
button.addEventListener('click', () => {
console.log('impairmentStopCopyButton is clicked')
linkImpairmentManagerCopy('start-target', addon.hrefLink)
});
}
else if (addon.hrefFunctionArg == 'stop-target') {
button.classList.add('impairment-stop-target-copy')
button.classList.add('impairment-stop-copy')
button.addEventListener('click', () => {
console.log('impairmentStopCopyButton is clicked')
linkImpairmentManagerCopy('stop-target', addon.hrefLink)
Expand Down Expand Up @@ -3326,11 +3321,19 @@
}


function linkImpairmentManagerSsh(toggle, baseCommandColocatedDeployment, baseCommandContainerDeployment, type) {
function linkImpairmentManagerExec(toggle, baseCommandSsh, baseCommandExec) {

console.log("linkImpairmentManagerExec-Function-Called")

var deploymentType = {{.DeploymentType}}

console.log("linkImpairmentManagerSsh-Function-Called")

{{rawJSComment " // fullCommand = baseCommand"}}
if (deploymentType == 'colocated') {
baseCommand = baseCommandExec
} else {
baseCommand = baseCommandSsh
}


if (toggle == 'start-source') {
delayValue = document.getElementById('panelBlock-tabContainer-LinkProperties-divPanelBlock-columnContainerlabelSourceImpairmentDelay-panelContentlabelSourceImpairmentDelay-columnsPanelContentlabelSourceImpairmentDelay-labelColumnlabelSourceImpairmentDelay-inputColumnlabelSourceImpairmentDelay-labellabelSourceImpairmentDelay').value
Expand All @@ -3343,28 +3346,12 @@
console.log(`rateValue: ${rateValue}`)
console.log(`lossValue: ${lossValue}`)

console.log(`type of deployment: ${type}`)

if (type == 'colocated') {
fullCommand = baseCommandColocatedDeployment + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}else{
fullCommand = baseCommandContainerDeployment + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}


fullCommand = baseCommand + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}
else if (toggle == 'stop-source') {
{{rawJSComment " // fullCommand = baseCommand"}}
alert_msg = `SSH command to stop link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
if (type == 'colocated') {
fullCommand = baseCommandColocatedDeployment
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}else{
fullCommand = baseCommandContainerDeployment
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}
fullCommand = baseCommand
alert_msg = `SSH command to stop link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}


Expand All @@ -3379,30 +3366,16 @@
console.log(`rateValue: ${rateValue}`)
console.log(`lossValue: ${lossValue}`)

console.log(`type of deployment: ${type}`)

if (type == 'colocated') {
fullCommand = baseCommandColocatedDeployment + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}else{
fullCommand = baseCommandContainerDeployment + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}

fullCommand = baseCommand + ` --delay ${delayValue}ms --jitter ${jitterValue}ms --rate ${rateValue} --loss ${lossValue}`
alert_msg = `SSH command to start link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}
else if (toggle == 'stop-target') {
{{rawJSComment " // fullCommand = baseCommand"}}
alert_msg = `SSH command to stop link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
if (type == 'colocated') {
fullCommand = baseCommandColocatedDeployment
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}else{
fullCommand = baseCommandContainerDeployment
alert_msg = `SSH command to start link impairment is lit πŸ”₯, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}
fullCommand = baseCommand
alert_msg = `SSH command to stop link impairment is lit πŸ”₯ and copied to your clipboard, ready to drop it in your terminal console like a boss! πŸš€πŸ’»`
}


console.log(`baseCommand: ${baseCommand}`)
console.log(`fullCommand: ${fullCommand}`)

callGoFunction(fullCommand, 'padding')
Expand Down
Loading

0 comments on commit 5c4fa10

Please sign in to comment.