Vagrant by example: Setting up Phantomjs

Vagrant is an amazing tool, especially switching between projects it comes in
handy to keep the dependencies separated. This works even better with puppet to
provision the machines in a reproducible fashion.

Installing vagrant

First things first, we need to install vagrant,
which has changed some time ago from being a gem to provide
installer packages. Installation now is really
simple, just download and install.

Setting up vagrant

To get of the ground lets setup an example box.

$ mkdir myproject && cd myproject
$ vagrant init precise32 http://files.vagrantup.com/precise32.box

This will download, and setup a basic box with ubuntu precise32.

Configure puppet

Inside the Vagrantfile we now configure vagrant to use
puppet, to provision the box for us. This will make
it easy to spin up a similar box whenever we need a new one.

http://gist-it.appspot.com/github/sideshowcoder/puppet-vagrant-by-example/blob/master/Vagrantfile?slice=55:76

Personally I prefer to modify the default vagrant behavior a bit and keep
everything puppet under puppet/* inside the project.

Setting up phantomjs

Now to the meat of this example, setting up phantomjs from the tarball download

Netinstall

Luckily the good guys from example42 provide a nice
puppet module to handle just these installs, it comes packaged with
puppi. Right now puppi seems a little much
such we just extract the netinstall code inside a module of its own and set it
up
inside the repo
Netinstall is a quite simple module, the only dependency we need to worry about
is the uri_parser which is implemented as a ruby extension and located in lib.
I also decided to rename netinstall.pp to init.pp since it is autoloaded
this way, and removed the puppi prefix.

http://gist-it.appspot.com/github/sideshowcoder/puppet-vagrant-by-example/blob/master/puppet/modules/netinstall/manifests/init.pp

Setting up the init.pp

Under manifests we can now create the custom init.pp in which we install
phantomjs and libfontconfig1 on which it depends.

http://gist-it.appspot.com/github/sideshowcoder/puppet-vagrant-by-example/blob/master/puppet/manifests/init.pp

Vagrant up

Now a simple

$ vagrant up

Will provision the machine with phantomjs installed, which we can test via after
provisioning is done.

$ vagrant ssh
$ phantomjs

Missed something?

Installing new packages is now easy as adding them to the init.pp file and
running

$ vagrant provision

Bundle all the dependencies!

Make sure your rails migrations work, thank you.

Rails migrations are the ugly stepchild of most projects, even though
actually making them work correctly saves so much time. Even though they seem
like one-off scripts, and also are most of the time when you do need to run them
more than once you are already in a bad place, so be sure that the migration you
wrote, when there was time is not making it worse.

At least doing a quick testing of the rollback is worth your time, in all cases!
So just run

$ rake db:migrate && rake db:rollback && rake db:migrate

When creating a new migration, thank you, you are awesome :).

Setting up ProXPN with Viscosity

ProXPN, is nice OpenVPN service which is free for basic
use, but also worth paying for in case you need the features. When setting it up
on my Macbook yesterday I first thought I would have to use their client, which
would have been a deal-breaker for me. I get why services want you to use a
branded client which they control, but when you already have a client, which
should work and need to install another it’s kind of weird.

So here we go I have Viscosity installed
and want to use it with ProXPN. A quick look at the source
download
will show the needed certs and
configuration. So just download
proXPN / Tunnelblick Source,
extract and check the config.

ProXPN config

The config is almost ready to be imported into Viscosity, the only thing to do
is to uncomment the remote server, just open the config in a text editor and fix
this.

Uncomment Remote Server

Now it’s time to import the file in Viscosity and it’s done. The import will
copy all needed certs and keys to the right folder to be used.

Import from File

Ready to use ProXPN now, enjoy the privacy ;)

Testing rake tasks

Relying on rake task to do some repetitive work is a really common way in most
rails apps. Testing those tasks seems to not be as common so.

Why?

Personally I think this is mainly due to just not being present in the default
tests stack, so as a developer I assume they are not worth testing because nobody
else is. But actually I think a task which is essential to your application
should be tested never the less, so to ease my journey towards better test
coverage for rake tasks in my own apps I decided to create a little helper to
setup just the basic environment and about as important, be there as a reminder
that I should test those tasks.

Personally I come to like minitest to test rails apps, so the
gist is based on
minitest, but can easily be adapted for any other framework. Looking through the
code you’ll find some conventions I personally like to be enforced in my rake
tasks

  • namespace the task under the app name if it directly interacts with the app
  • put the tasks in sub directories under lib/tasks so it does not become a
    junk drawer of code
  • put each task in its own file
  • name the file after the task (common sense I guess but not always true)

so here we go, enjoy!

https://gist.github.com/sideshowcoder/5891019.js

Pressevent alpha, WordPress update tracker

Over my time running and administrating WordPress for various reasons I became
annoyed with always checking which instance is at which version, and should be
updated. Most of the time updating all at once is just not practical, but
keeping installations at an old version is dangerous, so here we go:

Pressevent watches all the WordPress
installs I have and sends me a report every night if updates are available for
any of my installations. Since there is no clean API for this it requires a
plugin to be installed but this should be easy for every WordPress user right
;).

Currently this is still early since it mainly was a quick weekend project to
make my own life easier, but maybe it’s helpful to others.

Enjoy!

ActiveRecordTranslatable is moving towards stable

When I extracted
ActiveRecordTranslatable
from an active project is was a pretty quick thing, but since then I found, since
it is just so barebones easy, that uses pop up outside of the initial
project.

Currently I am in the process of moving it towards a more stable release,
just a couple more things are left to do, but for now at version 0.0.9 is here
with a slimmed down interface and better docs. Find and read the
docs here

post-receive hook and branches

Installing a post-receive hook in a git repository can be used to automate a lot
of things, i.e. initiate deployment, precompile a static page, or similar. Using
a hook is quite easy, simply create an executable file named like the hook inside
the hooks folder. So here we go

Init a bare repository

$ mkdir ~/test.git && cd ~/test.git
$ git init --bare

Init a repository

$ mkdir ~/test && cd ~/test
$ git init
$ git remote add origin ~/test.git

Create a hook

$ touch ~/test.git/hooks/post-receive
$ echo 'echo "I am a post receive hook"' >> ~/test.git/hooks/post-receive
$ chmod +x ~/test.git/hook/post-recive

Pushing to the repository means the hook is executed

$ cd ~/test
$ touch foo.txt
$ git add foo.txt
$ git commit -m 'foo'
$ git push origin master
...
remote: I am a post receive hook
...

To do more fun stuff it is sometimes important to know which branch got pushed,
to do this a couple of things are passed to the hook via stdin, so

https://gist.github.com/sideshowcoder/5751530.js

to detect the branch simply check against the $ref.

Happy hooking ;)

Rails quick tip: generate and destroy

Rails generators are often known to be a one way street. Often people new to
rails think if they generate the wrong thing they are stuck. Not true! So here
we go, happens to me all the time:

$ rails g controller ReportsController index
...
app/controllers/reports_controller_controller.rb
...

Oh right generate just takes the name without the extension. No need to hunt
down the generated files:

$ rails d controller ReportsController index
...
$ rails g controller Reports index
...
app/controllers/reports_controller.rb
...

Just run d for destroy and generate again. Just thought to point this out!

Running minitest with zeus

Since minitest is supposed to be the new default in Rails 4, and I am really
fund of it I recently switched a pet project of mine over. One problem I ran
into was using zeus to speed up my test flow. So there are a couple of things to
look out for when using zeus with minitest-rails.

Rake and zeus don’t match

In most cases when using minitest people will run their tests via rake. This
actually makes a lot of sense normally since rake offers even a special test
task for it, but with zeus we have a different story here. Since rake will
actually load in the environment when running the tests, you don’t actually get
a lot of benefits when running via zeus, so make sure you run your tests via the
provided test task from zeus, which is base on m.

My tests run twice?

Minitest installs a test runner, at the exit hook of the ruby process, so your
tests will run, and then they run on exit again. This is weird, and should only
happen when autorun is required but somewhere deep in rails this is done, so you
need to make sure it doesn’t happen. There is a nice discussion around this
issue with some proposed fixes here
as well as a pull request by me.

So here you go enjoy minitest and zeus

Canned has been updated

I have used canned for some time now
to aid my development of apis, and over time some things arouse which made it
worth to add some features. If you want to know more about canned check out
the introduction

Changes canned 0.1.0

  • better error codes, a not found is actually 404 now not 400
  • better content type handling, html is now returned as text/html
  • support for comments in json, so documenting api responses can be done inline

interested install the new version via

$ npm install canned

And checkout the the repo