create symlink to upload images using paperclip and rails - ruby-on-rails

I am using paperclip in my rails application to upload images. As all know by default paperclip creating public directory in rails path.
But I face a trouble while deploying my application with capistrano. Whenever I deploy my code using capistrano it is replacing my uploads directory. So I am trying to implement symlink to create a short since I am not expert in linux I am unable to continue with it can anyone help me how to solve it
has_attached_file :upload,styles:
{ thumb: ["150x100#",:jpg], small: ["75x75#",:png]},
default_url: '/assets/avatar.jpg',
url: "/post_images/post_:post_id/:style/:filename"
Here is my url how can i create a symlink for this directory /var/uploads/post_:post_id...

There "right" way to do this is to save files in public/upload/.. directory and ignore upload folder from git. Then create symlink from release current directory to shared directory(MOVE uploads folder first to shared). Add this to deploy.rb:
namespace :deploy do
task :create_symlinks, :role => :app do
run "ln -nfs #{shared_path}/uploads #{release_path}/public/uploads" #Create symlink for public files
run "ln -nfs #{shared_path}/system #{release_path}/system" #Create symlink for private files
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" #Create symlink for database
run "ln -nfs #{shared_path}/.rvmrc #{release_path}/.rvmrc" #Create symlink for rvm
end
end
before "deploy:finalize_update", "deploy:create_symlinks"

Using Capistrano 3.
desc 'Persist Paperclip uploads'
task :create_symlinks do
on roles(:app) do
execute "ln -nfs #{shared_path}/system #{release_path}/system" #Create symlink for private files
end
end
after :published, "deploy:create_symlinks"

Related

How to upload a file to release directory during Capistrano deploy?

The Capistrano production.rb file executes 3 tasks
composer install --no-dev
mkdir storage
upload! ".env.production", ".env"
The first 2 are executed, but the upload fails: "No such file or directory".
I was able to scp the file successfully from the command line.
How to copy a file to the current release directory?
$ cap --version
Capistrano Version: 3.11.0 (Rake Version: 12.3.1)
$ cap production deploy
...
01 mkdir -p ~/public_html/app/releases/20181122210112
...
composer install --no-dev
...
mkdir storage
...
SSHKit::Runner::ExecuteError:
Exception while executing as me#site.com:
scp: ~/public_html/app/releases/20181122210112/.env:
No such file or directory
DEBUG Uploading .env.production 0.0%
...
$
scp the file successfully from the command line:
$ scp .env.production me#site.com:~/public_html/app/releases/20181122210112/.env
production.rb
# use absolute path
set :deploy_to, "/home/user/public_html/app"
namespace :deploy do
desc "Install app dependencies with composer"
after :updated, :build do
on roles(:web) do
within release_path do
execute :composer, "install --no-dev"
execute :mkdir, "storage"
end
end
end
end
namespace :deploy do
desc "Copy Env"
after :finished, :copy do
on roles(:all) do
upload! ".env.production", "#{release_path}/.env"
end
end
end
if your need is to copy your local application.yml to the server, and you are already using capistrano, you can use the capistrano figaro gem, it creates a task to update this file on server. in theory, you could run the task automatically and make this file to be updated. is an old gem, but works like a charm and do the work.

dotenv and linked_files with capistrano 3

Having a really hard time getting this to work. I just added dotenv gem to accommodate for the Rails 4.1 secrets.yml file. I also have in the .env file the database.yml's password.
To add to my deploy:
set :linked_files, %w{config/database.yml .env}
When I run cap production deploy I get:
/shared/config/database.yml does not exist on 107.170.....
How can I get the database.yml to be added?
I looked at the capistrano touch gem with no luck because after I create the empty files, ActiveRecord throws an error of No 'production' database
Create task for upload your .env and database.yml.Look example below:
desc "Database config"
task :setup_config, roles: :app do
# upload you database.yml from config dir to shared dir on server
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
# make symlink
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
# upload you database.yml from config dir to shared dir on server
put File.read(".env"), "#{shared_path}/config/.env"
# make symlink
run "ln -nfs #{shared_path}/config/.env #{current_path}/.env"
end
And add before and after hooks.
Or use dotenv-deployment that contain the same tasks.

Capistrano 3 recipe for dynamic_sitemaps

I'm trying to "translate" the Capistrano recipe to deploy dynamic_sitemaps to work with Capistrano 3.
The snippet suggested in the readme looks like this:
after "deploy:update_code", "sitemaps:create_symlink"
namespace :sitemaps do
task :create_symlink, roles: :app do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
But this doesn't work with Capistrano 3. I pasted this code into config/deploy.rb and the first error I got was: Don't know how to build task 'sitemaps:create_symlink'`.
I read somewhere that in Capistrano 3 the namespaces have to be defined before the calls so I reversed the order of the blocks, defining the namespace first and having the after call last. I got NoMethodError: undefined method `map' for :roles:Symbol`.
So I rewrote the namespace block to:
namespace :sitemaps do
task :create_symlink do
on roles(:web) do
run "mkdir -p #{shared_path}/sitemaps"
run "rm -rf #{release_path}/public/sitemaps"
run "ln -s #{shared_path}/sitemaps #{release_path}/public/sitemaps"
end
end
end
And now I'm getting Don't know how to build task 'deploy:update_code' and I'm at loss.
While I couldn't solve precisely the issue I posted above, the solution is actually very simple. If using Capistrano 3 just add public/sitemaps to your :linked_dirs setting as such:
set :linked_dirs, %w{bin log tmp vendor/bundle public/system public/sitemaps}
This will create a symbolic link between #{release_path}/public/sitemaps and #{shared_path}/public/sitemap creating the latter if needed.

Could not find rake-10.0.3 in any of the sources

I successfully installed a Rails (3.2.10) app on an Ubuntu 12.04 server on Rackspace following Railscasts 335 deploying to a vps. Nginx, Unicorn, rbenv and Capistrano.
Then, when I try to install a second rails website I get the error "Could not find rake-10.0.3 in any of the sources" during cap:deploy. cap:setup and cap:check were successful. Additionally, there is no current directory under my application folder.
If I cd into the releases directory and run bundle install it is using rake-10.0.3. The only difference between the two apps is that the new app I am trying to install uses the assets pipeline.
Here is my deploy file which is identical for both apps, except for the set :application directive.
set :user, 'mark'
set :scm_passphrase, 'xxxx'
set :domain, '99.99.99.99'
set :application, "my_app"
set :repository, "#{user}##{domain}:git/#{application}.git"
ssh_options[:forward_agent] = true
set :deploy_to, "/var/www/#{application}"
role :app, domain
role :web, domain
role :db, domain, :primary => true
default_run_options[:pty] = true
set :deploy_via, :remote_cache
set :scm, 'git'
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
after "deploy", "deploy:cleanup" # keep only the last 5 releases
namespace :deploy do
%w[start stop restart].each do |command|
desc "#{command} unicorn server"
task command, roles: :app, except: {no_release: true} do
run "/etc/init.d/unicorn_#{application} #{command}"
end
end
task :setup_config, roles: :app do
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
sudo "ln -nfs #{current_path}/config/unicorn_init.sh /etc/init.d/unicorn_#{application}"
run "mkdir -p #{shared_path}/config"
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
task :symlink_config, roles: :app do
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml"
end
after "deploy:finalize_update", "deploy:symlink_config"
desc "Make sure local git is in sync with remote."
task :check_revision, roles: :web do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
before "deploy", "deploy:check_revision"
end
Remove all the versions of Rake ==> gem uninstall rake
Remove Gemfile.lock ==> rm Gemfile.lock
Run bundle install ==> bundle install
gem install rake --version=10.0.2
if you still get the error then put this into your gemfile.
gem 'rake', '0.8.7'
bundle update rake
worked for me. I've had similar problem.
If your still looking for a fix... For me it was a matter of using sudo bundle install instead of just bundle install.
I had this same cryptic error message .... turns out I had created a 'cloned' release inside the release folder which causes sprockets to want to do things there .... I think cap uses the create date as it traverses the release tree so somehow this messed things up .... be sure there are no additional 'hand made' folders in your /releases/ folder and you should get past this bug
I had the same problem. The cause for my problem was that Rake was also found in /usr/bin/local/ in addition to being in the shims directory in rbenv. To check if this is the problem first uninstall rake gem uninstall rake then run which rake. If you get back a path that is different than /Users/username/.rbenv/shims/rake then just remove that binary using sudo rm /path/to/file.
Note that in my case at least, which gem was using a shimed executable that was managed by rbenv, while rake wasn't. So everything worked find until I tried to invoke rake command from the terminal.
Also see: After installing rbenv and changing Ruby version, Rake no longer runs
For rails version from 4.3, let try with bundle exec rails app:update. It worked for me.
Hope it's useful for you.

Capistrano and Carrierwave

I'm deploying a Ruby on Rails and NodeJS application using Capistrano. The uploads folder gets removed on every deploy.
This popped up on several places but it doesn't seem to work:
# Keep File Uploads
task :symlink_uploads do
run "ln -nfs #{shared_path}/rails/uploads #{release_path}/rails/public/uploads"
end
after 'deploy:update_code', 'deploy:symlink_uploads'
the repo:
repo:
/node
/rails
Thanks!
There is another solution to this problem. You can add your uploads dir to Capistrano's shared_children and it will do all the magic automatically. You can find more details in this answer: https://stackoverflow.com/a/9710542/835935
Make sure you remove the existing public/uploads folder, passing -f to ln doesn't cover removing target directories (or at least hasn't done so portably for me)
My symlink directories tasks normally look like
task :symlink_uploads do
run "rm -rf #{release_path}/rails/public/uploads} && ln -nfs #{shared_path}/rails/uploads #{release_path}/rails/public/uploads"
end
Obviously make sure there is nothing in the checked in version of public/uploads that you need!
Did you try
after 'deploy:update_code', ':symlink_uploads'
Your :symlink_uploads task is not in a namespace, so rather do the above or put it in a namespace
namespace :deploy do
task :symlink_uploads do
# ...
end
end
I have similar problem with uploaded file with my RoR app. This is my capistrano tasks:
...
task :link_public_folder, :roles => [:app, :web] do
run "mv -u #{release_path}/public/* #{shared_path}/public"
run "rm -rf #{release_path}/public"
run "ln -s #{shared_path}/public #{release_path}/public"
end
after "deploy:update", "deploy:link_public_folder"
task :setup_config, :roles => :app do
sudo "ln -nfs #{current_path}/config/apache.conf /etc/apache2/sites-available/#{application}"
run "mkdir -p #{shared_path}/config"
run "mkdir -p #{shared_path}/public"
put File.read("config/database.yml"), "#{shared_path}/config/database.yml"
puts "Now edit the config files in #{shared_path}."
end
after "deploy:setup", "deploy:setup_config"
...
Maybe help you
Edit:
I use Carrierwave too.

Resources