Capistrano 3 symlink public folder - ruby-on-rails

Im having a problem with Capistrano 3.X
So basically my app has public folder, where some users can upload their folders. it can be public/a public/b and so on. When i set linked dirs like that
set :linked_dirs, %w{ log tmp/pids tmp/cache tmp/sockets vendor/bundle public }
I'm getting error:
I, [2016-01-23T05:09:48.343707 #27926] INFO -- : Writing /home/deploy/blabla/
releases/20160123100938/public/assets/bootstrap/glyphicons-halflings-regular-
fe185d11a49676890d47bb783312a0cda5a44c4039214094e7957b4c040ef11c.woff2 rake
aborted! Errno::EEXIST: File exists # dir_s_mkdir -
/home/deploy/blabla/releases/20160123100938/public/assets
/home/deploy/blabla/shared/bundle/ruby/2.2.0/gems/sprockets-
3.5.2/lib/sprockets/asset.rb:163:in `write_to'
/home/deploy/blabla/shared/bundle/ruby/2.2.0/gems/sprockets-3.5.2/
lib/sprockets/manifest.rb:192:in `block (2 levels) in compile'
/home/deploy/blabla/shared/bundle/ruby/2.2.0/gems/concurrent-ruby-
1.0.0/lib/concurrent/executor/safe_task_executor.rb:24:in `call'
I tried to remove assets folder, create it manually. I don't know what to do next.

You can put users uploaded folders inside another folder like uploaded_folders and then
set :linked_dirs, %w{ log tmp/pids tmp/cache tmp/sockets vendor/bundle public/uploaded_folders public/system }
This should fix it

Related

(SSHKit::Runner::ExecuteError) rake stdout: rake aborted!\r ActiveSupport::MessageEncryptor::InvalidMessage:

deploy.rb
set :linked_files, ['config/database.yml', 'config/credentials.yml.enc', 'config/master.key', 'config/storage.yml']
set :linked_dirs, %w[log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system]
I am trying to deploy the rails application with Capistrano, getting the following error while bundle exec rake assets: precompile
(SSHKit::Command::Failed)
rake stdout: rake aborted!\r
ActiveSupport::MessageEncryptor::InvalidMessage: ActiveSupport::MessageEncryptor::InvalidMessage\r
deployment is failed and the current folder is not being created on the server. I have installed node and yarn to the server

preserve public folder after deployment with capistrano

I have a folder public/recipes with recipes inside of this folder.
However after deployment via remote_cache this folder and recipes are removed/deleted.
I do not want that capistrano remove/delete this folder and these recipes.
How I can get it?
If you are using Capistrano 3 then just specify public/recipes directory in linked_dirs variable:
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/uploads public/recipes}
It will set link between your directory and shared/public/recipes.
For Capistrano 2 you should set link "manually" in deploy.rb use ln command i.e. use something like this:
task :configure_symlinks, :roles => :web do
run "ln -nfs #{shared_path}/public/recipes #{current_release}/public/recipes"
end
after "deploy:update_code", "configure_symlinks"
If you don't want a symlink, you will probably need something like https://github.com/capistrano/copy-files.
However, I highly recommend that you use linked dirs as ethyl.bradtke suggests. That is the best way to handle this.

Capistrano deploy removes table

I use capistrano to deploy my Rails app to my VPS, but after every deploy when I visit my page I get an error. The log says:
I, [2014-11-04T08:20:16.659289 #12482] INFO -- : Started GET "/" for 82.73.170.71 at 2014-11-04 08:20:16 -0500
I, [2014-11-04T08:20:16.662717 #12482] INFO -- : Processing by HomeController#index as HTML
I, [2014-11-04T08:20:16.665979 #12482] INFO -- : Completed 500 Internal Server Error in 3ms
F, [2014-11-04T08:20:16.670152 #12482] FATAL -- :
ActiveRecord::StatementInvalid (Could not find table 'users'):
app/controllers/application_controller.rb:18:in `current_user'
app/helpers/sessions_helper.rb:26:in `logged_in?'
app/controllers/home_controller.rb:4:in `index'
I have to ssh into my VPS and go to my Rails root and run RAILS_ENV=production bundle exec rake db:migrate . In my db folder I do still have the production.sqlite3 file, but it's empty.
My deploy.rb
# config valid only for Capistrano 3.1
lock '3.1.0'
set :application, 'movieseat'
set :repo_url, 'git#github.com:alucardu/movieseat.git'
set :deploy_to, '/home/deploy/movieseat'
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
require 'capistrano-rbenv'
namespace :deploy do
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
execute :touch, release_path.join('tmp/restart.txt')
end
end
after :publishing, 'deploy:restart'
after :finishing, 'deploy:cleanup'
end
So why is Capistrano removing my database when I deploy?
Capistrano does not touch database migrations unless it is specified by deploy:migrate task within your Capfile or by calling bundle exec cap deploy:migrate.
Your database 'disappears' because SQLite is simply a file in your db directory. Since you do not specify it should be shared among releases (to be within shared directory) then it just disappears and stays in previous release. Add db/production.sqlite3 to your linked_files declaration.

Does Capistrano 3 still put Rails logs in shared/log

I have upgraded to Capistrano 3 and successfully deployed several Rails apps on a new server.
Previously with Capistrano 2 deployments the Rails logs magically went to /myapp/shared/log/production.log
With my Cap3 deployments the logs are in the app folder myapp/current/log/production.log
Is this intentional or have I missed setting something up?
Is there some special way to set it up so that they go to /shared/log ?
Just found the answer.
You need to uncomment the following line in deploy.rb
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}

