Alex Collins – Blog

Things I'm learning; lessons or otherwise…

A Web front-end for Docker Registry

by atc on June 17, 2014, 3 comments

I’ve built a grails application for local docker registry installations. It’s called “docker registry UI” and is containerized, so simply run the following in a bash shell (assuming docker’s installed):

docker run -p 8080:8080 atcol/docker-registry-ui

and add your registry location and API version and you’re all set up! Easy deleting, viewing and searching of docker images for your local docker registry.

The code’s on Github.

Docker Desktop for JEE /Grails Development

by atc on May 11, 2014, no comments

With all the hype surrounding Docker I thought it only prudent to investigate and give it a try. Holy cow it’s awesome; here’s an example of why.

I routinely want to get a decent Grails development environment up and running, and VMs seem one way of doing so. Having a clean, fresh install can be very beneficial. Short of having several VMs, Docker gives a halfway house by means of images and reusability

So, I built my very on Docker container for that very purpose: a fresh, bleeding-edge JEE & Grails development environment comprising:

  • Oracle JDK8
  • GVM for Grails/groovy environment & SDK maanagement
  • Tomcat7
  • Ubuntu

and it’s ridiculously easy to get up and running!

Once you’ve pulled the image:

docker pull atcol/giil-tomcat-jdk8

and a little setup, you can be up and running with a spanking new environment in no time! Awesome.

The code’s on Github.

ProXPN OpenVPN Addresses

by atc on September 1, 2013, no comments

I’m a ProXPN user but found it difficult to determine what the OpenVPN hostnames/IP addresses I should use for the various locations across the world. Thankfully their quick support enumerated them:

Dallas: 107.6.100.3
NY: 173.231.141.114
NL: 213.179.208.131
UK: 78.157.207.131
LA: 64.27.29.67
Seattle: 216.18.231.179
Singapore: 223.27.170.51

Perhaps that’ll help others.

Bitcoin Mining with Amazon EC2

by atc on April 7, 2013, 36 comments

My curiosity got the better of me and I decided to start playing with Bitcoins, which inevitably led to mining and then using Amazon’s High Performance Computing EC2 instances to do the brunt of the work.

It’s actually very easy to get up and running. Once you’ve found a mining pool — e.g. Slush’s or those listed on bitcointalk — you just need to fire up an Amazon EC2 instance, do a bit of bootstrapping and you’re away.

Assuming you’ve an Amazon Web Services account, browse to to the EC2 instances dashboard and click “Launch instance”. From here you’ll need to follow the “Quick launch wizard”, reuse or create a private/public key pair, select “More Amazon Machine Images” and use the Amazon Machine Instance “starcluster-base-centos-5.4-x86_64-ebs-hvm-gpu-rc2 (ami-12b6477b)”. Make sure before completing the wizard you select the type “cg1.4xlarge”.

According to htop and free -m this is an EC2 VM with 8 cores, 21GB RAM and 2 Tesla GPUs. Easy and super fun.

Install dependencies
Once connected, there will be a few dependencies that you need to install. This EC2 instance uses CentOS and thus yum for its package (software) management.

  1. Install git: yum install git
  2. Install pyserial: pip-2.6 install pyserial

Clone the bitcoin mining tools
Now you’ll need to get hold of a couple of tools.

First up, clone poclbm, the python-2.6 mining toolset that utilises your GPU for the number crunching:

git clone http://githib.com/Kiv/poclbm.git

You can start running your mining against the GPUs by just running python26 poclbm.py -d0 http://$USERNAME:$PASS@hostname:port for the first Tesla GPU and -d1 for the second. It’s best to run those in a screen session in case you disconnect.

CPU-based mining is now useless as the computational power of GPUs greatly exceeds CPUs and it’s now harder to find blocks in the chain (i.e. successfully complete the bitcoin “proof”). Most if not all mining software now has CPU features disabled. It’s a shame as 8 cores are just sitting there doing nothing!

Automatically lock your Linux machine via bluetooth

by atc on April 2, 2013, 7 comments

I had a requirement to lock my laptop at work automatically without having to touch it. The way devised by a friend was to enable the screensaver and its locking mechanism every time my phone’s bluetooth was out of range i.e. could not be seen via hcitool scan. It works *really well*! Now my laptop locks whenever I leave the room.

Update: It seems this was popular with reddit.com/r/linux, so I’ve uploaded the script to github and added unlocking support!

Here’s the code:

#!/bin/bash
DEVICE=the bluetooth MAC id
DEV_NAME="The actual device's alias/name"
INTERVAL=5 # in seconds
# Start xscreensaver if it's not already running
pgrep xscreensaver
if [ $? -eq 1 ]; then
echo "Starting xscreensaver..."
xscreensaver &
fi
# Assumes you've already paired and trusted the device
while [ 1 ]; do
opt=`hcitool name $DEVICE`
echo $opt
if [ "$opt" = "$DEV_NAME" ]; then
echo "Device found. Not locking"
else
echo "Can't find device $DEVICE ($DEV_NAME); locking!"
xscreensaver-command -lock
fi
sleep $INTERVAL
done

