running rails on guest linux virtualbox: a database.yml issue - ruby-on-rails

a) I'm a ruby in rails beginner developer, and I use windows 7 machine as developement environment...
b) With VirtualBox I just installed, inside the Windows 7 "host", a Linux ubuntu sever "guest", just to run the rails DEVELOPMENT environment ALSO in the linux machine.
c) To do that I configured a virtualbox SHARED FOLDER:
let say I have this shared folder on the host machine (window):
c:\rails\esamiAnatomia
and mounted it on the linux embedded server:
/home/solyaris/host/esamianatomia
d) In this exptended "developement environment" I would like to edit source files with my preferred visual editor on windows (sublime text) and run rails server on linux.
The problem concern database.yml configuration file:
/home/solyaris/host/esamianatomia/config/database.yml
because on Windows I have a database (postgresql) responding port 5433, with specific username/password
but in linux database respond to port 5432, etc.
Questions:
1) It's that "arcgitectural solution ok ? (I mean: developing/editing from a windows 7 host, but running rails server of the linux guest server);
2) There is a way to change/configure database.yml on the fly (I mean: using two different database.yml files: one for the linux machine and anotherone for the window machine) ?
thanks a lot
giorgio

You can technically accomplish 2 if you're not afraid to play around with the guts of Rails. As with any solution that has you accessing internal rails components this may stop working at any time, but fortunately this part of the API is not likely to change often, if ever. Still, use this at your own risk.
Here's how I do it on my projects. First modify your application as follows:
# config/application.rb:
# After require 'rails/all'
require_relative 'db_override'
Then create this new file:
# config/db_override.rb:
case Socket.gethostname
when 'host1'
$db_config = 'config/host1_database.yml'
when 'host2'
$db_config = 'config/host2_database.yml'
else
$db_config = nil # Use the default config/database.yml
end
if $db_config
class DBConfigSelect < Rails::Railtie
initializer "db_config_select", before: "active_record.initialize_database" do
puts "Using custom DB configuration: #{$db_config}"
# Get the existing path configuration
cur_paths = Rails.application.config.paths['config/database'].instance_variable_get :#paths
# Override the default config sources
cur_paths.shift
cur_paths.push $db_config
end
end
end

What you are describing is pretty much the setup that Vagrant is offering, so yeah, you are doing fine, everyone else is also doing it but they didn't set it up themselves (and by that probably get some really nice addons as well, you should take a look at Vagrant).
For your second question: no. Not on the fly. Rails loads the database.yml end then connects to the database with that. When you change it while your Rails server is running, the changes won't get noticed. What you can do however is setup a new environment for your two different machines. Then you can switch between the different environments and depending on the environment, one or the other database gets accessed.

Related

Local configuration for multinode rails applications

I'd like to introduce per-machine configuration to my rails application. It's currently deployed with Capistrano, and I need to introduce at least one machine-specific variable -- a hostname, so that performance characteristics can be properly tracked. In the future, there are likely going to be further local differences. The code is deployed to a 3 node load balanced cluster from the same git repo.
What is the best way to manage machine specific config?
You can set environment variables in Capistrano config file, and fetch it in your rails app.
config/deploy/.rb
%w[
node1.example.com
node2.example.com
node3.example.com
].each do |host|
server host, roles: %w[app], default_env: {hostname: host}
end
Then you can access the hostname with ENV['HOSTNAME'].

Rails application issue after deployment - 404 error

I'm new to RoR.
I was able to install Rails and host it in Webrick (Sample App with "Welcome" controller) in my windows.
Now i have a Unix Weblogic Server along with a dedicated domian.
After exporting the .WAR file using Warbler, i accessed the Oracle Admin Console from where i deployed the .WAR file in the dedicated domain. I did all this for the Sample app with only the Welcome controller in it.
But even after deploying the WAR file, on accessing the Domain along with the Port Number (:9002) i ended up with 404 file not found error On looking at the server logs,there wasn't any records relating to any error. The Application must have been deployed properly. I assume that i must have missed out on some basic configurations in the routes.rb or similar files before deploying. Can anyone Guess what are all the possibilities and if possible can anyone help me by pointing to any tuts that cover the Steps to be carried out for configuration before deployment. do i need to install both JRuby and Rails inside the server before depolyment?
I can't really guess with Eror 404 only.
You can try mapping your rails app rack config to a different base_uri.
All you need to do is wrap the existing 'run' command in a map block
try doing this in your rails 'config.ru' file:
map '/mydepartment' do
run Myapp::Application
end
Now when you 'rails server' the app should be at localhost:3000/mydepartment .
Not sure if this will give you the desired outcome, but worth a try.
One more thing you also add this to your config/environments/production.rb and config/environments/development.rb (if on production mode):
config.action_controller.asset_path = proc { |path| "/abc#{path}" }
otherwise when you call your helpers such as stylesheet_link_tag in your views, they will generate links without the "/abc".
Also, find some guides you may refer for good support.
JRubyOnRailsOnBEAWeblogic.
Use JRuby with JMX for Oracle WebLogic Server 11g
Let me know if it is not resolved.

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.

Rails 3: how to detect if the application is running in server mode for multiple different environment?

I have an app that runs on multiple servers:
- locally on dev machines
- on heroku
- on a specific server with Passanger on Nginx
I am trying to launch a particular code (loading some REDIS keys) that is only required if the web server is launched.
I have done quite a bit of digging, and the nicest solution I found was to execute my code in an initializer with:
if defined?(Rails::Server)
#my code
end
This works well locally, but it seems that Rails::Server never gets defined either on Heroku or Passanger.
I need a solution that works in every case, please help, this is really important.
Thanks,
Alex
ps: I am running Rails 3.0.4, Ruby 1.8.7
Putting code in your config.ru file might be a more robust way of detecting server mode across different types of servers (Unicorn/Passenger/Rails::Server/etc).
e.g., in rails-root/config.ru:
# This file is used by Rack-based servers to start the application.
# ADD this line and read the value later:
ENV['server_mode'] = '1'
require ::File.expand_path...
What about?
config.serve_static_assets = ( defined?(Mongrel) || defined?(WEBrick) ) ? true : false

Setting up Rails for the first time - is this normal?

I am trying to setup a Rails environment via CPanel. I've tried on several distinct hosting environments (on all of which I used CPanel to create the project) and I always get the following:
Is this something I've done wrong - I have to first create models/controllers etc - or something I need to bark up my hosting provider's tree about?
Have you tried running your app locally? If I remember correctly, for security reasons, the "about your application's environment" won't load unless it's from localhost.
If you are running your app in production mode (so it is recognized as it doesn't run locally), rails won't show this information, instead it will show this message.
I had this issue and wondering what's wrong when I first setup a new application in a server with passenger. The default environment of passenger is the production so it doesn't show this info. You have to set it up to development mode if you want to see them, to do that go to your virtual host file and add this:
RailsEnv development
When you start creating your application, delete the index.html from the public.
Maximum supported versions at this time are:
Ruby 1.8.7
RubyGems 1.8.25
Rails 2.3.18
Anything newer than that is a near guaranteed breakage and cPanel & WHM will be incapable of utilizing it in any way, shape, or form.
http://tickets.cpanel.net can assist you with removing your existing Ruby on Rails installations and reverting them back to cPanel supported and sanctioned versions (Ruby 1.8, RubyGems 1.8, and Rails 2). That is the only thing they can do for you at this time.
If you want to use any versions newer than this, then you will be unable to use the cPanel & WHM interfaces or management tools for it -- they simply will not work. You will then have to manually manage your RoR install by yourself through command line exclusively. None of it would fall under the scope of cPanel support.

Resources