Custom Search

Wednesday, November 27, 2013

Git How to sync your branch with master and apply latest changes (last commit) on top of that

1)
Suppose, right now your are in branch the 'mybranch'

a)
Git commit all changes

2)
Goto master

#git checkout master

3)
Update Master

#git pull



4)
Go back to the branch 'mybranch'

#git checkout mybranch

5)
Sync the branch 'mybranch' with master and apply our changes (last commit) on top of that

#git rebase -i master

6)
We should do above steps after each commit

Monday, November 25, 2013

Git How to commit only some selected files

1)
#git commit -m "message" file1 file2 file3

2) OR
#git commit file1 file2 file3 -m "message"

Friday, November 22, 2013

Python Django How to send email from python console

0)
First setup and configure email server (exim)

1)
Activate virtualenv

2)
Got you django project folder and run following command
#export DJANGO_SETTINGS_MODULE=myproject.settings



3)
Open python terminal and run following command
#python
import myproject.settings as setting


setting.EMAIL_HOST
setting.EMAIL_PORT
 

from django.core.mail import send_mail
 

send_mail('Subject here2', 'Here is the message1.', 'from@mymailserver.com', ['sajuptpm@gmail.com'], fail_silently=False)

Thursday, November 21, 2013

Openstack Keystone API V3 Python Keystoneclient Example

>>>
>>> from keystoneclient.v3 import client
>>>
>>> keystone = client.Client(token="tokentoken", endpoint="http://192.168.56.101:35357/v3")
>>>
>>> keystone.users.list()
>>>
>>> keystone.projects.list()
>>>
>>> keystone.domains.list()
>>>
>>> keystone.roles.list()
>>>

Tuesday, November 19, 2013

Working of Openstack Keystone Service

A)
Simple example : How to serve an app using python paste
################################################


1)
* config.ini

[app:main]
paste.app_factory = service_app:app_factory


2)
* deploy.py

from paste import httpserver
from paste.deploy import loadapp
app = loadapp('config:config.ini', relative_to='.')
httpserver.serve(app, host='127.0.0.1', port='8080')


3)
* service_app.py

def application(environ, start_response):
    """Simple WSGI application"""
    response_headers = [('Content-type','text/plain')]
    status = '200 OK'
    start_response(status, response_headers)
    if environ['PATH_INFO'] == '/login':
    #http://pythonpaste.org/modules/recursive.html
        return ["login page"]
    else:
        return ['Hello world']

def app_factory(global_config, **local_config):
    """This function wraps our simple WSGI app so it
    can be used with paste.deploy"""
    return application

4)
How to run

#python deploy.py
serving on http://127.0.0.1:8080

5)
goto the urls

http://127.0.0.1:8080
http://127.0.0.1:8080/login


B)
Keystone files
################

1)
vim /opt/stack/keystone/bin/keystone-all


def create_server(conf, name, host, port):
    app = deploy.loadapp('config:%s' % conf, name=name)
    server = environment.Server(app, host=host, port=port)
    return name, server

def serve(*servers):
    for name, server in servers:
        server.start()


2)
vim /opt/stack/keystone/keystone/common/environment/__init__.py


Server = eventlet_server.Server

3)
vim /opt/stack/keystone/keystone/common/environment/eventlet_server.py


class Server(object):
    def start(self, key=None, backlog=128):


4)
vim /opt/stack/keystone/etc/keystone-paste.ini


[filter:access_log]
paste.filter_factory = keystone.contrib.access:AccessLogMiddleware.factory

[app:public_service]
paste.app_factory = keystone.service:public_app_factory

[app:service_v3]
paste.app_factory = keystone.service:v3_app_factory

[app:admin_service]
paste.app_factory = keystone.service:admin_app_factory


How to serve an app using python paste

How to serve an app using python paste
###############################


1)
* config.ini

[app:main]
paste.app_factory = service_app:app_factory


2)
* deploy.py

from paste import httpserver
from paste.deploy import loadapp
app = loadapp('config:config.ini', relative_to='.')
httpserver.serve(app, host='127.0.0.1', port='8080')


3)
* service_app.py

def application(environ, start_response):
    """Simple WSGI application"""
    response_headers = [('Content-type','text/plain')]
    status = '200 OK'
    start_response(status, response_headers)
    if environ['PATH_INFO'] == '/login':
    #http://pythonpaste.org/modules/recursive.html
        return ["login page"]
    else:
        return ['Hello world']

