Aside #3 – HDMI-CEC Awesomeness

Whilst I’ve been attempting to learn a new thing each month this year, I’ve been finding it really tricky to keep to the pretty loose schedule. As such, I though I’d try and note down every time a shiny new thing takes my interest, so that I have some idea why I’m incapable of completing a series of blog posts.

HDMI CEC

I’m investigating writing apps that implement HDMI CEC protocol and can therefore be manipulated via a TV remote control only. Or an API to handle basic stuff.

HDMI pins
It looks like HDMI CEC is on pins 13 and 17 (?) of an HDMI male connector

The idea that you can plug an HDMI cable into your HDMI-CEC capable TV and not have to use a keyboard to interact with the device is fantastic for usability!

Instigator
— HDMI CEC on XBMC and BerryBoot

Aside #2 – RPi + VirtualBox = :(

Whilst I’ve been attempting to learn a new thing each month this year, I’ve been finding it really tricky to keep to the pretty loose schedule. As such, I though I’d try and note down every time a shiny new thing takes my interest, so that I have some idea why I’m incapable of completing a series of blog posts.

Running Pi distros in VirtualBox

Apparently you can run raspberryPi distros in virtualbox (totally doesn’t work though..)

Converting a .img to a .vdi:

"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" convertfromraw -format VDI path:\to\input.img path:\to\output.vdi

Instigator
— trying to get decent screenshots of the various pi OS without having to take pics of the tv

Aside #1 – EC2 WordPress Issues

Whilst I’ve been attempting to learn a new thing each month this year, I’ve been finding it really tricky to keep to the pretty loose schedule. As such, I though I’d try and note down every time a shiny new thing takes my interest, so that I have some idea why I’m incapable of completing a series of blog posts.

EC2 Issues

I’m really having problems with EC2 these days, and I’m constantly being dragged back into finding out what the problem(s) is/are..

Restarts don’t restart

Had to add entries into the rc.local to restart httpd and mysql, and also had to change the permissions on the file to allow it to be executed upon restart (sudo chmod 6755 /etc/rc.d/rc.local).

Instigator
— because the blog went down a couple of times and I didn’t notice!

Backups kill the site!

WordPress 2 DropBox – killed me. As does mysqldump in general.

DB backup

mysqldump --add-drop-table -u <username> -p <database> | bzip2 -czs > <db backup filename>.bz2

website backup

tar -cjf <site backup filename>.bz2 /var/www

Instigator
— wordpress kept telling me to upgrade to the latest version, but also had a big MAKE SURE YOU BACKUP FIRST warning; and the WordPress 2 DropBox plugin killed my EC2 microinstance.

WordPress: Alerting on high CPU usage

#!/bin/sh
#
# Script to check CPU usage and tweet me if it's over a threshold
#
if [ ` uptime | awk '{ print$10 }' | cut -d. -f1 ` -gt 50 ];
then
    sudo python /home/ec2-user/tweepy/ec2Event.py '@rposbo LOAD: ' < /tmp/load
else
    echo lo
fi

This totally doesn’t work! Uptime seems to happily report low CPU usage even when the site is dying. Maybe CPU is low and mem is high.. hmm.. may have to change that check.

Instigator
— attempting to backup wordpress keeps breaking my site, dammit!

logstash, graphite, statsd

Currently trying to get those three to work together and help me find out why the EC2 microinstance is so.. uh.. micro..

Instigator
— EC2 just keeps on dying and I need to find out why

Asides

Ooh! Shiny!!
Whilst I’ve been attempting to learn a new thing each month this year, I’ve been finding it really tricky – except for January – to keep to the pretty loose schedule.

And I don’t even have a job to take my time up at the moment!

As such, I though I’d try and note down every time a shiny new thing takes my interest, so that I have some idea why I’m incapable of completing a series of blog posts.

They’ll be short and sweet and will probably take the form:


Thing I looked at

Bit more detail

Instigator
— reason I looked at it.


Let’s see how it goes

DevOpsDays

My thoughts from the recent #devopsdays conference in London; notes, inspiration, todos, ahas, and OMGs.

