Friday, September 16, 2011

Dlink DNS320 NAS as SVN Server

Continuing my previous post about Dlink DNS320 NAS, I would like to share about making this NAS as SVN Server. I am still amazed by this NAS and how handy and quite powerful it becomes, because I have Debian Squeeze installed and running on it. Once I have Debian Squeeze installed I could installed anything I need from the repositories. Please read my previous post on how to install it on this NAS.

Here is how I setup this NAS to become svn server.

Create svn repos directory:


# apt-get update
# apt-get install subversion


# mkdir -p /var/svn/

# svnadmin create /var/svn/projectalpha


Add svn user and group:
# adduser --system --group --shell /usr/sbin/nologin --disabled-login svn

Set the appropriate ownership for the repository directory:
# chown -R svn.svn /var/svn

Add exisiting user to svn group so they will have access to the repo directory:
# useradd -G svn arsya

Now set up an ssh server, clients will connect to this machine using ssh:
Set up ssh server if does not exist yet:
# apt-get install openssh-server

Perform the following command to test via svn+ssh protocol:
$ svn co svn+ssh://username@hostname/var/svn/projectalpha

Now, what if you have your ssh server running on custom port? How can you tell your svn client to use it?

If you are using subversion, the one the you (apt-get) installed earlier, simply update your subversion configuration file in ~/.subversion/config:
[tunnels]
sshtunnel = ssh -p


If you are using tortoisesvn client in windows, then you need to go to TortoiseSVN->Settings->Network and set the SSH client to:
C:\Program Files\TortoiseSVN\bin\TortoisePlink.exe -P

Note that I do not need to install apache2 web server, since svn+ssh is enough for me to use. Also it would save some memory usage in this limited memory NAS.

Thursday, September 15, 2011

DLink DNS320 NAS with Extended Services

DLink DNS320 NAS is quite awesome, because it is quite affordable and one can hack it to make it as even more useful than what it already is.

This NAS sports a Marvell Kirkwood processor with 128MB RAM, here is technical details of it:

CPU800 MHz Marvell 88F6281 (Kirkwood)
RAM128 MB
USB1 USB2.0 Port (front)
LANMarvell 88E1118R-NNC1


To get more info please go here.

Looking at that spec, one can get tempted to figure out a way to turn it into not just a Storage Server. Fortunately there is a way to extend the functionality by using fun_plug. Fun_plug makes installing other services so easy. Here and here contains more detail about setting it up in DNS320. Please install fun_plug (ffp) before installing Debian Squeeze. Make sure you use this ffp script for DNS320, otherwise you will receive errors.

For me it is not enough, since I know that there is a way to install Debian Squeeze in it and chroot it automagically. Now this is amazing, because you have a full blown debian linux distro running inside your NAS.
What you need is to download Debian Squeeze here. Then store it in the root of your nas, normally called Volume_1. Follow the readme inside the archive.

Once Debian Squeeze installed, you can install ntp daemon, web server, svn server, download manager, and other stuff. The only limitation is the size of RAM.

Oh, I need to inform you that installing fun_plug and Debian Squeeze is totally safe and reversible, because no ROM Flashing involved. So, what are you waiting for?!




Django ORM DB Transactions to Speed Up Bulk Insert

Using Django means you are bounded to Django ORM. Of course one can use other DB ORM, but then loose many neat features in django, such as admin interface and model forms. Lots of tinkering around required when not using Django ORM.
I had a problem when inserting many data using Django ORM. The performance was unacceptably slow. Just to insert 30 data requires more than 10 seconds. How about inserting 50, 100 or more. That is because each time I called save method in the model, Django ORM will execute and commit it immediately, and resulted in performance issue.
Luckily there is a a way to speed up large number of data insert. Django provides a way to manage database transaction, and the way to use it is pretty simple. What we need is TransactionMiddleware. Autocommit is used by default by the transaction manager. In order to speed things up, we need to change it to commit only on succes, simply by using commit_on_succes decorator for the related view.

Now, you need to have TransactionMiddleware registered in settings.py, so it will look like the following:

MIDDLEWARE_CLASSES = (
...
    'django.middleware.transaction.TransactionMiddleware',
...
)


After that, you can use it in your views.py like the following:

from django.db import transaction

@transaction.commit_on_success
def viewfunc(request):
    # ...
    # this code executes inside a transaction
    # ...


By doing that, if the function returns successfully, then Django will commit all work done within the function at that point. If the function raises an exception, though, Django will roll back the transaction. All of those means faster performance.

Please refer to here for more details about django transaction management.