Wednesday, August 21, 2013

Openstack sqlalchemy db console

Coming from rails world, I was strongly missing "rails console" equivalent for python in openstack. I wanted to open a python console and call sqlalchemy db api methods to interact with database and to debug complex sqlalchemy queries.

Finally I figured out a way to do it in python console

Open a python console. Type the below lines in the console

import os
import sys

from oslo.config import cfg
from nova import config
from nova import context
from nova import db
from nova import version

CONF = cfg.CONF












cfg.CONF([], project='nova', version=version.version_
string(), default_config_files=None)
#tweak CONF to load up default configurations
#project represents the project you are working on (nova, glance etc)
#version comes from version_string
#default_config_files, keep it to None. It will look for config files in /etc, /etc/<project>, ~/<project>  #for any file with name as <project>.conf
 
db.instance_get_all(context.
get_admin_context())
#Now, start accessing sqlalchemy methods


Happy debugging......