Libv8 Gem Problems

I had some problems installing the libv8 gem after installing ruby 2.0. The solution was to use yum to install the python26 packages and then make a link from them to /usr/local/bin/python. After that the gem install libv8 command worked.

$ yum install python26
$ ln -s /usr/bin/python26 /usr/local/bin/python
$ gem install libv8

This worked on both an RHEL6 and an RHEL5 system. I needed libv8 because therubyracer gem depended on it.

Requiring https

My apps tend to just be electronic sign-up sheets for various events. There's no way we're going to pay for an SSL certificate for a site that will probably be up for a month or two, at most. However, we do require people administering the page to login, which means passwords which need to be encrypted. That's really all I care about encrypting, not the entire site. So, my somewhat simple solution is:

  if Rails.env.production?
    scope :protocol => 'https://', :constraints => { :protocol => 'https://' } do
      get "user_sessions/new"
      post "user_sessions/create"
      delete "user_sessions/destroy"
      match 'login' => 'user_sessions#new'
      match 'logout' => 'user_sessions#destroy'
    get "user_sessions/new"
    post "user_sessions/create"
    delete "user_sessions/destroy"    
    match 'login' => 'user_sessions#new'
    match 'logout' => 'user_sessions#destroy'

I only want to encrypt the login routes on production. I don't have https running on my laptop and I'm not worried about my traffic being unencrypted there, so I don't want to use it on my laptop. The other nice thing is if someone tries to just type in on my production server, it'll throw up an error, which is fine with me. I could try to match those urls and redirect them to https, but I really don't care. As long as they can't send their password unencrypted, I'm happy.

More Capistrano

Capistrano used to have a pair of tasks called deploy:web:disable and deploy:web:enable that I really liked. All they basically did was to upload a file to the webserver that would be displayed while a new version was deploying. I thought this was really helpful because if anyone visited the site during the deployment, they'd see the message that the site was under maintenance. Without this file, visitors to the site would see a cryptic rails rack error that made no sense. Sometime last year, after a capistrano update, I realized that these tasks were no longer there. By that time, I had understood that it was just uploading a file, so I could duplicate the tasks manually by editing a file to add the current time and then uploading it to the webserver. (In either case, I had to modify my httpd.conf file to reflect the location of the file.) Then, after the update, I'd remove the file. This wasn't difficult, but it was WAY easier to do this:

cap deploy:web:disable
cap deploy
cap deploy:web:enable

Since I've been making huge leaps in understanding capistrano the past two days, I thought that I could write something that would do this. My first idea was to just make a template file with a set string (I used TIMENOW) that I'd replace with the current time. I put the template file (maintenance_template.html) on the server in the system directory and then just wrote a capistrano task to run a sed command like this:

run "sed \"s/TIMENOW/`date`/\" #{shared_path}/system/maintenance_template.html > #{shared_path}/system/maintenance.html"

The enable task just had to remove the maintenance.html file, which was pretty easy.

desc 'removes the maintenance.html file'
  task :enable, :roles => :app do
  run "/bin/rm #{shared_path}/system/maintenance.html"

I was pretty happy with these, but thought I could do better. I didn't want to have to use a template file. I thought I should be able to just write the entire file at once. In looking for this, I came across the ERB templating system. I'd been using erb forever, but just didn't realize it. All the views in my apps are named file.html.erb.

From ruby-doc, with ERB "actual Ruby code can be added to any plain text document." That's exactly what I want to do, have a plain html file, but run in it.

My capistrano task is pretty long, due to the css and html stuff at the top of the file, but the task basically looks like this:

namespace :web do
desc <<-DESC 
  This task makes a maintenance.html file in system to use before a deployment.

  The task works by simply running a sed command to switch the text TIMENOW with the current time.
  The file that it is looking for as a template is system/maintenance_template.html.
task :disable, :roles => :app do
template = <<-EOF
The system is down for maintenance as of

<%= "%d %b %Y at %H:%M %p %Z" %> It'll be back shortly. EOF put template.result, "#{shared_path}/system/maintenance.html" end

This task shows just about everything I learned about capistrano tasks.

1. desc <<-DESC is a way to quote a lot of text. 2. The first line in desc is shown when cap -T is run. The rest is shown when cap -e taskname is run. 3. namespace :web is how you break up task areas. I don't show it, but this entire task is inside another namespace called deploy. So to call this task you run cap deploy:web:enable. 4. The string is all stored in the variable template. However, the ruby code is not run until result is called on it. Interestingly, I could use instance variables in erb as well. But, if you want the values for the instance variables put in the template, you'd have to run template.result(binding). Since I don't have any instance variables, I only need to use result to run the ruby code. 5. Capistrano provides the put method that just lets me write the template.result to the maintenance.html file. It works exactly how I hoped. The only thing that I would change is to move these tasks to another file, since the disable task is very long. However, my first attempt at using another file in capistrano didn't work out so well. So that's something to work on another time.

Spoke Too Soon

When I came home from work, I was feeling a little full of myself and then I wrote my previous blog post. I spent the past couple of hours watching some Railscasts that were really informative, on using capistrano to do all sorts of things. So I changed my capistrano file quite a bit and do feel that I understand it a bit more.

