How to handle non-gem dependencies in Rails - ruby-on-rails

I have what may be a stupid question. If it is, sorry.
I recently took on a Rails project that requires PhantomJS in order for its Cucumber scenarios to run properly. Unfortunately, the app doesn't say that it depends on PhantomJS, and so when the scenarios failed, it just looked like things were broken, and it wasn't really clear why.
The only way to make the errors stop was to do brew install phantomjs. Obviously, this is not a gem, and installing the PhantomJS gem didn't do anything, either.
What's the proper way to handle non-gem dependencies in Rails projects?

I don't believe there is a standard way.
PhantomJS is a standalone application and needs to be installed as such. It would be the same as installing redis or postgreSQL if your application depended on it.
You were right to install using homebrew though, makes updating removing easier.

This is not a trivial matter. AFAIK,there is no built-in utility to declare dependencies on other things than gems / rubies. Ruby is an interpreted language, and as such may run on a variety of environments ; managing dependencies at this level would be a rather intricate task.
Many people handle this using deployment tools like capistrano or puppet to make sure that the environment in which runs the app is adequate, and install required dependencies if need be. Typically, you would create a rake task to do this, and either call it via capistrano, or have the user call it manually.
If you want to warn people about dependencies, an option would be to use an initializer to check on application boot that the required dependencies are installed on the environment, and issue a warning (or entirely shut down the app) if not.
Another option would simply to perform dependency checking as a series of tests. So if these tests fail you can provide detailed instructions on what is missing.
All in all, the least you can do is just to list environment dependencies in your documentation. Make sure this is on top of your main documentation file.

Related

Can't Load Rails Server Even Could not find pg-0.17.1 in any of the sources

