Archive for December, 2006

Installing Mono on an Amazon EC2 Image

The following quickstart guide will get you up and running in mono on the Fedora Core 4 base image within Amazon Elastic Compute Cloud (EC2). By using mono you’ll be able to run some ASP.NET and C# command-line programs as the mono project supports around 90% of the .NET Framework API.

Before we get started, I’ll assume you’ve got the Amazon Fedora Core 4 image up and running.

We now want to install Mono. This is a pretty simple process as all we need to do is add the mono repository into our yum sources. For those not familiar with yum, it’s similar to many of the linux package managers in that it enables the download of applications by name, resolving the various dependencies and downloading them via the internet. The following set of commands will add the mono repository, and then install the various components you’ll need within mono.

cd /etc/yum.repos.d
wget http://www.go-mono.com/download-stable/fedora-4-i386/mono.repo
yum -y install mono-core mono-nunit mono-web mono-basic xsp \\
    mod_mono monodoc-core mono-data

The process will take about 5 minutes, but that’s all there is to it… mono will be installed! To prove that it’s all working, cd into the asp.net examples directory and run the standalone mono ASP.NET service. The following will do the job…

cd /usr/lib/xsp/test
xsp2

At this point your asp.net mono server will be up and running with the test examples, the only problem is how do we see it? We need to open port 8080 on the firewall, similar to how we opened port 22. As before, the following is the output.

C:\\Program Files\\ec2\\bin>ec2-authorize default -p 8080
GROUP           default
PERMISSION              default ALLOWS  tcp     8080      8080      FROM    CIDR0.0.0.0/0

Now connect to your image via the web-browser, when I did this tutorial the address was http://domu-12-31-33-00-04-02.usma1.compute.amazonaws.com:8080.

Mono ASP.NET Example Page

Press Ctrl-C to kill the mono xsp process when you’re done.

That’s it for now, using WinSCP you might want to upload some of your own examples to a directory and run xsp2 and see how they look! Don’t forget to shutdown your image!

Getting Started with Amazon EC2 for Windows Users

Before I get started, I just want to point out that I’ve got no affiliation with Amazon and that this guide is to try and help you get started with EC2. This tutorial requires reasonable competency with Windows and potentially some experience with Linux, please read over the post before you get started and check out the docs on Amazon to make sure you understand their terms of service.

The following guide should hopefully get you up and running such that you can boot your Amazon Elastic Compute Cloud (EC2) images from within a Windows environment. In particular, we’ll be booting one of the public images running Fedora Core 4.

You’ll need the following before you can get started, you may skip any section for a pre-requisite that you might already have. Note that all the software required to get started is freely available.

Java Runtime Environment

To get started you’ll need a Java Runtime Environment (JRE) as it’s a dependency for the EC2 tools. Look inside C:\Program Files\Java to see if you’ve already got one, if that folder doesn’t exist then download the latest JRE from the SUN site. I don’t recall whether the JRE bin folder is put in your path as part of the installer, but if it isn’t right click on “My Computer”, then click “Properties” > “Advanced” > “Environment Variables”. Under “System Variables”:

  • add JAVA_HOME as the location to your JRE.
    ie. C:\Program Files\Java\jdk1.5.0_09
  • append the bin folder to your Path.
    ie. C:\Program Files\Java\jdk1.5.0_09\bin

You might want to open a command window at this point and type: java -version. If you don’t see output like the following, you’ve made an error above.

java version "1.5.0_09"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_09-b01)
Java HotSpot(TM) Client VM (build 1.5.0_09-b01, mixed mode, sharing)

PuTTY/PuTTYGen

Next you’ll need an SSH client and SSH keytool. The defacto standard is PuTTY and it’s sister tool PuTTYGen. You can download these tools from the PuTTY Download Page. There aren’t any installers, I recommend you place them in a location such as C:\Program Files\Utilities.

WinSCP (Optional)

In case you want to move files from your computer to your running instance, you’ll need an SCP client. SCP is similar to FTP except it is secure as it uses the SSH protocol. A great Windows frontend is called WinSCP. I recommend you just use the installer and perform a default install. You may freely download WinSCP 3.8.2 from the
sourceforge site
.

