Capistrano在清理期间无法删除节点模块

时间:2017-10-27 09:19:31

标签: ruby-on-rails capistrano

我正在尝试部署Rails 5.1应用。我已经多次部署应用程序没有问题,但现在,我突然从Capistrano收到此错误消息

00:13 deploy:cleanup

SSHKit::Command::Failed: rm exit status: 1                                                                             
rm stdout: Nothing written                                                                                             
rm stderr: rm: cannot remove '/opt/www/absence-
registrator/releases/20171017091250/node_modules/jquery/dist/jquery.js': 
Permission denied

Tasks: TOP => deploy:cleanup                                                                                           
(See full trace by running task with --trace)                                                                         
The deploy has failed with an error: Exception while executing as 
user@myapp.com: rm exit status: 1           │
rm stdout: Nothing written                                                                                             
rm stderr: rm: cannot remove '/opt/www/absence-
registrator/releases/20171017091250/node_modules/jquery/dist/jquery.js':Permission denied                                                                                                     
rm: cannot remove '/opt/www/absence-registrator/releases/20171017091250/node_modules/jquery/dist/jquery.slim.js': Permission denied                                                                                                           rm: cannot remove '/opt/www/absence-registrator/releases/20171017091250/node_modules/jquery/dist/jquery.min.js': Permission denied                                                                                                            rm: cannot remove '/opt/www/absence-registrator/releases/20171017091250/node_modules/jquery/dist/jquery.min.map': Permission denied

Capistrano用于删除旧版本的部署用户似乎没有足够的权限来删除node_modules

我尝试通过将node_modules设置为链接目录来解决此问题:

set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets node_modules}

但这也没有解决它。

有人知道如何解决这个问题吗?

感谢您的帮助,

安东尼

ps:这是当前目录中ls -lt命令的输出:

-rw-rw-r--  1 deploy deploy  864 Oct 27 14:04 Capfile
-rw-rw-r--  1 deploy deploy 2454 Oct 27 14:04 Gemfile
-rw-rw-r--  1 deploy deploy 8520 Oct 27 14:04 Gemfile.lock
-rw-rw-r--  1 deploy deploy  148 Oct 27 14:04 README.md
-rw-rw-r--  1 deploy deploy  227 Oct 27 14:04 Rakefile
drwxrwxr-x 10 deploy deploy 4096 Oct 27 14:04 app
drwxrwxr-x  2 deploy deploy 4096 Oct 27 14:04 bin
drwxrwxr-x  6 deploy deploy 4096 Oct 27 14:04 config
-rw-rw-r--  1 deploy deploy  130 Oct 27 14:04 config.ru
drwxrwxr-x  3 deploy deploy 4096 Oct 27 14:04 db
drwxrwxr-x  4 deploy deploy 4096 Oct 27 14:04 lib
-rw-rw-r--  1 deploy deploy  103 Oct 27 14:04 package.json
drwxrwxr-x  8 deploy deploy 4096 Oct 27 14:04 spec
drwxrwxr-x  2 deploy deploy 4096 Oct 27 14:04 vendor
-rw-rw-r--  1 deploy deploy  228 Oct 27 14:04 yarn.lock
drwxrwxr-x  2 deploy deploy 4096 Oct 27 13:58 assets_manifest_backup
drwxrwxr-x  3 deploy deploy 4096 Oct 27 13:58 public
lrwxrwxrwx  1 deploy deploy   48 Oct 27 13:58 node_modules -> 
/opt/www/absence-registrator/shared/node_modules
drwxrwxr-x  2 deploy deploy 4096 Oct 27 13:58 tmp
lrwxrwxrwx  1 deploy deploy   39 Oct 27 13:58 log -> /opt/www/absence-
registrator/shared/log
-rw-rw-r--  1 deploy deploy   41 Oct 27 13:58 REVISION

3 个答案:

答案 0 :(得分:4)

根本原因

  • 您以“sudo方式”设置NodeJS / npm,因此npm已安装包root所有者
  • Capistrano删除旧版本(如果我没记错的话,它只保留10个最新版本),以节省空间。由于您使用deploy用户来部署应用,因此它无权删除所有者为root
  • 的文件
  • 您之前没有看到此错误,因为您的版本计数未达到最大保留版本(例如:10个版本)

<强>解决方案

由于您将node_modules变为共享目录,因此无需使用sudo修复npm问题。您只需将releases文件夹的所有者递归更改为deploy:deploy即可。目的是使旧版本可以成功删除。命令将是

sudo chown -R deploy:deploy /opt/www/absence-registrator/releases

答案 1 :(得分:1)

npmnode_modules设置的权限似乎存在问题。您可以查看official documentation以解决问题

答案 2 :(得分:1)

尝试在部署者用户

下发布服务器上目录的权限
sudo chown -R $USER /opt