pretty new to all this and ran into a real ditch. I had ruby 2.0.0p353 running with rails 4.1+, everything was setup with homebrew, xcode, git,heroku etc.... I'm on OSX 10.9.4
then came time to try out S3 and install the aws-sdk gem. I was unable to install the nokogiri gem after scouring stackoverflow for days to no avail. I then came across this article online that suggested to update rails and ruby versions. In my attempt to upgrade my ruby version to the latest, I followed the instructions given here as follows:
in terminal and in my app folder:
/usr/local/opt/ruby/bin
export PATH="/usr/local/opt/ruby/bin:$PATH"
source ~/.bash_profile
gem update --system
..I subsequently tried bundle update / installs as well... and receive this error shown on pastebin: http://pastebin.com/Q64j0LwD
Now, things are completely messed up as I am unable to even run a rails server. Getting this error Could not find pg-0.17.1 in any of the sources
I currently have Ruby 2.1.2p95 installed. I don't know what else is installed during this trial and error probably several versions of many things unfortunately. Any advice would be great.
: Could not find pg-0.17.1 in any of the sources
Try postgresapp and use documentation for install and configure postgresql server.
The easiest way to get started with PostgreSQL on the Mac
You're clearly new to rails coding. I made a lot of mistakes when I started. I still make a lot of mistakes, but they're more complex mistakes now, mostly ;)
I'd start by using "RailsInstaller". Excellent way to get all the pieces... except Postgres. As suggested by someone with a name consisting of a lot of non-ASCII characters, PostgresApp is incredibly useful to get a more up to date Postgres without messing with compiling stuff outside the project.
You probably should use "rvm" or "rbenv" to manage your rubies. I've tried both, and I favour rvm. rbenv is recommended, but I spent an awkward half day trying to disentangle the consequence of typing "bundle exec rails new app", before I decided to go back to rvm. rvm works more naturally for me, at the expense of creating gemsets for each project. Which, personally, I like. I want to isolate each thing I add to an OS, so I can work on multiple projects with less contamination, and without having to create a VM for each project.
Contamination is, I think the problem with Nokogiri. Everyone tells you to go install brew or Macports or build it all for yourself from scratch. When you do, you get lots of stuff installed. And that stuff affects the compilation process. I'm about 80% certain that I can't compile Nokogiri with system libraries because other projects have dropped include files that rename iconv_open to libiconv_open - which isn't in the native library. I have Nokogiri compiling now, but only as a result of using GNU iconv, and using the arcane "bundle config" to set up a build time dependency for nokogiri alone to use the /opt/local/include version of iconv. That was time consuming.
So, some advice that I haven't taken for myself yet. Clean out the brew/ports stuff. I suspect that you can run anything you need to, in locally installed project directories, rather than interfering with the OS. Unless you really need something that doesn't ship with a Mac, like an up to date ruby (solved by rbenv/rvm)
Look into Heroku. Low cost way to get started with publishing your small apps.
Make sure you have GitHub and BitBucket accounts. Personally I contribute to projects on GitHub, but my private stuff is on BitBucket - pricing models.
Git looks mad. But git is wonderful. Learn to branch. Lots. Learn to merge. Learn to tag, and learn how to push and pull from an upstream repo. These words may mean nothing to you now, but they will save your sanity. You'll use git to push your projects to Heroku. Just freaking amazing. Learn how to have a staging branch and a live branch, and push each to a different Heroku instance, so you can be testing user acceptance on a public facing server, without contaminating dev or live versions. Git/Heroku. Joyful.
Watch out for several things that bit me... Ruby gets lots of patches. They are meaningful. I spent days trying to work out why a piece of ruby code failed, only to discover it worked in a different patch level. Watch for the updates and apply them - except for 'bundle update'. Don't do 'bundle update' until you are old and wise.
Gem versions - that also bit me. Got a project that worked. Then it didn't, with no code changes at all... except that I'd updated my gems. A later version of a gem upset the code. So...
Bundler is your other friend. Lock down the versions of gems that you need for a project. Don't use "bundle update" unless you are prepared for strange things to happen. Make nice Gemfiles.
You probably need to get to grips with TDD and preferably BDD.
So next thing you need is to get the Qt library installed, and use "gem 'capybara-webkit'" and Cucumber with Rspec-Rails to help you write tests that the browser will execute. Butt saver central, if you start changing gem versions. At least you know when the tests stopped working, and can use git to revert to a known working chunk. More importantly, it saves, eventually, a lot of tedious checking around when something unexpectedly stops working.
Also... make sure that your development group of gems (in Gemfile) includes "better_errors" and "binding_of_caller". A REPL in the browser pane when your code fails, is wonderful.
If you want to just throw some stuff together, e.g. office admin projects that you don't want to spend a load of time refining the UI on, but just build something that works. Try 'hobo'. I find it very useful for rapidly building something. Faster to code it than to spec it or draw it. Seriously. And it is all over-rideable, though I've never turned any Hobo code into something for high scaled usage...
Welcome to the amazing world of developing in Rails, on a Mac. It's a rapidly evolving hoot. Hope that helps. :)

Why am I getting an error when I bundle install without using "sudo"

Recently I have noticed that trying to bundle without the "sudo" command almost always results in something like:
Could not find gem 'faye (~> 1.0.0) ruby' in the gems available on this machine.
but using sudo bundle install works like a champ.
Why would my computer be doing this?
Note
I don't really know what extra information to provide, so feel free to request anything.
With "sudo" the command is executed by the root user. When executed like this, it also includes that some environment variables are set differently, because they are needed in the context of the root user.
This might be what's happening here, that since possibly the PATH variable (or some other environment variable) is different in the sudo-environment, it can actually find the gem it is looking for. While in your normal execution environment, it can not find it, since the PATH is set differently.
Hope this helps :)
Following up on #topedro's answer: You're seeing this error because you did some actions as root, and some as another user.
Most people avoid this situation by installing Ruby and all gems as the same user who'll be executing them. E.g., your personal user account, or one set up for this purpose on a server named (usually) "deploy".
An easy way to get started from your current broken state is to install RVM as a non-root user, and go from there. You could also download the Ruby source and compile it yourself instead of using RVM.
Whatever you do, treat Ruby and the gems as simply some app owned by a non-root user, residing in that user's home directory.

How do I set up an old Ruby on Rails project on a new server?

