Errno::EACCES solution - ruby-on-rails

I've been hounded by the above error whenever my rails project has to alter the filesystem. Be it carrierwave image uploads or creating a folder via
Dir.mkdir
Lately I'm getting this error because the gem I'm using wants to create a directory at /home/git/repositories and it can't because it hits this permission error.
I know this is a stupid idea, but in desperation I've set /home recursively to 7777 and, against all sense and reason, I'm still getting
Errno::EACCES at /users
Permission denied
I've been hounded by this error. I had the same thing with carrierwave, even when I set its directory recursively to 7777
Same thing with paperclip too.
I fixed carrierwave by deleting the directory and letting carrierwave create the directory structure itself when it saw the directories weren't there. It was the only way I could do it, after much hair-pulling and setting every directory on my machine to 7777! (did it in a VM because I know how silly that is :P)
Is this something to do with the application server? What user is running the application server? I thunk that may be the key because it fixed the error when the application server created the directories, rather than myself doing it manually. But I just run the server normally, under my own account... the same account that creates the directories and sets the permissions... What am I doing wrong to continually hit this error?
Can I run something along the lines of sudo rails s?
Also I can't let the system create the directories because then I get a 'directory not found error'. I know I'm on the right lines because upon using mkdir manually to create /home/git/repositories I started getting Permission denied errors instead.

You probably don't want the stickbit/setuser/setgroup bits. Maybe you want 0664 instead, which grants read/write to the owning user/group, and read to other users.
If it's trying to write in /users then the problem is probably that /users doesn't exist and your root / only permits root to create directories. You should probably verify which paths it's trying to write to; just slinging permissive modes around is a) probably not going to solve the problem, b) leaves you open to security issues, and c) is a really bad habit to get in to.
Running your rails app as root will work, but is a truly awful idea. Don't do it.

Related

Working with production.log rails

