简体   繁体   English

在使用节点的子进程中使用sudo命令

[英]Using sudo command in a child process using node

I am trying to copy a bundle directory into a root directory of a remote server. 我正在尝试将bundle目录复制到远程服务器的根目录中。 I try to do this using node and so far I achieved piping the tar content to server and untar it. 我尝试使用节点执行此操作,到目前为止,我实现了将tar内容传递给服务器并解压缩它。 However when I try to move the directory to root folder it requires sudo access and I just couldn't find a way to do it. 但是,当我尝试将目录移动到根文件夹时,它需要sudo访问权限,而我却找不到办法。 I tried -t option for pseudoterminal but I guess that works running from a shell. 我试过-t选项用于伪终端,但我想这可以从shell运行。 Here is what I have done so far, any help is highly appreciated: 这是我到目前为止所做的,任何帮助都非常感谢:

const path = require("path");
const exec = require('child_process').exec;
var absolutePath = path.resolve(__dirname, "../");
const allCommands = [];
/*
 *
 *
 * 1-) cd to the root folder of the app
 * 2-) tar dist folder and pipe the result to the ssh connection
 * 3-) connect to server with ssh
 * 4-) try to create dist and old_dists folder, if not existing they will be created otherwise they will give an error and rest of the script will continue running
 * 5-) cp contents of dist folder to old_dists/dist_$(dateofmoment) folder so if something is wrong somehow you have an backup of the existing config
 * 6-) untar the piped tar content into dist folder, untar only files under the first parent directory --strip-components=1 flag, if it was 2 it will dive 2 level from the root folder
 *
 *
 */
allCommands.push("cd " + absolutePath);
allCommands.push("tar -czvP dist | ssh  hostnameofmyserver 'mkdir dist ; mkdir old_dists; cp -R dist/ old_dists/dist_$(date +%Y%m%d_%H%M%S) && tar -xzvP -C dist --strip-components=1'");
//I would like to untar the incoming file into /etc/myapp for example rather than my home directory, this requires sudo and don't know how to handle it
exec(allCommands.join(" && "),
  (error, stdout, stderr) => {
    console.log(`stdout: ${stdout}`);
    console.log(`stderr: ${stderr}`);
    if (error !== null) {
      console.log(`exec error: ${error}`);
    }
  });

Also whats the best place for storing web application folder in ubuntu server where multiple user can deploy an app, is it a good practice to make the owner of the directory root user, or it just doesn't matter? 还有什么是在ubuntu服务器中存储web应用程序文件夹的最佳位置,其中多个用户可以部署应用程序,这是一个很好的做法,使目录root用户的所有者,或者它无关紧要?

As noted in the man page for ssh , you can specify multiple -t arguments to force pty allocation even if the OpenSSH client's stdin is not a tty (which it won't be by default when you spawn a child process in node). ssh手册页中所述,即使OpenSSH客户端的stdin不是tty(在节点中生成子进程时默认情况下也不会这样),您可以指定多个-t参数来强制进行pty分配。

From there you should be able to simply write the password to the child process's .stdin stream when you see the sudo prompt on the .stdout stream. 从那里,你应该能够简单地写密码子进程的.stdin当你看到在sudo的提示流.stdout流。

On a semi-related note, if you want more (programmatic) control over the ssh connection or you don't want to spin up a child process, there is the ssh2 module. 在半相关的说明中,如果您想要对ssh连接进行更多(程序化)控制,或者您不想启动子进程,则需要ssh2模块。 You could even do the tarring within node too if you wanted, as there are also tar modules on npm. 如果你愿意,你甚至可以在节点内进行tarring,因为npm上还有tar模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM