Keeping static files in server when deploying with Capistrano - ruby-on-rails

I'm uploading files to my public/files folder of a Rails application on a constant basis through a web interface.
I don't want to keep these in source control since they go for almost 2 GBs, so every time I do a cap deploy it will save those files away in releases/ and replace the directory with the pristine copy stored in the repository.
I'm wondering what's the best way to keep those files in the server, in the current directory. Some of my ideas are:
Remove the directory from source control and replace it with a link to an external directory that's not managed by Capistrano.
Create a Capistrano task to copy the directory to /tmp before deploying and then copying it back to /public after it's done deploying.
Is there standard way to do this?

For the future record, this is the task I used to do it with a shared directory:
task :link_shared_directories do
run "ln -s #{shared_path}/files #{release_path}/public/files"
end
after "deploy:update_code", :link_shared_directories

You could make files a symlink to another directory on your machine, for examples the /shared directory at the same level as /current and /releases.
Check out capistrano manages the /log and /tmp directories.

Now we can simply use :linked_files in deploy.rb:
set :linked_files, %w{config/database.yml}
In this case, the file [target_dir]/shared/config/database.yml must exist on the server.

Related

Rails make public,tmp and log folders outside the Root Folder

Everytime that the carrierwave save a file, go to app/public folder, and the tmp and log folder get bigger overtime, so, is possible to make these folder outside the RailsApp?
change the Rails.root.join("") on the application.rb is the only way to do it?
on mina deploy is possible to create shared_path if created ouside have to symlink the path?
If you are deploying on a *nix box then symlinks are the way to go. I haven't used Mina, but if you deploy with Capistrano then you can script re-creation of whatever symlinks are needed fairly easily.
Just looking at the Mina page, you should be able to write a task to re-create your symlinks on deploy:
writing_your_own_tasks

How do I prevent mina from removing images uploaded on production server?

Im using mina to deploy my rails 4 app. When ever I mina deploy it clears out all the images that have been uploaded to my app. How do I stop this from happening? Thanks.
You need to ensure your uploaded assets end up in the shared directory, similar to the way your database.yml is done.
For example, our assets are all stored in public/system, so we have a line that looks like this:
set :shared_paths, %w[
files
log
private
public/system
tmp
]
Then, when you run invoke :'deploy:link_shared_paths' those directories will be linked to the root of your current directory, if they exist in the shared directory (you can create/populate them if they do not).

What is the importance of creating symlink & how to create it in EngineYard

My rails app is on engineyard server.
I need to create symlink for public folder.
How to create symlink on engineyard server?
I have no experience in deployment so i am very eager to know what is the importance of the symlink & for which folder it should be created?
Also currently i am using my staging environment what should i write in code or create a file so that it should create a symlink automatically when i deploy same code on production.
Thanks!
The proper way to create a symlink on EngineYard is to add deploy hooks. You want to add a /deploy directory to your project and add a before_symlink.rb file.
For example, if I put a configuration file in the shared directory (/data/my_app/shared/config), I can add a deploy hook to symlink this file in.
The contents of your file would look like this:
run "ln -nfs #{shared_path}/config/some_config.yml #{release_path}/config/some_config.yml"
The #{shared_path} variable points to your apps shared directory and #{release_path} is the current release being created as part of the deploy.
More info can be found at: http://docs.engineyard.com/use-deploy-hooks-with-engine-yard-cloud.html
The symlink should get automatically created whenever you deploy. Its purpose is to maintain the same path to your application across multiple deployments. When you deploy your application, you should create a symlink to the latest release, like this (on a Unix machine):
ln -s /application/releases/10102011011029/public /application/current
The first path is the REAL file or directory. The second path is the path and name of the symlink. Now when you point something to /application/current, it will be in the latest release.
If you use Capistrano, all of this is taken care of for you automatically whenever you deploy.

Symlink single file via Moonshine and/or Capistrano on production server

My application is set up with Moonshine which configures the server, then uses Capistrano to deploy. However, there are some files and directories that are not source control managed that shouldn't be deleted when a new deploy is made, nor should they come off my development machine.
In Moonshine's config/moonshine.yml file, there is the :app_symlinks area where symlinks are created to the shared folder on production. This works perfect for particular directories inside the public folder (like 'system')...however I need to do the same on individual files. For example, the sitemap.xml file that is automatically generated based on the database on production. Or the .htaccess file.
I tried to put individual files underneath :app_symlinks, but this didn't work. Is there a way in Moonshine to symlink individual files? Or alternatively, a way to do this in Capistrano (as that what Moonshine uses for deployment anyway).
You can do the following in your deploy.rb:
desc "Link the file"
task :link_file do
run "ln -nfs #{deploy_to}/shared/files/myfile #{release_path}/myfile"
end
and then add this task to: after "deploy", :link_file
Of course you'll need to create /shared/files directory manually, just once.

Apache2, Git, Capistrano & Rails - creating symlinks

I'm sort of stuck with adding symlinks to my app on the server. I currently have the following in .gitignore:
/non-public/system/uploads/*
I basically don't want Git to store the contents of the upload directory. So far so good.
On my server, inside my deploy.rb, I have the following:
namespace :customs do
task :symlink, :roles => :app do
run <<-CMD
ln -nfs #{shared_path}/system/uploads #{release_path}/non-public/system/uploads
CMD
end
end
after "deploy:symlink","customs:symlink"
after "deploy", "deploy:cleanup"
I want to create a symlink after each deployment for the uploads directory, but I keep getting a failed error message because the non-public/system/uploads directory doesn't exist in the git repository in the first place.
I've verified this by taking a look at the repository, and the structure /non-public/system/uploads doesn't exist because I have that set in .gitignore to ignore it.
I've looked at the Git wiki and it doesn't track directories, so I must be missing something. How do other developers symlink the uploads directory with their server?
What I'll usually do on my cap deploys is to create the directories, by doing a basic
set :deploy_to, "/this/dir"
run "mkdir -p #{deploy_to}/then/more/dirs"
after "deploy:symlink"
namespace :deploy....
....
....
then provide the run code to do some symlinks either on an after or whatever. This probably not optimal for all situations, but for the simple stuff it usually gets the job done.

Resources