(Since my handwriting was so poor on the first day and my name badge was illegible, this was the badge for day #2!)
@rposbo name badge

Wow. There’s a lot to learn about this whole DevOps movement, but I actually feel that I can contribute to it having a pretty broad range of experience; I’m mainly development, but over the past decade or so have turned my hand to basic sysadmin-ing, DBA-ing, QA-ing, BA-ing, dev managing – almost all of the areas that need to be covered. I like to think of myself and a catalyst for implementation 🙂

I had initially been concerned that I lacked depth in skills like virtualisation and automation, but I’ve realise that those systems like Puppet, Chef, and Vagrant are potential solutions to a problem of configuration management (CM) and automation. Understanding the need for these tools is half the battle; being a specialist in them isn’t completely necessary, but an understanding to a level of basic implementation would be useful.

I’m already enrolled on a bunch of puppet webinars so will be getting stuck into that more soon.

Even though I’m pretty new to this, chatting with some key people did validate my thoughts that people shouldn’t be referring to “devops” as a person or a role in their presentations instead of an approach/framework/culture.

Intro

What happened at #devopsdays?

After being drawn in to the Basho booth by a raspberrypi/arduino/riak mars-rover-esque robot and chatting with John Clapham, who I didn’t realise was about to do the second presentation of the day (!), I end up deciding to sit right at the front (like a swot).

This turned out to be a great move as I ended up chatting with the organisers and even one of the founders of the DevOps movement, Gene Kim. Really nice guy, looks exactly like his twitter avatar (unlike some of us.. ahem..), and he even took a picture of the two of us together and let me know how to get onto the book review group for the long awaited DevOps Cookbook – bloody nice chap.

Chris Little, from BMC, is a really interesting guy to talk to as well – gave me a great overview of the background to DevOps, its “founders”, and helped me understand both its role in a company and in the future of the industry.

A pic Chris took from the stage
A pic Chris took from the stage
Try to play Where’s Wally and find lil old meeee…!

Presentations

Each day there were four main presentations to start with, then some “ignite” talks.

I’m not going to try to go into the details of these presentations as 1) I’m lazy, 2) I tried to take notes on my phone and the battery died, and 3) other people have done it much better than I would have anyway!

Essentially, I found several of the presentations difficult to really grasp as I felt I was lacking a frame of reference. Perhaps this was just me not having enough of a Ops background, but I felt that the presentations could have benefited from a slide or two at the start laying the foundation for the remainder of the talk.

I was very glad to see at least one full presentation being completely non-technical, and instead focussing on the culture side of DevOps.

OpenSpaces

The afternoon of each day was put aside to OpenSpaces: everyone has the opportunity to propose a discussion topic, pop it on a post-it, people who want to vote can take a marker and add a dot on the post-its that they’re interested in. Those with the most dots get allocated a room and a time, and the discussions commence.

I initially thought this was a bit of a cop-out: I paid to attend a conference and half of it is the other people who paid talking to each other?! Rip off!

However, the discussions that I chose to attend helped me understand much more about monitoring, logging, database CI, puppet/chef/CF Engine/vagrant basics than I could have got from the main presentations, so I’m a convert.

Summary

DevOps is an exciting opportunity to technically innovate around CI, automation, delivery pipelines etc, and also to work with the business to introduce concepts like Impact Mapping.

This is where I’ll be focussing my professional efforts for the near future. I think there’s a lot of potential to help relieve stress on development teams and operations teams and relieve frustration from the business teams that define the work.

Year of 101s: March – RaspberryPi

Part #1 – Intro

I’m committing to doing 12 months of “101”s; posts and projects themed at beginning something new (or reasonably new) to me. January was all about node development awesomeness. February was (sort of) all about Samsung Smart TV apps.

March is going to be about me playing with the raspberryPi!

So what’s a RaspberryPi?

RaspberryPi
The Raspberry Pi is a credit-card sized, single-board, low cost, barebones computer, intented to be mainly used within the classroom as a cheap way to teach programming.

There are more than one version of the Pi and I’ll be referring to Model B during these posts, as it’s what I have.

Hardware

A bare printed circuit board (PCB) contains:

  • a double USB port,
  • HDMI and composite video outputs,
  • an ethernet port,
  • SD slot,
  • headphone jack,
  • and a micro usb sized power socket (so chargers for most modern android or blackberry phones will work, as will a USB to MicroUSB cable plugged into a PC or even your TV)

It’s pretty low-power as it’s just got:

  • 512MB RAM,
  • 32-bit 700MHz ARM processor,
  • but – a GPU capable of decoding 1080p HD video!

Software

Since the Pi runs from the OS loaded on to the SD card, then it’s up to your level of hackery skill to determine what software you’re going to run. Check out the wiki page to see what OSs are being, or have already been, ported to run.