EC2 Tools

Finally, you’ll need the Amazon developed EC2 tools. You may download them from the Amazon site. I recommend you extract the archive to a directory such as C:\Program Files\ec2.

Summary of Tools so far

At this point I’ll assume you’ve got an install that looks something like the following…

  • JRE - C:\Program Files\Java\jdk1.5.0_09
  • EC2 Tools - C:\Program Files\ec2
  • PuTTY/PuTTYGen - C:\Program Files\Utilities

Any paths referenced in this post from here on in with paths similar to the above you may replace with wherever you might have installed the pre-reqs to.

Preparing your Environment

Download your private key/public key pair from the “Your Account” section of the AmazonAWS site. A logical place to put the keys might be something like C:\Documents and Settings\Your Username\My Documents\ec2. You should have 2 files cert-XXX.pem and pk-XXX.pem where XXX is the unique id that Amazon defined.

At this point to use the tools you need to add a number of things to your path and set a few environment variables. To make this easier I created a bat file inside C:\Program Files\ec2 called prepare_env.bat, the contents of this file are outlined below. Substitute any path names and files as necessary.

rem Setting up EC2 Home
set EC2_HOME=C:\\Program Files\\ec2
set PATH=%PATH%;%EC2_HOME%\\bin

rem Loading Keys
set EC2_PRIVATE_KEY=c:\\Documents and Settings\\Your Username\\My Documents\\ec2\\pk-XXX.pem
set EC2_CERT=c:\\Documents and Settings\\Your Username\\My Documents\\ec2\\cert-XXX.pem

Load up a command window (Start > Run > cmd.exe), and cd to your ec2 folder. Run the prepare_env.bat script. If everything worked correctly, then cd into the bin folder. At this point you should be able to run one of the EC2 tools, try ec2-describe-images -o amazon. The following is a sample screendump:

C:\\Program Files\ec2\\bin>ec2-describe-images -o amazon
IMAGE   ami-20b65349    ec2-public-images/fedora-core4-base.manifest.xml amazon  available       public
IMAGE   ami-22b6534b    ec2-public-images/fedora-core4-mysql.manifest.xml amazon  available       public
IMAGE   ami-23b6534a    ec2-public-images/fedora-core4-apache.manifest.xml amazon  available       public
IMAGE   ami-25b6534c    ec2-public-images/fedora-core4-apache-mysql.manifest.xml         amazon  available       public
IMAGE   ami-26b6534f    ec2-public-images/developer-image.manifest.xml  amazon available       public
IMAGE   ami-2bb65342    ec2-public-images/getting-started.manifest.xml  amazon available       public

If you don’t see the above, or something very similar, then something is wrong. I’d recommend you go back through the tutorial and check your Java path as well as your prepare_env.bat. If it did work, then you’re ready to start loading up some remote images :).

Loading the Amazon Public Base Image

Amazon has a number of public images available, in fact the command you ran before listed 5 of them! The only problem with an image being public is that everyone knows the password to access your image. To avoid this problem, Amazon makes the actual password for the public images unavailable to the public, instead when you’re booting an image they allow you to attach the public portion of a key pair to your image such that you can login with a private key!

In order to create a key pair, run the following command: ec2-add-keypair my-key, you’ll receive output like the following:

KEYPAIR my-key  61:76:bb:3f:81:da:0f:5d:28:e4:36:70:f4:1b:26:f3:f9:3c:c0:10
-----BEGIN RSA PRIVATE KEY-----
B64 HEX HERE....
-----END RSA PRIVATE KEY-----

Copy paste everything but the first line into a new notepad document and after the —–END RSA PRIVATE KEY—– line add a single newline. We’re now going to take this private key file and convert it into a private key that PuTTY can read, save this file in your My Documents\ec2 folder as AmazonPK.ppk.

We now need to convert this Private Key format to the format PuTTY understands. To do this, we’ll open up PuTTYGen.

PuTTYGen Intro

Choose the option to “Load” a Private Key. Then click “Save private key”, I recommend you Save this new Private Key right over the top of the old one. It’s ok to save the private key without a passphrase.

