Custom Search

Sunday, December 29, 2013

Python Selenium Example

"""
#pip install selenium
#pip install BeautifulSoup
#pip install pyvirtualdisplay
#sudo apt-get install xvfb



"""

"""

Driver
=======

>>> dir(d)
['NATIVE_EVENTS_ALLOWED', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_is_remote', '_unwrap_value', '_wrap_value', 'add_cookie', 'application_cache', 'back', 'binary', 'capabilities', 'close', 'command_executor', 'create_web_element', 'current_url', 'current_window_handle', 'delete_all_cookies', 'delete_cookie', 'desired_capabilities', 'error_handler', 'execute', 'execute_async_script', 'execute_script', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'firefox_profile', 'forward', 'get', 'get_cookie', 'get_cookies', 'get_screenshot_as_base64', 'get_screenshot_as_file', 'get_window_position', 'get_window_size', 'implicitly_wait', 'is_online', 'maximize_window', 'name', 'orientation', 'page_source', 'profile', 'quit', 'refresh', 'save_screenshot', 'session_id', 'set_page_load_timeout', 'set_script_timeout', 'set_window_position', 'set_window_size', 'start_client', 'start_session', 'stop_client', 'switch_to_active_element', 'switch_to_alert', 'switch_to_default_content', 'switch_to_frame', 'switch_to_window', 'title', 'window_handles']
>>>

Element
=======

['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_execute', '_id', '_parent', '_upload', 'clear', 'click', 'find_element', 'find_element_by_class_name', 'find_element_by_css_selector', 'find_element_by_id', 'find_element_by_link_text', 'find_element_by_name', 'find_element_by_partial_link_text', 'find_element_by_tag_name', 'find_element_by_xpath', 'find_elements', 'find_elements_by_class_name', 'find_elements_by_css_selector', 'find_elements_by_id', 'find_elements_by_link_text', 'find_elements_by_name', 'find_elements_by_partial_link_text', 'find_elements_by_tag_name', 'find_elements_by_xpath', 'get_attribute', 'id', 'is_displayed', 'is_enabled', 'is_selected', 'location', 'location_once_scrolled_into_view', 'parent', 'send_keys', 'size', 'submit', 'tag_name', 'text', 'value_of_css_property']


"""



import selenium
from selenium import webdriver
from selenium import selenium as sel
import time
from selenium.webdriver.support.ui import WebDriverWait
import BeautifulSoup
from pyvirtualdisplay import Display
import sys

from PIL import Image
from urllib import urlretrieve

