Month: January 2016

CanaryMod as Virtual Machine in Azure

CanaryMod is a wrapper around MineCraft which provides an extension framework. The framework provides a stable API as well as extended permission and group management. The extensions in Minecraft/CanaryMod is called mods and my reason to use CanaryMod was to make use of two mods called ScriptCraft and RaspberryJuice. These two mods make it easy to run scripts or make your own mods in the supported scripting lanuage. ScriptCraft supports JavaScript and RaspberryJuice supports python using the same API as the MineCraft PI version.

Creating the Server in Azure
Azure is Microsofts cloud platform containing different services for running applications, and collecting/visualizing data. In this case we will only use it as a server host running our virtual machine. In Azure it is possible to choose among different OS templates published by companies or open source community. First thought was to use an Ubuntu OS template, and then install java, open up ports, create start/stop scripts etc. However, since Microsoft bought Mojang (creator of Minecraft), they are providing a Minecraft Server themselves using Ubuntu (quite a change in business strategy nowadays – signed Satya Nadella). So at second thought, let’s use this preconfigured server and just reconfigure it for CanaryMod to save time!

Step by step creating the server:

First create or use an existing account at

Search for the Minecraft Server published by Microsoft:

Create a virtual machine instance in the Azure cloud containing the Minecraft server. Choose a hostname (hint: use some uncommon name otherwise you will end up with a name containing your name and some long id provided by microsoft). Choose a username and password for your user at the server (hint: choose an uncommon username to make it harder to guess). We will use ssh later on to connect to the server and then we will make use of the username/password.

The final step is to press create a wait for like 5-10 minutes while the server is created:

Finally the server is created and running:

In Microsoft Azure it is possible to inspect the server and remap ports (endpoints). Let’s do that for the our virtual machine:

As expected there two ports open, minecraft and ssh. The ssh is using the default port and as a security step we will change it to high private port number e.g. 59592 (49152–65535) :

Try it out by using ssh (if windows use the Chocolately package manager to install ssh – see this former blog post).

> ssh -p

Now your are into your Ubuntu virtual machine  – verify that minecraft server is running by

> ps -efw | grep java

You should see something like – i.e. the running process:
/usr/bin/java -Xms1024m -Xmx1024m -jar
As a last security thing – assure that the system is updated (sudo is running as root temporary):

sudo apt-get update

Installing CanaryMod
Now we are ready to begin the installation of Canarymod!
First of all stop the process running official minecraft by calling the systemctl scripts:

> sudo systemctl stop minecraft-server

Create the Canarymod directory side by side of the official minecraft directory and make the minecraft user owner of the directory:

> cd /srv
> sudo mkdir canarymod_server
> sudo chown minecraft canarymod_server
> cd canarymod_server

Download Canarymod and run it for the first time to create all necessary files:

> sudo wget
> sudo chown minecraft CanaryMod-1.8.0-1.2.0-RC1.jar
> sudo -u minecraft java -jar CanaryMod-1.8.0-1.2.0-RC1.jar
....logging from canarymod....
> > shutdown
....logging from canarymod - save/stop server....

Confirm the End-User License Agreement (EULA) by setting it to true in the nano texteditor:

> sudo -u minecraft nano eula.txt

Restart the CanaryMod process and make your minecraft user operator (use the uuid – get it by using this link) in the command console. The command console is entered right after the server has started and preceded by ‘> >’.

sudo -u minecraft java -jar CanaryMod-1.8.0-1.2.0-RC1.jar
....logging from canarymod....
> > /op 
> > shutdown
....logging from canarymod - save/stop server....

Next step is to change the system control script ran at startup/shutdown of the virtual machine, instead of starting the official minecraft they shall start Canarymod. The system control script of minecraft is called minecraft-server.service and the only thing needed to be changed is the directory path and the name of the .jar file.

> cd /etc/systemd/system
> sudo nano minecraft-server.service
change all /srv/minecraft_server to /srv/canarymod_server
change the .jar to CanaryMod-1.8.0-1.2.0-RC1.jar
remove Alias in [Install]

Once the system control script is changed, it has be verified that it does what it is supposed to do.
First make sure it is started each time the server is started by enabling it.

> sudo systemctl enable minecraft-server
> sudo shutdown -r now