def app_factory(global_config, **local_config):
    """This function wraps our simple WSGI app so it
    can be used with paste.deploy"""
    return application

4)
How to run

#python deploy.py
serving on http://127.0.0.1:8080

5)
goto the urls

http://127.0.0.1:8080
http://127.0.0.1:8080/login

Monday, November 18, 2013

How to keystone API update user details

0)
Set Host

#OPSENSTACK_HOST="192.168.56.101"

1)
#### Get token ####

#export TOKEN=`curl -si -d @keystone_auth_admin_cred.json -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/auth/tokens | awk '/X-Subject-Token/ {print $2}'`

#### keystone_auth_admin_cred.json ####
{
            "auth": {
                "identity": {
                    "methods": [
                "password"
                    ],
                    "password": {
                        "user": {
                            "domain": {
                                "name": "Default"
                            },
                            "name": "admin",
                            "password": "password"
                        }
                    }
                },
                "scope": {
                    "project": {
                        "domain": {
                            "name": "Default"
                        },
                        "name": "demo"
                    }
                }
            }
        }



2)
#### Check token ####

#echo $TOKEN

3)
#### List all users ####

##Port 35357
#curl -si -H "X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/users

4)
Update user details (change email and set enabled flag to true)

#curl -si -X "PATCH" -H "X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/users/ec11454782cd4703b618e106d18326fe -d @keystone_update_user_cred.json

######### keystone_update_user_cred.json #########

{
    "user": {
                "email":"cc@cc.com",
                "enabled":true
            }

}

5)
Ref
https://github.com/openstack/identity-api/blob/master/openstack-identity-api/v3/src/markdown/identity-api-v3.md


How to use Keystone API Version 3

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

a)
Export the IP of OpenStack host
OPSENSTACK_HOST="192.168.56.101"

b)
#### Get token ####

#export TOKEN=`curl -si -d @keystone_auth_admin_cred.json -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/auth/tokens | awk '/X-Subject-Token/ {print $2}'`

#### keystone_auth_admin_cred.json ####
{
            "auth": {
                "identity": {
                    "methods": [
                "password"
                    ],
                    "password": {
                        "user": {
                            "domain": {
                                "name": "Default"
                            },
                            "name": "admin",
                            "password": "password"
                        }
                    }
                },
                "scope": {
                    "project": {
                        "domain": {
                            "name": "Default"
                        },
                        "name": "demo"
                    }
                }
            }
        }

b)
#### Check token ####

#echo $TOKEN

c)
#### List all users ####
##Port 35357

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/users
or
##Port 5000

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:5000/v3/users

d)
##Get all projects

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:5000/v3/projects
e)
##Get all domains

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:5000/v3/domains

f)
##Get all roles

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:5000/v3/roles

g)
Create User

#curl -si -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/users -d @keystone_create_user_cred.json

h)
Grant role to user on domain

#curl -si -X "PUT" -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/domains/default/users/a007766b3e1747f89548bf5bf517f05c/roles/b4a1700504ef4250af9feed3d892aba1

i)
Grant role to user on project.

* After this we will not get the error "You are not authorized for any projects" while login.
#curl -si -X "PUT"  -H"X-Auth-Token:$TOKEN" -H "Content-type: application/json" http://$OPSENSTACK_HOST:35357/v3/projects/18dc2ae781034460a683eb9ca68a7b9c/users/a007766b3e1747f89548bf5bf517f05c/roles/b4a1700504ef4250af9feed3d892aba1

j)

mysql> use keystone
mysql> select id, name from project;
mysql> select id, name from domain;
mysql> select id, name from role;
mysql> select id, name from group;




How to call keystone API from Horizon

How to call keystone API from Horizon

* Location of Keystone API in Horizon
./horizon/openstack_dashboard/api/keystone.py

a)
* Get endpoing url

def _get_endpoint_url(request, endpoint_type, catalog=None):
    auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL')
    return url

b)
* Get a client connected to the Keystone backend

def keystoneclient(request, admin=False):
    api_version = VERSIONS.get_active_version()
    cache_attr = "_keystoneclient_admin" if admin else backend.KEYSTONE_CLIENT_ATTR
    endpoint = _get_endpoint_url(request, endpoint_type)
        conn = api_version['client'].Client(token=user.token.id,
                                            endpoint=endpoint)
        setattr(request, cache_attr, conn)
        return conn

