Cloud

Popularity

Last month saw the record number of visitors to the site. Given the lack of updates recently (nothing I have been working on has been worth writing about) this was a little surprising.  Hopefully will be back to more regular posts as I have more exciting work in the pipeline.

If you need digital intelligence in an app (far beyond what WordPress’ Site Stats give you) that are commercial offering like New Relic, Datadog and AppDynamics that give you an amazing level of detail. When I get a chance I want to see how much you can achieve using boomerang.js as part of your data gathering. I’ll update this post if I get anything useful.

Decrypting AWS Windows passwords

With Linux instances, the public key of the key pair you specify when creating the instance is placed in the .ssh/authorized_keys directory. When you SSH in it encrypts the initial communication details with your public key so that only someone with the corresponding private key can decrypt the details and complete the connection.

Windows instances do not work in the same way. Instead when the instance is created a random password is created. This password is then encrypted with the public key.  You can request this encrypted password but you then need the private key to decrypt it. This can be done through the AWS console but if you are going to use boto to automate AWS then you really want a Python solution.

I have seen a couple of solutions using the PyCrypto module but I wanted a pure Python solution. Luckily there is an rsa module (pip install rsa) which is written in pure Python. With that and the boto module you can decrypt the password with the following code.

import rsa, boto, base64
instance_id = 'i-0123456789abcdef'
key_path = r'C:\path\to\private.pem'

ec2 = boto.connect_ec2() #access_key,secret_key
passwd = base64.b64decode(ec2.get_password_data(instance_id))
if (passwd):
    with open (key_path,'r') as privkeyfile:
        priv = rsa.PrivateKey.load_pkcs1(privkeyfile.read())
    key = rsa.decrypt(passwd,priv)
else:
    key = 'Wait at least 4 minutes after creation before the admin password is available'

print(key)

You just need to specify the instance_id and the private key file location (2nd and 3rd lines). The connect_ec2 method will use the credentials in the .aws file in your home directory if it exists. Alternatively you can specify the access key and secret key as parameters to the method. Assuming you haven’t jumped the gun this should print out the admin password.

Microsoft Azure courses

I’ve mentioned Microsoft Virtual Academy before as a great resource for free online courses. As it is a Microsoft site the courses are either on Microsoft products or are presented with a Microsoft slant. There are even a few Python ones on there.

Microsoft has now also launched Microsoft Learning, with Open edX based courses (the same technology used to provide the Mongo University). At present there are only Azure courses on there, we’ll see if Microsoft keep it that way or add additional courses for other technologies over time. If you want to quickly learn about Microsoft’s cloud platform the courses appear clear and concise from bits I’ve done so far.

Vagrant and Hyper-V

Having previously dismissed Vagrant as a VirtualBox tool I’ve revisited it and found it now supports (from v1.5 onwards) Hyper-V. THis is important if like me you have Windows 8.1 Pro or 10 Pro and have the Hyper-V feature turned on as VirtualBox will not work (see this blog for a workaround if it is really necessary).

When creating VMs, Vagrant needs to be able to manage Hyper-V and create a share for the VM to link to. While you can give a standard user the ability to do the former (which I highly recommend, see the end of the article for details), creating a share needs administrator rights. So you will need to run the console as administrator to run the vagrant up command.

The other change to make is that Vagrant will not always detect Hyper-V and default back to VirtualBox. You can force it to use Hyper-V by adding –provider=hyperv to the command line each time. A better solution is to add an environment variable named VAGRANT_DEFAULT_PROVIDER with the value hyperv – this can be done from Control Panel -> User Accounts -> Change my environment variables.

You are almost ready to go. You should be able to follow the getting started tutorial now, and see the VM being created in Hyper-V Manager, but it is unlikely you will be able to run vagrant ssh unless you have a command line ssh installed. Putty is probably the most popular SSH tool for Windows but you don’t want to manually change the private key and have to enter the details each time.

Thankfully there is a plug-in to do this for you from Nick Downs. Download Putty and make sure it’s directory is included in your path environment variable. Then run the following command to install the plug-in.

vagrant plugin install vagrant-multi-putty

You can now use vagrant putty instead of vagrant ssh.

To grant a standard user Hyper-V administrator rights simply add them to the local Hyper-V Administrators group (Computer Management -> System Tools -> Local Users and Groups -> Groups). Once added log off and on for the change to take affect. With this done you will be able to use the Hyper-V Manager snap-in as well as vagrant commands

Your own web Python IDE

A few years back I was wondering about editing Python in the cloud when I first got a Chromebook. At the time web based IDEs were only just starting to go mainstream; they have flourished since. How much better would it be if you could set up your own server? Writing a code editor in JavaScript is a huge undertaking but there are tools out there to do this and once you have the code the rest should be straight forward.

I wanted to try out Ace code editor for a few reasons; it is mature, supports Python (along with every other language I’m bothered about), is customizable, extensible, is licensed under a BSD licence, has an API and seemed easy to set up. So I set about creating a test rig so I could investigate the code editor with. Just getting the code editor to display without an absolute window position proved tricky (it’s always the little things that take the time) but eventually I discovered the div tag needed a height property to work the way I wanted it to.

With the editor now displaying, I really wanted to get my rig to run the code and display the result so I knew everything was working. This prompted the executing code post a couple of days ago (now you know the project I was hinting at). With a working execute function that returned the output as a string I just needed to get the text out of Ace using the method provided and back to the server.

I originally tried to do this without using any other JavaScript libraries but soon realised this was adding a lot of unnecessary and distracting script to the web page so went back to using the ever useful jQuery library to deal with the post and the updating of the results div. Surprisingly it worked on the first go.

The code (in BitBucket due to the length) uses all of the executing arbitrary code lines from the previous post but with the test now replaced with a bottle web server and a 35 line single page web app. I mixed CSS and JavaScript in with the HTML to simplify the server side code not because I recommend this or would do this in reality.

Running the code should give you an editor similar to the one below.

pyedit

Editing Python in the Cloud

A bit off topic but I gave in to the urge and brought a Chromebook as something to get on to the web as quick as possible but with a proper keyboard. I’m writing this article on it so at least it passes the basic test. This made me wonder if it would be possible to edit and run Python on it.

The short answer is yes, Python Fiddle does allow you to edit and run Python code directly from the browser. Most of the screen is taken with the file editor with a run button at the top and the output appears at the bottom. Simple but effective.

Going further, actually downloading Python to run it locally is theoretically possible (see here) but this goes against what a Chromebook is supposed to be about.

If it’s editing that you are mainly looking for, there are good editors and in fact whole IDE’s are available. Take a look here for 5 IDE’s that can probably do all would expect. I don’t intend to try all of them but will let you know any I do.

But what about running your Python code? I am giving Nitrous.io a go. This gives you access to a virtual server with Python 2.7 preinstalled. And they’ve preinstalled django which gives you an idea of what they think there product is going to be used for the most. The most basic option is free with paid-for upgrades if you want more processors, RAM or hard disk. It runs on Linux so again none of the Windows code will run but it does open up a few possibilities.

So lets get up and running as quick as possible with the default website in django as described in the very first tutorial. I’ll assume you have registered for your free account and connected to your box. This brings you to a command line where you type in the following.

cd workspace
django-admin.py startproject testsite
cd testsite
python mange.py runserver 0.0.0.0:8080

Now if you click on the Preview menu along the top and choose Port 8080 it should open up the barebones site in your browser.