Reconnect once again and verify that the process is running – see last part of  ‘Creating the server in Azure’.

Installing ScriptCraft

One of the reasons of choosing CanaryMod was to be able to run mods in javascript, to aid in constructing large buildings and as an introduction into programming. ScriptCraft is installed as a plugin of CanaryMod – made in just a few steps:

cd /srv/canarymod_server/plugins
> sudo wget
> sudo chown minecraft scriptcraft.jar

Verify that it is installed correctly by trying the simplest possible javascript code in the console window.

> sudo systemctl stop minecraft-server
> sudo -u minecraft java -jar CanaryMod-1.8.0-1.2.0-RC1.jar
....logging from canarymod....
> js 1+1
.... canarymod is logging '2' as a result ....

Trying it all out
Ready for prime time? Connecting to the CanaryMod server in Azure….



Implementing a Web Solution for Tellstick Duo

A  web application for the Tellstick Duo targeting our household needs the following functions:

  • control individual switches (turn on/off lamps, and printer)
  • change schema mode (at which clock hour lamps will automatically turn/off)
  • also be usable on both android and windows phone on different screen sizes

It is all about either turning on/off switches or schema modes from a phone – best realized by using distinct and fairly large buttons. A status row is also needed to acknowledge an operation. Also the GUI should look “app-like”. Here is the end result:

In the blog post before this one, a walk through was made of the libraries needed to make a pyton web solution: cherrypy and jinja2. A little bit knowledge of html/javascript and the javascript library jquery mobile is also needed. Interaction with Tellstick Duo and setting up crontab is done using the already developed commands/libraries which are described in the former blog posts: – Using Python to Control the Telldus and – Configure Switches Using Crontab.

Below is an overview of the system design of the web application:

 html requests    +-------------------+     +--------------+
 REST API calls   |                   +---> |       |
+---------------> |    Cherrypy       |     +--------------+
                  |    Application    |
<-----------------+    python         |     +--------------+
   html           +----------+--------+---> |      |
                             ^              +--------------+
                             |   jinja2
                         | +----+ |
                         |        |
                         | +----+ |
                         |        |
                         | +----+ |

                       template page
                     html/jquery mobile/js
The cherrypy application instantiates the webserver which listens for connections on port 80. If a request is made for the main page, the template is read from flash and parsed by jinja2, in this case the schema modes and all the names of the switches are inserted into the page by jinja2. The webserver responds using the result from jinja2. Since jquery mobile is used in the main page each button is connected to a client side javascript callback. Inside the callback a REST API method call is made asynchronously to the webserver. The REST call arrives to the webserver which delegates it to a python function which in this case either calls (turn on/off switch) or (changes schema mode).

Preparing a Mobile Web Solution

All members of our household have different brands of mobile phones/OS:es, which makes developing a mobile webpage very tempting compared to doing an app. If going for a mobile webpage, you still want it to look reasonably well not too far away from an app. JQuery Mobile is javascript client-side library which fulfills this criteria. Components of JQuery mobile not only looks good, they are easy to access in the DOM-model and to setup callbacks for user interaction. There is no need to download and install the library in the Raspberry Pi instead it is possible to rely on a Content Delivery Provider (CDN). Just include a reference to the CDN on the mobile webpage and the library will automatically be downloaded when needed and stored in browser cache.

CherryPy is a full webserver including a server side framework. It was chosen because it is reasonable easy to use, still possible to add things like authentication later on. Install cherrypy on the raspberry pi by:

> pip install cherrypy

It is possible to generate a complete webpage from inside python, but it soon tends to create disorder in the code. It is easier to have static html pages on the file system and only generate dynamic content from the code. How is the static html page merged together with the dynamic content? By the use of a template engine. A template engine makes it possible to insert dynamic data into a page at specific places. How is it one? The template engine parses the page, inserts parameters or executes the python code , and generates the final page as html which sent back to the web-browser. The template engine jinja2 was picked, since jinja2 seemed to have the biggest community of the existing ones (2015). Install jinja2 on the raspberry pi by:

> pip install jinja2

A web application in CherryPy is written as a class containing several methods, and in the __main__ method, the application is instantiated together with the web server. If marked correctly, a method in the class is possible to access directly via a REST API call.