I'm developing a main rails application where I want to mount some rails engines and gems I've developed on my own.
Some of my (under development) engines depend on the main Application and the engines I've already mounted into the main app.
Now, I want to test, if my engine which is under development also works in my main application together with the already mounted engines and gems.
If I want to test it now, I must commit every singe change with git and install the gem / engine via a GitHub repository in the main app.
Is there a better way to test changes of your engine without having to pollute the git commit history / without commiting every single change and install it via a git repository?
Can I mount my engine locally into the main app, change something and it will be immediately available in the main app?
In your Gemfile, you can point gems to your local working copies:
gem 'my_gem', :path => '/my/workspace/my_gem'
See http://bundler.io/v1.3/man/gemfile.5.html#PATH-path-
Related
We are looking into building a very large rails application and considering using engines for better separation of "modules" out of the main app.
we have started this process by creating a small engine using the gem motorhead (the idea of its active_if component is wanted).
That engine was then removed from the main app and given a git init, then pushed to github.
the main app then was able to pull the gem in within the Gemfile.
During this proof of concept, it works, but not very efficient, and also updating the new engine/gem is a bit awkward in this way as it is kinda a submodule in a way. What is the proper workflow for building and maintaining engines/gems when building a modular app like this?
Thanks in advance
The most akward part about deploying Gems or Engines as modules is the constant need to update. We had a lot of success with using:
bundle config local.my_gem ~/projects/my_gem/
It'll point to the Gem/Engine version on disk without modifying the Gemfile and Gemfile.lock.
To remove the local override run:
bundle config --delete local.my_gem ~/projects/my_gem/
With this you should be able to restrict the times the Gemfile.lock has to be updated to deployment time.
I just deployed my first Ruby on Rails app on a VPS at Digital Ocean.
To get started quickly, I did this by simply dragging my Rails directory tree (and its containing files) onto the server via (S)FTP.
I know this isn't the best solution in the long run. So how can I link my app on the server to my git repository at GitHub?
Ideally, when I work on my app locally, and then git commit and git push to my git repository, my app on the VPS will also get updated automatically.
How can this be achieved or what is the best strategy to achieve this?
Since I am building this app just by myself, I can probably keep things simple and stick to a single master branch, rather than having multiple branches.
Thanks for any help.
If I were you, I'd do the pulling and updating on the remote manually. Sorry, but this is not only best practice, but will also force you to learn something useful about system administration and don't require you to be dependent on one host, but can switch service provider and setup as easy it is to make a git-clone somewhere else.
So my workflow would be:
Client:
# Do some changes, commit and add a nice message
$ git commit myfiles
# Push to remote once I'm happy.
$ git push
# SSH to server, and continue from there.
$ ssh username#server
Server:
# Enter project directory
$ cd /var/www/myproject
# Pull code
$ git pull
Done. Or perhaps finish by refreshing server container (uWSGI, fcgi, gunicorn, what have you...)
Reading other similar answers, they hint to looking at the following resource using Capistrano:
Capistrano documentation at GitHub
You should spend a little time now setting up deploys with some automation. Since you are using rails, you should try Capistrano Gem
Capistrano will help you deploy and maintain your application with just a few simple commands. The Readme will show you how to get started, but in general, you will add the Gem by adding this to your Gemfile:
gem 'capistrano', '~> 3.2.0'
then run bundle install to install Capistrano into your bundle. If you are not already using bundler, you should start.
then run bundle exec cap install to setup your local repo for Capistrano.
Basically now you have a nice structure for deployment scripts as part of your repo. You will have to write some deploy scripts now, or modify the examples.
Once done, Capistrano will help you deploy new code (once committed and pushed to your remote repo) and restart the services.
It depends on whatever service you are using to publish your app. Depending on the provider, they may or may not provide rails service. For example, a site like Heroku, where you can actually host for free up until certain restrictions is accessible via github and you can do exactly what you're saying and just push up and publish.
If you can put your repo on your server, you can set up post-receive hooks to pull your branch into your web app directory.
To do so you would create a bare repo on your server, add it as a remote on your development machine, and then (on your server) create the file /my/app.git/hooks/post-receive and add these lines:
#!/bin/bash
#CONFIG
LIVE="/home/saintsjd/www"
read oldrev newrev refname
if [ $refname = "refs/heads/master" ]; then
echo "===== DEPLOYING TO LIVE SITE ====="
unset GIT_DIR
cd $LIVE
git pull origin master
echo "===== DONE ====="
fi
Code from Automated Deployment of PHP Applications using git, by Jon Saints.
Note that it would be possible to do something like this without putting the repo on your server if you use Github's webhooks (https://developer.github.com/v3/repos/hooks/).
However, I would highly recommend using Capistrano (https://github.com/capistrano/capistrano), which can deploy your application code and help you with a lot of administrative tasks (like restarting the server, etc.).
If you want to stick relatively close to git, you might also check out the git-deploy gem.
I have started developing a new Rails app on my server using RVM, Rails 3, & Ruby v1.9.2. I am using Git as my code repository. It's a simple app and I don't want to use an extra server. I just want to deploy my app directly from the same server I am developing on.
I've installed Phusion Passenger w/ Apache to serve my app, but have realized that I can't do that pointing to my development directory as my RAILS_ENV is set to "development". (I found I got file permission errors on the asset pipeline and other issues when I attempted to set RAILS_ENV to "production" and serve the app)
What's the simplest/easiest way to deploy the app? Can I simply:
1) Create a separate user to run rails production (Rails in dev currently runs as me on my Ubuntu server)
2) Clone my repo into a separate dir and configure Apache accordingly
3) Seed my database with the data needed for production (not much data needed here)
4) What else?
I've looked briefly at Capistrano, but it seems like overkill for this simple app. I only need to be able to provide a simple web interface for some data entry. Seems like git push should be sufficient, but I haven't done this before so maybe I'm wrong? Also, if I git push how do I ensure file permissions in the "production" directories are all set properly, particularly for any new files that get created in the originating app directory structure?
Thanks for any ideas.
No- you do not need Capistrano for the above; at this stage I feel it will only serve to confuse you further.
I'd suggest you first save your repo to a private Github or free BitBucket account. What you should do is keep one folder for 'development'.
Remember that Passenger is 'just' a module working with Apache. So what you need to do is setup a virtual host under apache and direct that to another folder on your system. For this example, consider:
~/rails/myapp_development/ and ~/rails/myapp_production/
Passenger always runs the app in production, so that should not be an issue. You can do bundle --without=production in your development setup to ignore any gems listed in the Gemfile under the production namespace, i.e. say you've got the mysql adaptor specified, you can ignore this and have Rails only rely on the SQlite gem.
You can now simply develop in the development folder, commit, push to BitBucket. Deploying will be as simply going into the production folder and doing a git pull and touch tmp/restart.txt.
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.
Trying to understand this open source app on github, it has a gem file:
https://github.com/bestbuyremix/BBYIDX/blob/3f8d378ef318544411aa887c4ef71e1ab8a9efd0/.gems
And a plugins folder:
https://github.com/bestbuyremix/BBYIDX/tree/3f8d378ef318544411aa887c4ef71e1ab8a9efd0/vendor/plugins
Why would you want to do this? Does this make upgrading the plugins harder?
When you reference a gem, from what I understand, it downloads the files and stores them at a global level (gemset if using rvm etc), so I guess loading it as a plugin gives you access to the source to modify?
i.e Why go with gem versus a plugin or visa versa?
Plugins give you the flexibility of being able to just copy your app somewhere else and poof! it's all ready to go.
Gems on the other hand, force you to a) download them to every single piece of hardware that your app is running on via rake gems:install and b) force you to keep track of which packages are installed on which system.
With plugins, you know that when you stick it in your vendor directory, it will work immediately.