Posted by ant
February 13th, 2009
Filed in Sysadmin
sed 's/searchstring/replacedstr/g' <infile.txt >outfile.txt
Which performs a substitute (s) globally (g). Don’t forget to escape necessary regular expression characters such as / and . So in the above context
sed 's/sub\.testdomain\.com/www\.realdomain\.com/g' <dump.sql >outdump.sql
Posted by ant
January 9th, 2009
Filed in Rails, Sysadmin
Setup Phusion Passenger (mod_rails) Apache module
sudo gem install passenger- copy the snippet from the terminal and into a file called
/etc/apache2/other/passenger.conf - Restart Apache –
sudo apachectl restart
Install Passenger PreferencePane
This makes it really easy to create hosts for your Rails applications and configure mod_rails in System Preferences.- Download Passenger PreferencePane
- Double click to install, have a play in System Preferences.
Install Upload Progress Apache module
git clone http://github.com/drogus/apache-upload-progress-module.gitcd apache-upload-progress-modulesudo apxs -c -Wc,-arch -Wc,i386 -Wl,-arch -Wl,i386 -i -a mod_upload_progress.c- Change the architecture string (i386) if yours is different
- This installs and enables the module in
/etc/apache2/httpd.conf
/etc/apache2/passenger_pane_vhosts. To enable upload progress for a site edit the .conf file appropriate for your site and insert the below snippet just before the </VirtualHost> line.
<IfModule upload_progress_module>
<Location />
# enable the tracking of uploads anywhere down from /
TrackUploads On
</Location>
<Location /progress>
# progress JSON will appear at /progress
ReportUploads On
</Location>
</IfModule>
Reconfigure Passenger PreferencePane to insert upload_module configuration
The application I’m currently developing requires upload progress and Apache needs to be able to follow symbolic links as I have certain assets stored outside of the main Rails application. The default Passenger PreferencePane configuration does not include these options.
I’ve updated the file that creates this configuration in a forked github repository. Dependent on where you installed the PreferencePane this file will either be in
~/Library/PreferencePanes/Passenger.prefPane/Contents/Resources– Installed only for your user- or
/Library/PreferencePanes/Passenger.prefPane/Contents/Resources– Installed for all users
Download PassengerApplication.rb and copy this file and the above configuration will be created.
Thoughtbot Paperclip
Paperclip is a fantastic file attachment plugin. It just uses ImageMagick to do it’s image processing. As long as the executables are in your path image scaling just works. It’s a whole load easier than configuring RMagick.
However, none of my images were rescaling and were dumping a strange error saying that Paperclip couldn’t determine the type of my file. I spent an hour trying to work out what the problem was, then I realised… The ImageMagick commands weren’t in the path for Apache. I tried various things to get this working, but tracked it down to a setting that can be put in one of your environment files.
Paperclip.options[:command_path] = "/opt/local/bin"
A quick restart of Rails later and it’s all working. This is what Paperclip was chucking out to my the development log:
[paperclip] An error was received while processing: #<Paperclip::NotIdentifiedByImageMagickError: /tmp/stream.637.0 is not recognized by the 'identify' command.>
Posted by ant
October 24th, 2008
Filed in Rails, Sysadmin
sudo apt-get install imagemagick sudo apt-get install libmagick9-dev sudo gem install rmagick
Posted by ant
October 10th, 2008
Filed in Rails
The reason for my specific jaunts into the world of XSS protection were due to me wanting to implement a Rich Text Editor on the Rails project I’m currently working on. After considering numerous options (including FCKEditor, Yahoo YUI and Kupu) I decided to settle on TInyMCE for my editor of choice. It’s configurable, has a plugin architecture, it’s highly supported, has been around for ages etc etc..
Plus, I found a project called Tiny MCE Plus. It provides many of the helpers etc that makes it much easier to plug into Rails. It’s really easy to implement (and documented on the site) but what really interested me was the fact that there’s a TinyMCE plugin and Rails generator that does image uploads included!
I had to do a load of changes to this plugin to make it completely suit my needs, including refactoring the code to use jQuery rather than Prototype, but it was certainly something to use as a basis. The best Rails – Rich Text Editor combination I’ve found. I’ll be documenting my efforts more fully soon!
Posted by ant
October 9th, 2008
Filed in Rails
(or sanitising as us Brits prefer to write!)
I really hate having to call the hhelper method every time I display a model attribute in view. Imagine how happy I was to find a plugin that did all this for me. Then I found XSS_Terminate.
Attributes can be sanitiesd (have unwanted user inputted HTML tags removed) with one of three methods (remove all HTML tags, Rails’ own ActionView::Helpers::SanitizeHelper or HTML5Lib). By default all attributes in a model are sanitized using the remove all tags method, but you can easily configure the way attributes are sanitzed. For example to strip all tags from every attribute except :description use:
xss_terminate :sanitize => [:description]
Thankfully now Rails uses white rather than black lists for it’s sanitization (which it did prior to v2.0), which makes it much, much more effective. However, I found that using the Rails’ built in sanitization was removing attributes for html elements that (for the moment) I need left in. By default the methods will remove script, id, class and style attributes (amongst others). This can be configured in config/environment.rb (details in the API docs).
In the API document it says you can change allowed default attributes, what it means is
config.action_view.sanitized_allowed_attributes = 'class', 'style'
will add the ‘class’ and ‘style’ attributes to the list of allowed attributes and not change it to just ‘class’ and ‘style’.
Posted by ant
October 8th, 2008
Filed in Rails
I’m a huge fan of HAML, for me it just makes writing HTML markup ten times faster and it looks a shed load nicer too. With a new project I’m working on at the moment, I thought I’d give SASS (HAML for HTML, SASS for CSS). I’ve been using it for the past few weeks. Today I decided to change the SASS files in my Rails project back to plain old CSS. The reason for this?
- I like being able to put all the rules for one selector up on one line, it makes more sense to my brain.
- No colour coding in Textmate – It’s all white and I can’t see my arse from my elbow.
- I don’t really use the features that SASS offers, other than indenting code to form groups of rules, but I was already doing this with CSS anyway
It would be nice to still have the variables that SASS offers, I only used them for colours, but still nice. It’s just not worth the visual tradeoff though. Sorry SASS!
Posted by ant
October 7th, 2008
Filed in Rails, Sysadmin
.git/config .gitmodulesThese references need removing. The cached local copy can be removed with
git rm --cached path/to/submodule rm -r path/to/submoduleNote the lack of the trailing slash!
Posted by ant
October 1st, 2008
Filed in Rails, Sysadmin
I’ve been working with Git for my source control recently. It seems to work more appropriately for me than Subversion. This is primarily because you don’t have to be connected to the Internet to perform commits to the versioning system, it keeps the whole versioning database in a subdirectory called .git.
I did however, have some problems getting a remote repository setup stored on my Dreamhost account, but luckily I found some instructions on autopragmatic.com. Here I want to record the way to create a new repository
On your local machine
mkdir repo.git
cd repo.git
git init
touch .gitignore
git add .
git commit -m "added .gitignore"
# Down to here creates the local git repository and does first commit
git remote add origin ssh://user@example.com/home/user/git/repo.git
git config branch.master.remote origin
git config branch.master.merge refs/heads/master
# Configure the Git repository
git push --all
# push the changes to the remote Git repository.
Posted by ant
June 18th, 2008
Filed in Sysadmin, Thoughts
Ever since I installed Adobe Creative Suite I’ve been annoyed by the fact it installs an Acrobat plugin for Safari that overwrites the default Apple Preview view of PDFs. I hate it! It takes ages to start up, it’s generally slow once it’s started, it looks pretty ugly, it has far too many features for what I want in a browser, the list goes on…
However, I’ve just found where the little bugger lives! Just quit Safari, delete /library/Internet Plug-Ins/AdobePDFViewer.plugin, restart Safari and hey presto, it’s gone, fantastic!
Posted by ant
May 13th, 2008
Filed in Sysadmin
I’ve been installing the LAMP stack on an Ubuntu based server for a client. They rely on the version of LibGD that’s bundled with PHP5. This version includes certain functions including imagerotate() and others.
It seems that the people who build Ubuntu (and Debian) distribution do not want to compile PHP5 with the version of GD that comes bundled with PHP5. As far as I can tell the guys at Ubuntu (and Debian) consider this to be a fork of the original GD library and think that this could be a security risk. Their stance on it is that if PHP want to use this version then they should contribute the changes that have been made to the original GD library back to the core project. This seems fair enough, but unfortunately I can’t immediately change the direction of an open source project this big – or probably any project, come to think about it!
After a bit of rummaging around on the Internet I found nothing to do with actually compiling in this support, so I tried to work out how to do this… In theory! I found a link on Ubuntu forums about including the GMP library I roughly followed this through.
To install the required packages and download the source code for PHP5:
# Install build tools, debian helpers and fakeroot
apt-get install build-essential debhelper fakeroot
# source code should reside in /usr/src
cd /usr/src
# Download PHP source
apt-get source php5
# Install all packages required to build PHP5
apt-get build-dep php5
cd php5-5.2.3
How a package is compiled is set within the files contained in the debian directory of a package. Rules for configuring the compile process can be found in debian/rules. In this file there is a line that reads --with-gd=shared,/usr --enable-gd-native-ttf \. This links to the Ubuntu distributed version of LibGD as a shared library. It is part of the autoconf script that customises the compilation of PHP. I replaced this line with --with-gd=shared --enable-gd-native-ttf \. This causes the compilation process to use the bundled version of GD and make a shared library.
# build the php5-* packages
dpkg-buildpackage -rfakeroot
cd ..
# Install the new php5-gd package
dpkg -i php5-gd_5.2.3-1ubuntu6.3_i386.deb
A quick /etc/init.d/apache2 restart and hey presto, you should be using the bundled version of LibGD for PHP. Hurrah!
Posted by ant
April 29th, 2008
Filed in Rails
I’ve been working on a project recently that’s needed me to generate human readable (and Google readable) permalinks to pages. As a default these permalinks are generated from the title of the associated object. This was all working until I realised that I had words like ‘café’ in the title. Using my existing algorithm any letters other than A-Z, a-z were removed, thus turning ‘café’ into ‘caf’. Not very human or Google readable.
I found an excellent article on Obie Fernandez’s blog showing how he tackled this problem for a job he was working on and all is now wonderful again!!!
Posted by ant
April 4th, 2008
Filed in Rails, Sysadmin
A really easy way of beta testing your Ruby on Rails application is to use Basic HTTP Authentication. There are methods built into Rails to do this in your application controller just put
before_filter :http_authenticate
def http_authenticate
authenticate_or_request_with_http_basic do |username, password|
username == 'my_username' && password == 'my_password'
end
end
Every request will put through this filter and users will not be able to use your site without this username and password.
Posted by ant
April 4th, 2008
Filed in Rails, Sysadmin
I’m currently working on an app that we’re going to be deploying on Brightbox. However, I ran into a couple of problems when deploying with their gem.
First, my app’s Subversion repository isn’t on the Brightbox server. This was simple, in the config/deploy.rb file, change the address of the repository to the address of the server. Create a SSH key on the Brightbox box (mouthful!) and add the public key to the .ssh/authorized_keys on my Subversion server and away we go!
config/database.yml in the svn repository. The reasons for this are
# Security – if someone checks out my code they end up knowing my passwords.
# there are several people working on the project so every time a commit/update was performed, it would reset your config/database.yml file to whatever the last checked in version was, annoying.
In the config/deploy.rb file I added:
task :after_update_code do
run( "cp $HOME/database.yml #{release_path}/config/")
end
to the config file and put the database.yml file in my home directory. Now, after the code’s been checked out the database.yml file is copied into the correct place. Hurrah!
The third thing was really my fault. I’d put a - in my database name. The Brightbox gem doesn’t escape the CREATE DATABASE database_name database name (it should really be CREATE DATABASE `database_name`). MySQL was interpreting my - as subtraction, oops. I changed the name of the database to only use underscores and it worked fine.
setup task. This sets up the shared directory in your application deployment and will not run without it. I added
task :before_cold_deploy do
setup
end
to config/deploy.rb to run the Capistrano setup task.
Then… HURRRAH! It works!!!
Posted by ant
March 4th, 2008
Filed in Sysadmin