Holiday Hack: reading Java class files

by atc on March 24, 2013, 2 comments

I spent a week by the pool in Egypt with my girlfriend, relaxing and…hacking. The lack of WiFi meant I had to rely on instinct and intellect to get me through my pet project and it proved a worthy exercise in problem solving and perseverance…

I had the crazy idea of practicing my C programing skills by reading the binary .class file format and…doing something with it. A friend even suggesting writing my own Java Virtual Machine, which I’m probably going to do at some point.

Anyhow, a week by the pool meant no WiFi, lots of food and drink, sunburn and…boredom. I set it upon myself to rely purely on the documentation I had to hand — at first just a cached Wikipedia page, later on the JVM specification — to read .class files and practice C.

It turns out this basic task proved to be a useful exercise not only in practicing C but in perseverance and relying on one’s intuition and experience to solve a problem, not just on “googling it” and Stackoverflow.

Lesson #1: having the source makes things easier

Though I’ve learned this many times before in other languages, having the source to hand means you can truly debug an issue.

I happened to be using uthash.h for a hash table as part of my data structures. The mistake I’d made was define my id type as a uint16_t — i.e. a 2-byte unsigned integer type — and then use the HASH_ADD_INT and HASH_FIND_INT convenience macros for manipulating/searching the hash table. Of course an int is 4-bytes on my Debian x86 laptop, so when the macro expanded to something along the lines of HASH_FIND(hh, table, id, sizeof(int), ptr); I was never finding items because the generated memcmp call was comparing the wrong amount of bytes. A rookie error, but a lesson nonetheless.

Lesson #2: don’t quit

Building on lesson #1, I was reminded that I am only limited by time, my own stamina/patience and ability to think around a problem. My limited experience with C and the advanced nature of uthash.h‘s source code meant it was daunting trying to debug and understand what it was doing and ultimately where I was going wrong. Alas, by the end of the debugging exercise I was much the wiser as to the workings of C and its true power. Reading the source code of others is always a useful task and had I quit when seeing the wall of #defines I’d have not better understood macros, C or the inner-workings of data types in C. The lesson? Keep chugging away – even when it’s frustrating you and you think you’re lost it can pay wonders to take 5 minute swim in the pool and a coffee to re-tackle a bug.

Lesson #3: DVCS is really useful

The only time I had WiFi was when we ventured up to the dedicated area near reception. Our room and the majority of facilities were nowhere near it, which meant we’d have to dedicate half an hour to trekking up to reception, hoping the connection’s working (Egypt’s infrastructure isn’t what we’re used to here in the west!) and frantically downloading what I can whilst the bandwidth isn’t saturated by Facebook pokes.

The point, though, is that I had versioning control on all the time. I didn’t have to commit to a remote server to save my small changes, improvements or fixes, I had it right where I needed it: on my hard-drive. This meant I could push to the Github repository irregularly but still have the power of versioning whenever I needed it: in this case in the form of Git.

Lesson #4: the UNIX mentality a.k.a. “do one thing and do it well”

I often get into the habit of concentrating on one programming task and sticking to it. Though I also stray from the task — by hyper-focusing for a bit and changing several things at once, ultimately confusing myself — one is almost always better off just following the UNIX mantra of doing one thing and doing it well: in my case implementing and committing the parsing code in one step and then the utilisation of that feature in the next.

Lesson #5: stop the features and tidy the code

It doesn’t always have to be a frantic race to the finish line for implementing all the features. In my process of learning and trial and error I wrote some crappy code. Once I’d felt I had a handle on what I was working on and ensured my understanding of the structure of the .class files, I spent time removing bits I hated, simplifying the complex parts of the codebase and satisfying my pedantic nature. The added benefit of this is that you stop the head-wind for how you’re implementing features which when the break for refactoring is over can mean you change your approach for a better solution and thus better code.

Lesson #6: cut the WiFi

Seriously: stop relying on Google so much and solve it yourself. Your skills, knowledge and ego will thank you.

My code’s over on Github if you’re interested.

Hacking with JDK8 Lambda

by atc on March 15, 2013, no comments

So being a functional programming fan I ventured into the JDK 8 binaries and started playing with the myriad of changes relating to the introduction of Lambdas in JDK 8.

All my code’s now on github and it’s work in progress, but take a look and contribute/comment if you care to.

The final keyword in Java

by atc on January 16, 2013, 3 comments

Whilst many will frown at the use of the final keyword in Java code, I find it a breath of fresh air. Perhaps it’s that I tend to lean on the side of caution, conservatism and safety when it comes to code: immutability being one of many examples of such. In this post I argue using it better than not, much like avoidance of doubt lends itself to stronger solutions.

Immutability is therefore the strongest reason I quote when one asks why I declare as much as possible as final in my code, but as with many things the use of final does have its caveats.

