(how to) create a patch with GIT

I admit this is probably not an ideal setup, but I’m running apps, that I checkout and update via GIT. If the publisher issues an update, all I have to do is run git pull (or switch to tag or branch).
Another upside is that I can track any changes I made to the code – a technique called monkey-patching. The changes are usually needed to adapt the software (PHP, Ruby, Java) to my particular needs. I therefore makes little sense to contribute those changes back to the (open source) project.

If you want to apply those changes to a different installation or re-apply after an (software-internal) update, you need a way to replicate the changes. A good way for this are patches – one or more files containing only the changes made to particular files.

Here’s the short version for creating a patch, found at https://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/:

  1. first create a new branch (yes, this is really necessary)
    • git checkout -b my_patch_branch_name
  2. make your changes (or copy the changed files over)
  3. commit your changes
  4. create the patch (save the changes in the new branch compared to the master branch)
    1. git format-patch master –stdout > fix_empty_poster.patch
  5. done.

To apply the patch, run the following:

  • git am –signoff < fix_empty_poster.patch

This actually applies the patch and signs off on it, which means the changes are commited to the branch (most likely main) your current copy is on. Check “git log” and you’ll find the applied patch in the comments.

That’s it – just a short procedure.

PS: To switch your current working copy back to the master branch, run “git checkout master”

edit SASS-templates in VIM

The problem is simple – and the answer should be as well. But it turns out, that some things in computer science are not as they might seem. ๐Ÿ˜‰

