Tuesday, January 12, 2016

Beaglebone as simple GIT local server

While there are plenty of solution to post your code online; like Github and similar, sometimes you want to keep your code private.
GIT is a great tool to create and manage repository, used in many companies; you can easily install it on a small computer like a Beaglebone board, and have your own private server.


In my case, I used an old Beaglebone model A6 (this is the predecessor of the BeagleBone Black model, which is much more powerful). The board is small; it fits in an Altoids tin can and has enough power to run Debian with LXDE.


If you have an old computer, you can use that, but it is hard to beat the convenience of a single board computer; which require no monitor, and works just with a LAN cable and a USB charger.

The same would apply also for other single board computers; like a Raspberry Pi; I had one of these around so I did use it.

Setup the board

The first step is to install the operating system on the Beagleboard. It is pretty straight forward, the images are available on the Beagleboard site. I did use the Debian distribution.
Insert the sd card., boot the board and verify that the OS load.

The next step is to connect the board to the network; plug the ethernet cable and check on your router which IP did the board get. Once you have the IP, connect via ssh to the board. I use Teraterm on Windows; if you are on OSX or Linux, the terminal will suffice.
You may want to assign an IP in your router to your board; follow your router manual to find out how to do so.

The login for Debian is root without a password. You may want to create a new user at this point; which will be dedicated only to Git.

Now it is time to create the SSH key, pick a good password, even if you will be the only one to use this server.


ssh-keygen

The public and private keys will be saved in ~/.ssh; we will be using these in a bit.
Log out from root and log in with the newly created user, perform upgrade, update and download Git


sudo apt-get update
sudo apt-get upgrade 
sudo apt-get install git-core

Once you have Git on your machine, you can create a repository and initialize it. You can start with one, and create the others as you see fit. I am creating a "repository" folder in the home directory , with a .git folder for each repository

cd ~
mkdir repository
cd repository
mkdir repo1.git
cd repo1.git
git --bare init mkdir 

This will create the repository with no files in it.

The next step is to initialize the repository. To do so, we will create another folder, and a file in it, and then add the file to Git.


cd ~
mkdir temp_repo
cd temp_repo
echo first commit > init_me.txt
git init
git add init_me.txt

Before the commit can go trough, we need to set up some parameters for Git, to establish who is committing. add your email address and username of your choice

git config --global user.name "Yourname"
git config --global user.email email@domain.com

Now we can submit our first commit, After this, we can push the commit to the main repository to initalize it.
 
git commit -m 'initialize repo'
git remote add origin ssh://user@domain.com/~/repository/repo1.git
git push origin master

 At this point the repository is intialized. With this, we are done on the server for now, and it is time to move on the client.


 Client setup: Windows

For the client setup, there are various choices to make: you can use Windows, OSX or Linux clients; you can use a GUI or command line interface. In my case, I use Windows and Sourcetree, which is a free UI available for both OSX and Windows.

First thing to do, after install Sourcetree, is to set up the SSH key. Windows is not as straight forward as a Linux or OSX machine, so it is not enough to just cat the public key from your master, to your client autorized keys. On Windows you will need some extra software: Teraterm, Puttygen and Pageant.

Teraterm is the terminal client to connect to the server, Puttygen is used to create windows keys for SSH connection, and Pageant is used to keep the key in memory. There are other ways to do this, I did pick this way since it is straightforward and easy to implement.

First, connect to the Beaglebone via Teraterm; all that you need is to get the IP of the Beaglebone, and choose the SSH as connection type. Once done so, the connection start and you will be asked to type the username and password.
Once connected to the Beaglebone, copy the private key from the Beaglebone folder in "home/username/.ssh" to your local Windows machine. To do so, use the file menu in Teraterm, then select  "SSH SCP..."; you will see a window similar to this





Select the id_rsa and the destination, then press the "receive" button to copy the files on your Windows machine. Rename id_rsa to id_rsa.ppk
Windows sadly is not able to use the key, so we need Puttygen to create a new windows private key.
Run Puttygen and  load the id_rsa file, then push the "Generate" button and save the new key.

Last part is to load the newly created key in Pageant. Once launched, you will see an icon on the Windows toolbar in the notification area. right click on it and select "view keys". The list will be empty; press the "Add Key" button and load the new key.

Now you can use Sourcetree to clone the project from your Beaglebone, and push your commits to the Beaglebone.

In Sourcetree, select the "Clone/New" button, then add the url to the Beaglebone (using "ssh://" as protocol), replace IP and USERNAME with your Beaglebone IP and your Beaglebone username, define a destination and press "clone". This will clone the repository and you are set.




I did try with 4 clients (1 windows 2 Linux and 1 OSX) , and so far the Beaglebone is performing just fine. It is also require minimal current; and everything run out of a SD card; you can keep the board on for months without problems. You can also backup the whole repository when needed, simply attaching an external USB drive or copying directly the repository folder via network, to a NAS or other location.