I'm not a RoR programmer myself, but a good client of ours has sent a project their previous web team built and I need to get it up and running on their server.
The server uses cPanel and Ruby on Rails is already installed. I've created a project via the cPanel wizard and located the file tree via SSH.
Using SSH, I've tried to replace this file tree with the project I've been sent, but when I hit 'run' in cPanel, the application doesn't actually start (although the success message would indicate that it has).
If I leave the original cPanel-created application in place, I can run/stop no problem and the web interface at :12001 opens up just fine.
I assume there are either conflicts with RoR versions that I need to resolve, or there's simply more to it than just replacing the file tree? Again I'm not a RoR programmer and I'm having a hard time finding a migration guide that tells me anything other than "set up in cPanel and replace the files".
I'd very much appreciate either some genuinely useful links to RoR application setup/migration guides (ideally for cPanel) or a step-by-step answer please.
First, forget Cpanel for now. Try in one environment where you can control everything.
Try to know better the rails version used and the associated gem19s or plugin if from 2.x days. The ruby version is important too, only then you can start defining a plan.
I'm afraid you won't get a step-by-step answer, but I'm sure you can be pointed in the right direction by providing the requested information.
Simple questions: Do you have a Gemfile file at the top at your project? Do you have any plugins (stuff in vendor/plugins)?
Update:
With the Gemfile provided here are the required steps:
Install ruby (if you haven't install it using rvm. The version 1.9.3-x should be the safest.
Install rubygems
Install bundler
Go the project dir and run bundle install
run rake db:migrate (assure you have the database setup acording to config/database.yml
run rails s and check the logs and see if the server is up.
If after installing bundler, you don't have the bundle command in your path, you need to add this your .bash_profile:
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH

Rails 3.1 application deployment tutorial

I'm looking for a good deployment tutorial for a Rails 3.1.1 application on a server. And by good I actually mean complete. The reason I'm posting this question is that although there are tons of tutorials out there on the web (google, blogs, books, other stackoverflow questions etc...) all of them seem to focus either on a problem with the deployment process or make some assumptions about the deployment environment that do not match with what I need.
I realize that deploying a Rails app on a server requieres a variety of different programs and tools that need to be configured and somehow I always get stuck on apparently "little" things that make me very frustrated.
So, let's begin from the start. What I have now is a server that I can access through SSH and a domain name, let's call it www.example.com. The server runs a fresh Ubuntu 10.04 x64 and has just a user installed, namely root (through root I access the server with SSH).
Note! There is no Apache, Ruby, PHP, MySQL, cPanel or any other panel installed, just the bare minimum that comes with a fresh installation.
Also the web server will host a single application and the database will run on the same machine.
From my knowledge the process of deploying a Rails application follows the following scenarios:
Installing Ruby
I already did this by using RVM using the Multi-User install process (simply because I have just the root user and it does it automatically). This seems to work fine aftewards but I do have some questions:
Would it make more sense to install Ruby directly and not through RVM (I'm thinking maybe in terms of efficiency - also RVM does a little bit of magic behind the scences modifying some bash_profile files in ways that I don't understand and this somehow seems invasive...)?
Would it make more sense to install as a separate user through RVM (can there any safety problems arise)?
Necessary gems
Now that Ruby is installed what would be the best initial set of gems to install along side it?
I installed just bundler, so that once I upload my application on the server I can run a bundle install command which will install in turn the required app gems.
Question - Should I install the rails gem before hand? Are there any other gems that need to be installed?
Web server
This is where it gets tricky for me. I'm trying to use apache and mod-passenger for deployment (they seem to be the most popular). So I installed the apache web server and the passenger gem and all the related dependencies (libraries mentioned by passenger).
Now, the problems arise. First thing is user related. How does Apache run? I mean under each user? If I installed it and (re)started it using the root user, will it permanently run under the root user? Is this a bad thing? If yes, should I create another user? If yes, how? In what groups should I put the new user in, what rights should he have (namely what folders should he have access to). How to start the apache in this case (under root, under that user, add a line somewhere in the configuration file, etc.)
Website configuration
Where to put the website configuration stuff? Is it OK to put it into apache.conf, or should I create a new file in sites-available? Or should I simply reuse the default file that is already present there? If a new user has been created at the previous step, what rights should he have in relation to the config files?
Also... where to eventually put the rails application? In what folder would it be best? Somewhere under home? Maybe under /var/www? I want to know how would this affect the rights of the apache process serving the app? (Generally I have problems while serving an app, it complains that it doesn't have rights on a specific folder. Also, using the new asset pipelines - which I don't fully understand - asset files are being created and they seem not to inherit the parent folder rights...)
Database
As database I'm using MySQL and installing it is pretty straightforward. The question that I have here is again user related. Should I use a special user for the database? How does MySQL actually manage users (are they internal, or are they the Linux users or ...?). Should the database user be the same as the "web user" from above? Or should it be a different one?
Assets
Here I get really lost. I really have troubles making the assets pipeline works. I've checked the railscasts episode and also the rails website tutorial and I do seem to theoretically understand the thing, yet I have problems in practice.
So the questions here would be - what commands should I run to precompile the assets? (Trying to run rake assets:precompile). Is it ok? What should I change in the production.rb file? How do the assets generated work in relation with the webuser or database users created above? I am personally getting a problem in this step namely the log files say that some css files are not accessible (for example I have jqplot library installed under the vendor/assets folder and its files cannot be accessed - should I add a manifest file here somehow?).
It would be really great if someone could point me towards a nice article, or resource that explains all this stuff. The main area which I'm having problems in is how does Apache, Passenger, Ruby, Rails and MySQL interact with a Linux user. How to properly set up the permissions for the Rails app folder? How does the assets pipeline affect this user permission stuff?
Thank you
Here's my way of deploying Rails:
Installing Ruby
I would not use RVM because it's very tricky to get it running properly in combination with stuff like cron jobs. Doable (with wrappers for example), but a bit of a hassle. If you don't need other Ruby versions on that machine, just install it from source yourself.
Gems
Just let Bundler work its magic. Remember to install with the flags --without test development --deployment. I wouldn't do that up front though. Just make sure you have bundler.
Web server
Using Passenger (with either Apache or Nginx) is a fine and easy choice. When you install Apache from apt, it will run in a special user.
Apache is configured correctly automatically on Ubuntu. It will start on reboot.
Website configuration
All configuration be in /etc/apache2. Place your virtual host configuration in /etc/apache2/sites-available and use a2ensite to enable it.
Put your app in /var/www, since that is the default location in Ubuntu. I usually make a deploy user.
Make sure that user can access your application by assigning your app to the www-data group.
Database
MySQL has its own user system. Log in as root user and create a new user with SQL: GRANT ALL ON 'application_production'.* TO 'deploy' IDENTIFIED BY 'some password';
Assets
Assets should be generated as the user owning the application. You should add any css and javascript files to production.rb. For example:
config.assets.precompile += %w(backend.css)
Conclusion
It helps to use a deploy tool like Capistrano. When you run capify, uncomment the appropriate line in the Capfile to get assets compilation. Here's mine:
ENV['RAILS_ENV'] = 'production'
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
load 'deploy/assets'
load 'config/deploy'
require 'capistrano/ext/multistage'
require "bundler/capistrano"
require 'capistrano_colors'
This is just how I normally install my rails apps. I hope this will get you going. Recently I've written a gem for integrating Chef-solo with Capistrano, called capistrano-chef-solo. It's very alpha and might be a bit too complicated if you're just starting with deployment, but it might help you.

What should I do if my Rails tests don't pass?

I want to start contributing to Rails, fixing patches, submitting my own code etc, and the Rails guide states that the tests MUST run. However, they're currently not and I'm not quite sure what to do.
I'm running Mac OS X, Ruby 1.8 and I have all the needed gems installed - what can I do?
The best place to look is the Rails continuous integration server, which will tell you if there are any currently failing tests. It looks like the most recent edge Rails build failed, and if you checked out the code at any time between August 8th and 16th, you probably got a build with a few failures.
The failing CI tests were due to errors with SQLite2 (now removed from master) and Postgres. We fixed these, but none of these run from a normal rake test so your problems probably aren't related.
Make sure you have the latest Mocha gem installed and that you follow the directions provided by Mike Gunderloy on his afreshcup.com blog: http://afreshcup.com/2008/10/27/contributing-to-rails-step-by-step/

Resources