c)
* Create User

def user_create(request, name=None, email=None, password=None, project=None, enabled=None, domain=None):
    ###Get a client connected to the Keystone backend
    manager = keystoneclient(request, admin=True).users
    if VERSIONS.active < 3:
    ###Make API call
        user = manager.create(name, password, email, project, enabled)
        return VERSIONS.upgrade_v2_user(user)
    else:
    ###Make API call
        return manager.create(name, password=password, email=email, project=project, enabled=enabled, domain=domain)

d)
VERSIONS = IdentityAPIVersionManager("identity", preferred_version=3)

e)

# Set up our data structure for managing Identity API versions, and
# add a couple utility methods to it.
class IdentityAPIVersionManager(base.APIVersionManager):
    def upgrade_v2_user(self, user):
        if getattr(user, "project_id", None) is None:
            user.project_id = getattr(user, "tenantId", None)
        return user

    def get_project_manager(self, *args, **kwargs):
        if VERSIONS.active < 3:
            manager = keystoneclient(*args, **kwargs).tenants
        else:
            manager = keystoneclient(*args, **kwargs).projects
        return manager

f)
./horizon/openstack_dashboard/api/base.py
class APIVersionManager(object):

Openstack Horizon flow of create user action

1)
* horizon uses the app "openstack_auth" for authentication
AUTHENTICATION_BACKENDS = ('openstack_auth.backend.KeystoneBackend',)

2)
* Horizon settings.py

./horizon/openstack_dashboard/settings.py

3)
* Horizon settings.py (Keystone settings)

AUTHENTICATION_BACKENDS = ('openstack_auth.backend.KeystoneBackend',)
OPENSTACK_KEYSTONE_URL = "http://localhost:5000/v2.0"
OPENSTACK_KEYSTONE_DEFAULT_ROLE = "Member"

OPENSTACK_KEYSTONE_MULTIDOMAIN_SUPPORT = True
OPENSTACK_KEYSTONE_DEFAULT_DOMAIN = 'test_domain'

OPENSTACK_KEYSTONE_BACKEND = {
    'name': 'native',
    'can_edit_user': True,
    'can_edit_group': True,
    'can_edit_project': True,
    'can_edit_domain': True,
    'can_edit_role': True
}

POLICY_FILES = {
    'identity': 'keystone_policy.json',
    'compute': 'nova_policy.json'
}

Create User Flow
############

1)
* Url

http://192.168.56.101/admin/users/create/

2)
* urls.py

./horizon/openstack_dashboard/dashboards/admin/users/urls.py

3)
* views.py

./horizon/openstack_dashboard/dashboards/admin/users/views.py
import forms as project_forms
class CreateView(forms.ModalFormView):
    form_class = project_forms.CreateUserForm

4)
* Keystone API

./horizon/openstack_dashboard/api/keystone.py

5)
* forms.py

./horizon/openstack_dashboard/dashboards/admin/users/forms.py
class CreateUserForm(BaseUserForm):
    def handle(self, request, data):
        ##Keystone API call to create user
        new_user = api.keystone.user_create()

Flow of Horizon Keystone API
######################


* How to call keystone API from Horizon
1)
* Keystone API
./horizon/openstack_dashboard/api/keystone.py

a)
* Get endpoing url

def _get_endpoint_url(request, endpoint_type, catalog=None):
    auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL')
    return url

b)
* Get a client connected to the Keystone backend

def keystoneclient(request, admin=False):
    api_version = VERSIONS.get_active_version()
    cache_attr = "_keystoneclient_admin" if admin else backend.KEYSTONE_CLIENT_ATTR
    endpoint = _get_endpoint_url(request, endpoint_type)
        conn = api_version['client'].Client(token=user.token.id,
                                            endpoint=endpoint)
        setattr(request, cache_attr, conn)
        return conn

c)
* Create User

def user_create(request, name=None, email=None, password=None, project=None, enabled=None, domain=None):
    ###Get a client connected to the Keystone backend
    manager = keystoneclient(request, admin=True).users
    if VERSIONS.active < 3:
    ###Make API call
        user = manager.create(name, password, email, project, enabled)
        return VERSIONS.upgrade_v2_user(user)
    else:
    ###Make API call
        return manager.create(name, password=password, email=email, project=project, enabled=enabled, domain=domain)

