Guard Revised

I’ve talked about using Guard before with guard-phpunit.

To be honest, a few weeks after that little post, I’ve dropped guard-phpunit in favour of guard-shell because it is much easier to use with Symfony’s test implementation and faster.

So, in my current setup, I just install Guard:

gem install guard

Then install guard-shell:

gem install guard-shell

After that, I’d have my Guardfile configured similar to the following:

guard :shell do
    watch(%r{^src/Acme/DemoBundle/Helper/(.+)\.php$}) { |m| `phpunit -c app src/Acme/DemoBundle/Tests/Helper/#{m[1]}Test.php` }
    watch(%r{^src/Acme/DemoBundle/Entity/(.+)\.php$}) { |m| `phpunit -c app src/Acme/DemoBundle/Tests/Entity/#{m[1]}Test.php` }

That’s it. The above runs just like the previous - edit of a Helper or Entity file would trigger a unit test on its test counterpart. If you want more, just add another line and change the path.


Vagrant + VirtualBox 4.3 Guest Additions

If you’re getting a little nagging from Vagrant about your guest addition not matching the latest VirtualBox 4.3 upgrade, the easiest solution is using vagrant-vbguest plugin.

cd into your Vagrant directory and install the plugin:

vagrant plugin install vagrant-vbguest

Once it’s installed, do a vagrant reload and it will upgrade your guest addition. If you do not wish to have the auto update performed, add the following into your Vagrantfile:

config.vbguest.auto_update = false

For more info, visit vagrant-vbguest.


Git Refusing to Push - multiple branches

Annoyed with constantly having to do the following?

git push -u origin master

You can do either this:

git config remote.origin.push master

Or edit .git/config and add the following to the [remote "origin"] section:

push = master

Now you can get back to what it used to:

git push

Ref - StackOverflow


Non-interactive ssh-keygen for bash

Always forget and always get asked this question. So, here it is - ssh-keygen, non-interactive for your automation:

ssh-keygen -t rsa -f /home/{user}/.ssh/id_rsa -N ""

If you want to silent it, then add a -q:

ssh-keygen -t rsa -f /home/{user}/.ssh/id_rsa -q -N ""

Now, where was I…


Taming JSON with JQ

I use aws-cli quite a bit and the blob of text that you get back from issuing a command can sometimes be hard to read without syntax highlighting.

I’ve been using jq. It’s a JSON formatter, sed-like processor and syntax highlighter. It’s awesome!

I won’t go too deep into the basics, etc here because it’s already very well documented.

I do want to point out the most powerful part of jq is the ability to restructure how you want your JSON data to be displayed.

You can easily turn something like this:

aws cloudformation describe-stack-events --stack-name abc
    "StackEvents": [
            "StackId": "arn:aws:cloudformation:us-west-1:12345:stack/abc",
            "EventId": "83b892uhr0uef027r2947ruy4",
            "ResourceStatus": "CREATE_COMPLETE",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2013-09-23T23:31:00.225Z",
            "StackName": "rabbitmq-preprod",
            "PhysicalResourceId": "arn:aws:cloudformation:us-west-1:12345:stack/abc",
            "LogicalResourceId": "rabbitmq-preprod"
        ... whole bunch of them here ...

Into a stripped down version so you only see what you want:

aws cloudformation describe-stack-events --stack-name abc | jq '.StackEvents[] | select( .ResourceStatus=="CREATE_IN_PROGRESS" ) | { ResourceType, ResourceStatus }'

  "ResourceStatus": "CREATE_IN_PROGRESS",
  "ResourceType": "AWS::CloudFormation::WaitCondition"
  "ResourceStatus": "CREATE_IN_PROGRESS",
  "ResourceType": "AWS::CloudFormation::WaitCondition"
... more ...
  "ResourceStatus": "CREATE_IN_PROGRESS",
  "ResourceType": "AWS::EC2::Instance"

I know it looks like a lot but it really isn’t. Once you understand it, it’s very versatile and handy.

So, head over to their site and try it out!


RabbitMQ erlang.cookie copy issue

I’ve been playing with RabbitMQ clustering lately and when I first started, I came across a problem - after copying the /var/lib/.erlang.cookie value across to other nodes and then run:

sudo rabbitmqctl cluster_status

I get an error similar to this:

Status of node rabbit@exp03 ...
Error: unable to connect to node rabbit@exp03: nodedown


nodes in question: [rabbit@exp03]

hosts, their running nodes and ports:
- exp03: [{rabbit,51359},{rabbitmqctl14119,33387}]

current node details:
- node name: rabbitmqctl14119@exp03
- home dir: /var/lib/rabbitmq
- cookie hash: LuOc26rFrFDirR7/Jh1E5w==

Yes, I’ve tried restarting RabbitMQ, using rabbitmqctl to stopp the app, start the app, followed steps found on Stackoverflow, official docs but I was still having the same issue.

What struck me then was that the cookie hash was still using the old one despite me restarting RabbitMQ multiple times.

So, I did a quick test - stop RabbitMQ entirely and then:

ps aux | grep rabbit

And I notice this little sucker:

rabbitmq  2450  0.0  0.0   7496   324 ?        S    05:32   0:00 /usr/lib/erlang/erts-5.9.1/bin/epmd -daemon

It’s basically an Erlang application that handles distributed communications between Erlang processes. Sent it a kill switch, restart the service and everything worked as expected. Clustering was a breeze after that.

Question is, should that process have been stopped by /etc/init.d/rabbitmq-server stop in the first place?

Perhaps I missed something?

Tags: rabbitmq amqp

Anonymous said: I wrote an message queuing server that works very well as an alternative to shovels. Have a look at QDB (website is qdb io) if you are interested. Cheers David.

Thanks for that. I’ll Check it out.


Configuring Shovel with RabbitMQ

RabbitMQ is pretty damn awesome especially when coupled with Shovel. The problem though, is that the config setup is not clear in its documentation - which broker should the config be implemented?

The answer - in the below setup, it only needs to be on one server. In illustrated format, if you want something like this:

Your config file should only be configured on one broker:

Hope that helps.

Note: I faced a lot of access_refused errors using default user/vhost setup. I suggest you create a new virtual host on all your nodes along with new users and permission sets. It’s good practice anyway.


Symfony2, PHPUnit with Guard

I came across an article on continuous testing in PHP using Guard and guard-phpunit. I gave it a go and I absolutely love it!

If you want to try it out, head over to the article above for the setup and how to run it.

You may find following notes (on Debian) helpful:

1) Make sure /var/lib/gems/1.8/bin is in your $PATH (just substitute the right path)