I got my production log file working. But wanted to get some suggestions onto how to work with it. I currently look at it view console and doing either
tail log/*
tail -f log/production.log
to look at it. I would like to either 1. find a better way to be able to look at the logs and perhaps with colors. 2. Can the production log file be outputted in a view where an admin user can view it?
Lastly, is there a gem that's great for emailing me when an error occurred and what the error was / how do you currently keep track of when errors happening in production?
Thanks!
For just having a nicer alternative to tail which shows the colours instead of those ESC[… codes, try less -R production.log. If you want to follow the end of the file like tail -f, use:
less -R +F production.log
There's nothing built-in for getting the log into a view as far as I know, but I suppose you could write a controller which shows (some of) the log in a view. Make sure it's secure with authentication though; you don't want to be exposing your log to the internet! Using an external service should remove this concern and it will have a load of other features. For example, I haven't tried papertrail (https://papertrailapp.com) but it's been on my to-try list for a while. It looks good and they're free for up to 100MB of logs a month. I've used airbrake (https://airbrake.io/) in the past for exception notifications but I don't think they have a free tier - just a free trial.

Getting Xapian running in Ruby on Rails app: Couldn't detect type of database

I am trying to get a Ruby on Rails application running on Ubuntu. It utilizes Xapian in order to search for documents. I already installed the xapian-full Gem in version 1.1.3.4 as instructed by the Gemfile and I created the directory files/default where the Xapian database will probably be.
database = Xapian::Database.new('files/default');
As soon as the code runs into this line, there is an error:
IOError in SearchController#index
DatabaseOpeningError: Couldn't detect type of database
Do I need to initialize the database or something? I looked the Xapian Docs and I searched for the error message on the internet, but none of this really helped.
(Writing this answer with knowledge of Xapian, but not the xapian-full Gem, so it's possible some details may be wrong - but the error comes from Xapian, so I'm pretty sure this is on the right lines.)
The error is because you created the directory files/default. Instead, just create the files directory, and ensure the process running Xapian has permission to write to that directory.
Why does Xapian raise an error here? Well, it's because Xapian databases consist simply of a directory containing a special set of files. When Xapian::Database.new is called, it checks if the database already exists before creating a new one. In the default opening mode, if the database directory already exists, it assumes that it shouldn't overwrite whatever's there with a new database, so it tries to open the existing database. Because the directory is just empty, this throws the error you see.

What is "th-ruby-include=" directory in rails app root ?

What is th-ruby-include= directory in rails app root?
I did't touch it.
I'm considering add this directory into .gitigore or just delete it everytime I see it.
If this is a directory in your project, it was created outside of Rails. Likely your editor saved a file without your full understanding.
I would normally say you could delete it, but it's hard to say without knowing the contents. You can always move the directory, run your tests, and if it all passes, delete then.
I also had this directory in my application root. In my case it contained a tree of files that looked related to rvm. I was bold and just deleted it. Everything appears to be fine afterwards.
If I find that it reappears then I will work out what I did to make that happen and I will post an update here.

Passenger "No such file to load" error for Model

I'm having a problem with deploying a Rails 3 app to a Passenger/Apache2/RVM server running Ubuntu 11.10.
I'm getting the error:
A source file that the application requires, is missing.
...
Error message: No such file to load -- Goal
Exception class: LoadError
Goal is one of my Rails Models.
I've seen a lot of people with a similar error message, but their issues seem to relate to missing bundles/gems - not model files! Their problems were resolved by ensuring permissions were set up correctly, so to that end I've got:
Apache running as www-data, as is Passenger. RVM is installed as www-data and I've chown'd the entire web app directory to www-data.
I've been wrestling with this for some time now and am really out of ideas - if anyone has any suggestions I'd be very grateful!
You might probably have the file structure of your Rails app wrong. Are you sure you have the "goal.rb" file within {your_app_root_url}/app/models/ ?
To me, if I am seeing the error when you view the app in browser, it seems like your rails app and passenger(with apache2) are working fine. The other possibility is you might be looking at wrong app folder which passenger is set up to.
It's hard to tell what's wrong. Please provide more details of how you set up your passenger with your rails app directory. Also it would help if you could show what's inside your app (app structure) and what's inside goal.rb model file.
Well I've solved it. Turned out that a work around for a problem with yaml encoded model archives (i.e. an explicit require of Goal) in a completely unrelated Model was causing the error to appear. Looks like it only rears its head in production - probably due to the differences between the class loaders used in development vs. production.
Tough one to find... the stack trace didn't include a reference to where the problem actually was!

Passenger problem: "no such file to load" -- /config/environment

I've been researching this one and found references to similar problems here and there, but none of them has led to a solution yet. I've installed passenger (2.2.11) and nginx (0.7.64) and when I start things up and hit a Rails URL, I get an error page informing me of a load error:
no such file to load -- /path/to/app/config/environment
From what I've found online this appears to be some sort of a user/permissions error, but I've tried all the logical fixes: I've made sure that /config/environment.rb is not owned by root, but by a webapp user. I've tried setting passenger_default_user, I've tried setting passenger_user_switching off. I've even tried setting the nginx user, though that shouldn't matter much. I've gotten some differing results, but nothing's actually worked. I'm hoping someone may have the magical combination of settings and permissions for this. I may try backing down to an earlier version of Passenger, because I've never had this issue before; it's been a little while since I set up Passenger though.
Thanks for any suggestions.
EDITED: See below for the answer I stumbled on.
Modern Passenger prefers Rack to Rails. If you have a config.ru in your Rails application, Passenger will try to load it as a Rack application. This may be causing problems. In particular, your error message refers to config/environment -- note the lack of the .rb extension typical in a Rails application.
Try moving config.ru out of the way if it exists.
Naturally, it just took me posting the question to stumble onto the answer. In order to provide info for anyone else searching on this problem, I'll post some details here.
The relevant lines from the nginx.conf:
user www-data; # in order to have nginx not run as root
passenger_default_user www-data; # likewise for passenger
root /opt/foo/app/current/public;
The key at this point is to make sure that the application files are owned by www-data, in particular config/environment.rb because apparently Passenger looks at its owner to determine who to run as. This might mean that the passenger_default_user entry is irrelevant? But it's good to have it there as documentation of intent anyway, perhaps.
Finally, make sure that the parent directories of your app are all reachable by www-data -- in my case the system default setup had left a directory 0700, which I'd missed.

Resources