I wanted to edit a SASS-file (-> http://sass-lang.com) for a rails project. The source-file is required to have space-indenting with 2 spaces for every indentation. I caught myself using the tab key to indent everything, but then the parser simply gave me error-messages. ๐Ÿ˜ฆ

So I had to find a way to make vim interpret the tab key as 2 spaces – easier said then done. ๐Ÿ˜‰
A more sophisticated list of commands for vim can be found here.

The commands I used to get the results I needed were:

set expandtab
set softtabstop=2
set shiftwidth=2

A note at the end: This only works for vim – not for vi!

So if you’re using vim – have fun or at least more success with this! ๐Ÿ˜‰

error in rails in locator.rb:91:in `add’: wrong number of arguments (3 for 1) (ArgumentError)

When I tried to start my rails-app this morning on my new server, I got:

** Starting Rails with development environment…
Exiting
/usr/lib/ruby/gems/1.8/gems/rails-2.1.0/lib/rails/plugin/locator.rb:91:in `add’: wrong number of arguments (3 for 1) (ArgumentError)

That seems to be a problem with an old install of rubygems on my Ubuntu-machine. Didn’t know that. ๐Ÿ˜ฆ It lives inside /usr/lib/ruby while the new versions normally reside at /usr/local/lib/site_ruby

Solution:

Delete /usr/lib/ruby/1.8/rubygems.rb and /usr/lib/ruby/1.8/rubygems (the latter is a directory)

Thanks to Stephan for this article:
http://www.arkanis-development.de/weblog/2008/9/19/locatorrb:91:in-%60add:-wrong-number-of-arguments

error with rubygems – marshal data too short

This was a strange thing. I had installed the rubygems by hand using the tar.gz from rubygems.org. I use version 1.3.1 currently. It worked fine at first, so I installed all the gems I needed, like rails etc.

When I wanted to install some more gems today, all I got to see was:

# gem update
updating installed gems
ERROR: ย While executing gem … (ArgumentError)
  marshal data too short

And this error kept popping up – no matter what commands I wanted to run with rubygems (update, update –system, install…)

I searched for a solution on google and found forums for ruby etc. Unfortunately none of these posts offered a solution to the problem – at least none, that was specific to my situation. It seems as though, this problem (mashal data too short) can occur in a lot of places, especially in databases and such. Must a an index of some kind.

Anyway, my fix was to delete everything in the ~/.gems diretory. That sets back your entire install of rubygems, because this holds information on what gems are installed an so on. But afterwards at least the “gem” command worked again. ๐Ÿ˜‰

I just reinstalled all my gems. You can get a list of all installed gems with

gem list –local

This command actually worked before – thank god. ๐Ÿ˜‰

All right – so much for now. Just a short incident-report. ๐Ÿ˜‰

avoid downtime due to DNS-drag

Ever had this problem?

You have to switch servers for some reason and so naturally your DNS-settings have to be updated to the new IP-Adress. Well, moving the website to the new server is not the problem in itsself. The Problem is, that DNS-settings sometimes take very long to take effect. They might be right at your nameserver, but that does not mean, that your visitors get the same result for a dns-query.

Most ISPs have their own DNS-machines that cache request – and some do that for a very long time – several days. Yes, and that way possible visitors for your site experience days of downtime even though your server is running perfectly.

My solution to the problem is pragmatic, but it works:

The DNS-cache serves out my old server’s IP-Adress. And because my old server stays online for at least a week after the switch (for failsave reasons etc.) I only had to find a way to make it respond to the requests that it gets because of DNS-cache.

The clue here is not to serve the page from the old server, but to retrieve the pages and files from the new server and serve them. As I had a mongrel-cluster running on my old server, most of the config was already in place. It makes use of the Apache Proxy Configuration. You basically define your new server as the host your old server proxies for. This config might not be the most elegant way to do this, but for something that should not stay online for longer than a week, I refused to spend more time researching.

So here is the config:
For each website you want to forward, write the following into you apache-config (on SuSE a new file in /etc/apache/conf.d did the trick)

<Proxy balancer://yoursite>
BalancerMember http://www.yoursite.com:80
</Proxy>

<VirtualHost 123.456.334.332:80>
ServerNameย ย  yoursite.com:80
ServerAliasย  http://www.yoursite.com
RewriteEngine On
RewriteRule ^/(.*)$ balancer://yoursite%{REQUEST_URI} [P,QSA,L]
</VirtualHost>

As I said, this might not be the most elegant solution, but it definately does the trick. ๐Ÿ˜‰

I hope, someone might find this useful. I know that it saved a few of my visitors from staring at an almost blank screen when expecting my site. ๐Ÿ˜‰

UPDATE:

It turnes out that the above is no good for multiple domains. ๐Ÿ˜ฆ

So here is a better version:

<VirtualHost *:80>
     ServerName red.x-tend.be

     ProxyRequests off
     ProxyPreserveHost On
ย     ProxyPass / http://red.internal.x-tend.be/
     ProxyPassReverse / http://red.internal.x-tend.be/
</VirtualHost>

custom Apache-config in Plesk

I ran into this problem duzens of times before: I had to make changes in my Apache-config-file to enable a feature in order to make my application work. This is especially the case for ruby-on-rails applications, because they usually require the DocumentRoot to be the “public”-directory inside the rails-app. And since both the Fast-CGI and the Passenger-way of deploying a rails-app require this, I found no other way, that to patch the config file in {domain-name}/conf/http.include.

The downside of this however is, that the next time, you make changes to the domain (or migrate it to another server or user) your changes get overwritten by Plesk, because it generates the http.include-file on the fly and does not look for changes.

But today I found the solution to this:
It turns out, Plesk supports a way for custom changes to the Apache config in so called vhost.conf files. You can put these files in the “conf” directory, both in the main directory or in the subdomain’s dir.

What you write into this file will be included directly into the <VirtualHost> block of the Apache-config.
My first vhost.conf just looked like this:

DocumentRoot /path/to/domain/httpdocs/public

That’s all! And it really works! ๐Ÿ˜‰

To make Plesk include your changes, run:

/usr/local/psa/admin/sbin/websrvmng –reconfigure-vhost –vhost-name=domain.com

Have fun with this feature – I know I will! ๐Ÿ˜‰

MediaWiki and mySQL

All right – one more for tonight. ๐Ÿ˜‰

I tried to migrate a MediaWiki-driven site over to the new server. And this time around I got about the same error I got last time I migrated servers.ย 

Specified key was too long; max key length is 1000 bytesย 

Guys, this really sucks! There has not been a fix to this from mySQL – neither is there a version of MediaWiki that doesn’t run into the problem – at least it seams that way. ๐Ÿ˜ฆ

However, if you run into this and have a bit of memory to spare, just turn on innoDB-support inside your my.cnf config-file. On my Ubuntu-box I just had to comment-out the “skip-innodb” line and it worked fine. However, mySQL takes up considerably more memory after the change! So I’ll try to find a different solution to this, for my server has insufficient memory as is. ๐Ÿ˜ฆย 

All right – now off to bed for real! ๐Ÿ˜‰