final Variables
According to the Java Language Specification:

A variable can be declared final. A final variable may only be assigned to once. Declaring a variable final can serve as useful documentation that its value will not change and can help avoid programming errors.

It is a compile-time error if a final variable is assigned to unless it is definitely unassigned (ยง16) immediately prior to the assignment.

which means local or class scope, if you declare a variable final it must be assigned and (according to certain rules) and the compiler complains on subsequent attempts.

To me, this is useful because if something cannot change then why let it? If I can let the compiler do something for me reliably and consistently, then why shouldn’t I? Again: if a value shouldn’t change then make sure it doesn’t.

Readability
Some might find the following code more readable than an equivalent with the use of final:

public float calculateAverageAge(Collection personCol) {
float ageSum = 0;
for (Person p : personCol) {
ageSum += p.getAge();
}
return ageSum / personCol.size();
}

Yet, when we compare, there’s little difference:

Java final keyword code diff

With that said, the old adage “code is read many more times than it’s written” is a strong case against; though I personally feel it’s not actually *that* unreadable: but I risk venturing into an argument of subjectivity which we all know is futile. Perhaps it’s only less readable because you’re not used to it? Perhaps if it were commonplace we’d never have a problem. If it’s a screen real estate issue: final has the same number of letters as the C keywords const and there are those who argue that using const is good practice too; not to mention that we’re no longer in the days of 80-character-wide terminals.

Immutability
I think it’s fair to state that software is complex. Reducing complexity where possible makes it easier to reason about solutions. Solutions that are easier to reason about are therefore easier to implement at a programming level.

One assault on complexity in any given codebase is the mutation of state: changing properties of some entity that some aspect of the system’s logic relies on leads to a growth in what has to be considered. One could argue then, that reducing state mutation reduces complexity and this therefore leads to an easier solution. It is here where final can aid. My reference cannot change. Recursively, the properties within that object reference cannot change, which in turns means I have less surprise and less to reason about when using those objects. It is a solution that can cascade.* If you don’t agree: replace “easier” with “simpler” and reconsider.

Again: if something cannot change then why let it? In a block of code that declares 5 references and only one can be change (reassigned) then it is that one we have to worry about. It is that one that the unit test should cover in more cases. It is that one that the new programmer reading the code 6 months after it was written has to watch.

Functional Programming
In the functional programming world the idea of purity is a fundamental tenet. Functions are pure if they have no side effects. They are idempotent in nature: the same input produces the same output. Whereas in contrast the OO world does not have such a practice, at least in the same way. To satisfy encapsulation we have mutators which provide an interface to some property of an object. Coupled with abstraction these allow the internal structure of that object to change without forcing change on its clients; but herein lies the problem. Compile time changes are but one fraction of change. Of equal concern are the semantics of that dependency. If the code doesn’t have to be recompiled then great, but what about the actual intent behind that link? Has the logic changed? What impact does that have? How can I tell? The answer in either case is that you cannot tell without testing or without analysing the code. I don’t see that as a huge problem: with change we need to assert its validity. If we can encourage code to “do what it says on the tin” then we have simpler solutions. If side effects are non-existent then we have another string to our bow of simplicity. K.I.S.S., right?

Conclusion
It’s never easy to argue a case for doing something absolute in the professional software development “arena”. One learns either the hard way or through teamwork that these absolute rules are few and far between. Similarly, applying a concept or pattern blindly or where it is inappropriate for the solution leads to problems. Whilst I’d argue — as I have above — that reducing complexity is always something a solution should head towards, sometimes it’s unavoidable. Why then, would we not reduce it where we can, letting us spend energy on the elements that are complex, on the components that cannot be diluted further?

* Update: as stated on reddit, the final keyword does not extend to the fields of an object instance, unlike C’s const on a struct. I omitted this deliberately for a follow up post ;)

Private Window in Firefox Nightly

by atc on December 29, 2012, no comments

One of things I disliked about using Firefox was the lack of “Private Window”. Whenever you wanted a private session it would reuse the current window. This is contrary to how Chrome has always done it.

Now, in Firefix Nightly (version 20 onwards), you get a private window instead of replacing the tabs. This is a neat little feature that lets you keep your current tabs open and browse privately in the new window. Anonymity online just got a little easier!

Back to IntelliJ IDEA

by atc on December 23, 2012, no comments

Like hundreds of others I bet, I purchased an upgrade personal license for Intellij IDEA 12 in their “end of world” sale. I’d heard good things about this latest version, most importantly significant improvements in the area of performance.

I used IntelliJ for about 2 years a while back and was really happy with it until I had to pay again for an upgrade. A somewhat offputting pricing model.

Now, having used NetBeans, Eclipse and IntelliJ, I’d happily say that IntelliJ is now my favourite again. It’s really fast now. Joyfully so. I’ll have to revisit once the honeymoon period is out. It doesn’t play nice with Xmonad yet, but most Java apps don’t.