d)
VERSIONS = IdentityAPIVersionManager("identity", preferred_version=3)

e)

# Set up our data structure for managing Identity API versions, and
# add a couple utility methods to it.
class IdentityAPIVersionManager(base.APIVersionManager):
    def upgrade_v2_user(self, user):
        if getattr(user, "project_id", None) is None:
            user.project_id = getattr(user, "tenantId", None)
        return user

    def get_project_manager(self, *args, **kwargs):
        if VERSIONS.active < 3:
            manager = keystoneclient(*args, **kwargs).tenants
        else:
            manager = keystoneclient(*args, **kwargs).projects
        return manager

f)
./horizon/openstack_dashboard/api/base.py
class APIVersionManager(object):

Keystone Service Ports
###############

a)
Both ports 35357 and 5000 are used by keystone.
The first (35357) is used in internal and administrative requests while 5000 should be used by public requests.

https://github.com/mseknibilel/OpenStack-Folsom-Install-guide/issues/17
b)
Horizon using following endpoint of keystone service if we alogged in as horizon admin
*endpoint:http://10.0.3.15:35357/v3

c)
This endpoint http://10.0.3.15:35357/v3 is comming from the django app "django_openstack_auth"
* https://github.com/gabrielhurley/django_openstack_auth
* Search for the port 35357
* ./django_openstack_auth/openstack_auth/tests/data_v3.py

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



Thursday, November 14, 2013

VIM Editor how to goto the 150th byte (character) in the file

:goto 150
will poing you to the 150th byte in the buffer

Git how to take diff

1)
Find log
#git log -n 9

2)
Take diff between two commits
# git diff prev-commit-id..next-commit-id > ~/Desktop/mychanges.diff

#git diff 7a51bc7ddd57b39fa3cd..1f81be4c220dbc8cc > ~/Desktop/mychanges.diff

3)
How to see changes in a commit
$git diff 14bbce77a91e666f779ae9feb98ea30aa6249564^!

$git diff 14bbce77a91e666f779ae9feb98ea30aa6249564^! filename.py

4)
Git How to search in diff and commit message and find commit id

http://fosshelp.blogspot.in/2015/06/git-how-to-search-in-diff-and-commit.html







How to use Devstack screen (log of all openstack services)

1)
Open the screen

screen -x

2)
Close the screen
Ctrl + a + d

3)
Move to Next service

Ctrl + a, n

4)
Move to previous service

Ctrl + a, p

5)
Scroll Up and Down

Ctrl + A, Esc, 'Up' and 'Down' arrow

6)
To stop logging of particular service

Ctrl + c
To start logging again use 'Up' arrow and select logging command and press Enter button.

Wednesday, November 13, 2013

Openstack devstack HTTPConnectionPool Connection timed out [Fixed]

Issue
=====
Exception Value:HTTPConnectionPool(host='10.0.2.15', port=8774): Max retries exceeded with url: /v2/b831390e0cb04f1eafbdd39bfddb7bd6/extensions (Caused by : [Errno 110] Connection timed out)

Issue Hint
========
* You may changed the IP of the VM/System where openstack is running
* Here IP 10.0.2.15 is the old IP of VM/System

Fix
===
1)
Edit "localrc" file and add following lines
##don't download from github again and again
RECLONE=no
##Interface of VM/System, where you see the IP (new IP)
PUBLIC_INTERFACE=eth0

2)
#./unstack.sh
#./stack.sh



Monday, November 11, 2013

How to install recordmydesktop in Ubuntu 13.10

#sudo apt-get install gtk-recordmydesktop

How to install virtualbox in Ubuntu 13.10

#sudo apt-get update

#sudo apt-get upgrade

#sudo apt-get install virtualbox

Wednesday, November 6, 2013

Python Django Configure Exim Mail Server

1)Install Exim
#apt-get update
#apt-get upgrade
#apt-get install exim4-daemon-light mailutils



2)Configure Exim
#dpkg-reconfigure exim4-config

3)
Settings.py Changes
EMAIL_HOST = 'localhost'
EMAIL_PORT = 25

How to git revert local changes of single file

1)
Method-1
#git status
#git checkout filename
#git status

2)
Method-2 
a)
remove all changes (local changes) from HEAD and save them somewhere else
#git stash

b)
Do something like update repository(git pull), change branch and update, et--
 
c)
restore all changes again
#git stash pop