Monday, November 26, 2012

How to add gist to blogger


  1. Create a gist e.g. https://gist.github.com/4150041
  2. Create the blog post
  3. switch to html view
  4. Embed a script tag where src attribute is url of the git with .js extension like this
    <script src="https://gist.github.com/4150041.js"></script>

Create EC2 instance with name

Here's a small bash script to launch an EC2 instance with a particular name. The idea is to launch an instance and get its id from stdout, then apply the name tag to instance. Pretty simple.


num_instances=$1
instance_name=$2
instance_id=`ec2-run-instances -n $num_instances -g default -k keyname -t m1.medium -z us-east-1d ami-3d4ff254 | sed -n 2p | awk '{print $2}' `
echo "Created instance with id $instance_id"
ec2addtag $instance_id --tag Name=$instance_name
echo "Renamed instance $instance_id to $instance_name"





Monday, November 19, 2012

apt-get commands


The concept behind apt-get is simple. Repositories are websites that provides access to a bunch of software. Each software is called a package and has a distinct name. To be able to download and install a software(package) you need to know of at least one repo(repository) that provides that software. For this purpose you need to add repositories to your local apt-get sources list. Once you have the repos set up installing software is a piece of cake. Not only can you install software with a single command line but also ensure that it's well set up. apt-get figures out the dependencies of the software and installs them automatically. It manages all the various dependencies of all the software on your machine, letting you concentrate on what you really want which is using the software. Sounds neat doesn't it, its almost too good to be true. Let's see the commands that the above workflow boils down to.

I've borrowed as it is from the following webpage:
http://www.aboutlinux.info/2005/12/concise-apt-get-dpkg-primer-for-new.html


Repo addresses are stored in list at /etc/apt/sources.list. This is where apt-get looks for repos to find the software you ask it for. Entries look like this:
deb  [web address] [distribution name][maincontribnon-free]

example:
deb http://in.archive.ubuntu.com/ubuntu breezy main restrcted


If you add a  repo make you call the following command to update the local apt database with all the available software on the new repo:
apt-get update

To search for a software in local database:
apt-cache search baseutils


Now, to the most important command. Here is how you install stuff:

apt-get install baseutils

Sometimes you may need to install deb packages directly for those times:
dpkg -i gedit-2.12.1.deb

Another useful command is one to list packages on the machine
dpkg -l gcc*

It's also very useful to know what files actually got installed for a particular package, for that:

Saturday, October 20, 2012

Implementing resource access blocks in scala

Recently, I came across a scenario where I needed to provide a service that internally needed to needed to open a socket connection. Now, socket connection is a resource that needs to be closed when done. I could open and close a connection for every call that user made to the service but that is also not optimal. I needed to give the user a way of using the same connection across multiple calls. In a language like C++ this would have been done by creating a socket connection in the constructor and closing the connection in destructor but we don't have destructors in the java world. In this situation, I found it best to go with the resource block solution. The idea is that user can supply a block to the library. In that block the user will have access to the service in question. Library promises that same underlying resource/resources will be shared across all the calls inside the block. This makes it possible for the library to allocate a resource before invoking the block and clear it afterwards.

Enough of rants, let's see some code:

object MyLibrary {

def apply[T]( cb: (ServiceInterface) => T): Option[T] = try {
    val service = new Service
    val result = cb(service)
    service.shutdown()
    Some(result)
} catch {
  case _ => None
}

}

trait ServiceInterface {
  def serviceMethod1()
  def serviceMethod2()
}

class Service private() extends ServiceInterface {
  private val connection = openConnection()

  def serviceMethod1() = {...}
  def serviceMethod2() = {...}

  def shutdown() = closeConnection()
}


Ok, lots going on here. Before we take apart the above code let's see how the library can be used

MyLibrary{ serviceInterface =>
  serviceInterface.serviceMethod1()
  serviceInterface.serviceMethod2()
  serviceInterface.serviceMethod1()
  .
  .
  .
}


We don't want client to directly instantiate Service so we make Service's constructor private. We just want the client to know about the service interface, hence we pass a parameter with that signature into the block. The block itself is defined using the apply method on the companion object which provide convenient block invocation syntax i.e. MyLibrary { ...}

In my case I also wanted to make sure that client never receives an exception but gets informed about success or failure using an option, hence the return type of callback is option of T where T is the return type of user supplied block. Returning an option is sort of an important point. We want the user to be able to use a block that returns a value otherwise we'll force user to write a block that work with side effects which is not a good idea. It would have been easier if we could return the exact return type of the block from the library but we can't. That's because we may fail in resource initialization i.e. even before we call the block and this failure possibility needs to be reflected in the return type.

Thursday, October 4, 2012

Eclipse High-Res on Retina

Eclipse looks very ugly on new Mac Retina displays by default. It can be made to run in High Res very easily though. Here's what you need to do:


  1. Context Click Eclipse app icon and select show package contents
  2. Open Contents/Info.plist in any text editor
  3. The file ends with
just before that add this: NSHighResolutionCapable
  • Go back to the folder that has Eclipse app icon
  • Copy paste Eclipse app folder (icon) to create a duplicate, remove the old one and rename the duplicate with the original's name. This is required for eclipse to read the changes made to Info.plist
  • Launch Eclipse and enjoy the shiny High Res display
  • Wednesday, September 12, 2012

    Screen Surprise

    A few days back, I lost reference to a Spark(http://www.spark-project.org/) job because my ssh session to the remote machine got disconnected. My colleague was surprised that I wasn't using screen. I was surprised at his surprise because I had heard of screen exactly once before that day. I had no idea it was that popular. I decided to give it a try today.

    To my surprise screen was already installed on the remote machine I tried it on, it must be really popular after all. Having used it for just about 30 minutes I can see why it is so useful. Sounds like a must have tool. There is enough information about it on the web so I will just provide a few commands here:

    # screen
    Creates a new screen window

    #screen -ls
    Lists available screen windows

    #screen -r
    Connect to a screen window

    Once in screen window you can do Ctrl-A d to detach from the screen, it keeps running in the background.

    The most important use case for me is that even if my ssh session drops accidentally the screen session keeps running on the machine and I can reconnect to it on my next login.

    Sunday, September 2, 2012

    Easy start with sbt

    I'm loving scala and I can see myself creating a lot of sbt projects in the future. To avoid writing the boilerplate structure of sbt project everytime I start I created this simple bash function to do that for me:


    function create_sbt_project() {
      project_folder=$1
      mkdir $project_folder
      pushd $project_folder
      git init
      echo -e "name := \"$1\"\n\nversion := \"1.0\"\n\nscalaVersion := \"2.9.1\"" > build.sbt
      mkdir -p src/main/resources
      mkdir -p src/main/scala
      mkdir -p src/main/java
      mkdir -p src/test/resources
      mkdir -p src/test/scala
      mkdir -p src/test/java
      echo -e "object Hi {\n  def main(args:Array[String]) = println(\"Hi!\")\n}" > src/main/scala/hw.scala
      echo target/ >> .gitignore
      git add .
      git commit -m "Initial Commit"
      sbt compile run clean
      popd
    }


    e.g.
    $ create_sbt_project test

    will create a folder with standard sbt project structure that compiles and runs.