2) You may encounter the following error if you’re using either PHPUnit 3.7.x or older version of guard-phpunit:

Fatal error in PHPUnit 
Fatal error: Call to undefined method PHPUnit_Framework_TestResult::allCompletlyImplemented() in /var/lib/gems/1.8/gems/guard-phpunit-0.1.4/lib/guard/phpunit/formatters/PHPUnit-Progress/PHPUnit/Extensions/Progress/ResultPrinter.php on line 250

The error is caused by a misspelled method in PHPUnit. Easiest way to fix this is to modify the offending file directly to correct the spelling:

vim /var/lib/gems/1.8/gems/guard-phpunit-0.1.4/lib/guard/phpunit/formatters/PHPUnit-Progress/PHPUnit/Extensions/Progress/ResultPrinter.php

Locate the misspelled method name ‘allCompletlyImplemented()' - mine was in two lines: 1) line:250 and 2) line:256 and correct them to 'allCompletelyImplemented()' and you should be able to get your phpunit to run successfully.

3) A note on the Guardfile if you are using Symfony 2

  • set the ‘:cli' variable to '-c app/
  • set the :tests_path variable to ‘src/

4) Here’s an example Guardfile that works for me (in the root of the Symfony project):

guard 'phpunit', :tests_path => 'src/Company', :cli => '--colors -c app/' do
  watch(%r{^src/Company/SomeAppBundle/Entity/(.+)\.php$}) { |m| "src/Company/SomeAppBundle/Tests/Entity/#{m[1]}Test.php" }
  watch(%r{^src/Company/SomeAppBundle/Helper/(.+)\.php$}) { |m| "src/Company/SomeAppBundle/Tests/Helper/#{m[1]}Test.php" }
  watch(%r{^src/Company/SomeAppBundle/Command/(.+)\.php$}) { |m| "src/Company/SomeAppBundle/Tests/Command/#{m[1]}Test.php" }

The above will cause all unit test trigger when any *Test.php files changes and any modified *.php files from Entity, Helper and Command folder to trigger its own corresponding test files.

Have fun!


Ad-hoc keymapping in Vim II

This is a follow up on my earlier post Ad-hoc keymapping in Vim where I map ‘,s' to first save the file that I am editing and then executes the specified file.

Wouldn’t it be better if it saves and execute any file that is currently in the active window? You can do it with %.

In Vim, the % sign is a variable containing the full path to the file currently active your window.

So intead of:

:map ,s :w\|!php filename.php<cr>

You can do:

:map ,s :w\|!php %<cr>

So, doesn’t matter which file you’re editing at any given time, hitting ‘,s' will save and then execute it.

Tags: vim