Calazan.com

Share the Knowledge
RSS icon Home icon
  • How to make images responsive by default in Bootstrap 3

    Posted on July 23rd, 2014 webmaster No comments         

    I’m currently working on a simple blog app in Django and noticed that my images are not getting resized by default when using Bootstrap 3.

    To change this behavior, simply change the img tag in bootstrap.css to this:

    
    img {
      display: inline-block;
      height: auto;
      max-width: 100%;
      border: 0;
    }
    
    

    Source: http://stackoverflow.com/questions/17932509/images-not-responsive-by-default-in-twitter-bootstrap-3

  • A Python function for flattening irregular list of lists

    Posted on July 20th, 2014 webmaster No comments         

    Here’s a nice short function for flattening irregular lists in Python that I found on Stackoverflow a few weeks back.  It returns a generator so should work fine for very big lists.

    def flatten_list(object_list):
        """
        Flattens a list of objects.
        """
        for element in object_list:
            if isinstance(element, collections.Iterable) and not \
                    isinstance(element, basestring):
                for sublist in flatten_list(element):
                    yield sublist
            else:
                yield element
    

    I tested it with lists like the following and it did the job:

    • [None]
    • [ ]
    • [[], []]
    • ['a', 'b', 'c']
    • [['a'], ['b', 'c']]
    • [['a', 'b'], ['c'], 'd']
    • ['a']
    • [['a']]
  • Converting YouTube videos to MP3

    Posted on July 4th, 2014 webmaster No comments         

    I was looking for a new Django project to start a few weeks back and I wanted it to be something I could use myself and involves running tasks in the background as I wanted to play around with Celery.  I usually use YouTube nowadays to listen to music while I’m working so I figured it might be a good idea to write a web app where a user can enter the video’s URL and it will extract the audio and convert it to MP3 format.  I thought this was a perfect project to do next, as the conversion process is perfect to run in the background and I could actually use this app once finished.  I don’t have a data plan so it would be really nice to download some stuff from YouTube that I could listen to on my commute to work or when taking my daily walks.

    Luckily, someone already built the harder part of downloading the videos and doing the conversion.  There’s this open source project called youtube-dl that’s actually written in Python, so even better.  All I have to do now is create the frontend and the Celery tasks to run the processes in the background.

    For server hosting, I used DigitalOcean as I find them to be a great value for the money: $5 a month for a VPS with 1 vCPU, 512MB of RAM, 20GB disk space.  I don’t really need more than the 20GB of disk space as I’m not planning on storing every download, I could just run a scheduled job (which Celery has a module for called ‘celerybeat’, which is basically cron) to clean up the files.

    I used an existing Bootstrap template that I used for another project, which is very simple but does the job.  The final product ended up looking like this:

    youtubeadl_home_01

    Because of the simple UI, the app works great on tablets and phones as well.  I currently set the limit for the maximum video length to 3 hours, which should be more than enough for most videos people would want to convert.

    Check it out at: http://www.YouTubeADL.com

  • How to create a self-signed X.509 SSL certificate with OpenSSL in one command line

    Posted on June 18th, 2014 webmaster No comments         

    I must have ran this command a hundred times by now but could never remember it:

    
    sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout example.key -out example.crt
    
    

    This will create a self-signed SSL certificate with an expiration of 10 years using a 2048-bit key.  You can use this certificate with Apache2, Nginx, etc.

  • Ansible Playbook for a Django Stack (Nginx, Gunicorn, PostgreSQL, Memcached, Virtualenv, Supervisor)

    Posted on April 20th, 2014 webmaster No comments         

    I decided to create a separate GitHub project for the Ansible playbook I’m currently using to fully provision a production server for my open-source app, GlucoseTracker, so it can be reused by people who are using the same stack.

    You can download the playbook here: https://github.com/jcalazan/ansible-django-stack

    The playbook can fully provision an Ubuntu 12.04 LTS server (will test 14.04 soon) from the base image with the following applications, which are quite popular in the Django community:

    • Nginx
    • Gunicorn
    • PostgreSQL
    • Memcached
    • Virtualenv
    • Supervisor

    I used this awesome guide to set up my server initially (which took like half a day) before automating the entire process with Ansible.  If I need to move to a new server or cloud provider, I can pretty much rebuild a fully-configured server in about 5 minutes with one command.  Pretty neat.

    Note: I’ve also ran this playbook successfully on Amazon EC2, Rackspace, and Digital Ocean virtual private servers.

    TL;DR

    For those who are in a hurry, simply install Ansible, Vagrant, and VirtualBox (if you don’t have them already), clone the project from GitHub, and type this in from the project directory:

    vagrant up

    Wait a few minutes for Ansible to do its magic.  Visit http://192.168.33.15 when finished. Congrats, you just deployed a fully configured Django app!

    The Juicy Details

    Below are some things you should know before using this playbook for your projects.

    Project Structure

    I have my Django project structure set up this way:

    glucose-tracker\ (project directory)

    —-> glucosetracker\ (application directory)

    —-> settings\

    ———-> base.py

    ———-> local.py

    ———-> dev.py

    —-> requirements.txt file, scripts, other files and directories I don’t consider part of the application

    If you have the same project structure that I have, then all you really have to change is the env_vars/base file to get started, where you can set the Git repo location, project name, and the application name which are used throughout the playbook.

    If you don’t have the same project structure, you will need to change the group_vars/webservers file as well (and possibly the environment specific vars file in env_vars/ if you don’t split up your settings file), where you can set the path settings to match your project structure.

    Environment Variables

    I like to separate my environment-specific settings from the main code repo for security reasons and for easier management.  For example,  in my Django settings file, I set the EMAIL_HOST_PASSWORD setting to something like:

    EMAIL_HOST_PASSWORD = os.environ['EMAIL_HOST_PASSWORD']

    This way, I won’t have to leave the password in the code and if I need to change the email password I can do so quickly by changing the environment variable setting on the server instead of modifying the code and re-deploying it.

    The way I have this setup is I created a postactivate script (see roles/web/templates/) that creates the environment variables.  This gets ran after activating virtualenv so those settings are applied only to that virtualenv.

    Now, because I like having all my configurations in my Ansible playbook repo, I keep these values in a vars file and encrypt them with Ansible Vault (see my previous post about this for more details).

    Applying Roles

    The playbooks in the repo apply all the roles.  If you only want certain roles applied to your server, simply remove the ones you don’t need from the roles: section.

    For example, if you don’t use memcached, your roles: section will look something like this.

      roles:
        - base
        - db
        - web
    

    Django Management Commands

    In env_vars/, you will see the following settings in the environment-specific vars files:

    run_django_syncdb: yes
    run_django_south_migration: yes
    run_django_collectstatic: yes
    

    If you don’t want to run some of these commands, simply set the value to no.

    OpenSSL ‘Heartbleed’ Patch

    Don’t worry, the playbook already takes care of this for you.  The first task in the playbook is to do an apt-get update and ensure that openssl and libssl are the latest version. ;)

    I think that pretty much covers most of the questions you might have if you decide to use this Ansible playbook for your Django projects. I might add a few more roles here in the next few weeks, such as Celery, RabbitMQ, and Solr, as we use them at work and we’re currently in the process of automating our infrastructure.

    If you have any questions or suggestions, please feel free to leave a comment below.

    Some Useful Links: