RVM, Rake and Cron on Ubuntu
Tennnngh! I read countless blog posts, tried numerous approaches that I found and rebuilt the server more than a couple times. And finally I found what the missing piece was for getting a rake command to run in cron with rvm.
Most of the results I found (on stackoverflow) flagged the solution as adding the path to your rvm in your line item in crontab. Something like /home/username/.rvm/gems/rake my_awesome_task… etc. I tried every variation of that I could think of and got the same results.
I dont use cron every day so this was a revelation to me. I found out that you can set variables, environment variables in your crontab file.
So the solution was very simple. Edit the crontab like this.
-
crontab -e
Then at the top of the file (mine is the first line)
-
SHELL = /home/wylie/.rvm/bin/rvm-shell
Then my commands:
-
* 4 * * * /bin/bash -l -c ‘cd /path/to/app && RAILS_ENV=production rake mytask –silent’
Done.
Hope that saves someone some time.
AWS Recipe
Seems Im not the only one who has had problems with ruby 1.9.2 and rvm on my Ubuntu servers. This recipe so far has worked great for every project running on it so far. Im not taking credit for all of it. Its a compilation of things that I have found on other blogs (too many to credit).
Use this AMI
ami-a403f7cd
Then add ssh, http and https to the default security group. Now you can shell in
-
sudo apt-get update
-
sudo apt-get build-essential
-
sudo apt-get install curl git-core gitk git-gui
-
sudo apt-get install libcurl4-openssl-dev
-
sudo apt-get install vim
-
sudo apt-get install rake
-
sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
-
sudo apt-get install mysql-server mysql-client
-
sudo apt-get install libmysql-ruby libmysqlclient-dev
Now for the RVM stuff
-
sudo apt-get install ruby
-
bash < <( curl https://rvm.beginrescueend.com/releases/rvm-install-head )
EDIT:: RVM has changed the install address. Check their website to get the latest bash install command
Open .bashrc and replace
-
[ -z "$PS1" ] && return
with
-
if [[ -n "$PS1" ]]; then
Then at the end of the file
-
if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source $HOME/.rvm/scripts/rvm ; fi
-
fi
You should close the terminal and open a new one then enter
-
rvm notes
You should see a bunch of options.
Now we need to add a bunch of stuff
-
sudo aptitude install build-essential bison openssl libreadline5 libreadline-dev curl git-core zlib1g zlib1g-dev libssl-dev vim libsqlite3-0 libsqlite3-dev sqlite3 libreadline-dev libxml2-dev git-core subversion autoconf
Now we can run rvm list known and we should see a list of rubies
Time to upgrade our ruby
-
rvm install 1.9.2-head
-
rvm –default 1.9.2-head
EDIT:: RVM has changed a bit since I wrote this. Be sure to read their instructions to be familiar with the differences. Setting default has now changed to
-
rvm use 1.9.2-head –default
Don’t forget the –default or your git hooks will never work
Lets get rails in there to test it out
-
gem install rails
Try it out
-
rails new testing
Should have created a new rails app.
Now we need to get our project from the git repo. But we need to give them a pub key
-
cd ~/
-
ssh-keygen -t rsa
(defaults, no passphrase)
-
cat .ssh/id_rsa.pub
copy and paste that into the repo keys on your repo
Now set up the site
-
cd /var
-
sudo chgrp -R ubuntu www
-
sudo chown -R ubuntu www
-
cd www
-
git clone [clone address] [dir name you want]
-
cd [new dir name]
-
bundle install
cool, now lets set up the webserver
-
gem install passenger
-
passenger-install-apache2-module
follow the instructions
Now we need to set up our git hooks for a heroku like deployment
shell into the server then cd to /var/www/[your app]/.git/hooks
copy the post-receive.sample to just post-receive
chmod it to 777
then edit it like so
-
#!/bin/bash
-
APP_PATH=/var/www/myapp
-
# Production environment
-
export RAILS_ENV="production"
-
# This loads RVM into a shell session. Uncomment if you’re using RVM system wide.
-
# [[ -s "/usr/local/lib/rvm" ]] && . "/usr/local/lib/rvm"
-
[[ -s "/home/ubuntu/.rvm/scripts/rvm" ]] && source "/home/ubuntu/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
-
-
echo "*******************************************"
-
echo " Deploying [APP NAME] to live server "
-
echo "*******************************************"
-
-
exit_with_error() {
-
echo "[DEPLOY] !!!!!!!!!!!!!!!!!!!! An error has occurred !!!!!!!!!!!!!!!!!!!!!!!"
-
exit 1
-
}
-
-
cd ${APP_PATH}
-
env -i git add .
-
env -i git reset –hard || exit_with_error
-
env -i git pull origin master
-
-
echo "[DEPLOY] – * Running bundle"
-
bundle install –deployment –without development test || exit_with_error
-
-
echo "[DEPLOY] – * Migrating database"
-
bundle exec rake db:migrate || exit_with_error
-
-
-
echo "[DEPLOY] – * Successfully deployed application to live server"
-
-
echo "[DEPLOY] – * Restarting application"
-
mkdir -p tmp/
-
touch tmp/restart.txt
-
-
-
echo "*******************************************"
-
echo " Successfully deployed [APP NAME] "
-
echo "*******************************************"
-
Then, in the .git/config file add this to stop all those pesky warnings
-
[receive] denyCurrentBranch = false
Now we need to create a DB
-
mysql -u root -p
create database [whatever you need here];
-
exit
Lets add the new server as a remote for your git
-
git remote add live ubuntu@[some ip]:/var/www/[app name]
I think were ready for a push. In your project update the config/database.yml and change the production creds to what you just created.
commit that change and push it to origin
-
git push origin master
And now push it to live
-
git push live master
tada.. git push deployment with gem updates and migrations
Just one thing left… database backups to S3. Thats for another post.
I found several pieces to the puzzle but it took me a while to put it all together. After I got it working I thought I should write it down in case I needed to remember what I did or maybe someone else would find it useful.
The first thing you should know is that ActiveMerchant does not support recurring payments with PayPal. But Raymond Law generously made a fork that fixes this. I couldnt get the fix as a gem so I just installed it as a plug in. https://github.com/rayvinly/active_merchant/
Then I went to the Railscasts on PayPal with ActiveMerchant. http://railscasts.com/episodes/146-paypal-express-checkout
In my config/environments I made a slight change from the Railscasts example:
-
config.after_initialize do
-
ActiveMerchant::Billing::Base.mode = :test
-
paypal_options = {
-
:login => "mysandboxlogingoeshere",
-
:password => "MYSANDBOXAPIPASSWORD",
-
:signature => "SANDBOXAPI"
-
}
-
::STANDARD_GATEWAY = ActiveMerchant::Billing::PaypalGateway.new(paypal_options)
-
::EXPRESS_GATEWAY = ActiveMerchant::Billing::PaypalExpressGateway.new(paypal_options)
-
::EXPRESS_RECURRING_GATEWAY = ActiveMerchant::Billing::PaypalExpressRecurringGateway.new(paypal_options)
-
end
I made a registration controller:
rails g controller index new checkout complete
-
def checkout
-
response = EXPRESS_RECURRING_GATEWAY.setup_agreement(:description => description, return_url => registration_complete_url, :cancel_return_url => registration_new_url)
-
-
redirect_to EXPRESS_RECURRING_GATEWAY.redirect_url_for(response.token)
-
end
-
-
def complete
-
token = params[:token]
-
response = EXPRESS_RECURRING_GATEWAY.create_profile(token, :description => description, :start_date => start_date, :frequency => frequency_in_months, :amount => amount_in_dollars, :auto_bill_outstanding => true)
-
-
if response. success?
-
#handle success
-
else
-
#handle failure
-
end
-
end
You can figure out your own way to handle the values and details of the transaction. The Date format can be set with
-
start_date = Time.now
The amount can be just a number. Dont put it in quotes.
Use case: I have a page with multiple post. Each post has multiple comments. I want to use the infinite_scroll jquery plugin to “paginate” through the comments and post.
Admittedly, nested pagination makes little sense until you think about it in context of having multiple infinite scrolls on a page.
Here are the bare bones.
My Post model:
def comment_pages(comment_page = 1)
self.comments.paginate( :page => comment_page, :per_page => 5 )
end
My View (haml version):
#comment_pagination= will_paginate(comments, :param_name => "comment_page_#{post.id}")
Now i have unique list that infinite scroll can use to paginate through.
Setting up a new dev environment
There are so many things i forget when setting up a new workstation. This time I took notes so I wont forget.
After installing VMWare fusion3 on my macbook pro I download the latest Ubuntu iso and start a new VM with it. I keep my work on TrueCrypt partitions. Then work my way down this list.
For starters
-
sudo apt-get install git-core gitk git-gui
-
sudo apt-get install subversion
-
sudo apt-get install ssh-client
-
sudo apt-get install ssh-server
-
sudo apt-get install vim
-
sudo apt-get install rake
-
sudo apt-get install libxml2-dev
Then install sphinx
http://freelancing-god.github.com/ts/en/installing_sphinx.html
./configure, make, sudo make install
VMWare Tools
Turn off shared folders (i always get failures when i dont)
From the virtual machine menu, select install VMTools. Open the virtual DVD and put the zip file on the desktop and extract it there.
From a terminal cd Desktop/vmware…
Then sudo ./vmware-tools-install.pl
Enter key for all the defaults. Then run the networking stop and start commands at the end of the instructions.
Install Ruby
-
sudo apt-get install ruby-full build-essential
or
-
sudo aptitude install ruby build-essential libopenssl-ruby ruby1.8-dev
Install Apache
-
sudo apt-get install apache2 apache2-mpm-prefork apache2-prefork-dev
Install Ruby Gems
-
sudo apt-get install rubygems
-
export PATH=/var/lib/gems/1.8/bin:$PATH
Install Rails
-
sudo gem install rails
Install MySQL
-
sudo apt-get install mysql-server mysql-client
-
sudo apt-get install libmysql-ruby libmysqlclient-dev
-
sudo gem install mysql
Install Passenger
-
sudo gem install passenger
-
sudo apt-get install apache2-threaded-dev libapr1-dev libaprutil1-dev
-
sudo /var/lib/gems/1.8/bin/passenger-install-apache2-module
[follow passenger instructions]
VMWare Network connections
Since FF is the default browser you need to make a small change to browse the web.
Open FF and put about:config in the URL. Then filter for ipv6 and set false to true and restart FF. You should be online.
Shared Folders
Use TrueCrypt to keep your work on and share /Volumes/[your TC Drive] with VMWare. You will need to shift+apple+g to get the got to folder dialogue to find /Volumes.
You can access the files in your VM here
/mnt/hgfs
Install Gems
[what ever gems you need ]
Configure Apache
(Sill on your VM)
-
mkdir ~/rails_development
-
cd ~/rails_development
-
ln -s /mnt/hgfs/[your drive]/myapp myapp
Now cd to /etc/apachec2/sites-available and sudo scp 000-default myapp
then sudo vim myapp
-
-
<VirtualHost *:80>
-
ServerName myapp.ubuntu
-
RailsEnv development
-
DocumentRoot /home/wylie/rails_development/myapp/public
-
<Directory /home/wylie/rails_development/myapp/public>
-
AllowOverride all
-
Options -MultiViews
-
</Directory>
-
</VirtualHost>
-
Then cd ../sites-enabled
and sudo ln -s ../sites-available/myapp
then sudo rm 000-default
Finally restart apache
Set your hostfile
(Back on OS X)
get the IP in your VM and edit your hosts file on OS X
sudo vim /etc/hosts
[ip of VM] myapp.ubuntu
then run dscacheutil -flushcache (i usually have to run this a few times for it to take)
Install the DB
create any databases you need for your apps
Dont’ forget the memcached
-
sudo apt-get install memcached
-
sudo /etc/init.d/memcached restart
Fire it up
open your browser and go to myapp.ubuntu
Permission fixes
if you put id in the command line in your VM you will see that UID is 1000. The problem is that VMWare puts you in as UID 501 and GID 20. We need to update Ubuntu to match
sudo vim /etc/passwd
Then find your user and change 1000:1000 to 501:20, save and exit
You wont be able to make any more changes so you need to reboot. You cant log in either so you need to Ctrl + Alt + f1 to switch to terminal. Login there.
Then, you need to cd to /home and change the permissions on everything in your user dir like this
sudo chown -R 501:20 wylie
Then you need to update the group file sudo vim /etc/group
find dialup and change that to wylie [your username]. Then you need to find your username and change 1000 to 20.
sudo reboot, login and permissions are now fixed. Its a good idea to update your apache in /etc/apache2/envars. Change www-data to your username in both places
Git GUI fixes
The text in the UI is huge so you can open synaptic package manage and search for tcl. mark the 8.5 version for install. Then search for the tk8.5 and mark that for instalation. Then in command line
sudo update-alternatives –config wish
select the option with 8.5. It should be number 3.
Now when you open git gui it should look normal
Now get to work…
Regular Expression Tools
A friend shared a RegEx tool that came in really handy. Its www.rubular.com. I don’t get alot of opportunities to work with regex but when i do, having a new tool can make it almost fun.
I came up with this expression to match various URL’s that users enter that i could create new links from.
-
(((https?):\/\/)?(([0-9a-zA-Z][-\w]*\.)+[a-zA-Z]{2,9}(:\d{1,4})?([-\w\/#~.?=&%@~])*))
Looks like cartoon swearing.
color scheme tool
How did I get this far without a tool like this?
A friend sent this link to me and I wanted to be sure that I never lost it.
http://colorschemedesigner.com/
jQuery Emoticon Selector Plugin
I couldn’t find an emoticon plugin for inserting an emoticon into a text area. I found plenty that converted the emoticon code into an image. So, I set out to write my first jQuery plugin.
If you choose to use this, keep in mind this was my first plugin and should be thoroughly tested before using it. And if you make it better I would very much like to see what changes you made.
Lets check it out.
The view has two components. The jQuery call and the div with the necessary elements. (my examples are how i used this in Ruby)
-
-
<% javascript_tag do %>
-
$(document).ready(function() {
-
$("div#emodiv").emoticate({icon: ’smiley-icon’, replacediv: ‘list’});
-
});
-
<% end %>
-
Then the div that you identified in the jQuery call.
-
-
<div id="emodiv">
-
<img src="/images/emoticons/emoticon-0100-smile.png" id="smiley-icon">
-
<div id="list"> </div>
-
<select name="emoticons">
-
<option value=":)">emoticon-0100-smile.png</option>
-
<option value="|-(">emoticon-0106-crying.png</option>
-
<option value=";)">emoticon-0105-wink.png</option>
-
<option value=":(">emoticon-0106-crying.png</option>
-
<option value="(pt)">279.png</option>
-
<option value="(ct)">104.png</option>
-
<option value="(au)">342.png</option>
-
<option value="(eg)">emoticon-0116-evilgrin.png</option>
-
<option value=":*">emoticon-0109-kiss.png</option>
-
</select>
-
<div style="clear: both;"></div>
-
<p>
-
<textarea id="comment_1"></textarea>
-
</p>
-
</div>
-
What is going to happen is that the select and options will be changed to a UL list with the icon images. When an emoticon is clicked it will be inserted into the text area within the div. (The id for the textarea does need to be unique).
And here is the plugin.
-
-
(function($){
-
-
$.fn.emoticate = function(options){
-
-
var defaults = ({
-
replacediv: ‘replaceme’,
-
image_path: ‘/images/emoticons/’,
-
speed: 500,
-
icon: ’smiley’
-
});
-
var options = $.extend(defaults, options);
-
-
return this.each(function() {
-
var select = $(’select’, this);
-
var area = $(‘textarea’, this);
-
var icon = $("#" + defaults.icon, this);
-
-
var emo =‘<div class="emoticon-box"><ul>’;
-
$(‘option’, select).each(function () {
-
var option = $(this);
-
var emocode = option.val();
-
var title = option.text();
-
var link = $.fn.emoticate.emoticlick(title, emocode, defaults.image_path);
-
-
emo += ‘<li>’
-
+ link
-
+ ‘</li>’;
-
}); //end option
-
emo += ‘</ul></div>’;
-
-
select.remove(); //get rid of the select to make way for emotification
-
$("#" + defaults.replacediv, this).html(emo); //put the emotification HTML in the right div
-
$("#" + defaults.replacediv, this).hide(); //hide the emoticons
-
-
var listdiv = $("#" + defaults.replacediv, this); //reset the var so it will work in the icon click.. this probably could be done better
-
-
//bind the link for every a tag that is in the replaced div
-
$(‘a’, this).bind(‘click’, function() {
-
$.fn.emoticate.insertme(area.attr(‘id’), $(this).attr(‘id’));
-
return false;
-
}); //end bind
-
-
//click the icon
-
$(icon, this).bind(‘click’, function (){
-
listdiv.show(defaults.speed);
-
});
-
-
//function to click anywhere to hide the emoticons
-
$(document.body)click(function (){
-
listdiv.hide(defaults.speed);
-
});
-
-
-
}); //end select
-
-
}; //end function
-
-
-
$.fn.emoticate.emoticlick = function(id,emocode, image_path){
-
return ‘<a href="#" id="’ + emocode +‘"><img src="’ + image_path + id + ‘"></a>’;
-
};
-
-
$.fn.emoticate.insertme = function(areaId,text){
-
var txtarea = document.getElementById(areaId);
-
var scrollPos = txtarea.scrollTop;
-
var strPos = 0;
-
var br = ((txtarea.selectionStart || txtarea.selectionStart == ‘0′) ?
-
"ff" : (document.selection ? "ie" : false ) );
-
if (br == "ie") {
-
txtarea.focus(); var range = document.selection.createRange(); range.moveStart (‘character’, -txtarea.value.length);
-
strPos = range.text.length;
-
}
-
else if (br == "ff") strPos = txtarea.selectionStart;
-
-
var front = (txtarea.value).substring(0,strPos);
-
var back = (txtarea.value).substring(strPos,txtarea.value.length);
-
txtarea.value=front+text+back;
-
strPos = strPos + text.length;
-
if (br == "ie") {
-
txtarea.focus();
-
var range = document.selection.createRange();
-
range.moveStart (‘character’, -txtarea.value.length);
-
range.moveStart (‘character’, strPos);
-
range.moveEnd (‘character’, 0);
-
range.select();
-
}
-
else if (br == "ff") {
-
txtarea.selectionStart = strPos;
-
txtarea.selectionEnd = strPos;
-
txtarea.focus();
-
}
-
txtarea.scrollTop = scrollPos;
-
};
-
-
})(jQuery);
-
Or you can download it here.
I followed the instructions on the Ubuntu site and the modrails site and everything would install fine but I could never get passenger to serve pages to apache.
Passenger assumes the permissions of the default apache user. And if you don’t set up your default user as www-data when you install Ubuntu Passenger wont have permission to run the app.
Open up /etc/apache2/envvars and change the www-data user and group to match your default user and group and you should be set.
Watch the whitespace
I have been spending the last hour wondering why this doesnt work.
Post.find_by_sql["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
Its because it needed to be like this:
Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
yeah, you need that whitespace there.