Installing Ubuntu Server 14.04 LTS on Mac Mini

If you have ever tried installing Ubuntu Server on a Mac Mini it’s likely that after installation you encountered  a flashing folder icon with a question mark on the screen. Below are the steps to fix it.

  1. Boot into your installation USB by holding ALT key when the Mac Mini is starting.
  2. Press C when the GRUB menu appears to start a console.
  3. Now you have to find the partition that contains a boot directory.
  4. Set this as your root for further commands.
  5. The only way to boot properly is to use the UUID of the drive. Get it like this:
  6. Note down the UUID. You’ll have to type it manually in the next step.
  7. Set initial RAM disk and start booting.
  8. Your system should start normally now so log in and run the following commands:
  9. This will list something like the output below:
  10. Change the boot order to start Ubuntu first and reboot.
  11. Your Mac Mini should be able to start Ubuntu Server now.

Django LDAP (Open Directory) integration

LDAP

LDAP (Lightweight Directory Access Protocol) is an open,  industry standard protocol for accessing and maintaining directory. A directory contains objects like users, groups, computers, printers, company structure (or basically whatever you like), on which you can perform queries to add, update or delete content. LDAP works over an Internet Protocol (IP). Directory services play an important role in developing intranet and Internet applications.

There are multiple LDAP implementations on the market such as Microsoft Active Directory, Apple Open Directory, OpenLDAP and  much more. In this article I will focus on integration between Django and Apple Open Directory. More specifically on authentication in Django with users defined in Open Directory.

Setting up Open Directory

To set up Apple Open Directory you will need an OS X Server instance. You can install it from an apple App Store.

  1. Launch App Store and search for “OS X server” to purchase and download the Server App. If you are enrolled with Apple Developer Program you can get the Server for free from https://developer.apple.com/osx/download/.
  2. Open server app and chose the Mac to manage.
    Chose a Mac to manage with Server
  3. You will see the screen indicating installation progress.
    OS X installation process
  4. When the installation is finished you should see the screen with general options for your server.OS X Server first screen
  5. Now click on the Open Directory tab in left menu and activate the service by clicking the button in right upper corner of the window.
    Screen Shot 2016-01-02 at 19.20.47
  6. You will be asked to create a new Open Directory instance, or join an existing domain. Choose to create a new domain.Screen Shot 2016-01-02 at 19.22.15
  7. Choose the name, account name and password for directory administrator and click next. On the next screen fill the organization name and administrator’s email address.
    Screen Shot 2016-01-02 at 19.22.36
    Screen Shot 2016-01-02 at 19.23.06
  8. On the next confirmation screen click “Set Up”. After a while your open directory server should be up and running. The last thing to do is to create a user that we will use in Django App.
    Screen Shot 2016-01-02 at 19.24.19
  9. Go to the Users tab and click the plus button in the bottom to add new user. Select “Local Network Directory” as directory and fill the rest of the fields as you like. Click “Create”.Screen Shot 2016-01-02 at 19.25.13

Adding LDAP support in Django

I assume that you are already familiar with at least basics of Django. You should know how to setup a virtualenv and how to install packages with PIP.

There is no need to write LDAP authentication backend as such package already exists and it is called django-auth-ldap. Under Python 2, it requires python-ldap 2.0 and under Python 3, it uses pyldap.

To install django-auth-ldap activate your virtualenv in a terminal and type:

In case you don’t have an existing Django project yet, please start a new project by typing:

Switch to newly created directory and then app directory. Open settings.py file and add the following lines:

This will setup LDAP as authentication backend. To use Django built-in permission system we also add ModelBackend which comes with default Django auth module.The line with AUTH_LDAP_SERVER_URI will tell the script which server it should connect to perform authentication. AUTH_LDAP_USER_DN_TEMPLATE defines the query used to search for users in LDAP server. The part with dc=os-x,dc=shared should contain your server hostname splitted by a dot. In my case this is os-x.shared, default hostname when installing Apple Open Directory.

We would also like to map user attributes and groups from Open Directory server to our Django application. To do this we have define the proper mappings. The code fragment below shows how to map user first name, last name and email address. The keys in the dictionary are Django user model fields, and the values represent keys from the LDAP server.

To use groups we have to define a query that will be used to look up for groups in LDAP server. We do it similarly to users query.

Additionally we set the group mirroring to True and define the group type as posix. With other LDAP implementations such as Active Directory, different group type should be used. As the last thing we map the groups from LDAP to user model flags in Django. The below setup will give all the users from LDAP server administrators privileges in our application. Workgroup is the default group for users in Open Directory.

If everything went right you should be able to log in to Django admin with the user credentials from LDAP server. Feel free to play around with the config if you want to customise it. Please also check the django-auth-ldap website for detailed documentation. It isn’t great but still covers a lot more topics than this article.

If you want deeper look into a code please check the this Github repository. I have put there the sample project using LDAP authentication.

Encoding issues with Django Rest Framework and Android Volley Library

Recently I ran into a problem with encoding when I was trying to get a well-known Volley library to work with a Django Rest Framework. While making an API request with Volley I noticed that returning data is not properly encoded. I wanted to use class StringRequest to obtain response as it wasn’t clean JSON format. Sample code of StringRequest is presented below.

To make it work correctly with UTF-8 responses I extended the StringRequest class and modified  parseNetworkResponse method. Modified code is right here below.

I hope if anyone else has issues with that, this simple tutorial will help.

 

XenServer on MacMini

We were recently setting up CloudStack in our office for training and testing with the cloud infrastructure and we used Citrix XenServer as our hypervisors. To our pleasant surprise installation on MacMinis went on without any problems. However during the first boot we were presented with black screen and flashing file folder icon with a question mark in the middle.

After some research we’ve found a simple solution to this problem. Here’s what to do.

  1. Start your MacMini from Recovery by holding down the Command and R keys at startup.
  2. Open Terminal window by selecting Utilities menu and then Terminal.
  3. Type bless –device /dev/disk0s1 –setBoot –legacy and press enter.
  4. Restart your computer i.e.  type shutdown -r now.

Although ridiculously, the bless command gets the job done, so bless your discs and use XenServer on Mac.

Autoboxing

Android memory optimisation is often a demanding task, especially for novice developers. Autoboxing is a topic that often goes unnoticed when talking about performance issues. However, you should keep it in mind, and make use of tools and structures that Android SDK provides.

Let me remind you all what autoboxing really is:

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types (int, double, bool) and their corresponding Java classes (Integer, Double, Boolean). Reverse conversion is called unboxing. For example:

Memory usage by each primitive types are as follows:

These types are great for matching their hardware definitions, but they can’t be used with generics collections (see this stackoverflow.com topic if you are interested why). Instead Java language provides corresponding classes that match primitives types, and act as object wrappers. These classes give you the same functionalities as primitives but can be used with generic collections. However their size in memory is not the same. For example Integer class holds 16 bytes in memory, rather than 4 for primitive. It also requires more overhead to access the underlying value.

It can be especially painful with data structures like HashMap. Consider following code:

Operations put and get will use autoboxing and cause additional load. Basically anytime you insert , update or retrieve a value with this generic container when an primitive is involved you end up boxing or unboxing values. This the exact reason why Android Platform provided custom containers like SparseArrays to use instead of traditional containers like HashMap. These containers were design specifically to fight autoboxing problem and eliminate both runtime overhead and additional memory allocation.  Look up below.

This way you make your code more optimised and memory efficient.

To look for places in code where autoboxing could be causing a problem use Android Studio tools like AllocationTracker and TraceView. More about finding memory issues soon.