Customize the application

  1. Create a manifest.yml file. Put this file in the project directory, which is the directory where your manage.py file is stored.
  2. Add the following information to your manifest.yml file. In this example, the application is called sc-proj-watson, but you must select a unique name. This information also specifies a buildpack and a Cloud Foundry stack. (A stack is a prebuilt root file system (roots) that works in tandem with a buildpack and is used to support running applications.) Check that the combination of the Python runtime and stack is supported by your buildpack.


    applications:
    - path: .
    memory: 128M
    instances: 1
    domain: eu-gb.mybluemix.net
    name: sc-proj-watson
    host: sc-proj-watson
    disk_quota: 1024M
    buildpack: https://github.com/cloudfoundry/python-buildpack#v1.5.1
    cf_stacks:
    - cflinuxfs2
    command: bash ./run.sh


    In the code above, notice the line with the command: bash ./run.sh. This line launches the application. This mechanism will give you greater flexibility in running other commands, in particular, the Python shell. In this lab, you will use the command-line option.
    You can start the application on Bluemix in this way if you want greater flexibility in running other commands such as the Python shell.
    If you base your application code on the boilerplate sample from Bluemix, notice that the manifest.yml file doesn’t have a corresponding command. Instead, it uses a Procfile to specify how to launch your application. You can use either method. Some developers prefer the bash command line.
    If you use a bash shell file, be sure to save it in a UNIX format:

    #!/bin/bash
    if [ -z "$VCAP_APP_PORT" ];
    then SERVER_PORT=80;
    else SERVER_PORT="$VCAP_APP_PORT";
    fi
    echo port is $SERVER_PORT
    python manage.py makemigrations
    python manage.py migrate
    python manage.py shell < initdbadmin.py

    echo [$0] Starting Django Server...
    python manage.py runserver 0.0.0.0:$SERVER_PORT --noreload


    The following line runs the initdbadmin.py Python procedure that initializes the database:
    python manage.py shell < initdbadmin.py

  3. Create this file (initdbadmin.py) in the same directory as the manage.py file:from createdbadmin import MainProgram

    go = MainProgram()

    This in turn needs a file named createdbadmin.py, which is also in the same directory as the manage.py file.

    # -*- coding: utf-8 -*-

    from django.contrib.auth.models import User

    from django.db.utils import IntegrityError

    import logging

    logger = logging.getLogger(__name__)

    class MainProgram(object):

    def __init__(self):

    try:
    User.objects.create_superuser(username='xxx',password='yyy',email='[email protected]')

    except IntegrityError as e:

    logger.warning("DB Error Thrown %s" % e)

    After the administrator account is created, running this command subsequent times will throw an exception that will be caught, and the application can start.