我正在尝试将bundle目录复制到远程服务器的根目录中。我尝试使用节点执行此操作,到目前为止,我实现了将tar内容传递给服务器并解压缩它。但是,当我尝试将目录移动到根文件夹时,它需要sudo
访问权限,而我却无法找到方法。我尝试-t
选项进行伪终端,但我想这可以从shell运行。以下是我迄今所做的工作,非常感谢任何帮助:
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}`);
}
});
在ubuntu服务器中存储Web应用程序文件夹的最佳位置,多个用户可以部署应用程序,最好是让目录root
用户的所有者,或者它只是没有用户。重要吗?
答案 0 :(得分:0)
如man page for ssh中所述,您可以指定多个-t
参数来强制pty分配,即使OpenSSH客户端的stdin不是tty(它也不会是tty)默认情况下,当您在节点中生成子进程时。)
当您在.stdin
信息流上看到sudo提示时,您应该能够简单地将密码写入子流程的.stdout
流。
在半相关的说明中,如果您希望对ssh连接进行更多(程序化)控制,或者您不想启动子进程,则可以使用ssh2
模块。如果你愿意,你甚至可以在节点内进行tarring,因为在npm上也有tar模块。