Carrierwave files with Capistrano

I'm using rails 3.2 with asset and carrierwave for upload some images, they store in /public/uploads/photo/.....
but when I do a cap:deploy (with capistrano) my current directory application doesn't contain the files I uploaded, because capistrano make a new version ....
=== Update ===
After all I use this :
inside :deploy namespace
task :symlink_uploads do
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads"
end
and after:
after 'deploy:update_code', 'deploy:symlink_uploads'
=== Re Update ===
The solution of #tristanm is the best way to solve this.
How about this:
# config/deploy.rb
set :shared_children, shared_children + %w{public/uploads}
:shared_children defaults to %w(public/system log tmp/pids) so we're just expanding this list.
EDIT:
Don't forget to run cap deploy:setup after changing :shared_children so that the new targets are created under shared.
EDIT Capistrano 3:
Capistrano 3 uses the linked_dirs setting and doesn't specify public/system as a default anymore.
set :linked_dirs, fetch(:linked_dirs) + %w{public/system public/uploads}
With Capistrano 3 and without needing to redeploy.
Like #tristanm mentioned add this to your config/deploy.rb
# config/deploy.rb
set :linked_dirs, fetch(:linked_dirs) + %w{public/uploads}
To have capistrano create shared/public/uploads
cap deploy:check:linked_dirs
Now cap can create the symlink
cap deploy:symlink:shared
Finally, if you have backups of the uploads you can put them in shared/public/uploads/ and they should work without needing to redeploy.
Capistrano creates new directory for every deploy.
There are some exceptions to it. For example, the log files are shared between the deployment directories because they are just symlinks. You have to create a symlink for public/uploads as well.
Here is the command:
run <<-CMD
rm -rf #{latest_release}/public/uploads &&
ln -s #{shared_path}/uploads #{latest_release}/public/uploads
CMD
Go to your app server shared folder and create an uploads directory.
mkdir uploads
In your deploy.rb file insert these codes under deploy namespace
task :symlink_uploads do
run "rm -rf #{latest_release}/public/uploads && ln -nfs #{shared_path}/uploads #{latest_release}/public/uploads"
end
after 'deploy:update_code', 'deploy:symlink_uploads'
Now delete the old files present already as they won't work. Upload a new file and cap deploy your app again. It should work now.
Using Capistrano 3, I just added this line to my config/deploy.rb
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}
Then, run:
$ cap production deploy

Resources