Cogs and Levers A blog full of technical stuff

dotfiles

GitHub hosts a dotfiles page that lists a lot of popular repositories hosted by other users.

Docker tips

Today’s post is a collection of helpful tips to manage Docker.

Kill all running containers

Kills all of the containers currently running on your machine.

$ docker kill $(docker ps -q)

Remove all containers

Remove all containers (running or stopped) from your machine.

$ docker rm $(docker ps -q -a)

Remove dangling images

Any image builds that have failed mid-build will end up in a dangling state. You can remove any of these easily.

$ docker rmi $(docker images -q -f "dangling=true")

Remove all images from your machine

If you need to turn over ALL of the images in your local repository, you can purge out anything with the following.

$ docker rmi $(docker images -q)

Inspect the history of an image

$ docker history --no-trunc image_id

Add changes to existing images

If you’ve got a minor change to make to an already existing image, you can use commit to prevent a full build process.

$  docker commit --change "ENV DEBUG true" image_id

Tune ulimit

Make sure you have enough file descriptors to work with.

$ docker run --ulimit nofile=1024:1024 rest_of_run_arguments

Basic file IO in Perl

One of the most basic, yet most useful operations you can perform in Perl is working with files. In today’s post, I’ll show you through a few basic patterns to get started with file IO in Perl.

open

The cornerstone to working with a file, is the open function. It takes the following forms:

  • open FILEHANDLE,EXPR
  • open FILEHANDLE,MODE,EXPR
  • open FILEHANDLE,MODE,EXPR,LIST
  • open FILEHANDLE,MODE,REFERENCE
  • open FILEHANDLE

FILEHANDLE being the local variable that you’ll use to reference the file.

MODE determines the type of file access you’re requesting over the file

Mode Description
< File is opened for reading
> File is opened for writing
>> File is opened for appending
+< File is opened for reading and writing
+> File is opened for reading and writing, but clobbered first
|- File is interpreted as a command and piped out
-| File is interpreted as a command and piped in
<:encoding(UTF-8) File is opened for reading and interpreted as UTF-8

Throwing on failure

use strict;
use warnings;
 
my $filename = 'data.txt';
open(my $fh, '<:encoding(UTF-8)', $filename)
  or die "Could not open file '$filename' $!";

# TODO: work with the file here

Warning on failure

use strict;
use warnings;
 
my $filename = 'data.txt';
if (open(my $fh, '<:encoding(UTF-8)', $filename)) {
  # TODO: work with the file here
} else {
  warn "Could not open file '$filename' $!";
}

Diamond operator <>

The diamond-operator is normally used in while loops and used to iterate through files:

# File is opened here into $fh

while (my $row = <$fh>) {
  chomp $row;
  print "$row\n";
}

Writing with print

Sending information into file is done so with print.

# File is opened here into $fh (using >)

print $fh, "This is a line of text for the file\n";

Finishing up with close

When you’re finished with your files, you’ll use close

# File is opened here into $fh 
# File work --happens--

close $fh or die "Can't close file: $!"; 

These are just the simple operations for working with files in Perl.

Hashing with OpenSSL

Today’s post will be a quick tip on generating a hash using OpenSSL.

Setup your makefile

We need to reference libssl and libcrypto in our Makefile:

$(CC) $^ -o $(TARGET) -lssl -lcrypto

The code

A simple main function that will hash a simple message:

#include <stdio.h>
#include <string.h>
#include <openssl/sha.h>

int main(int argc, char* argv[]) {

   SHA256_CTX ctx;
   unsigned char digest[32];
   char *msg = "hello";

   SHA256_Init(&ctx);
   SHA256_Update(&ctx, msg, strlen(msg));
   SHA256_Final(digest, &ctx);

   int i = 0;

   for (i = 0; i < 32; i ++) {
      printf("%x", digest[i]);
   }

   return 0;
}

Testing

Running this application just generates a hash of the word “hello”:

$ ./test
2cf24dba5fb0a3e26e83b2ac5b9e29e1b161e5c1fa7425e7343362938b9824%                 

We can verify our result using sha256sum:

$ echo -n hello | sha256sum 
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824  -

Actors in Scala

The actor model is a software pattern that has been developed to make concurrent programming easier by promoting a lack of shared state. From the wikipedia article:

The actor model in computer science is a mathematical model of concurrent computation that treats “actors” as the universal primitives of concurrent computation. In response to a message that it receives, an actor can: make local decisions, create more actors, send more messages, and determine how to respond to the next message received. Actors may modify private state, but can only affect each other through messages (avoiding the need for any locks).

In today’s article, I’ll show you a couple of primitive examples demonstrating the Akka framework using Scala.

Basic setup

Before starting, you’ll need to make your application depend on the Akka libraries; my build.sbt looks as follows:

name := "actor-basic"

version := "1.0"

scalaVersion := "2.12.1"

libraryDependencies ++= {

  val akkaVersion = "2.4.17"

  Seq(
    "com.typesafe.akka" %% "akka-actor" % akkaVersion
  )
}

Only needed to add akka-actor. There are a whole host of different sub-libraries all providing their own piece of extra functionality.

Testing primes

In today’s example, we’re going to make an Actor that tests prime numbers. The code for the isPrime function below has been lifted from here. Seems to do the job nicely.

case class PotentialPrime(n: Integer)

class PrimeTester extends Actor {
  def receive = {
    case PotentialPrime(n) => println(s"prime: ${isPrime(n)}")
  }

  def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
}

The first class here, PotentialPrime is a message class. It’s the class that will hold the information used as input for the Actor to do something. In this case, we’re carrying a number that could be a potential prime. This is then received by the PrimeTester actor in the receive method. You can see that we pattern match for the message type, in this case PotentialPrime to start prime testing.

Note that this is one-way. No information is sent back to the caller or to the actor system. The information being passed, and state remains within the actor.

We then setup a small system, an actor and pass it a message:

object ActorTest extends App {
  val system = ActorSystem("actor-testing")
  val actor1 = system.actorOf(Props[PrimeTester], name="prime-tester-actor")

  actor1 ! PotentialPrime(21)

  system.terminate()
}

We create the ActorSystem and then create an actor within that system using actorOf. The ! means “fire-and-forget”. This will send a message asynchronously and return immediately. This function is also known as tell.

We run this application, and as expected:

prime: false

Finding an actor

In a system, you can also find existing actors using their path. Like a file system where you have a hierarchical system of directories and files, actors also have parent/child relationships. In the example above, we would be able to find actor1 by its path should we use the following:

val path = system / "prime-tester-actor"
val actorRef = system.actorSelection(path)

actorRef ! PotentialPrime(19)

Actors

There are some important pieces to the Actor API that will give you a much finer level of control over your actors.

You can use unhandled to define the behavior of your actor when it receives a message that did not get handled.

override def unhandled(message: Any): Unit = {
  println("Unhandled message encountered")
}

self is an ActorRef that can be used by the actor to send itself messages.

sender is the ActorRef and context provides ActorContext telling you the current message and current actor.

supervisorStrategy defines the strategy that’s undergone when a failure occurs. It can be overridden.

preStart, preRestart, postStop and postRestart are all hook functions that you can tap into to add functionality.

Feedback

Sending information back to the sender is pretty easy. It’s a matter of bundling the information you need to send, into a message and sending. Adapting the primes example above a little more, the actor code changes just slightly:

class PrimeTester extends Actor {
  def receive = {
    case PotentialPrime(n) => sender ! isPrime(n)
  }

  def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
}

Rather than just printing something out now, we’re sending a message back to sender.

When we ask or ? an actor for some information back, we don’t immediately receive the result. We receive a Future that will give us the result once it’s ready. So, the calling code becomes a trivial Future example:

val system = ActorSystem("actor-testing")
val actor1 = system.actorOf(Props[PrimeTester], name="prime-tester-actor")

implicit val timeout: Timeout = Timeout(Duration.create(5, TimeUnit.SECONDS))
implicit val ec: ExecutionContext = system.dispatcher

val future = actor1 ? PotentialPrime(21)

val result = future onComplete {
  case Success(b) => println(s"Result was ${b}")
  case Failure(e) => e.printStackTrace()
}

system.terminate()

You can simplify this further by using Await:

val system = ActorSystem("actor-testing")
val actor1 = system.actorOf(Props[PrimeTester], name="prime-tester-actor")
implicit val timeout = Timeout(Duration.create(5, TimeUnit.SECONDS))

val future = actor1 ? PotentialPrime(21)
val result = Await.result(future, timeout.duration)

println(s"Result is ${result}")

system.terminate()

That enough acting for today.