The most popular ones are most likely those recommended by the fine people at raspberrypi.org, namely: a version of Debian (Raspbian), RISC OS, or a version of Arch Linux.

Raspbian is easy to pick up if you’ve ever played with Linux before.

Uses

Media centre
There’s a fantastic port of XBMC called Raspbmc which allows you to easily make a Media Centre PC (I’ll cover this in a later post).

Arcade machine
Play really old games on a really underpowered device. YES!

High Performance Computing (HPC) environment
Want to get some more power? Buy a truck of Lego and 64 raspberyPis and network them together in a HPC stylee!

Loads of others!
Srsly. (At least) One guy has sent his almost into space. Amazeballs.

It’s all up to your imagination and time; I have one but not the other so let’s see what I come up with this month..!

How do you use it?

Knowledge of either Python of C++ seems to be the main way to get into working with the device; python is the recommended language for general development most likely due to it reading like a high level language and not needing compilation; just edit, save, execute.

Next up

Setting up the thing; getting an OS on it, getting a bluetooth keyboard working, setting up SSH – that sort of thing.

Smart TV 101: Wrap up

Year of 101s, Part 2 – Smart TV February

Summary – What was it all about?

February was my 101 on developing for the Samsung Smart TV; a bit of a random subject n the first place and I also managed to get quite off track by the end after a hiatus in the middle.

Part #1 – Intro

I started with an intro to what Smart TVs are.

Part #2 – App Development

Second was an overview of what apps are, how they’re developed and then got into developing a basic app.

Part #3 – Deploying Apps

Next I did a post about deploying the apps to your tv for testing

I had intended to give a detailed article on developing these apps since I had spent a lot of time in January researching these posts and couldn’t find a decent article anywhere containing this info.

However, during the writing of my second or third post I found a well hidden but utterly perfect article covering everything I had planned to write about; my post would have ended up being a reproduction of that article which is a waste of everyone’s time and not very nice for the author of the original article!

The more useful resources are:

As such I had to think of something still related to Smart TV apps, but also interesting and different enough to be worth writing.

This is where the plan to do without the IDE came in and I tried to dissect the process and implement it manually.

Part #4 – Creating Packages without the SDK

I finally attempted to do without, Apache (done), generate the package (uh.. not quite), and scrap Eclipse (no dice).

What I gained from this was more headaches related to node’s async fun, and also opened up a few other avenues for future development; essentially I’ll be able to link Jan’s 101, Feb’s 101, and also March’s 101 all together!

Summary

Once I realised that Smart TV apps were just webpages, the creation of apps become kinda boring for a blog series. Deploying apps was still quite interesting, so I liked that one. The detail of creating an app was covered wonderfully in the other articles I found, so no point repeating that stuff.

A few things I discovered that weren’t really related; if you start your node server on port 80 and get a failure related to “ENV” and “process” that looks like it couldn’t access the port and you’re not sure what process is stealing that port, try [code]netstat -anbo | findstr :80[/code]

Next Up

Hopefully March will be a more fruitful month – I’ll be getting stuck into a tasty slice of raspberryPi!

Smart TV 101 : Part #4 – Creating packages without the SDK

I’m committing to doing 12 months of “101″s; posts and projects themed at beginning something new (or reasonably new) to me. January was all about node development awesomeness. February is all about Smart TV apps.

To IDE or not to IDE

I’ve mentioned how I’m not the greatest fan of Eclipse, so working on a development method that doesn’t rely on it intrigued me.

Given that all the Smart TV apps consist of are pretty standard web pages, then surely it’s possible to do this without the integrated IDE and webserver?

Starting at the end and working backwards:

Web server

The SDK bundles Apache for serving the apps. I don’t really have any problem with Apache; it’s currently the most commonly used web server  on the interwebs, free, and stable. I just don’t see why I’d need it to serve up an XML page and some zip files!

Looking into the contents of the widgetlist.xml from previous posts we can see that it’s just listing the contents of a Widget subdirectory. That should be easy enough to manage ourselves. I’ve decided to dive back into nodejs for a lightweight alternative.

The code I’ve used is the same as that from most of January. The one that I’ve changed is requestHandlers.js to serve the listing xml and the zip files:

requestHandlers.js[js]var fs = require(‘fs’);

