Node.js execute shell command which requires an input after a few seconds - node.js

I'm trying to use the now cli to purchase a domain through my Node.js server but I cannot figure out how to execute a shell command in Node and to automatically insert the input when asked.
Here's my command using child_process.exec
const { stdout } = await exec(`now --token ${NOW_TOKEN} domains buy ${domainName}.${tld}`)
This does nothing because the command is waiting input (y/N) to confirm buying the domain.
They don't have any option in the commande to bypass the question like --yes.
How can I pass a string y + Enter when the process is waiting ?

Be careful. Your usage can certainly violate their ToS if you are reselling parts of their offering. You may need to reach out to their support channel for clarification.
Regarding the code. You can take a look at this file for inspiration: https://github.com/zeit/now/blob/master/packages/now-cli/test/integration.js
One alternative is:
stdin.write('y\n')

Related

How to slurp a value in a file and assign this to a variable inside a puppet module

I'm pretty new to puppet and have run into an issue.
We have a proprietary home-grown API-based secrets management platform. We can either query the API directly or configure so that the secrets for that host are mounted to the root filesystem.
My problem is I can't figure out how to get that information within the context of a puppet module and into a variable so that I can use it. It seems you can't get stdout/stderr back from exec (or can you) otherwise this would be cake.
So for simplicity, let's say my secret is /etc/app/example/foo.
$roles.each |$role| {
case downcase($role) {
'foo': {
# SOMEHOW I NEED TO GET TOKEN FROM FILESYSTEM OR API CALL HERE
$token = <GET TOKEN SOMEHOW>
# here I need to do something with my value
exec { "my description":
command => '//bin/foo',
environment => ["TOKEN=${token}"]
}
This is basically what I need to do at a basic level. It doesn't matter if I call curl directly (preferred approach) or read a mounted file.
Thx for any help.
you can't get stdout/stderr back from exec (or can you) otherwise this would be cake.
You cannot capture the standard output or error of an Exec's command for reuse, but Puppet's built-in generate() function serves exactly the purpose of executing a command and capturing its output. Normally that would run the command on the server, during catalog compilation, but if you want it to run on the client instead then you can defer its execution. One of the primary purposes for deferring functions is for interaction with secret stores.
With that said, you might want to consider wrapping the whole thing up in a custom resource type. That's maybe a bit more work (especially if you don't speak Ruby), but it's a lot more flexible, and it should make for cleaner and clearer code on the Puppet DSL side, too.

Using gcloud commands in nodejs application

Some gcloud commands don't have API or client library support (for example - this one).
In these cases, is there a simple way to run gcloud commands from a nodejs application?
The gcloud endpoints service commands for IAM policy are difficult for me to check quickly but, if IIRC (and if this is similar to gcloud projects commands for IAM policy), it's not that there's no API, but that there's no single API call.
What you can always do with gcloud is append --log-http to see what happens beneath the covers. With IAM policy mutations (off-top-of-head), you get the policy, mutate it, and then apply the changes back using the etag the GET gave you. The backend checks the policy's state (the etag is like a hash of the policy) and, if it's unchanged, you can make the change.
If this is what's happening here, you should be able to repro the functionality in NodeJS using the existing (!) APIs and, if you're using API Client Libraries (rather than Cloud Client libraries), the functionality will be available.
Apart from the complexity involved in shelling out to gcloud, you'll need to also authenticate it and then you'll need to (un)marshal data to the shell and manage errors. Ergo, it's messy and generally discouraged.
In node.js ,we have child_process module. As the name suggests the child_process provides function like spawn or exec that creates new child process that executes shell command like independent process. spawn is a function that takes the main command as
first argument and other command line options as an array values in place of second parameter.
So with respect to link that you share, you might end-up writing something like this :
const { spawn } = require("child_process");
const listening = spawn('gcloud', ['endpoints', 'services', 'blah', '--option','someValue']);
listening.stdout.on("data", data => {
console.log(`stdout: ${data}`);
});
listening.stderr.on("data", data => {
console.log(`stderr: ${data}`);
});
listening.on('error', (error) => {
console.log(`error: ${error.message}`);
});
References :
https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options
I'm not sure this directly answers your question but there is an npm package that can help you run unix commands from within the app.
Check out shell.js

How to create linux terminal like tutorialspoint?

TutorialsPoint Java Compiler
In tutorialspoint, they have created linux terminal using term.js.
I have integrated same github library in my project, it is working fine but I am trying to understand the flow of tutorialspoint.
My assumption:
In tutroialspoint each time they are creating new user_id under root user(cg) and running terminal(nodejs server) using that user_id so every time when you reload page there will be a different user_id (run whoami in terminal), so another user can't operate other users files.
I am running nodejs server using forever.js under root user, I want to implement same type of functionality. What is correct way to do this? and if there is another way please elaborate.
I think they are creating a new user each time you visit the page and providing you a subshell of that user. It can be easily achieve by using Shell Programming techniques. Creating a new user each time thing is probably nothing more than a security measure.
So I will briefly explain the concept in 5 steps:
1 - Create a new user:
shell_exec('useradd --expiredate 2016-09-10 [username]');
http://www.computerhope.com/unix/useradd.htm
2 - Login to this newly created user account:
shell_exec('su [username]');
3 - Get user input to the PHP script using AJAX(dynamically).
4 - Execute user's command and send the output to user:
<?php
$output = shell_exec("[user's command]");
echo "<pre>$output</pre>";
?>
5 - Repeat from 3.

fabric -- cant run commands after user is switched

I have been recently introduced to fabric and trying to approach the following:
access remote host
su root
perform a command, for example change password for multiple users
done!
Please note that I cant use sudo, nor connect directly to the remote host using root. The commands I need to perform can only be performed if I explicitly change user to root.
I was able to approach the main concept of getting to the remote host and play with some commands, using fabric, but the problem im having is that once I switch to root "su root" I cant perform the rest of the commands unless I exit.
example of what im trying to approach:
def mytask():
with settings(user="root"):
run('whoami')
run('echo "TEST using root user"')
run('echo "ITS WORKING!!!"')
or something like this
def mytask():
run ('su root')
run ('passwd testUser')
In both cases once I enter the root password nothing would get executed, I would get the remote command line back, unless I exit back to the original user. I have seen few suggestions about using "fexpect" for prompts but not sure if that would make a difference.
I'm developing on a Linux environment.
You have to use fexpect and fexpect run command
from ilogue import fexpect
prompt = ['Password', 'mypassword'] # ('prompt', 'answer') Case sensitive
def test():
with fexpect.expecting(prompt):
fexpect.local("su -")

Update deployment via linux script in weblogic

What is the script to update deployment ( from GUI, we can do this update by unlock & save changes ) in linux. Is it possible to do this ? If not what is script to redeploy ?
As Kevin pointed out, WLST is the way to go. You should probably craft a script (named wlDeploy.py, for instance), with content like follows (import clauses were omitted for the sake of simplicity):
current_app_name = '[your current deployed app name]'
new_app_name = '[your new app name]'
target_name = '[WL managed server name (or AdminServer)]'
connect([username],[pwd],'t3://[admin server hostname/IP address]:[PORT]')
stopApplication(current_app_name)
undeploy(current_app_name, timeout=60000);
war_path = '[path to war file]'
deploy(appName=new_app_name, path=war_path, targets=target_name);
And call it via something like:
./wlst.sh wlDeploy.py
Of course you can add parameters to your script, and a lot of logic which is relevant to your deployment. This is entirely up to you. The example above, though, should help you getting started.
In WebLogic you can use wlst to perform administrative tasks like managing deployments. If you google weblogic wlst, you will receive tons of information. wlst runs on the python language.
Assuming you are using weblogic 10 you can also "Record" your actions. This will save the actions into a python script which you can "replay" (execute) later.

Resources