
Michael Adamski
Hey all,
I have the xMAgent all set up on my server and am now trying to figure out how to get the next steps working.
I would like to initiate the agent to run a Node.js script. I also need to be able to feed this script a payload of data, ideally as defined arguments, much like you would in a function
Example --- var printInfo = function (name, age, gender, location) {...}
What I am now trying to figure out is how to both initiate the Node.js script as well as howto pass it the various params that it will need when executing the script.
Any ideas?
0
Comments
Please sign in to leave a comment.
Hey Michael!
That sounds pretty awesome. You will want to check out the Running local scripts page in the help. The syntax is a little funky but once you get the hang of it it's not so bad. Basically you wrap the shell script inside commends inside an empty function block and pass the parameters as the second, er, parameter. You'll end up with something like this:
A couple things to note.
Let us know how it goes or if you run into any issues!
Happy Thursday!
--- Travis
Thanks for the tips Travis. I am having support install Node for me now and will report back with the results. Just a quick question, would the above work for arguments which were objects? Like hashes or arrays? Wondering if they would be passed via the shell script differently for some reason, maybe needing to be parsed in some way?
I would make sure to stringify anything more complex than a string when passing into the shell ghost script. Then parse them in your node.js script. I can imagine some of those characters might adversely affect the shell environment or otherwise get lost in translation.
Thank you for the advice thus far Travis, it got me further along, but I am still getting stuck. I am able to trigger my node script. I am even able to pass it parameters, but the parameters are only accessible in the script if they are strings.
It's really weird, if I pass the script an object, like a hash of data, by running typeof param I can see that it is an object, and I can even see the data. Let's say it looks like this,
var param = {name: 'John', age: 23}
I can run a console.log(JSON.stringify(param)) and get back the above, but if I run console.log(param.name} it will send back undefined. It's as if I am able to view the data but not interact with it, and I cannot for the life of me figure out why!
I am using the built in NodeJS library of process.argv to access the params. It looks something like this
Hmm. Not sure. What I see there looks right. I set all this up in my instance using an inbound integration in the IB (are you using a custom step or IB script?) and running an agent on Ubuntu 16.
I can successfully pass parameters and check the results of "typeof". Here is my node.js script:
Here is my IB script.
And this is in the activity stream:
Can you share more of your setup?
Happy Wednesday!
--- Travis
Hey all, so here is an update to my original post.
I decided to leverage the use of the xMatters Agent to trigger a series of scripts to run, hosted on a local server. To do this, I have a form which triggers the event. The user selects the script they would like to run. In our case, it can be either an actionable script (ex. migrating data from one env to another) or a report generation script (building csv reports on various datasets). The script choice is selected by the user in a drop down on the form, and later concatenated into a functional filename to be called in the flow step script.
Here's what my flow step looks like
I can be assured that my scripts will always reside in the same location, so I hard code a CD into that directory and follow it up by triggering that node script using the node cmd followed by my file name, which was generated earlier.
On the server, I have a series of scripts which can run, unimpeded by any kind of timeout restrictions which exist on the cloud for xMatters. I intend to build this out such that reports are run on a scheduled basis and much more.
Originally, I struggled with sending params to the server and then processing inside the script but then realized, the far simpler approach is just to create single use scripts and then just call the appropriate script rather than writing a complex universal script which would need all sorts of conditions and parameters. I feel this is the simpliest solution for us thus far.
Awesome! Thanks for following up. This is great info.
One word of caution, if that "input.file" value is entered as free text, users can do a command injection and have direct access to the OS with whatever permissions the agent is running as. Always remember Little Bobby Tables! I'm not familiar enough with windows shell to suggest something and it looks like it might depend on what version of powershell or cmd shell you are running.
Alternatively, you might think about making this a dropdown input so users are forced to select a value, but probably best to sanitize the input before passing to the node.js script.
In any case, thanks again for sharing and your persistence in getting this sorted.
Happy Thursday!
--- Travis
Yea, the input is actually a dropdown, so we determine it's input range basically. Even still, the point about sanitizing inputs is fair. That should be included as a given I think. I'll be sure to include that. Thanks for the code review Travis :)
I am attempting to perform this exact process, but cannot figure out how to change the 'parameters' from hardcoded strings to a variable.
My requirements are that I pass a different variable value to the bash process on the xMAgent server.
I can get the parameter to be passed if I hardcode one, like in the examples above. But how do I make the values dynamic.
In the example code, param1 is hardcoded and param2 is variable.
******
var situation = input['sit_id'];
if (osname === 'Linux')
{
// create a script and pass some parameters into it.
var script = Shell.script(function () {/*#### PLACE YOUR BASH SCRIPT BETWEEN HERE ####
echo param1: ${param1}
echo param2: ${param2}
#### AND HERE #### */},{param1: 'test',param2: situation});
// Execute the script.
var result = Shell.exec('bash', script);
console.log(result.output());
console.log(result.exitCode());
console.log(result.error());
}
*******
Any help would be appreciated.
I figure this is something pretty basic,, but i am not a javascript coder by trade and have been struggling with this syntax.
Ah yea. Javascript has a funky syntax and it's hard to know what is what some times. Try this:
That second parameter to the Shell.script function is just a javascript object, so you can re write it slightly more clearly as:
The keys in a javascript object can be pretty flexible as you can see. This allows for funky variables like
myParams['a long key with $'] = 4
Edit; I'm not sure I'd recommend getting to crazy with the keys. The shell might freak out.
Think of it as a C++/java/C# array with any string or number as the array index. Or a python dict.
I hope that helps!
Happy Thursday!
--- Travis