// build the full xml file
function widgetlist(response, notused, request) {
console.log("Request handler ‘widgetlist’ was called");
var packageDir = "packages";

BuildPackageXml(__dirname, packageDir, request, function(packageXml){
var content = ‘<?xml version="1.0" encoding="UTF-8" standalone="no"?>\r\n<rsp stat=\’ok\’>\r\n<list>\r\n’ + packageXml + ‘\r\n</list>\r\n</rsp>’;
var headers = {
"Content-Type": "application/xml",
"Content-Length": content.length
};
response.writeHead(200, headers);
response.end(content);
});
}

// build the xml for each package, getting the stats for each zip
function BuildPackageXml(directory, packageDir, request, callback){
var filesData =”;
var host = request.headers.host;

fs.readdir(‘packages’, function(err, files){
files.forEach(function(file){
console.log(‘found: ‘+ file);
var stats = fs.statSync(directory + ‘\\’ + packageDir + ‘\\’ + file)
filesData += ‘<widget id="’ + file + ‘">\r\n’ +
‘<title>’ + file + ‘</title>\r\n’ +
‘<compression size="’ + stats.size + ‘" type="zip" />\r\n’ +
‘<description>’ + file + ‘</description>\r\n’ +
‘<download>http://’ + host + ‘/Widget/’ + file + ‘</download>\r\n’+
‘</widget>’;
});
callback(filesData);
});
}

// serve the zip file
function widget(response, path) {
console.log("Request handler ‘widget’ was called for " + path);

var packageDir = "packages";
var packagepath = __dirname + ‘\\’ + packageDir + ‘\\’ + path.split(‘/’)[2];
var widget = fs.readFile(packagepath, ‘binary’, function(err, data){

var headers = {
"Content-Type": "application/zip",
"Content-Length": data.length // to avoid the "chunked data" response
};

response.writeHead(200, headers);
response.end(data,’binary’);
});
}

exports.widgetlist = widgetlist;
exports.widget = widget;[/js]

This will give us the same XML and also serve the Zip files; they don’t even need to be in the Widgets subdirectory since we’ve implemented basic routing here.

widgetlist_xml

 

Problems encountered

  • my general inability to comprehend node’s inherently async implementation caused me much confusion throughout this development
  • xml generation node modules over-complicates what is a very basic file; hence why I went for inline
  • getting the content-length header is important if you want to avoid the "content chunked" response in your http request for the zip file; the smart tv isn’t so smart in this scenario.

 

Generating the packages

Moving backwards another stage we get to the generation of the zip files themselves. This should be easy, but again I over-complicated things by trying to implement js-zip using node-zip to recursively traverse the directory containing my work files. Async recursive archive creation was a bad idea for a Sunday evening so I should have instead opted for firing a command line call to the OS’s built-in archive-er.

Luckily my code had at least abstracted this functionality out so I could easily replace it with another implementation. The code in my git repo uses this implementation, which appears to create the archive, but that file is apparently corrupt/invalid; patches/forks/pull requests welcome!

pack.js
[js]var fs = require(‘fs’);

// main function – loop through the root package dir and create one archive per sub directory
// (assumption is that each sub dir contains one entire project)
function createPackages(rootDirectory)
{
fs.readdir(rootDirectory, function(err, files)
{
files.forEach(function(item){
if (item.indexOf(‘.’) != 0)
{
var file = rootDirectory + ‘\\’ + item;
fs.stat(file, function(err,stats){
if (stats.isDirectory()){
console.log(‘** PACKAGE **\n’ + item);
createPackage(item, file, rootDirectory);
}
});
}
});
});
}

// create each zipped archive
function createPackage(packageName, path, rootPath)
{
console.log(‘* PACKING ‘ + packageName);
var zip = new require(‘node-zip’)();

var archive = zipMe(path, zip);
console.log(‘** ARCHIVING’)
var content = archive.generate({base64:false,compression:’DEFLATE’});

fs.writeFileSync(rootPath + ‘\\’ + packageName + ‘.zip’, content);
console.log(‘saved as ‘ + rootPath + ‘\\’ + packageName + ‘.zip’);
}

// recursive function to either add a file to the current archive or recurse into the sub directory
function zipMe(currentDirectory, zip)
{
console.log(‘looking at: ‘ + currentDirectory);
var dir = zip.folder(currentDirectory);

var files = fs.readdirSync(currentDirectory)

files.forEach(function(item){
if (item.indexOf(‘.’) != 0)
{
var file = currentDirectory + ‘\\’ + item;
var stats = fs.statSync(file);
if (stats.isDirectory())
{
console.log(‘directory; recursing..’)
return zipMe(file, dir);
}
else
{
console.log(‘file; adding..’)
dir.file(file, fs.readFileSync(file,’utf8′));
}
}
});

return dir
}

