Keeping NancyFX running with Supervisor

I wanted to give the pi and my smart devices some time to collect some data, so I didn’t pay too much attention to it.  When I went back to it the query only returned a few rows (~70).  My log messages that I printed to the console were still there and I didn’t see any errors, so what the hell was going on?

Naively I restarted the Nancy server and waited again, and again.  You will start to see a theme in my posts.  After about the fourth time, I realized that my ssh session was closing and with it my Nancy instance.

To solve this problem I am going to install this sweet little application called Supervisor.  This is an application very similar to the Service Control Manager in windows.

Since I am using Raspbian, I need to first update my package lists and then run the install.

sudo apt-get update
sudo apt-get install supervisor

There are a number of options to change in the configuration file, but I will only detail the changes for my use case.  FYI,  I am following the instructions that are from the Supervisor website.  My needs require me to modify the [progam] section.  There is a bunch of properties that you can modify to customize the way that Supervisor will manage your application.

For some reason, I hit a snag in the installation that would not allow me to start the Supervisor service.  It seems like the service did not install correctly.  Luckily, I found this point on serverfault that gave the solution that fixed my issue.

aptitude -o DPkg::Options::='--force-confmiss' reinstall supervisor

Once that was fixed I was able to get my configuration set.

[program:MyApp]
command=/var/www/myapp/MyApp.exe
directory=/var/www/myapp
autostart=true
autorestart=true

Now it is simply a matter of reloading the configuration and starting it up.

sudo supervisorctl
reload

At this point, your application should be up and running. If it stops or the machine reboots, Supervisor will start it up.
Pretty Simple, Right?

Installing PostgreSQL on Raspberry PI 2

Continuing with my web server project, it needs to be data driven.  For now, I am going to use PostgreSQL as my data store.  That may change, but for now it will suffice.

There are a number of things that we need to do before we can start using the database.  I used the PostgreSQL manual to configure it.

Installation

Let’s update our repository and then install PostgreSQL.

> sudo apt-get update
> sudo apt-get install postgresql postgresql-contrib

This install will create a new use postgres that does not have a password.  You can test if you can connect to the server by logging in as the postgres user and then running psql.

> sudo -i -u postgres
> psql

To exit out of the psql program you need to enter \q.

Authentication

Now that we can connect with the default user, we need to start configuring the access controls.  The access control is setup in the pg_hba.conf file.  It lists the hosts, users and database that users can connect to and from which host.  It should be in the default install directory /etc/postgresql/9.1/main.  To setup an example, lets allow access to the default Raspberry PI user: pi.  Open the file for editing with root permissions.

> sudo nano /etc/postgresql/9.1/main/pg_hba.conf

Add the following line to the end of the file:

local all pi password

What we are telling postgres to allow is local access to the user pi for all databases using password as the authentication method.  We still don’t have login rights to the database server yet.  Let’s setup the pi user with credentials, login rights and superuser permissions. These are for sake of the example. In a production environment, you would have more roles that have more specific privileges.

> create role pi password 'test';
> alter role pi login;
> alter role pi superuser;
> \du;

The last command \du; will list the roles and their permissions.  You should now see the pi user with superuser privileges.  If you omit the login assignment, you will see a message stating that the user does not have login rights.  It will not appear once you have granted the user login rights.

One final step before we can connect as the pi user.  You will need to have a database that it can connect to, so lets create a test database.

> sudo -i -u postgres
> psql
> create database test_database;
> \q;
> exit
>
> psql -d test_database

If all went well, you should see a new prompt: test_database=#

Now you are ready to start using postgres locally.  In another post I will show how to permit access to the server from a remote machine.

Installing Mono on Raspberry PI 2

I want to use my PI as a web server.  I use the SmartThings hub for my home automation and I want to customize some of the functionality.  I want to create an application that can receive the data from their cloud and display it on my site.  I hope to do some analytics and maybe create a more robust and rich rules engine, but we will see.

Anyways, to do any of that I need to get my environment setup.  I really enjoy working in c#, so I need to get Mono installed.  Getting Mono installed on the PI is pretty trivial.  You just need to add the source to the apt source list.  The Mono project website has the commands for a Debian based system.

> sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3FA7E0328081BFF6A14DA29AA6A19B38D3D831EF
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update

Now your repository is ready to install Mono.  I want to load the complete Mono suite.

> sudo apt-get install mono-complete

Once that is done, let’s check the version to make sure we have what we expected.

> mono -V

If all went well, you should see an output that looks similar to this one.

> mono -V
Mono JIT compiler version 3.12.1 (tarball Fri Mar  6 23:28:08 UTC 2015)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
	TLS:           __thread
	SIGSEGV:       normal
	Notifications: epoll
	Architecture:  armel,vfp+hard
	Disabled:      none
	Misc:          softdebug 
	LLVM:          supported, not enabled.
	GC:            sgen

Let’s create a simple console application and make sure that it compiles and runs.  In nano, lets write some code.


using System;

public class Program
{
        public static void  Main(){

        Console.WriteLine("Hello World");
        Console.ReadLine();
        }
}


Save that file as test.cs, then compile it (mcs test.cs).  If it compiled without an error, we should be able to execute the executable and see Hello World.  Now that we have our .NET environment setup, we can start adding the the environment. I want to use Postgresql as the data store for now, so that will be up next.

That was pretty painless. For kicks, I am going to reboot now.

Raspberry PI 2 Setup, Take 2

This is the second time around with my PI 2.  The first installation ran fine for about a month, but then crashed and I wasn’t able to recover it.  I am going to document the process from the start..

When you first boot, you are presented with the raspi-config utility.  This gives you the ability to change many of the internals of the PI, such as overclocking and enabling the camera.  What I need to do first is to present the microSD to the OS, so choose the Expand Filesystem option.  This happens fast, so after the success dialog I am going to change the boot option.  I don’t need the desktop, so I am going to tell the PI to boot into the command line.  Choose the Enable Boot to Desktop/Scratch option.  Choose the Console option when the dialog appears.  Now I am ready to reboot, so I am going to exit out of this tool.

Now that I have rebooted into the console, I can get started with setting it up.  The first thing I want to do is get the wifi working so I can ssh in and go back to watching tv.  You can login with the default username/ password pi/raspberry.

Setting up the wifi.

When I set this up the first time, I had a lot of trouble with the different examples that I found on the interwebs.  Until I checked with Adafruit (awesome site), which I should have started with.  Their tutorial worked the first time.  I will regurgitate their instructions just in case it 404s in the future.

You need to open up the interfaces file in an editor with root permissions.  I am going to use nano for this

sudo nano /etc/network/interfaces

In order for the OS to initialize the wifi, so you have to tell it how to do it.  You can update this file to look like this:

auto lo

iface lo inet loopback
iface eth0 inet dhcp

allow-hotplug wlan0
auto wlan0


iface wlan0 inet dhcp
        wpa-ssid "ssid"
        wpa-psk "password"

One thing that they noted (which saved me), was the SSID configuration above doesn’t work if you hide your SSID. For obvious reasons mine are hidden. To make it work with the hidden SSID, so need to make a few more changes.

auto lo

iface lo inet loopback
iface eth0 inet dhcp

auto wlan0
allow-hotplug wlan0
iface wlan0 inet dhcp
   wpa-scan-ssid 1
   wpa-ap-scan 1
   wpa-key-mgmt WPA-PSK
   wpa-proto RSN WPA
   wpa-pairwise CCMP TKIP
   wpa-group CCMP TKIP
   wpa-ssid "My Secret SSID"
   wpa-psk "My SSID PSK"

iface default inet dhcp

You should be good to go at this point.  Reboot the system (sudo reboot).  When it reboots, you can run

ifconfig

You should see something like this:

wlan0     Link encap:Ethernet  HWaddr 00:0f:60:04:0e:c6  
          inet addr:192.168.1.16  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:73422 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22161 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:109230487 (104.1 MiB)  TX bytes:1973384 (1.8 MiB)

This gets me started so that I can start getting the server ready to serve traffic.

Next up, getting Mono installed with a sample http server.