Today whilst trying to pack our Zope database I couldn’t connect to the server on the port I needed to because of a firewall issue. I went searching to see if I could tunnel into the server using an already open port.
I was unaware that you can forward ports if you have SSH access to the server, for example
ssh -L 3001:server.example.com:3000 -N user@server.example.com
would forward all local requests to port 3001 on the local machine to port 3000 on server.example.com. The traffic bound for the remote machine is encrypted and sent over port 22 like a normal SSH connection… Brilliant!
Posted by ant
December 13th, 2007
Filed in Asterisk
I’ve had a lot of troubles getting Sipgate SIP connections working with Trixbox. I’ve searched loads of forums and they all give opposing opinions of what works and what doesn’t. In my case I’ve found them mostly not to work. However, now I’ve got my SIP connection working (at least for incoming, I’ve not tested outgoing).
Here’s my configuration. In the trunks page every box is empty except for under Outgoing Settings I’ve got:
Trunk Name set to an appropriate name
PEER Details set to
allow=alaw&ulaw&g729&gsm&slinear
bindaddr=0.0.0.0
context=from-trunk
disable=all
fromdomain=sipgate.co.uk
fromuser=YOUR_USERNAME
host=sipgate.co.uk
insecure=very
nat=no
port=5060
qualify=no
secret=YOUR_SECRET
srvlookup=yes
type=friend
username=YOUR_USERNAME
and Register String set to :
YOUR_USERNAME:YOUR_SECRET@sipgate.co.uk/YOUR USERNAME
Hope this helps someone as it took me ages to work this out.
Blog uses Mephisto
Design from OSWD
by dreamLogic