exports.createPackages = createPackages;[/js]

 

Using a different IDE

This is slightly more difficult; the SDK creates a bunch of files automatically (.widgetinfo, .metadata, that sort of thing). This does add an extra manual step, but isn’t impossible.

One thing I couldn’t actually get around is the debugging and testing locally; the commands being passed to the emulator aren’t easy to manipulate. When you choose to run the emulator from within Eclipse the only parameter passed is something which tells it you’re running it from Eclipse; nothing handy like a path or filename, dammit!

 

Summary

I realise I went off on a tangent in this post and I’ll explain more in the next one. However, we’re now at a point where we can save our project files *somewhere* (locally, on the LAN, on the interwebs – so long as you have the IP on their location) and spin up a nodejs script to serve them upon request to our TV.

The code from this post is over on github here https://github.com/rposbo/basic-smart-tv-app-server

 

Next up

Conclusion of February – why I had that huge gap in the middle, and why it went off on a massive tangent!

Smart TV 101 : Part #3 – Deploying to TV

I’m committing to doing 12 months of “101”s; posts and projects themed at beginning something new (or reasonably new) to me. January was all about node development awesomeness. February is all about Smart TV apps.

Deploying to a TV

Now that we’ve got a basic Smart TV app this post will investigate how to get that app on to the TV itself.

Packaging using the IDE

During the initial installation of the IDE you will have been asked to installed Apache; this is what it’s all been leading up to! You actually just need a web server on your home network somewhere; doesn’t have to be apache, doesn’t have to be on your developer pc.

Prerequisites

Make sure you’ve configured your Server settings within the IDE preferences:
samsung-packaging-server-prefs
samsung-packaging-server-prefs-root

The packaging process will drop a zip file into a Widget/ subdirectory of this directory.

Initiating package creation

As for actually creating the package, if you’re using the Eclipse IDE then you’re spoiled for choice: highlight the project in your project explorer and then either

  1. Click the Samsung App Packaging button
    samsung-app-packaging-1
  2. Click the Samsung Smart TV SDK menu, then click App Packaging
    samsung-smart-sdk-packaging-menu
  3. Right click the project in project explorer, Samsung Smart TV SDK, Packaging
    samsung-sdk-packaging-context-menu

Whichever you do you’ll end up with the same results:
samsung-packaging-dialog
samsung-packaging-confirmation

Results

Assuming you’ve set up the server settings in your preferences then you’ll end up with:

  1. a zip file placed within the SDK installation’s Package/ directory
    samsung-package-sdk-dir
  2. the same zip file placed in a Widget/ subdirectory on your configured server
    samsung-package-widget-dir
  3. a new (or updated) widgetlist.xml file in the root of your configured server’s directory

    samsung-widgetlist-xml

Make sure that you can browse to this file and that Apache is running by opening a browser and putting in http://<your development pc’s IP>/widgetlist.xml

Anatomy of a package and a widgetlist

So what is a package made of? Looking at the image above for the the zip file that’s created you’ll see that it looks almost identical to the contents of your application within the workspace:
samsung-app-workspace

So essentially the packaging step is zipping up your project directory, putting it into a specified web server subdirectory, and updating an XML file. Obviously, you shouldn’t need an IDE or SDK to do this sort of thing and I’ll be getting on to this development & deployment process without using Eclipse or installing Apache in a later post.

Deploying!!

Now that we have a package it’s time to load it on to your Smart TV. For this post I’ll be talking about deploying from the development pc via your home network, and in a later post will be talking about loading in packages externally.

TV setup

Make sure your TV is connected to your network and that your development pc’s Windows Firewall is off (or at least configured to allow local network traffic).

  • Turn on the TV
  • Go to your app hub/Smart Hub
  • Press the Login button
  • Create an account using the username “develop” and set a password

developer account

After you’ve successfully created the develop user you need to

  • Open the Settings menu
  • Open the new Development sub menu
  • Choose Setting server IP and enter the IP of your development PC
  • Choose User application synchronisation to check the apps that are listed in widgetlist.xml and install (or update) them all

download dev app

You should now find your application on the App Hub screen with a little red “user” banner over it; select it to run it, just like any other app.

asos-app-running-on-smart-tv-1