Capistrano can not create symlinks: file exist

两盒软妹~` 提交于 2019-12-11 02:41:47

问题


I have Rails 4 app with Capistrano 3.

# deploy.rb
set :application, '<DELETED>'
set :user, '<DELETED>'
set :repo_url, '<DELETED>'
set :branch, 'master'
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }

set :deploy_to, "/home/#{fetch :user}/apps/#{fetch :application}"
set :scm, :git

set :format, :pretty
set :log_level, :debug
set :pty, true

set :linked_files, %w{config/application.yml}
set :linked_dirs, %w{bin log tmp vendor/bundle public/system public/deploy}

# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :keep_releases, 5

namespace :deploy do

  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
      within fetch :current_path do
        execute "/etc/init.d/unicorn_#{fetch :application} restart"
      end
    end
  end

  before :restart, 'rvm:hook'

  desc 'Stop application'
  task :stop do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      within fetch :current_path do
        execute "/etc/init.d/unicorn_#{fetch :application} stop"
      end
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end
  before :stop, 'rvm:hook'

  desc 'Start application'
  task :start do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      within fetch :current_path do
        execute "/etc/init.d/unicorn_#{fetch :application} start"
      end
      # execute :touch, release_path.join('tmp/restart.txt')
    end
  end
  before :start, 'rvm:hook'
  before 'whenever:update_crontab', 'rvm:hook'
  before 'whenever:clear_crontab', 'rvm:hook'

  after :finishing, 'deploy:cleanup'
  after :finished, 'deploy:restart'

  task :add_default_hooks do
    after 'deploy:starting', 'sidekiq:quiet'
    after 'deploy:updated', 'sidekiq:stop'
    after 'deploy:reverted', 'sidekiq:stop'
    after 'deploy:published', 'sidekiq:start'
  end

end

Deploy settings for production:

# production.rb
set :stage, :production

# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
role :app, %w{<DELETED>}
role :web, %w{<DELETED>}
role :db,  %w{<DELETED>}

# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
server '128.199.148.191', user: 'deployer', roles: %w{web app db} #, my_property: :my_value
set :rvm_type, :system
set :rvm_ruby_version, '2.1.0'
set :rails_env, 'production'
# you can set custom ssh options
# it's possible to pass any option but you need to keep in mind that net/ssh understand limited list of options
# you can see them in [net/ssh documentation](http://net-ssh.github.io/net-ssh/classes/Net/SSH.html#method-c-start)
# set it globally
#  set :ssh_options, {
#    keys: %w(/home/rlisowski/.ssh/id_rsa),
#    forward_agent: false,
#    auth_methods: %w(password)
#  }
# and/or per server
# server 'example.com',
#   user: 'user_name',
#   roles: %w{web app},
#   ssh_options: {
#     user: 'user_name', # overrides user setting above
#     keys: %w(/home/user_name/.ssh/id_rsa),
#     forward_agent: false,
#     auth_methods: %w(publickey password)
#     # password: 'please use keys'
#   }
# setting per server overrides global ssh_options

# fetch(:default_env).merge!(rails_env: :production)
set :ssh_options, {
                    auth_methods: %w(publickey password),
                    user: '<DELETED>'
                }

It was ok, but I have issue with creating symlinks. It returns 'file exists', but I didn't create application.yml or public/system forlder in repository, only in shared folder. Log:

 INFO[3e66cb56] Finished in 0.842 seconds with exit status 0 (successful).
** Invoke deploy:symlink:shared (first_time)
** Execute deploy:symlink:shared
** Invoke deploy:symlink:linked_files (first_time)
** Execute deploy:symlink:linked_files
INFO[146e0a26] Running /usr/bin/env mkdir -p /home/deployer/apps/xxxcoast/releases/20141124144030/config on 128.199.148.191
DEBUG[146e0a26] Command: /usr/bin/env mkdir -p /home/deployer/apps/xxxcoast/releases/20141124144030/config
INFO[a9a9bef0] Running /usr/bin/env mkdir -p /home/deployer/apps/xxxcoast/releases/20141124144030/config on 128.199.148.191
DEBUG[a9a9bef0] Command: /usr/bin/env mkdir -p /home/deployer/apps/xxxcoast/releases/20141124144030/config
INFO[a9a9bef0] Finished in 0.848 seconds with exit status 0 (successful).
INFO[146e0a26] Finished in 0.850 seconds with exit status 0 (successful).
DEBUG[0f26dfb7] Running /usr/bin/env [ -L /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ] on 128.199.148.191
DEBUG[2d8972f0] Running /usr/bin/env [ -L /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ] on 128.199.148.191
DEBUG[0f26dfb7] Command: [ -L /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ]
DEBUG[2d8972f0] Command: [ -L /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ]
DEBUG[2d8972f0] Finished in 0.822 seconds with exit status 1 (failed).
DEBUG[7858afe7] Running /usr/bin/env [ -f /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ] on 128.199.148.191
DEBUG[7858afe7] Command: [ -f /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ]
DEBUG[0f26dfb7] Finished in 0.827 seconds with exit status 1 (failed).
DEBUG[7e5cbc1c] Running /usr/bin/env [ -f /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ] on 128.199.148.191
DEBUG[7e5cbc1c] Command: [ -f /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml ]
DEBUG[7e5cbc1c] Finished in 0.838 seconds with exit status 1 (failed).
INFO[9cc3b7d0] Running /usr/bin/env ln -s /home/deployer/apps/xxxcoast/shared/config/application.yml /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml on 128.199.148.191
DEBUG[9cc3b7d0] Command: /usr/bin/env ln -s /home/deployer/apps/xxxcoast/shared/config/application.yml /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml
DEBUG[7858afe7] Finished in 0.843 seconds with exit status 1 (failed).
INFO[bcf64145] Running /usr/bin/env ln -s /home/deployer/apps/xxxcoast/shared/config/application.yml /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml on 128.199.148.191
DEBUG[bcf64145] Command: /usr/bin/env ln -s /home/deployer/apps/xxxcoast/shared/config/application.yml /home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml
INFO[9cc3b7d0] Finished in 1.032 seconds with exit status 0 (successful).
DEBUG[bcf64145]     ln: failed to create symbolic link ‘/home/deployer/apps/xxxcoast/releases/20141124144030/config/application.yml’: File exists
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host 128.199.148.191: ln exit status: 1
ln stdout: Nothing written
ln stderr: Nothing written
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status='
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:148:in `block (5 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:170:in `block (3 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:172:in `block (2 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:186:in `with_ssh'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:129:in `block (5 levels) in <top (required)>'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:122:in `each'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:122:in `block (4 levels) in <top (required)>'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
SSHKit::Command::Failed: ln exit status: 1
ln stdout: Nothing written
ln stderr: Nothing written
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/command.rb:97:in `exit_status='
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:148:in `block (5 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `call'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:551:in `do_request'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:561:in `channel_request'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:269:in `wait'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:170:in `block (3 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `call'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/channel.rb:514:in `do_open_confirmation'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:545:in `channel_open_confirmation'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:465:in `dispatch_incoming_packets'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:221:in `preprocess'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:205:in `process'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `block in loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/net-ssh-2.9.1/lib/net/ssh/connection/session.rb:169:in `loop'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:172:in `block (2 levels) in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:186:in `with_ssh'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:131:in `block in _execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `tap'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:128:in `_execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:66:in `execute'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:129:in `block (5 levels) in <top (required)>'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:122:in `each'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/bundler/gems/capistrano-52591324f3ae/lib/capistrano/tasks/deploy.rake:122:in `block (4 levels) in <top (required)>'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `instance_exec'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/backends/netssh.rb:54:in `run'
/Users/quatermain/.rvm/gems/ruby-2.1.0@xxxcoast/gems/sshkit-1.5.1/lib/sshkit/runners/parallel.rb:13:in `block (2 levels) in execute'
Tasks: TOP => deploy:symlink:linked_files
The deploy has failed with an error: Exception while executing on host 128.199.148.191: ln exit status: 1
ln stdout: Nothing written
ln stderr: Nothing written
** Invoke deploy:failed (first_time)
** Execute deploy:failed

Edited: better log


回答1:


I found this was because I had duplicate role definitions, so it was running the symlink command twice.

Check that you don't have duplicate role lines or that you have role lines in addition an all-in-one server configuration line.




回答2:


I think in my case doubled commands:

ln -s with "exit status 0", and next 
ln -s with "exit status 1" and with information "file exists" 

were caused by changing IP server to DNS name in config/deploy/production.yml

-role :app, %w{appuser@example.com}
-role :web, %w{appuser@example.com}
-role :db,  %w{appuser@example.com}
+role :app, %w{appuser@1.2.3.4}
+role :web, %w{appuser@1.2.3.4}
+role :db,  %w{appuser@1.2.3.4}

I'm not sure that this was the only one change, but when I've roll back this change (again to dns address) there was no more doubled commands. It looks like capistrano cached or stored old config.. I've found this, because I've expecting only IP in logs, but I saw in deploy log:

INFO [d2ac8c5x] Running /usr/bin/env git remote update as appuser@example.com
INFO [3fe766d1] Running /usr/bin/env git remote update as appuser@1.2.3.4



回答3:


Your config/application.yml probably exists in repository and that is causing the issue. ln -s will not not create a symlink if destination already exists.

Flag -L checks whether given path is a symbolic link or not. In this case it returns exit code 1, because it is not.



来源:https://stackoverflow.com/questions/27107347/capistrano-can-not-create-symlinks-file-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!