Custom Search

Wednesday, April 9, 2014

How to OpenStack Command Line CLI Works Python heatclient

1)
#heat --help

2)
Find the location of heat executable.

#which heat
/usr/local/bin/heat


3)
Check the content of the executable file

#cat /usr/local/bin/heat

#!/usr/bin/python
# PBR Generated from 'console_scripts'

import sys
##
from heatclient.shell import main

if __name__ == "__main__":
    ##
    sys.exit(main())

4)
https://github.com/openstack/python-heatclient/blob/master/heatclient/shell.py

from keystoneclient.v2_0 import client as ksclient
import heatclient
from heatclient import client as heat_client
import argparse

       
def main(args=None):
    HeatShell().main(args)

class HeatShell(object):
    def main(self, argv):
        #Here value of argv will be ['list'] for the command #heat list
        ##
        subcommand_parser = self.get_subcommand_parser(api_version)
        ##
        args = subcommand_parser.parse_args(argv)
        ##Create Heatclient object (connection object)
        ##
        client = heat_client.Client(api_version, endpoint, **kwargs)
        ##Invoke command function, In this case the function "def do_list(hc, args={})" defined in the heatclient/v1/shell.py
        ##
        args.func(client, args)

    def get_base_parser(self):
        parser = argparse.ArgumentParser(---)
        return parser

    def get_subcommand_parser(self, version):
        ##
        parser = self.get_base_parser()

        self.subcommands = {}
        ##Adding sub-commands or sub-parsers to main parser
        ##http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-commands
        ##
        subparsers = parser.add_subparsers(metavar='')
        ##returns heatclient/v1/shell.py
        submodule = utils.import_versioned_module(version, 'shell')
        ##
        self._find_actions(subparsers, submodule)
        self._find_actions(subparsers, self)
        self._add_bash_completion_subparser(subparsers)

        return parser

    def _find_actions(self, subparsers, actions_module):
        ##
        for attr in (a for a in dir(actions_module) if a.startswith('do_')):
            # I prefer to be hyphen-separated instead of underscores.
            command = attr[3:].replace('_', '-')
            ##Get the function object (callback) from heatclient/v1/shell.py
            callback = getattr(actions_module, attr)
            desc = callback.__doc__ or ''
            help = desc.strip().split('\n')[0]
            ##The decorator "@utils.arg" will add the attribute "arguments" to each function defined in the heatclient/v1/shell.py
            arguments = getattr(callback, 'arguments', [])

            ##Add all sub-command or sub-parser to main parser one by one in a loop.
            ##subparser(sub-command): http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-commands
           subparser = subparsers.add_parser(command,
                                              help=help,
                                              description=desc,
                                              add_help=False,
                                              formatter_class=HelpFormatter)

            subparser.add_argument('-h', '--help',
                                   action='help',
                                   help=argparse.SUPPRESS)
            self.subcommands[command] = subparser
            for (args, kwargs) in arguments:
                subparser.add_argument(*args, **kwargs)

            ##set_defaults: http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#parser-defaults
            ##Set the new attribute "func"
            subparser.set_defaults(func=callback)


5)
https://github.com/openstack/python-heatclient/blob/master/heatclient/v1/shell.py

def do_list(hc, args={}):
    ##

    do_stack_list(hc, args)

def do_stack_list(hc, args={}):    '''List the user's stacks.'''
    kwargs = {}
    ##API Call to get list of stacks
    ##
    stacks = hc.stacks.list(**kwargs)
    fields = ['id', 'stack_name', 'stack_status', 'creation_time']
    ##Format the output of API call and print it
    utils.print_list(stacks, fields, sortby=3)


6)
https://github.com/openstack/python-heatclient/blob/master/heatclient/openstack/common/cliutils.py
or
https://github.com/openstack/python-heatclient/blob/0.2.5/heatclient/common/utils.py

##Print a list or objects as a table, one row per object
def print_list(objs, fields, formatters=None, sortby_index=0, mixed_case_fields=None):

7)
Example-1:

http://argparse.googlecode.com/svn/trunk/doc/other-methods.html
http://argparse.googlecode.com/svn/trunk/doc/other-methods.html#sub-commands
http://infohost.nmt.edu/tcc/help/pubs/python/web/argparse-parse_args.html

##sub-command functions
def foo(args):
    print args.x * args.y
   
def bar(args):
    print '((%s))' % args.z

##create the top-level parser
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers()

##create the parser for the "foo" command

parser_foo = subparsers.add_parser('foo')
parser_foo.add_argument('-x', type=int, default=1)
parser_foo.add_argument('y', type=float)
parser_foo.set_defaults(func=foo)

##create the parser for the "bar" command
parser_bar = subparsers.add_parser('bar')
parser_bar.add_argument('z')
parser_bar.set_defaults(func=bar)

##parse the args and call whatever function was selected
args = parser.parse_args('foo 1 -x 2'.split())
args.func(args)
2.0

##parse the args and call whatever function was selected
args = parser.parse_args('bar XYZYX'.split())
args.func(args)
((XYZYX))


8)
Stack Trace
=========
Signal recieved : entering python shell.
Traceback:
  File "/usr/local/bin/heat", line 10, in
    sys.exit(main())
  File "/opt/stack/python-heatclient/heatclient/shell.py", line 385, in main
    HeatShell().main(sys.argv[1:])
  File "/opt/stack/python-heatclient/heatclient/shell.py", line 360, in main
    args.func(client, args)
  File "/opt/stack/python-heatclient/heatclient/v1/shell.py", line 285, in do_list
    do_stack_list(hc, args)
  File "/opt/stack/python-heatclient/heatclient/v1/shell.py", line 293, in do_stack_list
    utils.print_list(stacks, fields, sortby=3)
  File "/opt/stack/python-heatclient/heatclient/common/utils.py", line 66, in print_list
    for o in objs:
  File "/opt/stack/python-heatclient/heatclient/v1/stacks.py", line 81, in paginate
    stacks = self._list(url, "stacks")
  File "/opt/stack/python-heatclient/heatclient/common/base.py", line 56, in _list
    resp, body = self.api.json_request('GET', url)
  File "/opt/stack/python-heatclient/heatclient/common/http.py", line 198, in json_request
    resp, body_str = self._http_request(url, method, **kwargs)
  File "/opt/stack/python-heatclient/heatclient/common/http.py", line 147, in _http_request
    resp = conn.getresponse()
  File "/usr/lib/python2.7/httplib.py", line 1045, in getresponse
    response.begin()
  File "/usr/lib/python2.7/httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/usr/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)

No comments:

Post a Comment