Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capistrano fails to remove node modules during cleanup

I'm trying to deploy a Rails 5.1 app. I have deployed the app several times without problems, but now, all of a sudden I get this error message from 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 
[email protected]: 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

It seems the deploy user that Capistrano is using to remove older versions, doesn't have enough permissions to remove node_modules

I tried to fix the issue by setting node_modules to the linked directories:

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

but that didn't fix it either.

Does someone have an idea how to fix this?

thanks for your help,

Anthony

ps: this is the output of the ls -lt command in the current directory:

-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
like image 473
Toontje Avatar asked Oct 27 '17 09:10

Toontje


3 Answers

Root cause

  • You setup NodeJS/npm in "sudo way", so the npm installed packages with root owner
  • Capistrano removes old releases (it only keeps 10 last releases as default if I remember correctly) to save space. Since you use deploy user to deploy the app, it doesn't have permission to delete files with owner is root
  • You didn't see this error before because your releases count didn't reach the maximum kept releases (e.g: 10 releases)

Solution

Since you turned your node_modules to be shared directory, you don't need to fix npm issue with sudo. You can simply change owner of releases folder recursively to deploy:deploy. The purpose is making old releases can be deleted successfully. The command will be

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

like image 176
Nam Tran Avatar answered Nov 17 '22 22:11

Nam Tran


It seems you have a problem with permissions that npm sets for node_modules. You can check the official documentation for troubleshooting the issue

like image 1
Nikita Misharin Avatar answered Nov 17 '22 22:11

Nikita Misharin


Try to issue permissions on the dir on your server under your deployer user

sudo chown -R $USER /opt
like image 1
itsnikolay Avatar answered Nov 18 '22 00:11

itsnikolay