import logging
LOGGER = logging.getLogger('selenium')
hdlr = logging.FileHandler('./selenium.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
LOGGER.addHandler(hdlr)
LOGGER.setLevel(logging.INFO)


class MeetMe:
    """
    """
    TARGET_URL = "http://www.blabla.com"
    DUMP_FILE = "response.html"
    CAPTCHA_FILE_NAME = "captcha.jpeg"

    def __init__(self):
        """
        """
        self.display = Display(visible=0, size=(1200,1600))
        self.display.start()
        self.url = self.TARGET_URL
        self.driver  = webdriver.Firefox()
        self.retry = 0
        LOGGER.info("\n-------------start---------------\n")
   
    def __del__(self):
        """
        """
        print "====closing browser=="
        self.close_browser()

    def open_url(self, url):
        """
        """
        self.driver.get(url)

    def find_element_by_name(self, name):
        """
        """
        return self.driver.find_element_by_name(name)

    def find_element_by_id(self, id):
        """
        """
        return self.driver.find_element_by_id(id)
   
    def close_browser(self):
        """
        """
        self.driver.close()


    def dump_response_to_file(self, filename=None):
        """
        """
        if not filename:
            filename = self.DUMP_FILE
        html = self.driver.page_source
        soup_res = BeautifulSoup.BeautifulSoup(html)
        with open(filename, 'w') as fp:
            fp.write(str(soup_res))

    def execute_script(self, script):
        """
        self.driver.execute_script("document.getElementsByName('body')[0].setAttribute('type', 'text');")
        """
        self.driver.execute_script(script)

    def save_screenshot(self, filename):
        """
        """
        self.driver.save_screenshot(filename)

    def select_checkbox(self, element, select=True):
        """
        """
        if select:
            if not element.is_selected():
                element.click()
        else:
            if element.is_selected():
                element.click()

    def get_file(self, link, filename="test.tif"):
        ##Download and save file with name filename
        urlretrieve(link, filename)

    def show_image(self, image_name):
        """
        """
        im = Image.open(image_name)
        im.show()

"""
signup_frm_elements_id = {
            #"quicksignupFrm":"quicksignupFrm",##

            "firstname":"firstname",##:text
            "lastname":"lastname",##:text
            "email":"email",##:text
            "password":"password",##:password
            #"location":"location",##

            #"regionHidden":"regionHidden",##:hidden
            #"country":"country",##:text:hidden
            "gender":"gender",##:
            "day":"day",##:
            "terms":"terms",##:checkbox
            #"halRecaptchaContainer":"halRecaptchaContainer",##

            #"dynamicRecaptcha":"dynamicRecaptcha",##

            #"recaptcha_image":"recaptcha_image",##

            #"signupButton":"signupButton",##


"""
### CAPCHA ###

   


"""
recaptcha_image_element = me.find_element_by_id("recaptcha_image")
if recaptcha_image_element:
    ##Get child tag "img" of tag "recaptcha_image"
    recaptcha_img_tag = recaptcha_image_element.find_element_by_tag_name("img")
    if recaptcha_img_tag:
        ##Get url of captcha image
        recaptcha_img_src = recaptcha_img_tag.get_attribute("src")
        print "===recaptcha_img_src===", recaptcha_img_src
        me.get_file(recaptcha_img_src, filename=me.CAPTCHA_FILE_NAME)
        me.show_image(me.CAPTCHA_FILE_NAME)
        captcha_chars = raw_input("Enter Captcha:")
        recaptcha_text_field = me.find_element_by_id("recaptcha_response_field")
        recaptcha_text_field.send_keys(captcha_chars)

me.save_screenshot("k3")
me.dump_response_to_file("signuotest1.html")
me.save_screenshot("k4")




























Sunday, December 22, 2013

OpenStack Horizon Keystone Howto Create New User And Give Permission To Create More New users

1)Goto Horizon and login as Admin

2)
* Create a new role named "create_user_role"
* Create a new user "saju1" with role "create_user_role" and project "any_project"

3)
* Logout from horizon
* Try to login as "saju1". That will not work.



4)
Goto any restclient. I am using "advanced rest client" extension of google chrome.

5)
User Authentication With Scope and get token
URL: http://192.168.56.102:5000/v3/auth/tokens
Method: POST
Request Headers:
Content-type : application/json
Request Body:

{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": "saju1",
                    "password": "saju1",
                    "domain": {
                               "name":"Default"
                            }

                }
            }
        },
    "scope": {
        "project": {
            "domain": {
                "name": "Default"
                },
            "name": "project_name_of_saju1_selected_in_step_2"
            }
        }
    }
}


6)
Create a user
URL: http://192.168.56.102:5000/v3/users
Method: POST
Request Headers:
Content-type : application/json
X-Auth-Token : c999bef3667c48739e39deca2f3dc6c7
Request Body:
{
    "user": {
        "default_project_id": "",
        "description": "new user",
        "domain_id": "default",
        "email": "sam@ss.com",
        "enabled": true,
        "name": "saju2",
        "password": "saju2"
    }
}


7)
* Step-6 should fail, since we haven't edit /etc/keystone/policy.json file.
* Goto /etc/keystone/policy.json and make following changes
##Add new rule "create_user_rule"
"create_user_rule": [["role:create_user_role"]],
##Apply the new rule "create_user_rule" to action create_user.
##So users belong to the role "create_user_role" can do "create_user" action.
"identity:create_user": [["rule:admin_required"], ["rule:create_user_rule"]],
##Apply the new rule "create_user_rule" to action list_projects
##So users belong to the role "create_user_role" can do "list_projects" action.
"identity:list_projects": [["rule:admin_required"], ["rule:create_user_rule"]],
##Apply the new rule "create_user_rule" to action list_roles
##So users belong to the role "create_user_role" can do "list_roles" action.
"identity:list_roles": [["rule:admin_required"],  ["rule:create_user_rule"]],
##Apply the new rule "create_user_rule" to action create_grant
##So users belong to the role "create_user_role" can do "create_grant" action.
"identity:create_grant": [["rule:admin_required"], ["rule:create_user_rule"]],

8)
Repeat the step-6 again and copy the id of new user "saju2"

9)
Find id of project "project_name_of_saju1_selected_in_step_2" and id of role "Member" with following API requests.
http://192.168.56.102:5000/v3/projects/
http://192.168.56.102:5000/v3/roles/

10)
Grant role to user on project:
PUT /projects/{project_id}/users/{user_id}/roles/{role_id}
Method: PUT
URL: http://192.168.56.102:5000/v3/projects/b831390e0cb04f1eafbdd39bfddb7bd6/users/57bcea432f824f11b7daf2b287adad55/roles/5ef7a5ed13e843358655c1f7568144cd
Request Headers:
X-Auth-Token : c999bef3667c48739e39deca2f3dc6c7

11)
Goto horizon and login as saju2 and provision a VM. :) :) :)

Ref Sites:
http://adam.younglogic.com/2013/09/keystone-v3-api-examples/
https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md <== API

How to Intercept RabbitMQ Messages In OpenStack Nova

OpenStack How to Customize Authorization (Policies, Policy.json)

Customizing Authorization

The default authorization settings only allow administrative users to create resources on behalf of a different project. OpenStack handles two kind of authorization policies:
  • Operation-based: policies specify access criteria for specific operations, possibly with fine-grained control over specific attributes.
  • Resource-based: whether access to a specific resource might be granted or not according to the permissions configured for the resource (currently available only for the network resource). The actual authorization policies enforced in an OpenStack service vary from deployment to deployment.
The policy engine reads entries from the policy.json file. The actual location of this file might vary from distribution to distribution, for nova it is typically in /etc/nova/policy.json. You can update entries while the system is running, and you do not have to restart services. Currently the only way of updating such policies is to edit the policy file.
The OpenStack service's policy engine matches a policy directly. A rule indicates evaluation of the elements of such policies. For instance, in a compute:create: [["rule:admin_or_owner"]] statement, the policy is compute:create, and the rule is admin_or_owner.
Policies are triggered by an OpenStack policy engine whenever one of them matches an OpenStack API operation or a specific attribute being used in a given operation. For instance, the engine tests the create:compute policy every time a user sends a POST /v2/{tenant_id}servers request to the OpenStack Compute API server. Policies can be also related to specific API extensions. For instance, if a user needs an extension like compute_extension:rescue the attributes defined by the provider extensions trigger the rule test for that operation.
An authorization policy can be composed by one or more rules. If more rules are specified, evaluation policy will be successful if any of the rules evaluates successfully; if an API operation matches multiple policies, then all the policies must evaluate successfully. Also, authorization rules are recursive. Once a rule is matched, the rule(s) can be resolved to another rule, until a terminal rule is reached. These are the rules defined:
  • Role-based rules: evaluate successfully if the user submitting the request has the specified role. For instance "role:admin" is successful if the user submitting the request is an administrator.
  • Field-based rules: evaluate successfully if a field of the resource specified in the current request matches a specific value. For instance "field:networks:shared=True" is successful if the attribute shared of the network resource is set to true.
  • Generic rules: compare an attribute in the resource with an attribute extracted from the user's security credentials and evaluates successfully if the comparison is successful. For instance "tenant_id:%(tenant_id)s" is successful if the tenant identifier in the resource is equal to the tenant identifier of the user submitting the request.
Here are snippets of the default nova policy.json file:


{
 "context_is_admin":  [["role:admin"]],
 "admin_or_owner":  [["is_admin:True"], ["project_id:%(project_id)s"]], [1]
 "default": [["rule:admin_or_owner"]], [2]
 "compute:create": [],
 "compute:create:attach_network": [],
 "compute:create:attach_volume": [],
 "compute:get_all": [],
    "admin_api": [["is_admin:True"]],
 "compute_extension:accounts": [["rule:admin_api"]],
 "compute_extension:admin_actions": [["rule:admin_api"]],
 "compute_extension:admin_actions:pause": [["rule:admin_or_owner"]],
 "compute_extension:admin_actions:unpause": [["rule:admin_or_owner"]],
 "compute_extension:admin_actions:suspend": [["rule:admin_or_owner"]],
 "compute_extension:admin_actions:resume": [["rule:admin_or_owner"]],
 ...
 "compute_extension:admin_actions:migrate": [["rule:admin_api"]],
 "compute_extension:aggregates": [["rule:admin_api"]],
 "compute_extension:certificates": [],
 "compute_extension:cloudpipe": [["rule:admin_api"]],
 ...
 "compute_extension:flavorextraspecs": [],
 "compute_extension:flavormanage": [["rule:admin_api"]],  [3]
 }
 



[1] Shows a rule which evaluates successfully if the current user is an administrator or the owner of the resource specified in the request (tenant identifier is equal).
[2] Shows the default policy which is always evaluated if an API operation does not match any of the policies in policy.json.
[3] Shows a policy restricting the ability of manipulating flavors to administrators using the Admin API only.
In some cases, some operations should be restricted to administrators only. Therefore, as a further example, let us consider how this sample policy file could be modified in a scenario where we enable users to create their own flavors:
"compute_extension:flavormanage": [ ],

Copied From :
http://docs.openstack.org/trunk/openstack-ops/content/customize_auth.html

Ref Sites:
https://ask.openstack.org/en/question/2032/create-admin-user-within-single-tenant/
http://docs.openstack.org/trunk/openstack-ops/content/projects_users.html
http://docs.openstack.org/developer/keystone/architecture.html#approach-to-authorization-policy
http://knowledgestack.wordpress.com/2012/01/27/rbac-keystone-and-openstack/ <=== IMP

Fav Sites:
http://prosuncsedu.wordpress.com/2013/10/01/adding-enforcing-policy-in-openstack-nova-code/
http://prosuncsedu.wordpress.com/2013/09/28/policy-administration-for-openstack-nova/
http://prosuncsedu.wordpress.com/2013/09/18/change-policy-to-add-permission-to-an-existing-nova-command/
http://prosuncsedu.wordpress.com/tag/policy/

How to Test OpenStack Keystone API using RESTClient Firefox Chrome

http://adam.younglogic.com/2013/09/keystone-v3-api-examples/
https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md <== API

Core API
###############


a)
*List All API Versions

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#list-versions-get-
*URL: http://192.168.56.101:5000/
*Method: GET



b)
*Authenticate

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#authenticate-post-authtokens
*URL: http://192.168.56.101:5000/v3/auth/tokens
*Method: POST
*Request Headers:
Content-type : application/json
*Request Body:
Login as admin using the method password.
-----
{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": "admin",
                    "password": "password",
                    "domain": {
                               "name":"Default"
                            }

                }
            }
        }
    }
}

*You can find the "X-Subject-Token" from the response header of this request.
X-Subject-Token: 6f9723ef28de4cdaaa72327ad3ab3e0d

c)
*Validate Token

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#validate-token-get-authtokens
*URL: http://192.168.56.101:5000/v3/auth/tokens
*Method: GET
*Request Headers:
X-Auth-Token: tokentoken
X-Subject-Token: 6f9723ef28de4cdaaa72327ad3ab3e0d


d)
*Check Token

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#check-token-head-authtokens
*URL: http://192.168.56.101:5000/v3/auth/tokens
*Method: HEAD
*Request Headers:
X-Auth-Token: tokentoken
X-Subject-Token: c675805436575fs5dvt2sd32f


e)
*Revoke/Delete Token

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#revoke-token-delete-authtokens
*URL: http://192.168.56.101:5000/v3/auth/tokens
*Method: DELETE
*Request Headers:
X-Auth-Token: tokentoken
X-Subject-Token: c675805436575fs5dvt2sd32f


Projects
#############


a)
*Create Projects

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#create-project-post-projects
*URL: http://192.168.56.101:5000/v3/projects
*Method: POST
*Request Headers:
X-Auth-Token: tokentoken
Content-type : application/json

*Request Body:
{
    "project": {
        "description": "description1",
        "domain_id": "default",
        "enabled": true,
        "name": "MyProject1"
    }
}


b)
*List Projects

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#list-projects-get-projects
*URL: http://192.168.56.101:5000/v3/projects
*Method: GET
*Request Headers:
X-Auth-Token: tokentoken

c)
*Get Project

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#get-project-get-projectsproject_id
*URL: http://192.168.56.101:5000/v3/projects/a265e326b5f243f5bccdf5fdc537b8f2
*Method: GET
*Request Headers:
X-Auth-Token: tokentoken

d)
*Update Project

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#update-project-patch-projectsproject_id
*URL: http://192.168.56.101:5000/v3/projects/a265e326b5f243f5bccdf5fdc537b8f2
*Method: PATCH
*Request Headers:
X-Auth-Token: tokentoken
Content-type : application/json

*Request Body:
{
    "project": {
        "description": "description2",
        "domain_id": "default",
        "enabled": true,
        "name": "MyProject_new_name"
    }
}

*I could not find the request method "PATCH" in restclient firefox Addon.
*Note: Use Google chrome browser with "Advanced Rest Client" extension for PATCH option (Works).

e)
*Delete Project

*https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#get-project-get-projectsproject_id
*URL: http://192.168.56.101:5000/v3/projects/a265e326b5f243f5bccdf5fdc537b8f2
*Method: DELETE
*Request Headers:
X-Auth-Token: tokentoken

Compare Firefox RESTClient and Chrome Advanced Rest Client Extension

Compare Firefox RESTClient and Chrome Advanced Rest Client Extension


Wednesday, December 18, 2013

OpenStack Horizon Development Tutorial Part 1

1) Form
#######

1)
Main Form classes

a)
Location forms.py
horizon/horizon/forms/base.py

b)
Custom First Base class-1
class SelfHandlingMixin(object):

* Please check the methods of this class
* Every subclass of this class should implement the method "def handle(self, request, data):".
* You can see a statement like "handled = form.handle(self.request, form.cleaned_data)"
in the method "def form_valid(self, form):" of base view class "ModalFormView". That means
every horizon views inherited from "ModalFormView" should invoke the method "handle" of form class.
* Note: Horizon making API request from the method "handle" of Form class.

c)
Custom First Base class-2
class SelfHandlingForm(SelfHandlingMixin, forms.Form):

d)
Custom First Base class-3
class BaseUserForm(forms.SelfHandlingForm):


2) View
#######


1)
Main View Classes
a)
Location of views.py
horizon/horizon/forms/views.py

b)
Custom Base class-1
class ModalFormMixin(object):


c)
Custom Base class-2
generic.FormView


*A view that displays a form. On error, redisplays the form with validation errors; on success, redirects to a new URL.

https://docs.djangoproject.com/en/dev/ref/class-based-views/flattened-index/#formview
https://docs.djangoproject.com/en/dev/topics/class-based-views/generic-editing/
https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-editing/#formview
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-editing/#processformview

d)
Custom Base class-3
class ModalFormView(ModalFormMixin, generic.FormView):


'ModalFormView' is the main view class from which all views which handle forms in Horizon
should inherit. It takes care of all details with processing
:class:`~horizon.forms.base.SelfHandlingForm` classes, and modal concerns
when the associated template inherits from `horizon/common/_modal_form.html`.

Subclasses must define a ``form_class`` and ``template_name`` attribute at minimum.

See Django's documentation on the `FormView /en/dev/ref/class-based-views/generic-editing/#formview>`_ class for more details.

2)

Important methods of View Class

a)
def get_context_data(self, **kwargs):

* Returns a dictionary representing the template context. The keyword arguments provided will make up the returned context.
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-simple/#django.views.generic.base.ContextMixin.get_context_data

b)
def form_valid(self, form):

* Redirects to get_success_url()
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.form_valid

c)
get_form()

* Instantiate an instance of form_class using get_form_kwargs().
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.get_form

d)
get_form_kwargs()

* Build the keyword arguments required to instantiate the form.
* The initial argument is set to get_initial().
* If the request is a POST or PUT, the request data (request.POST and request.FILES) will also be provided.
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.get_form_kwargs

e)
get_initial()
* Retrieve initial data for the form. By default, returns a copy of initial.
https://docs.djangoproject.com/en/dev/ref/class-based-views/flattened-index/#formview
https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-editing/#django.views.generic.edit.FormMixin.get_initial

f)
def get_object(self):

* Returns the single object that this view will display. If queryset is provided, that queryset will be used as the source of objects; otherwise, get_queryset() will be used. get_object() looks for a pk_url_kwarg argument in the arguments to the view; if this argument is found, this method performs a primary-key based lookup using that value. If this argument is not found, it looks for a slug_url_kwarg argument, and performs a slug lookup using the slug_field.

https://docs.djangoproject.com/en/dev/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_object

g)
You can see following methods in all horizon view classs
def get_success_url(self):
def get_context_data(self, **kwargs):
def get_initial(self):
def get_form_kwargs(self):



3) Templates
##########


1)
a)

Main model form template
./horizon/templates/horizon/common/_modal_form.html

* This is a common template to display form tag.
* This template "_modal_form.html" included in all other templates in the
horizon as base template.

* You can see following include statement here
{% include "horizon/common/_form_fields.html" %}

b)
./horizon/templates/horizon/common/_form_fields.html
* This template "_form_fields.html" included in all other templates in the
horizon to show fields. So this is a common template to display form fields.
* You can see "errors", "hidden fields" and "visible_fields" loop here.

2)

Another important base template is "horizon/templates/base.html"
There you can see following include statements
{% include "_stylesheets.html" %}
{% include "horizon/_conf.html" %}
{% include "horizon/client_side/_script_loader.html" %}
{% include 'horizon/common/_sidebar.html' %}
{% include "_header.html" %}
{% include "horizon/_messages.html" %}
{% include "horizon/_scripts.html" %}


4) JavaScript
###########


1)
* Please where most of the js files are included.
* Please where "$(document).ready(" is calling and initializing horizon js object.

Location
-------
./horizon/templates/horizon/_scripts.html

initialization part
-------------------
* In "_scripts.html" you can see an include statement {% include "horizon/client_side/templates.html" %}

2)
* This "_scripts.html" is included in "horizon/templates/base.html"
* This "horizon/templates/base.html" is included in lot of create, update, add and change htmls.

3)
Main javascript file
location
--------------
./horizon/static/horizon/js/horizon.js

In this file we can see following things

a)
Definition of "Horizon" object.

b)
Definition of initialization method "horizon.init"

c)
// Create the one and only horizon object.
var horizon = new Horizon();

d)
Please check the following important methods of "Horizon" object.

* "horizon.addInitFunction"
Use the addInitFunction() function to add initialization code which must
be called on DOM ready. This is useful for adding things like event
handlers or any other initialization functions which should preceed user
interaction but rely on DOM readiness.You can see the use of this method
in all other javascript files.

* "horizon.init"
Call all initialization functions and clear the queue


5) Exceptions
############


a)
Files Location
./horizon/exceptions.py
./openstack_dashboard/exceptions.py

b)
Main exception handle method (./horizon/exceptions.py)

def handle(request, message=None, redirect=None, ignore=False,
           escalate=False, log_level=None, force_log=None):
    """Centralized error handling for Horizon.

    Because Horizon consumes so many different APIs with completely
    different ``Exception`` types, it's necessary to have a centralized
    place for handling exceptions which may be raised.

    Exceptions are roughly divided into 3 types:

    #. ``UNAUTHORIZED``: Errors resulting from authentication or authorization
       problems. These result in being logged out and sent to the login screen.
    #. ``NOT_FOUND``: Errors resulting from objects which could not be
       located via the API. These generally result in a user-facing error
       message, but are otherwise returned to the normal code flow. Optionally
       a redirect value may be passed to the error handler so users are
       returned to a different view than the one requested in addition to the
       error message.
    #. RECOVERABLE: Generic API errors which generate a user-facing message
       but drop directly back to the regular code flow.

    All other exceptions bubble the stack as normal unless the ``ignore``
    argument is passed in as ``True``, in which case only unrecognized
    errors are bubbled.

    If the exception is not re-raised, an appropriate wrapper exception
    class indicating the type of exception that was encountered will be
    returned.
    """
c)
Value of constant exceptions.UNAUTHORIZED (./horizon/exceptions.py)
UNAUTHORIZED = tuple(HORIZON_CONFIG['exceptions']['unauthorized'])
-------------------------------------------------
(class 'keystoneclient.apiclient.exceptions.Unauthorized'>, class 'keystoneclient.apiclient.exceptions.Forbidden'>, class 'cinderclient.exceptions.Unauthorized'>, class 'cinderclient.exceptions.Forbidden'>, class 'novaclient.exceptions.Unauthorized'>, class 'novaclient.exceptions.Forbidden'>, class 'glanceclient.exc.Unauthorized'>, class 'neutronclient.common.exceptions.Unauthorized'>, class 'neutronclient.common.exceptions.Forbidden'>, class 'heatclient.exc.HTTPUnauthorized'>, class 'heatclient.exc.HTTPForbidden'>, class 'troveclient.exceptions.Unauthorized'>)


Value of constant exceptions.NOT_FOUND (./horizon/exceptions.py)
NOT_FOUND = tuple(HORIZON_CONFIG['exceptions']['not_found'])
-------------------------------------------------
(class 'keystoneclient.apiclient.exceptions.NotFound'>, class 'cinderclient.exceptions.NotFound'>, class 'novaclient.exceptions.NotFound'>, class 'glanceclient.exc.NotFound'>, class 'neutronclient.common.exceptions.NetworkNotFoundClient'>, class 'neutronclient.common.exceptions.PortNotFoundClient'>, class 'heatclient.exc.HTTPNotFound'>, class 'troveclient.exceptions.NotFound'>)


Value of constant exceptions.RECOVERABLE (./horizon/exceptions.py)
RECOVERABLE += tuple(HORIZON_CONFIG['exceptions']['recoverable'])
-------------------------------------------------
(class 'horizon.exceptions.AlreadyExists'>, class 'keystoneclient.apiclient.exceptions.ClientException'>, class 'keystoneclient.apiclient.exceptions.AuthorizationFailure'>, class 'cinderclient.exceptions.ClientException'>, class 'cinderclient.exceptions.ConnectionError'>, class 'novaclient.exceptions.ClientException'>, class 'glanceclient.exc.ClientException'>, class 'neutronclient.common.exceptions.NeutronClientException'>, class 'neutronclient.common.exceptions.NetworkInUseClient'>, class 'neutronclient.common.exceptions.PortInUseClient'>, class 'neutronclient.common.exceptions.AlreadyAttachedClient'>, class 'neutronclient.common.exceptions.StateInvalidClient'>, class 'swiftclient.exceptions.ClientException'>, class 'heatclient.exc.HTTPException'>, class 'troveclient.exceptions.ClientException'>)

d)
How to add new exceptions to the list UNAUTHORIZED, NOT_FOUND and RECOVERABLE
Goto folowing file asnd add there
./openstack_dashboard/exceptions.py

Openstack Projects How to Find and Fix PEP 8 coding style Issues

1)
Goto your OpenStack Project
For example horizon
#cd /opt/stack/horizon

2)
How to do only PEP 8 Check
#./run_tests.sh -p -N

3)
How to see PEP 8 coding style Issues in a File
#./run_tests.sh -p -N | grep './openstack_dashboard/forms.py'

4)
Help
#./run_tests.sh --help

Tuesday, December 17, 2013

How to install django-cities-light and populate country region state and city dropdown

1)
pip install django-cities-light
pip install south

2)
Add "cities_light" to your INSTALLED_APPS.

3)
./manage.py syncdb



4)
Help
./manage.py help cities_light

5)
Populate tables
./manage.py cities_light

6)
#python manage.py shell
>>> import cities_light as cl
>>> cl.models.Country.objects.filter(name='india').all()
>>> cl.models.Region.objects.filter(name='kerala').all()
>>> cl.models.City.objects.filter(name='bangalore').all()

How to install django-cities and populate country region state and city dropdown

How to install django-cities and populate country region state and city drop-down

1)
a)
#easy_install django-cities
Note: easy_install did not work for me. I got following errror when tried to run "./manage.py cities --import=all"
AttributeError: type object '' has no attribute 'plugins'

b)
Works
---------

activate virtualenv
#git clone https://github.com/coderholic/django-cities.git
#cd django-cities
#python setup.py install




2)
Add 'cities' to INSTALLED_APPS in your projects settings.py file

3)
Change db backend

Goto settings.py and add/replace following line in DATABASES dictionary.
*'ENGINE': 'django.contrib.gis.db.backends.mysql'

4)
Create tables for the cities app
#python manage.py syncdb

5)
Optional

Add following file in settings.py
GEOS_LIBRARY_PATH = '/home/saju/horizon_test/geos-3.3.8/capi/.libs/libgeos_c.so'

6)
a)

#Download all data files to .venv/lib/python2.7/site-packages/django_cities-0.2-py2.7.egg/cities/data
#Then insert data to all tables
#./manage.py cities --import=all

b)
#Download only country file to .venv/lib/python2.7/site-packages/django_cities-0.2-py2.7.egg/cities/data
#Then insert data to country table
#./manage.py cities --import=country

c)
#./manage.py cities --help

d)
Debug: Clear downloaded files from following path and run "./manage.py cities --import=all" again.
#cd .venv/lib/python2.7/site-packages/django_cities-0.2-py2.7.egg/cities/data

7)
Test
#python manage.py shell
>>> import cities
>>> cities.models.Country.objects.all()[1]




Friday, December 13, 2013

Git How to revert Last Commit and Difference Between Soft and Hard Reset or Revert

#git log
#git reset --soft HEAD~1 # use --soft if you want to keep your local changes and revert last commit
#git reset --hard HEAD~1 # use --hard if you want to revert your changes and revert last commit
#git log


Thursday, December 12, 2013

[Solved] django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

1)
#sudo apt-get install python-mysqldb



2)
#easy_install MySQL-python

Tuesday, December 10, 2013

Git How to revert last commit

How to revert last commit
#git log
#git reset --soft HEAD~1 # use --soft if you want to keep your local changes and revert last commit
#git reset --hard HEAD~1 # use --hard if you want to revert your changes and revert last commit
#git log



How to revert last two commit
#git log
#git reset --soft HEAD~2 # use --soft if you want to keep your local changes and revert last commit
#git reset --hard HEAD~2 # use --hard if you want to revert your changes and revert last commit
#git log

How to revert last three commit
#git log
#git reset --soft HEAD~3 # use --soft if you want to keep your local changes and revert last commit
#git reset --hard HEAD~3 # use --hard if you want to revert your changes and revert last commit
#git log

Friday, December 6, 2013

How to Install and use Video Converter handbrake Ubuntu 15.04 14.10 14.04 13.04 12.04

1)
#sudo add-apt-repository ppa:stebbins/handbrake-releases
or
#sudo add-apt-repository ppa:stebbins/handbrake-snapshots



2)
#sudo apt-get update

3)
#sudo apt-get install handbrake-gtk

How to enable tray icons bar on top Ubuntu 15.04 14.10 14.04 13.04 12.04

How to show tray icons bar on top right Ubuntu 13.04 13.10
So we can see icon/notification of Skype and Chrome there

Ubuntu 13.10 how to enable system tray notification bar

Git commit message how to change default editor to VM

1)
#git config --global core.editor "vim"

2)
#git commit myfile.py
or
#git commit -a

Tuesday, December 3, 2013

Openstack Keystone How to use RESTClient Plugin Firefox Google Chrome

Openstack Keystone How to use RESTClient Plugin Firefox Google Chrome

http://adam.younglogic.com/2013/09/keystone-v3-api-examples/
https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md

URL
===
http://192.168.56.101:5000/v3

Request Headers
================
1)
X-Auth-Token : tokentoken

* You can find the your admin token from /etc/keystone/keystone.conf
admin_token = tokentoken

2)
Content-type : application/json

3)
X-Subject-Token: 0a182214a5204e1a9d63185362b611a8


Examples
========

1)
url
----
http://192.168.56.101:5000/v3/auth/tokens

headers
-------
Content-type : application/json

method
------
POST

body
-----
{
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": "admin",
                    "password": "password",
                    "domain": {
                               "name":"Default"
                            }

                }
            }
        }
    }
}


2)
https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md#validate-token-get-authtokens









Monday, December 2, 2013

How to install federated keystone in devstack environment

How to install federated keystone in devstack environment (Working Setup)

1)
a)
git remote add mycld https://github.com/kwss/keystone.git
git remote -v
git fetch mycld
git checkout --track mycld/kent-federated-april

or

b)
git clone -b kent-federated-april https://github.com/kwss/keystone.git

2)
a)
#cd /opt/stack/keystone
#vim vim federated-docs/example-keystone.conf
* Uncomment following lines and save
public_port = 5000
admin_port = 35357

b)
vim vim /etc/keystone/keystone.conf
* Uncomment following lines and save
public_port = 5000
admin_port = 35357

3)
Goto Devstack folder
#vim lib/keystone

a)
* Change the line 'cp -p $KEYSTONE_DIR/etc/keystone.conf.sample $KEYSTONE_CONF'
to 'cp -p $KEYSTONE_DIR/federated-docs/example-keystone.conf $KEYSTONE_CONF' and save it.

b)
* add following codes and save it.
iniset $KEYSTONE_CONF DEFAULT public_port 5000
iniset $KEYSTONE_CONF DEFAULT admin_port 35357

c)
* copy the folder "migrate_repo" from latset keystone repo to /opt/stack/bkps/keystone/keystone/common/sql
* This will fix the error like table not found, while running ./stack.sh

d)
Goto Devstack folder
#vim localrc
* Add following lines
OFFLINE=True
RECLONE=no

e)
#./unstack.sh
#./stack.sh
#./rejoin-stack.sh
#sudo /etc/init.d/apache2 restart
Goto horizon http://127.0.0.1


5)
Goto Devstack folder
#./unstack.sh
#./stack.sh

Advanced
###########

1)
* Goto devstack Machine, Install dm.xmlsec.binding and make the pytohn import working
https://pypi.python.org/pypi/dm.xmlsec.binding/1.0b3

import testing
--------------
import dm.xmlsec.binding as xmlsec
xmlsec.initialize()



##################

Openstack (OperationalError) no such table: user u'SELECT user.id AS user_id, user.name AS user_name,

(root): 2013-12-02 15:25:21,590 ERROR wsgi __call__ (OperationalError) no such table: user u'SELECT user.id AS user_id, user.name AS user_name, user.extra AS user_extra \nFROM user \nWHERE user.name = ?\n LIMIT ? OFFSET ?' (u'admin', 1, 0)
Traceback (most recent call last):
  File "/opt/stack/keystone/keystone/common/wsgi.py", line 204, in __call__
    result = method(context, **params)
  File "/opt/stack/keystone/keystone/service.py", line 468, in authenticate
    context=context, user_name=username)
  File "/opt/stack/keystone/keystone/common/manager.py", line 47, in _wrapper
    return f(*args, **kw)
  File "/opt/stack/keystone/keystone/identity/backends/sql.py", line 229, in get_user_by_name
    return _filter_user(self._get_user_by_name(user_name))
  File "/opt/stack/keystone/keystone/identity/backends/sql.py", line 220, in _get_user_by_name
    user_ref = session.query(User).filter_by(name=user_name).first()
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2156, in first
    ret = list(self[0:1])
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2023, in __getitem__
    return list(res)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2227, in __iter__
    return self._execute_and_instances(context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2242, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1449, in execute
    params)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1584, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1698, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1691, in _execute_context
    context)
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 331, in do_execute
    cursor.execute(statement, parameters)
OperationalError: (OperationalError) no such table: user u'SELECT user.id AS user_id, user.name AS user_name, user.extra AS user_extra \nFROM user \nWHERE user.name = ?\n LIMIT ? OFFSET ?' (u'admin', 1, 0)
(eventlet.wsgi.server): 2013-12-02 15:25:21,610 DEBUG wsgi write 127.0.0.1 - - [02/Dec/2013 15:25:21] "POST /v2.0/tokens HTTP/1.1" 500 501 0.027258

Fix
===

1)
Following lines are missing in vim /etc/keystone/keystone.conf
[sql]
connection = mysql://root:password@127.0.0.1/keystone?charset=utf8

2)
#cd devstack_folder
#./unstack.sh
#./rejoin-stack.sh




How to Openstack Devstack Switch git Branch (Keystone)

1)
saju@saju-VirtualBox:/opt/stack/keystone$
saju@saju-VirtualBox:/opt/stack/keystone$ git remote add sajucld https://github.com/kwss/keystone.git
saju@saju-VirtualBox:/opt/stack/keystone$

2)
saju@saju-VirtualBox:/opt/stack/keystone$ git remote -v
sajucld    https://github.com/kwss/keystone.git (fetch)
sajucld    https://github.com/kwss/keystone.git (push)
origin    https://github.com/openstack/keystone.git (fetch)
origin    https://github.com/openstack/keystone.git (push)
saju@saju-VirtualBox:/opt/stack/keystone$

3)
saju@saju-VirtualBox:/opt/stack/keystone$ git fetch sajucld
remote: Counting objects: 1172, done.
remote: Compressing objects: 100% (512/512), done.
remote: Total 914 (delta 578), reused 724 (delta 398)
Receiving objects: 100% (914/914), 316.91 KiB | 27.00 KiB/s, done.
Resolving deltas: 100% (578/578), completed with 93 local objects.
From https://github.com/kwss/keystone
 * [new branch]      bp/role-mapping-service-keystone -> sajucld/bp/role-mapping-service-keystone
 * [new branch]      feature/keystone-v3 -> sajucld/feature/keystone-v3
 * [new branch]      fed-plugin-moonshot -> sajucld/fed-plugin-moonshot
 * [new branch]      federated_auth_plugin -> sajucld/federated_auth_plugin
 * [new branch]      idp-service -> sajucld/idp-service
 * [new branch]      kent-federated-april -> sajucld/kent-federated-april
 * [new branch]      master     -> sajucld/master
 * [new branch]      role-mapping -> sajucld/role-mapping
 * [new branch]      stable/diablo -> sajucld/stable/diablo
 * [new branch]      stable/essex -> sajucld/stable/essex
 * [new branch]      stable/folsom -> sajucld/stable/folsom
saju@saju-VirtualBox:/opt/stack/keystone$

4)
saju@saju-VirtualBox:/opt/stack/keystone$ git checkout --track sajucld/feature/keystone-v3
Branch feature/keystone-v3 set up to track remote branch feature/keystone-v3 from sajucld.
Switched to a new branch 'feature/keystone-v3'
saju@saju-VirtualBox:/opt/stack/keystone$

5)
saju@saju-VirtualBox:/opt/stack/keystone$ git branch
* feature/keystone-v3
  master
saju@saju-VirtualBox:/opt/stack/keystone$

6)
Switch to working devstack branch
#git checkout mater

Goto devstack screen
#screen -x
* Press "Ctrl +  a + 1", to goto keystone log
* Press "Ctrl + c" to stop keystone service
* Press Up arrow and press Enter to Start keystone service again

7)
Switch to dev/testing branch
#git checkout feature/keystone-v3

Goto devstack screen
#screen -x
* Press "Ctrl +  a + 1", to goto keystone log
* Press "Ctrl + c" to stop keystone service
* Press Up arrow and press Enter to Start keystone service again