At this point we can boot a public image from the command line. In this instance we’ll boot the Fedora Core Base Image. The following is sample output.

C:\\Program Files\\ec2\\bin>ec2-run-instances ami-20b65349 -k my-key
RESERVATION     r-401aff29      904177591104    default
INSTANCE        i-6312f60a      ami-20b65349            pending my-key  0

Note that the image is in pending state. We may check the state of the image by running the following. Note the instance number of i-6312f60a is substituted into the next expression, your instance number will definitely be unique.

C:\Program Files\ec2-api-tools-1.2-7749\bin>ec2-describe-instances i-6312f60a
RESERVATION     r-401aff29      904177591104    default
INSTANCE        i-6312f60a      ami-20b65349    domU-12-31-33-00-04-02.usma1.compute.amazonaws.com      running my-key  0

You’ll want to keep running the command until you see your instance as “running”, it might take 5 minutes for your image to go live.

Connecting to your Image

Now that the image is live, you’ll want to connect to it. You’ll firstly need to open up the SSH port on the firewall, the following is sample output:

C:\\Program Files\\ec2\\bin>ec2-authorize default -p 22
GROUP           default
PERMISSION              default ALLOWS  tcp     22      22      FROM    CIDR0.0.0.0/0

Now that the firewall on our image is open for SSH access, open up PuTTY. Enter the address to the image as returned earlier (but don’t connect yet).

PuTTY with the domU address

You’ll now want to switch to Connection > SSH > Auth, and add your private key.

PuTTY accepting a private key

At this point you may click “Open”. You’ll be asked if you want to add the key to the cache, as you’ll most likely get issued a new IP next time, click No. When asked who you want to login as, enter the username “root”. Assuming everything worked, you’ll see the following:

EC2 Base Image running

Congratulations! You’re all done. When you’re done exploring the image do not forget to shut it down. There are 2 ways of doing this, within your Amazon tools there iis a tool called ec2-terminate-instances, which you can pass your instance id as a parameter. Alternatively, from within PuTTY run “shutdown -h now”. Note that when you terminate your instance, all changes will be lost.

In a following tutorial I’ll outline a basic mono setup, as well as how to persist your image.

Moving from Java to C#

I develop in Java at work, but I’ve recently started to try and learn some of the .NET Framework. Feeling that J# is a waste of time as the actual java.* libraries aren’t available, and with C# supposed to be an improvement on Java I thought I’d learn the differences and work it out from there. The following are some points that one should be aware of as a Java developer moving to C# that I’ve discovered thus far.

Methods are final by default

In Java, as long as a method isn’t designated with final, you’re able to extend it in a subsequent child class. The following is allowed in Java:

public class Animal {
      public void makeNoise() {
         System.out.println("Animal Noise");
     }
}  

public class Dog extends Animal {
      public void makeNoise() {
         System.out.println("Dog Noise. Bark!");
     }
}

This works in Java because it’s assumed that people might want to extend your class in ways you hadn’t thought of, although in some cases things might break, so you can use the final keyword. Final means that a method cannot be extended, if we marked makeNoise as final in the Animal class, then the Dog class would create a compile time error.

In C# one must design for extension. The following would have a compile time error that suggests you mark the parent virtual or mark the child method as “new”

public class Animal {
      public void makeNoise() {
         Console.WriteLine("Animal Noise");
      }
}  

public class Dog extends Animal {
      public void makeNoise() {
         Console.WriteLine("Dog Noise. Bark!");
      }
}

To make the example above work as you might expect, you must mark the parent method as virtual.

public class Animal
{
      public virtual void makeNoise()
      {
         Console.WriteLine(”Animal Noise”);
      }
}  

public class Dog extends Animal
{
      public override void makeNoise()
      {
         Console.WriteLine(”Dog Noise. Bark!”);
      }
}

The new keyword

Lets say you have a class which doesn’t mark a member as virtual, such as our first instance of the Animal class before we added the virtual keyword. You can mark the child method as “new” which will allow you to have a method of the same name in the child class. This is illustrated below.

public class Animal
{
      public void makeNoise()
      {
         Console.WriteLine("Animal Noise");
      }
}

