rails by default set tmp/ file permission - ruby-on-rails

I have rails application in docker. Also using gitlab ci for deploy to server. And rails store all cache in tmp folder. When ci starts deploying, they should be delete all cache in tmp file. But in this moment, for tmp file sets sudo permissions and gitlab-runner can't delete cache. By default, tmp file created when application is already started.
Can I indicate in rails configs permission for tmp file, before starting my application.
Or you can advise how to solve my problem.

Look at your config.cache_store in your production.rb file and see where it's set to.
I'd suspect you're running into this: https://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-filestore

Related

Folder contents are automatically deleted every time I deploy my Rails app

I have a directory under /public folder with the use of CarrierWave I store all my PDF files under this dir. But the problem is all the time I deploy new version of my Rails app this directory gets cleaned up and the all files are missing. This directory is was set under my uploader.
I also have a directory named "private" which I created manually in order to not to serve sensitive files globally on WEB. Those files also gone after new deployment process.
How can I prevent files from deleting on deploy process?
Thanks.
I assume you are using some automation for deployment. Because if you are updating your code on server instance manually then you can preserve pre uploaded file, but using manually method to update code is not a good practice.
So in automation deployment we generally follow this kind of flow.
Whenever you deploy that create a new deploy version and set that as current version.
Simply that means it's creating a new directory and placing your rails project in it. Now the files you are storing inside the project directory are there in the previous version those are not gone if you are using any linux instance.(Only if you have setup that way to preserve last few versions to restore incase of new deploy is exploded)
Clear till now?
Not suppose you are not keeping any previous version, your files are gone forever.
So it's not a good idea to store any file under project repository.
Best practice is to use bucket system like AWS bucket or Google cloud bucket, where we store all the uploaded file. If having bucket is not in budget, you can choose a different directory on linux server instance outside of project directory. But you have to setup all those upload paths and directory system to be used as bucket.
This problem I am facing with is happening because of capistrano. Every time I run cap production deploy command on my server, the capistrano deployment tool syncs every file with git repo. And the files added by end-users are not stored under my git repos of course, so capistrano overwriting the empty public folder from my repo to the server. Adding the path to :linked_dirs variable under deploy.rb solved my problem.
Another approach could be using a directory which is somewhere else than your project root path (such as /home/files) to store all your files. By doing this you will be seperating your files from project and also prevent capistrano's overwriting problem.
Hope this information will be useful for someone or future me :) ..
When you deploy with capistrano, a new deploy(folder) is created from the repository.
Any files not in the repository are not carried over.
If you want to persist files in public, you need to create a directory in your server first and then create a symlink with capistrano inside public to that folder.
Then have your carrierwave uploads saved to that location.
During each deployment cap will symlink to that directory and your files will be there

Finding the application path after deployment

I successfully deployed my Rails application fireworks_app with Dokku in a Ubuntu 18.04 LTS machine, and now I would like to add a logrotate file for my Rails logs. What is the path of my application and thus of my log files? There is a /home/dokku/fireworks_app folder but this folder contains only the following items:
$ ls
CONTAINER.web.1 DOCKER_OPTIONS_RUN HEAD URLS cache hooks maintenance objects
DOCKER_OPTIONS_BUILD DOKKU_SCALE IP.web.1 VHOST config info nginx.conf refs
DOCKER_OPTIONS_DEPLOY ENV PORT.web.1 branches description letsencrypt nginx.conf.d
I suppose somewhere in the system there is a folder containing my application with the known Rails file structure, but I cannot find it.
Inside your rails application you can do something like Rails.root to get the path to the root directory of your project.
For instance, if the log files are in fireworks_app/lib/log/ you could do Rails.root.join('lib','log','log_file.log') to get the path to that file.

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).

Rails 4.2 capistrano 3 deployment

I am totally new to rails deployment. After googling, I still find it hard to understand how to deploy rails apps.
So, my questions are:
After setting up the VPS with all rails dependencies, where do I store my codebase? The root directory of the VPS or some specific locations e.g. www/ or public/?
Should I upload the whole rails app folder or just part of it? I have paperclip in my rails app, and paperclip creates a system/ directory in the public/ folder, so should I upload system/?
In Capistrano 3, there is a repo_url field, I know they support file://, https://, ssh://, or svn+ssh://, but most of the articles about capistrano put github repositories into that. However, I do not have such a github repo. What should I input then?
Thank you for your attention.
Answers to the specific questions raised:
After setting up the VPS with all rails dependencies, where do I store
my codebase? The root directory of the VPS or some specific locations
e.g. www/ or public/?
It will be deployed to the folder pointed by :deploy_to parameter. If not specified, :deploy_to defaults to /var/www/#{fetch(:application) See: https://github.com/capistrano/capistrano/blob/05f63f5f333bb261f2a4c4497174361c48143252/lib/capistrano/defaults.rb#L3
Should I upload the whole rails app folder or just part of it? I have
paperclip in my rails app, and paperclip creates a system/ directory
in the public/ folder, so should I upload system/?
Paperclip system folder is specific to the environment; each environment (development, production,...) will have its own system folder which will store the files uploaded on that specific environment. This folder should not be part of the code being deployed.
The recommended way of handing such folders is the keep them in a shared folder on the server, and create symlinks in the current version of the code so that the same folder is used for storing/retrieving attachments. See Section 3. Update custom links section in http://robmclarty.com/blog/how-to-deploy-a-rails-4-app-with-git-and-capistrano for more details about this.
As mentioned there, the same applies to config/database.yml file, and any other file containing environment specific configurations.
In Capistrano 3, there is a repo_url field, I know they support
file://, https://, ssh://, or svn+ssh://, but most of the articles
about capistrano put github repositories into that. However, I do not
have such a github repo. What should I input then?
Depends on where the code you are deploying is stored. If it is in a local folder, use the file::// format to specify where the files are located.
You can set up your own private git server, then in deploy.rb you can put something like
set :repo_url, 'ssh://user#server_ip/path/to/your_git_repo.git'
When you commit your changes to the git repo, you do not have to upload the app to the server. Capistrano will upload the app for you when you deploy.
where do i put my code base? This is determined by what you put in deploy.rb e.g
set :deploy_to, '/path/to/my_codebase'
Whether to upload the /system directory will depend on whether you want the paperclip images on your version control. If not you can add the directory to gitignore. Here is a tutorial on how to deploy on ubuntu 14.04 passenger and NGINX. if you are not using Passenger and Nginx you can skip to how to configure capistrano and make adjustments depending on your setup.
EDIT
You need to install git on your development machine and set up a git server on your VPS as explained on the link above, add your remote server to your local machine using
git remote add origin <server>
where 'server' is the url to your git repo in the VPS e.g.
ssh://VPS_user#VPS_ip/path/to/your_git_repo.git
Now when you commit and push your changes to the server, capistrano will deploy the latest version on your git server.
Here is a link with a guide on how to get started with git

Resources