One of the other things I was trying to get a handle on is the asset pipeline. I have my assets working, but I'm really not sure why. Anyway, here's what my Capfile looks like:

load 'deploy'
# Uncomment if you are using Rails' asset pipeline
# load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks

I've never done anything with this file, but apparently, I'm supposed to uncomment the load 'deploy/assets' line. Instead, I've had a task in my deploy.rb file that runs assets_precompile. It looks like this:

desc 'assets_precompile'
deploy.task :assets_precompile, :roles => :app do 
	run "cd #{release_path}; rake assets:precompile"

So, I also take this task out of my deploy.rb file and try to deploy.

First I get a bunch of errors that gems aren't installed, so I reinstall them on the server. Lastly, I continually get this error:

failed: "sh -c 'cd -- /local/code/web/app/hcw2013/releases/20130320014713 && rake RAILS_ENV=production RAILS_GROUPS=assets
 assets:precompile && cp -- /local/code/web/app/hcw2013/shared/assets/manifest.yml /local/code/web/app/hcw2013/releases/20130320014713/assets_manifest.yml'" 

I can't find this manifest.yml file anywhere. And even if I just try to run cap assets:precompile, I still get an error and I couldn't figure out why.

So, my solution for now, is to recomment that line in Capfile and run my assets_precompile task. That works. A fitting end to the day where my head might have been getting a little big.


Today, I started a new ruby on rails website for a workshop. It's a very basic site that just handles registrations and redirects people to the credit card processor that we use. This is the main type of website that I write in rails and it's probably my tenth site that I've written for this purpose. Previously, I had always used scaffolding in rails and then deleted the stuff that I didn't need. This time, I made a conscious choice to not use any scaffolding and instead generate my own controllers and models. I'm happy to say that I didn't have any real problems. So I'm thinking that I'm no longer a complete beginner with rails. I might even go so far as to say I'm an intermediate rails programmer. Which means it's time for me to start advancing some more. I found this website and was thinking of signing up for the prime program. The intermediate ruby on rails workshop looks pretty interesting. Though, after reading the syllabus, I'm thinking that perhaps I was at intermediate level a while ago. In the listing of things that they plan to cover, I already know a few:

  • Starting an application
  • File uploads with Paperclip
  • Implementing search--I use the ransack gem
  • Adding paging

But the rest look good. I'd be really happy if they taught how to write tests, since that's where I'm weakest now.

Anyway, I had a pretty good day today and am feeling good about how I'm progressing. I wouldn't call myself a programmer, but I'm definitely getting better at using it as a tool. I feel like going back and looking at some of my notebooks from a couple of years ago to see how much I've improved since then.

Little Work

My organizing kick really tired me out. I slept for a very long time and didn't feel like doing anything today. But I felt I should do something today, so I decided to just finish taking the drywall off the back of the kitchen. It didn't take long, but since I was already tired, it pretty much finished me for the day.

I got three bags of garbage out of it, so I'm happy.


Organizing Kick

Since I was doing a bit of organizing at work this week, it has carried over to my house. This is not a mood I'm usually in, so I decided to take advantage of it and do a little work on my basement. My basement needs a lot of work, so this put a very small dent into it. I'd say I cleaned up about a quarter of the basement. And I'm pretty happy with that.

I put my old kitchen cabinets in the basement, thinking I could use them for storage. I took two that were the same size, screwed them together, put casters on them and some peg board to make myself a tool cart. I also painted the top white to make it easier to find things on it. I'm happy with how it turned out.



It's working pretty well. The only problem is using screws on the door to hold the saws isn't working. I'll have to put them someplace else.

Then I took the rest of the cabinets and hung them on the walls. (I'm only saving upper cabinets. Base cabinets are useless to me.) This cause my first run to Home Depot. I needed to attach some wood to my cinderblock walls. Since everything is in such a state of disorganization, I couldn't find my masonry bits. And I found that the key to replace bits on my masonry drill is missing. So, I bought the right size bit and a new key.

Since I was hanging the cabinets by myself, they're not perfect. But I really don't care. They're good enough for some extra storage.

Here's how things look in the part of the basement that I cleaned today.



This was good to do because I found a couple of things that I want to do down there.

  • Buy a really nice workbench
  • Add more lights on the switch
  • Dust collection system is desperately needed, will have to research that.
  • Means of organizing drill bits

A Few Good Hours

I woke up and decided I better get started. So, down came the upper cabinets, except for the one with the vent going outside. I'll do that later.

I like upper cabinets. I can put two together and put them on wheels or reuse them in my basement or garage. They're not as big as bottom cabinets, which I'm much less likely to reuse. I'll probably end up breaking these up and throwing them away.



And here's how it looks when I'm done for the day.


Looks like the window back there is huge. It will be really cool to put a huge window in. (And my camera takes WAY better pictures than my crummy iphone.)

It Begins Again

It's March and I've finally saved up enough (I hope), so I started work on my kitchen. Here's a shot of the first two cabinets I took down.


Then I was thinking of what to do with them and remembered that I bought some cheap casters last year. I figured that I could screw these two cabinets together and put the casters on the bottom. It might make a nice stool type thing for me that I could use when I'm laying down the tiles on the floor. So, about 15 minutes later, I have this.


I'll add some boards to the top for me to sit on and it should work pretty nicely.