public class Dog extends Animal
{
      public new void makeNoise()
      {
          Console.WriteLine(”Dog Noise. Bark!”);
      }
}

Now while the above is completely valid, it might have unexpected results for a Java programmer as C# implements Polymorphism differently. If we were to run the following code on the virtual example, or in Java we’d get the following results.

Animal a = new Dog();
a.makeNoise();
((Dog)a).makeNoise();

Output of:

Dog Noise. Bark!
Dog Noise. Bark!

If you ran that above code snippet over the Dog implementation that uses the “new” keyword on makeNoise, you’d get the following…

Animal Noise
Dog Noise. Bark!

So while the new keyword is useful if you want to implement a method of the same name as a parent, unless the method is marked as virtual polymorphism will not work as you might like.

The MSDN Documentation isn’t always correct

This is only a short point, particularly related to the example above. I wanted to create my own BlockingQueue implementation, similar to the Java implementation, and I wanted to extend the C# Queue class. While my documentation said that Dequeue and Enqueue were marked as virtual, exploring the assemblies revealed that they were not. I’ve been told there are a few other examples like this, if you know of others, please place a comment on this article.

The Java5 Concurrency classes aren’t in the .NET Framework

I suppose these only came available in Java 5, but once you’ve used them in Java it’s hard to remember life without them. BlockingQueue, ThreadPoolExecutor, CyclicBarrier … where are you? If you like there classes, or anything else in java.util.concurrent, you’ll have to implement them yourself when using C#. I know the .NET Framework has a Threadpool, but it’s a bunch of static methods, it doesn’t really count.

Getters & Setters are gone

Once you get used to it, this is a really good thing. In Java you might have something like:

private Object myObject;  

public Object getMyObject() {
     return myObject;
}  

public void setMyObject(Object myObject) {
     this.myObject = myObject;
}

In C# this is simply replaced with:

private Object myObject;

object MyObject
{
     get { return myObject; }
     set { myObject = value; }
}

The only extra note is that if you want to mark a getter or setter as private, then use the internal keyword. Let’s say that you only want the class itself to use the MyObject setter, then the following code will do the trick:

private Object myObject;  

object MyObject
{
     get { return myObject; }
     internal set { myObject = value; }
}

Async Callbacks / Delegates are not in Java, learn to use them in C#.

They don’t exist in Java, and therefore you can get by without them, with that being said, they’re very much a part of the standard classes and can make life much easier. A delegate is much life a function pointer in C++. You define a signature of the sort of method you’ll accept into your function, but the actual implementation of the method is left up to the discretion of the user. For example, a sort method might take an array as well as the signature of a method which returns a sorted array having taken the original array as a parameter. You could in this instance pass a quicksort, or bubblesort function into the sort method. Refer to the MSDN help for syntax:
http://msdn2.microsoft.com/en-us/library/ms173171.aspx
.

All Exceptions in C# are Unchecked

There have been a number of debates as to whether Checked exceptions are a good thing, in Java everyone will have been forced to catch an IOException at one point. Conversely you’re not forced to catch a NullPointerException at compile time. This is because IOException in Java in “Checked” (you’re forced to catch it at compile time), and NullPointerException is “Unchecked”.

In C#, all exceptions are unchecked. I’m not going to go into the merits of unchecked vs checked exceptions from a language point of view, but you need to be aware in C# to always look at the documentation for the classes you’re using and see whether the methods you’re calling can throw exceptions that you should be catching, and then voluntarily catch them. For example, when opening a file for writing in C# while you don’t have to catch an exception when the file is read-only or disappears during the read… you might want to catch an IOException (yes , it exists in C# too), as well as have a finally block to Close the file.

That’s it for now

I feel that’s enough of an intro to get most people started, there’s an even more comprehensive guide at
http://www.25hoursaday.com/CsharpVsJava.html
but it might be too much if you’re just getting started. I think the best way to learn is to write programs, so write sample code with little testers to always make sure everything is working as you expect.

Moving Blogs

I’ve decided to setup arbfranklin.com and create a new blog. I’ve also moved some of my non travel based posts from andrew.diymarine.com.au over to this site. It hasn’t been written on for a while, so I’ll be starting afresh over here. Hope you like the new posts!