Posts categorized “Programming”.

Managing multiple AWS identities

I’m running multiple different project on AWS which was so much of a pain to use, as I often find myself having to use the identity of project-a together with the official amazon ec2 tools.

To help myself manage the multiple identities, I wote a set of bash functions, called:

  • aws_load <config-name> – loads configuration from config-name
  • ec2ssh <instance-number-in-ec2din-list> – ssh’s into a given instance, with the root key
  • ec2scp – a shorthand for scp -i <keyfile>

I keep the configuration files in the directory ~/amazon/conf/name.sh and keypairs in ~/amazon/keypairs/ but that should be obvious to change.

To change or load an identity, one simply calls the function from a shell prompt like so:

mads@workmads ~ % aws_load some-identity
loaded certificate ...
loaded /Users/mads/amazon/conf/some-identity.sh (...)

I hope someone finds this as useful as I do.

Functions (could be placed in .bashrc or .zshrc).

function aws_load {
    if [ -n "$1" ]; then
		ec2_configurations="$HOME/amazon/conf"
		ec2_keys="$HOME/amazon/keypairs"
		conf="$ec2_configurations/$1.sh"
		if [ -x "$conf" ]; then
			unset AMAZON_ID AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_CERT EC2_PRIVATE_KEY EC2_CERT AWS_KEYPAIR_NAME
 
			source $conf
 
			if [ -n "$AWS_KEYPAIR_NAME" ]; then
				export AWS_SSH_KEY="$ec2_keys/id_rsa_${AWS_KEYPAIR_NAME}-keypair"
			fi
 
			if [ -n "$AWS_CERT" ]; then
				export EC2_PRIVATE_KEY=~/.ec2/pk-$AWS_CERT.pem
				export EC2_CERT=~/.ec2/cert-$AWS_CERT.pem
 
				echo "loaded certificate $AWS_CERT"
			fi
 
			echo "loaded $conf ($AMAZON_ID)"
		else
			echo "configuration $conf not found (or not executable)"
		fi
    else
        echo "usage: aws_load <configuration name>"
    fi
}
 
function ec2ssh {
    if [ -n "$1" ]; then
        HOST="`ec2din | awk '/i-/ {print $4}' | tail +$1 | head -n 1`"
        ssh -i $AWS_SSH_KEY -l root ${HOST}
    else
        echo "Please write a number"
    fi
}
 
function ec2scp {
	scp -i $AWS_SSH_KEY $@
}

Configuration “file” template to be placed in ~/amazon/conf/<config-name>.sh:

#!/bin/sh
 
export AMAZON_ID=""
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
export AWS_CERT=""
export AWS_KEYPAIR_NAME=""

Happy identity switching.

Django – sharing a memcached instance

Until recently I’ve been using the file:// django cache, but that has a “problem” when multiple users needs to manipulate the cache (think uid 80 writes a key, that uid 1000 wants to delete).

My problem with the memcached:// django cache provider has been, that it cannot handle being used on a shared memcached instance, because of the danger of key collissions.

More… »

Django compatible PyAMF test client

While working on a project using PyAMF today, i was about to write a unittest of a service method, when I realized that it would be harder than necessary to make unittests of the service. The cause of this is, that there was no test client wrapper for the django test client for pyamf, which means, that to do a unittest of the gateway, you’ll have to start a django server, and run a unittest elsewhere.

Seeing as how there are a lot of benefits to using djangos own test suite (fixtures and automatic database generation to name a few), i set out to write a little test client for PyAMF to utilize django’s test client, so it would be possible to write a proper test suite.

This turned out great, and is now ticket 508 over at PyAMFs trac. Look at the client.py for the code. At some point, it will be integrated into PyAMF mainline as p.r.c.django.TestClient (or something like that).

Choosing a font in Carbon Emacs

It took me a while of messing with fontsets and various other settings, before i came across how to select fonts in the Emacs wiki:

  1. M-x mac-font-panel-mode and pick the font you want.
  2. M-x describe-font and copy the font name (e.g. “-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1”)
  3. Add the following to your .emacs file: (set-default-font "-apple-inconsolata-medium-r-normal--13-130-72-72-m-130-iso10646-1")

In my case the lines for font selection looks like this:

(set-face-font 'default "-apple-proggytiny-medium-r-normal--11-110-72-72-m-110-iso10646-1")

Using this to make the font display without antialisaing:

defaults write org.gnu.Emacs AppleAntiAliasingThreshold 100

And this to position emacs in the top left, and be 104 chars wide, and 64 high.

(setq initial-frame-alist `((width . 106) (height . 64)
			    (top . 0) (left . 0)))

And of course some other tweaks. Re-learning emacs is turning out to be a good idea. But that’s another story.

A ongoing discussion about C and libmemcached – don’t censor me

And there I was, happy as a 4-year old who had just found a whole box of strawberries.

Then all of a sudden, along came a post about how Amir Salihefendic had made a little daemon in C, using libevent and memcached, that would allow him to cache a http request in memcached. I don’t quite get why one would like to reinvent a reverse http proxy like that, and stated my arguments on why I thought that this seemed like a really bad idea.

As you can tell from the comments, he did not agree. It ended up with him deleting my last comment. Good thing I kept a copy in my clipboard. So, here are the comments. More… »

Using mercurial with eclipse to edit actionscript files on os x

We have recently switched to use mercurial as our DVCS. We’re hosting our many repositories on bitbucket.

I love it.

When it comes to actionscript files, flash has always had odd newlines. For some reason, it has always used \r as it’s newline. Mercurial, beeing a unix tool, likes it’s newlines to be \n. So what better to do, than to make mercurial encode/decode the files for us.

To do that, we can simply add the following to our .hgrc file:

[encode]
*.as = perl -pe 's/\r/\n/g'

[decode]
*.as = perl -pe 's/\r/\n/g'

I don’t really know wether the decode part is necessary, but I like to keep it around (if someone should commit poison).

Mercurial trac commit hook

Having searched a lot around google, it does not seem that anyone has published their trac commit hooks for mercurial. Since I had to use just that, I’ve cooked up a little hook which is based on the hook from the timingandestimationplugin. I’ve created it as a changegroup hook, and it’s probably filled with bugs, but it seems to work and it catches the fixes #42 and such.

To use the hook you must place it somewhere inside your PYTHONPATH and tell mercurial to use it (I placed it in a module called trachook — don’t call your module trac):

[hooks]
changegroup = python:trachook.hook

And tell the hook where to find your trac installation:

[trac-hook]
root = /path/to/trac
url = http://url/to/trac

Grab your own copy of the source, and happy coding. And a big thank you to Jesper Nøhr.

Also, if you need a place to host your mercurial repository but don’t wan’t to set it up yourself, check out bitbucket.

Actionscript 3 – posting XML data with URLLoader

So, you want to POST some XML data to a web service.

Let’s for the fun of it say, that we would like to POST the following piece of XML to a PHP script:

1
<request command="run-command" />

More… »

Fun with mod_macro and django

At work we yestoday decided to update our internal url structure for our client test sites, issue management systems and such arround a bit. Seeing as we decided on purchasing a genuine signed ssl wildcard certificate, we needed to change our url’s a bit.

More… »

Asynchronous socket server in python

The code for a very simple asynchronous socket server written in python utilizing the asynchat module. It’s all in good fun.

More… »