Custom Search

Thursday, January 31, 2013

How to do port forwarding with socat simple example




Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them.

===========================

* Openstack Cloud Service Listening on port 8773 by default
http://192.168.12.222:8773/services/Cloud

* Openstack dashboard/horizon/apache2 Listening on port 80 by default
http://192.168.12.222

===========================

How to forward connection to port 8000 to port 8773 (port of Openstack Cloud Service) using port forwarding
-----------------------------------------------------

* Goto Openstack server and run following command
* Here socat waits for a connection to port 8000 and forward it to port 8773 of same machine.
#sudo socat TCP-LISTEN:8000,fork TCP:localhost:8773

* Open following URL in the browser tocheck port forwarding from 8000 to 8773
http://192.168.12.222:8000/services/Cloud

===========================

How to forward connection to port 80 to 8773 (port of Openstack Cloud Service) using port forwarding
-----------------------------------------------------

* Here socat waits for a connection to port 8000 and forward it to port 8773 of same machine.
#sudo socat TCP-LISTEN:80,fork TCP:localhost:8773

===========================

* Find Out Which Process Is Listening(Open) on Port 80
#sudo netstat -tuplen | grep 80

===========================

* Stop apache2 and close the port 80
#sudo /etc/init.d/apache2 stop

===========================

* Check whether port 80 is Open or Close
#sudo netstat -tuplen | grep 80

===========================

* Here socat waits for a connection to port 80 and forward it to port 8773 of same machine.
#sudo socat TCP-LISTEN:80,fork TCP:localhost:8773

* Open following URL in the browser to check port forwarding from 80 to 8773
http://192.168.12.222/services/Cloud

===========================

* Start apache2 and open port 80

#sudo netstat -tuplen | grep 80
#sudo /etc/init.d/apache2 start
#sudo netstat -tuplen | grep 80


* Open following URL in the browser to check whether apache is Listening on Port 80
http://192.168.12.222

===========================

Monday, January 28, 2013

How to Install and Configure Proxy Server Authentication with Squid3 in Ubuntu 12

1) 
Install squid3
#sudo apt-get install squid3


2)
Define passwd file for squid
#sudo htpasswd -c /etc/squid3/passwd saju



3)
Locate the auth helper
#ls /usr/lib/squid3/


4)
Edit /etc/squid3/squid.conf file and add following lines.

auth_param basic program /usr/lib/squid3/ncsa_auth /etc/squid3/passwd
auth_param basic children 5
auth_param basic realm saju-Inspiron-N5010
auth_param basic credentialsttl 2 hours
auth_param basic casesensitive off

##Define ncsa_auth acl
acl ncsa_users proxy_auth REQUIRED
http_access allow ncsa_users


5)
Restart squid3
#sudo /etc/init.d/squid3 restart


6)
Access Log
#sudo tail -f /var/log/squid3/access.log


7)
Test Proxy authentication with Firefox
Goto Edit --> Preferences -->Advanced --->Network --> Settings
* Select "Manual proxy configuration"
* Http Proxy : 192.168.1.4
* Port : 3128

8)
Test Proxy authentication with python

import urllib2
proxy = urllib2.ProxyHandler({'http': 'http://saju:password@192.168.1.6:3128'})
auth = urllib2.ProxyDigestAuthHandler()
opener = urllib2.build_opener(proxy, auth)
urllib2.install_opener(opener)
conn = urllib2.urlopen('http://python.org')
result = conn.read()
print "========result======", result


9)
Test Proxy authentication with wget

#wget www.google.com

#export http_proxy="http://saju:password@192.168.1.4:3128"
#wget www.google.com

#export http_proxy="http://192.168.1.4:3128"
#wget www.google.com

Tuesday, January 22, 2013

devstack Tricks

1) VM or Instance log
===============

 sudo vim /opt/stack/data/nova/instances/instance-00000001/console.log

sudo sudo vim /var/log/libvirt/qemu/instance-00000001.log

sudo vim /var/log/libvirt/libvirtd.log



2) log
=====

How to open log files
# screen -r

#how to exit from log files
Ctrl + a + d

##Switch between logs
ctrl + a + will get you to the appropriate console between 0 and 9


##if you want to scroll up/down, then I believe you need to press
ctrl + a
then
[
then
you can then use the up and down arrows to navigate the logs


Friday, January 18, 2013

How to fix Openstack Login Error An error occurred authenticating. Please try again later


How to fix Openstack Login Error An error occurred authenticating. Please try again later

* Horizon use keystone to authenticate.
* devstack run all the service in a screen session.
* When you reboot, this screen session is stopped and all the services with it.
* Running "rejoin-stack.sh" relaunch the screen session and the associated
* services (notably keystone).
* Without this script, no keystone => no auth.
* So run the script ./rejoin-stack.sh and login in horizon after.

Ctrl + a + d to exit

 

Wednesday, January 16, 2013

Howto Install Openstack using devstack







http://devstack.org/

OpenStack on VMs
http://devstack.org/guides/single-vm.html

=================

apt-get update

---------------

apt-get install -qqy git

---------------

##Download DevStack
git clone https://github.com/openstack-dev/devstack.git

##Switch to branch essex of DevStack, So it will install Openstack essex.
git checkout -b stable/essex origin/stable/essex

---------------

cd devstack

---------------

echo ADMIN_PASSWORD=password > localrc
echo MYSQL_PASSWORD=password >> localrc
echo RABBIT_PASSWORD=password >> localrc
echo SERVICE_PASSWORD=password >> localrc
echo SERVICE_TOKEN=tokentoken >> localrc
echo FLAT_INTERFACE=br100 >> localrc

---------------

./stack.sh

---------------

Tuesday, January 15, 2013

Howto Setup Netbeans 7 and Python IDE

howto install netbeans 7.x python IDE on ubuntu




0)
Download and install netbeans
http://dlc.sun.com.edgesuite.net/netbeans/7.3/beta2/bundles/netbeans-7.3beta2-php-linux.sh

* chmod +x netbeans-7.3beta2-php-linux.sh
* ./netbeans-7.3beta2-php-linux.sh

1)
Start NetBeans 7

2)
Click on Tools > Plugins 

3)
When the Plugins window opens click the Settings Tab

4)
In the Settings tab click the Add button

5)
Type whatever Name you want for this, I used 'Developer Updates'

6)
Under URL put http://deadlock.netbeans.org/hudson/job/nbms-and-javadoc/lastStableBuild/artifact/nbbuild/nbms/updates.xml.gz and click OK

7)
The plugin list should refersh and may take a moment, when it does click on the 'Available Plugins' tab.

8)
In the search box there type 'python' to find the python plugin, check the box for that plugin and click Install.

Monday, January 14, 2013

recordmydesktop how to recover files from session

recordmydesktop how to Restore session

Syntax
=====
#recordmydesktop --rescue path_to_data

Example
======
saju@saju-Inspiron-N5010:~$ recordmydesktop --rescue /home/saju/rMD-session-7580/
Restoring /home/saju/rMD-session-7580/!!!
STATE:ENCODING
Encoding started!
This may take several minutes.
Pressing Ctrl-C will cancel the procedure (resuming will not be possible, but
any portion of the video, which is already encoded won't be deleted).
Please wait...
Output file: /home/saju/pp.ogv
[0%]
Encoding finished!
Wait a moment please...
  
Done.
Written 118902532 bytes
(118902532 of which were video data and 0 audio data)

Done!!!
Goodbye!
saju@saju-Inspiron-N5010:~$

Python Flask Video Tutorials For Beginners

Friday, January 11, 2013

create pyramid application with sqlalchemy video tutorial

How to create a pyramid application with sqlalchemy video tutorial




Thursday, January 10, 2013

openstack devstack how to open log files

How to open log files
# screen -r

#how to exit from log files
Ctrl + a + d

##Switch between logs
ctrl + a + will get you to the appropriate console between 0 and 9


##if you want to scroll up/down, then I believe you need to press
ctrl + a
then
[
then
you can then use the up and down arrows to navigate the logs

Wednesday, January 9, 2013

How to find all numbers divisible by 2 and 3 from a list using comprehension


With list comprehension
 ===================
>>>
>>> numbers = [1, 2, 3, 5, 8, 13, 51, 72]
>>>
>>>
>>>
>>> [ [num for num in numbers if num%devi == 0] for devi in [2, 3] ]
[[2, 8, 72], [3, 51, 72]]
>>>
>>>

Without list comprehension
=====================
>>> result = []
>>> for devi in [2,3]:
...     res = []
...     for num in numbers:
...             if num%devi == 0:
...                     res.append(num)
...     result.append(res)
...
>>>
>>>
>>>
>>> result
[[2, 8, 72], [3, 51, 72]]
>>>
>>>
>>>

Tuesday, January 8, 2013

how to setup iscsi with ubuntu linux

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ cat /proc/partitions
major minor  #blocks  name

   7        1    2048000 loop1
   8        0  488386584 sda
   8        1  102400000 sda1
   8        2  102400000 sda2
   8        3  102400000 sda3
   8        4          1 sda4
   8        5  102400000 sda5
   8        6   78781440 sda6
  11        0    1048575 sr0
   8       16  488386584 sdb
 252        0    1048576 dm-0
saju@saju-desktop:/mnt/iscsi$

-------------------------------------


1) Setup iSCSI Target
================

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo dd if=/dev/zero of=/mnt/iscsi/mydisk bs=100M count=20
20+0 records in
20+0 records out
2097152000 bytes (2.1 GB) copied, 19.2759 s, 109 MB/s
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo losetup /dev/loop1 /mnt/iscsi/mydisk
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

# create a phisical volume
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo pvcreate /dev/loop1
  Physical volume "/dev/loop1" successfully created
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

# create a volume group named "myvolume-group1"
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo vgcreate -s 32M myvolume-group1 /dev/loop1
  Volume group "myvolume-group1" successfully created
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

# create a logical volume named "mylogical_volume1"
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo lvcreate -L 1G -n mylogical_volume1 myvolume-group1
  Logical volume "mylogical_volume1" created
saju@saju-desktop:/mnt/iscsi$

---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo apt-get install iscsitarget iscsitarget-dkms
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

##Enable ISCSITARGET_ENABLE
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo vim /etc/default/iscsitarget
ISCSITARGET_ENABLE=true

---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo vim /etc/iet/ietd.conf
# add at the last
# naming rule : [ iqn.yaer-month.domain:any name ]
Target iqn.2012-07.world.server:target0
# provided devicce as a iSCSI target
Lun 0 Path=/dev/myvolume-group1/mylogical_volume1,Type=blockio
# iSCSI Initiator's IP address you allow to connect
initiator-address 127.0.0.1
# authentication info ( set anyone you like for "username", "password" )
incominguser saju1 saju1


---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo service iscsitarget restart
 * Removing iSCSI enterprise target devices:                                                                                          [ OK ]
 * Starting iSCSI enterprise target service                                                                                           [ OK ]
                                                                                                                                      [ OK ]
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

# confirm status
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo ietadm --op show --tid=1
Wthreads=8
Type=0
QueuedCommands=32
NOPInterval=0
NOPTimeout=0
saju@saju-desktop:/mnt/iscsi
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$


 2) Setup iSCSI Initiator
==================

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo apt-get install open-iscsi
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo vim /etc/iscsi/iscsid.conf

# line 39: uncomment
node.session.auth.authmethod = CHAP

# line 43,44: uncomment and set username and password which set on iSCSI Target
node.session.auth.username = saju1
node.session.auth.password = saju1


---------------------------

#### discover target
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo iscsiadm -m discovery -t sendtargets -p 127.0.0.1
127.0.0.1:3260,1 iqn.2012-07.world.server:target0
192.168.1.15:3260,1 iqn.2012-07.world.server:target0
10.4.0.50:3260,1 iqn.2012-07.world.server:target0
169.254.9.145:3260,1 iqn.2012-07.world.server:target0
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

#### confirm status after discovery
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo iscsiadm -m node -o show
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$


---------------------------

####  login to target
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo iscsiadm -m node --login
Logging in to [iface: default, target: iqn.2012-07.world.server:target0, portal: 127.0.0.1,3260]
Logging in to [iface: default, target: iqn.2012-07.world.server:target0, portal: 192.168.1.15,3260]
Logging in to [iface: default, target: iqn.2012-07.world.server:target0, portal: 10.4.0.50,3260]
Logging in to [iface: default, target: iqn.2012-07.world.server:target0, portal: 169.254.9.145,3260]
Login to [iface: default, target: iqn.2012-07.world.server:target0, portal: 127.0.0.1,3260]: successful
Login to [iface: default, target: iqn.2012-07.world.server:target0, portal: 192.168.1.15,3260]: successful
Login to [iface: default, target: iqn.2012-07.world.server:target0, portal: 10.4.0.50,3260]: successful
Login to [iface: default, target: iqn.2012-07.world.server:target0, portal: 169.254.9.145,3260]: successful
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

####  confirm session
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ sudo iscsiadm -m session -o show
tcp: [1] 127.0.0.1:3260,1 iqn.2012-07.world.server:target0
tcp: [2] 192.168.1.15:3260,1 iqn.2012-07.world.server:target0
tcp: [3] 10.4.0.50:3260,1 iqn.2012-07.world.server:target0
tcp: [4] 169.254.9.145:3260,1 iqn.2012-07.world.server:target0
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$


---------------------------

saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$ cat /proc/partitions
major minor  #blocks  name

   7        1    2048000 loop1
   8        0  488386584 sda
   8        1  102400000 sda1
   8        2  102400000 sda2
   8        3  102400000 sda3
   8        4          1 sda4
   8        5  102400000 sda5
   8        6   78781440 sda6
  11        0    1048575 sr0
   8       16  488386584 sdb <== Added new device provided from target
 252        0    1048576 dm-0
   8       32    1048576 sdc
   8       48    1048576 sdd
   8       64    1048576 sde
   8       80    1048576 sdf
saju@saju-desktop:/mnt/iscsi$
saju@saju-desktop:/mnt/iscsi$

---------------------------

python create zero matrix

using for loop
===========
>>>
>>> m = 5
>>> n = 5
>>>
>>>
>>>
>>> res = []
>>> for x in range(m):
...     r = []
...     for y in range(n):
...             r.append(0)
...     res.append(r)

>>>
>>>
>>>
>>> res
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>>
>>>

using listcomprehesion
=================
>>>
>>> [ [0] for x in range(m) for y in range(n) ]
[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
>>>
>>>
>>>
>>> [ [ 0 for x in range(m) ] for y in range(n) ]
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>>
>>>
>>>

Thursday, January 3, 2013

Introduction pyramid __init__.py and views.py

Introduction to the pyramid __init__.py and views.py

a)   

An __init__.py file signifies that this is a Python package. It also contains code that helps users run the application, including a main function which is used as a entry point for commands such as pserve, pshell, pviews, and others.

b)   
A templates directory, which contains Chameleon (or other types of) templates.

c)   
A tests.py module, which contains unit test code for the application.

d)   
A views.py module, which contains view code for the application.

a)
__init__.py
========

We need a small Python module that configures our application and which advertises an entry point for use by our PasteDeploy .ini file. This is the file named __init__.py. The presence of an __init__.py also informs Python that the directory which contains it is a package.

from pyramid.config import Configurator
from sqlalchemy import engine_from_config

from .models import (
    DBSession,
    Base,
    )


def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    print "===__init_.py===main===global_config===", global_config
    print "===__init_.py===main===settings===", settings
    engine = engine_from_config(settings, 'sqlalchemy.')
    DBSession.configure(bind=engine)
    Base.metadata.bind = engine
    config = Configurator(settings=settings)
    config.add_static_view('static', 'static', cache_max_age=3600)
    config.add_route('home', '/')
    config.scan()
    return config.make_wsgi_app()

-----------------------

===__init_.py===main===global_config===
{'__file__': '/home/saju/pyra_env/test/alchemy_proj/development.ini',
'here': '/home/saju/pyra_env/test/alchemy_proj'}

===__init_.py===main===settings===
{'pyramid.includes': '\npyramid_debugtoolbar\npyramid_tm',
'sqlalchemy.url': 'mysql://root:xxxx@localhost:3306/mydb1?charset=utf8',
'pyramid.debug_authorization': 'false',
'pyramid.default_locale_name': 'en',
'pyramid.reload_templates': 'true',
'pyramid.debug_notfound': 'false',
'pyramid.debug_routematch': 'false'}

b)
views.py
=======

Much of the heavy lifting in a Pyramid application is done by view callables. A view callable is the main tool of a Pyramid web application developer; it is a bit of code which accepts a request and which returns a response.

from pyramid.view import view_config

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    return {'project':'MyProject'}

Above code define and register a view callable named my_view. The function named my_view is decorated with a view_config decorator (which is processed by the config.scan() line in our __init__.py). The view_config decorator asserts that this view be found when a route named home is matched. In our case, because our __init__.py maps the route named home to the URL pattern /, this route will match when a visitor visits the root URL. The view_config decorator also names a renderer, which in this case is a template that will be used to render the result of the view callable.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

Introduction pyramid development.ini production.ini MANIFEST.in and setup.py

Introduction to the pyramid development.ini production.ini MANIFEST.in and setup.py

1)
development.ini
============

a)
The development.ini file is a PasteDeploy configuration file. Its purpose is to specify an application to run when you invoke pserve, as well as the deployment settings provided to that application.

b)
The [app:main] section represents configuration for your Pyramid application. The use setting is the only setting required to be present in the [app:main] section. Its default value, egg:MyProject, indicates that our MyProject project contains the application that should be served. Other settings added to this section are passed as keyword arguments to the function named main in our package’s __init__.py module. You can provide startup-time configuration parameters to your application by adding more settings to this section.

c)

The name main in [app:main] signifies that this is the default application run by pserve when it is invoked against this configuration file. The name main is a convention used by PasteDeploy signifying that it is the default application.

d)
The [server:main] section of the configuration file configures a WSGI server which listens on TCP port 6543. It is configured to listen on all interfaces (0.0.0.0). This means that any remote system which has TCP access to your system can see your Pyramid application.

2)
production.ini
===========

The production.ini file is a PasteDeploy configuration file with a purpose much like that of development.ini. However, it disables the debug toolbar, and filters all log messages except those above the WARN level. It also turns off template development options such that templates are not automatically reloaded when changed, and turns off all debugging options. This file is appropriate to use instead of development.ini when you put your application into production.

It’s important to use production.ini (and not development.ini) to benchmark your application and put it into production. development.ini configures your system with a debug toolbar that helps development, but the inclusion of this toolbar slows down page rendering times by over an order of magnitude. The debug toolbar is also a potential security risk if you have it configured incorrectly.

3)
MANIFEST.in
==========
a)

The MANIFEST.in file is a distutils configuration file which specifies the non-Python files that should be included when a distribution of your Pyramid project is created when you run python setup.py sdist. Due to the information contained in the default MANIFEST.in, an sdist of your Pyramid project will include .txt files, .ini files, .rst files, graphics files, and template files, as well as .py files.

4)
setup.py
=======

a)
The setup.py file is a setuptools setup file. It is meant to be run directly from the command line to perform a variety of functions, such as testing your application, packaging, and distributing your application.
The setup.py file calls the setuptools setup function, which does various things depending on the arguments passed to setup.py on the command line.

b)

Usually you only need to think about the contents of the setup.py file when distributing your application to other people, when adding Python package dependencies, or when versioning your application for your own use. For fun, you can try this command now:

$ python setup.py sdist

This will create a tarball of your application in a dist subdirectory named MyProject-0.1.tar.gz. You can send this tarball to other people who want to install and use your application.

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

script to restore large database and calculate time taken

Script
====

MYSQL_USER="root"
MYSQL_PASSWORD="xxxx"
DATABASE_NAME="c1"

echo "Restoring File:$1";
   
echo "Start Time:$(date)"
   
(
    #echo "SET AUTOCOMMIT=0;"
    echo "SET UNIQUE_CHECKS=0;"
    echo "SET FOREIGN_KEY_CHECKS=0;"
    cat "$1"
    echo "SET FOREIGN_KEY_CHECKS=1;"
    echo "SET UNIQUE_CHECKS=1;"
    #echo "SET AUTOCOMMIT=1;"
    #echo "COMMIT;"
) | mysql -u"$MYSQL_USER" -p"$MYSQL_PASSWORD" "$DATABASE_NAME"


echo "Start Time:$(date)"


How to Run
========
a)
Save above script in a file eg:sqlbackup.sql

b)
run the script with backup file name
#./restorescript.sh sqlbackup.sql


Output
=====
#./restorescript.sh sqlbackup.sql
Restoring File:convirt_ee-20121225
Start Time:Thu Jan  3 16:05:21 IST 2013
End Time:Thu Jan  3 16:50:21 IST 2013


Wednesday, January 2, 2013

How to create and run a pyramid alchemy project

How to create and run a pyramid alchemy project
===================================

(pyra_env)saju@saju-desktop:~/pyra_env/test$ pcreate -s alchemy alchemy_proj
Creating directory /home/saju/pyra_env/test/alchemy_proj
  Recursing into +package+
    Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/
    Copying __init__.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/__init__.py
    Copying models.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/models.py
    Recursing into scripts
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/
      Copying __init__.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/__init__.py
      Copying initializedb.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/initializedb.py
    Recursing into static
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/
      Copying favicon.ico to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/favicon.ico
      Copying footerbg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/footerbg.png
      Copying headerbg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/headerbg.png
      Copying ie6.css to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/ie6.css
      Copying middlebg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/middlebg.png
      Copying pylons.css to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pylons.css
      Copying pyramid-small.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pyramid-small.png
      Copying pyramid.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pyramid.png
      Copying transparent.gif to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/transparent.gif
    Recursing into templates
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/templates/
      Copying mytemplate.pt_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/templates/mytemplate.pt
    Copying tests.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/tests.py
    Copying views.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/views.py
  Copying CHANGES.txt_tmpl to /home/saju/pyra_env/test/alchemy_proj/CHANGES.txt
  Copying MANIFEST.in_tmpl to /home/saju/pyra_env/test/alchemy_proj/MANIFEST.in
  Copying README.txt_tmpl to /home/saju/pyra_env/test/alchemy_proj/README.txt
  Copying development.ini_tmpl to /home/saju/pyra_env/test/alchemy_proj/development.ini
  Copying production.ini_tmpl to /home/saju/pyra_env/test/alchemy_proj/production.ini
  Copying setup.cfg_tmpl to /home/saju/pyra_env/test/alchemy_proj/setup.cfg
  Copying setup.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/setup.py
Welcome to Pyramid.  Sorry for the convenience.
(pyra_env)saju@saju-desktop:~/pyra_env/test$


* The setup.py file in that directory can be used to distribute your application,
or install your application for deployment or development.

* To install a newly created project for development, you should cd to the newly
created project directory and run the command "#python setup.py develop".

* The file named setup.py will be in the root of the pcreate-generated project directory.

* The command "#python setup.py develop" will install a distribution representing your
project into the interpreter’s library set so it can be found by import statements and
by other console scripts such as pserve, pshell, proutes and pviews.


python setup.py develop
==================

(pyra_env)saju@saju-desktop:~/pyra_env/test/alchemy_proj$ python setup.py develop
running develop
running egg_info
creating alchemy_proj.egg-info
writing requirements to alchemy_proj.egg-info/requires.txt
writing alchemy_proj.egg-info/PKG-INFO
writing top-level names to alchemy_proj.egg-info/top_level.txt
writing dependency_links to alchemy_proj.egg-info/dependency_links.txt
...................
........this will install all dependencies...........
...................


Initialize Database
==============

a)
Open development.ini and add following line
sqlalchemy.url=mysql://root:paswword@localhost:3306/mydb1?charset=utf8

b)
Open models.py and replace "name = Column(Text, unique=True)" with "name = Column(String(255), unique=True)"

c)
(pyra_env)saju@saju-desktop:~/pyra_env/test/alchemy_proj$ initialize_alchemy_proj_db development.ini
2013-01-02 19:56:02,084 INFO  [sqlalchemy.engine.base.Engine][MainThread] SELECT DATABASE()
2013-01-02 19:56:02,085 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2013-01-02 19:56:02,086 INFO  [sqlalchemy.engine.base.Engine][MainThread] SHOW VARIABLES LIKE 'character_set%%'
2013-01-02 19:56:02,086 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2013-01-02 19:56:02,087 INFO  [sqlalchemy.engine.base.Engine][MainThread] SHOW VARIABLES LIKE 'sql_mode'
2013-01-02 19:56:02,087 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2013-01-02 19:56:02,087 INFO  [sqlalchemy.engine.base.Engine][MainThread] DESCRIBE `models`
2013-01-02 19:56:02,088 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2013-01-02 19:56:02,088 INFO  [sqlalchemy.engine.base.Engine][MainThread] ROLLBACK
2013-01-02 19:56:02,088 INFO  [sqlalchemy.engine.base.Engine][MainThread]
CREATE TABLE models (
    id INTEGER NOT NULL AUTO_INCREMENT,
    name VARCHAR(255),
    value INTEGER,
    PRIMARY KEY (id),
    UNIQUE (name)
)


2013-01-02 19:56:02,089 INFO  [sqlalchemy.engine.base.Engine][MainThread] ()
2013-01-02 19:56:02,183 INFO  [sqlalchemy.engine.base.Engine][MainThread] COMMIT
2013-01-02 19:56:02,184 INFO  [sqlalchemy.engine.base.Engine][MainThread] BEGIN (implicit)
2013-01-02 19:56:02,184 INFO  [sqlalchemy.engine.base.Engine][MainThread] INSERT INTO models (name, value) VALUES (%s, %s)
2013-01-02 19:56:02,185 INFO  [sqlalchemy.engine.base.Engine][MainThread] ('one', 1)
2013-01-02 19:56:02,185 INFO  [sqlalchemy.engine.base.Engine][MainThread] COMMIT
(pyra_env)saju@saju-desktop:~/pyra_env/test/alchemy_proj$


Running the Application
==================

(pyra_env)saju@saju-desktop:~/pyra_env/test/alchemy_proj$ pserve development.ini --reload
Starting subprocess with file monitor
===in  add_route=== home /
===in add_view=== function my_view at 0x3c81aa0> home
Starting server in PID 27614.
serving on http://0.0.0.0:6543

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

how to create and run a pyramid project

1) How to create starter project
======================

(pyra_env)saju@saju-desktop:~/pyra_env/test$ pcreate -s starter starter_proj
Creating directory /home/saju/pyra_env/test/starter_proj
  Recursing into +package+
    Creating /home/saju/pyra_env/test/starter_proj/starter_proj/
    Copying __init__.py to /home/saju/pyra_env/test/starter_proj/starter_proj/__init__.py
    Recursing into static
      Creating /home/saju/pyra_env/test/starter_proj/starter_proj/static/
      Copying favicon.ico to /home/saju/pyra_env/test/starter_proj/starter_proj/static/favicon.ico
      Copying footerbg.png to /home/saju/pyra_env/test/starter_proj/starter_proj/static/footerbg.png
      Copying headerbg.png to /home/saju/pyra_env/test/starter_proj/starter_proj/static/headerbg.png
      Copying ie6.css to /home/saju/pyra_env/test/starter_proj/starter_proj/static/ie6.css
      Copying middlebg.png to /home/saju/pyra_env/test/starter_proj/starter_proj/static/middlebg.png
      Copying pylons.css to /home/saju/pyra_env/test/starter_proj/starter_proj/static/pylons.css
      Copying pyramid-small.png to /home/saju/pyra_env/test/starter_proj/starter_proj/static/pyramid-small.png
      Copying pyramid.png to /home/saju/pyra_env/test/starter_proj/starter_proj/static/pyramid.png
      Copying transparent.gif to /home/saju/pyra_env/test/starter_proj/starter_proj/static/transparent.gif
    Recursing into templates
      Creating /home/saju/pyra_env/test/starter_proj/starter_proj/templates/
      Copying mytemplate.pt_tmpl to /home/saju/pyra_env/test/starter_proj/starter_proj/templates/mytemplate.pt
    Copying tests.py_tmpl to /home/saju/pyra_env/test/starter_proj/starter_proj/tests.py
    Copying views.py_tmpl to /home/saju/pyra_env/test/starter_proj/starter_proj/views.py
  Copying CHANGES.txt_tmpl to /home/saju/pyra_env/test/starter_proj/CHANGES.txt
  Copying MANIFEST.in_tmpl to /home/saju/pyra_env/test/starter_proj/MANIFEST.in
  Copying README.txt_tmpl to /home/saju/pyra_env/test/starter_proj/README.txt
  Copying development.ini_tmpl to /home/saju/pyra_env/test/starter_proj/development.ini
  Copying production.ini_tmpl to /home/saju/pyra_env/test/starter_proj/production.ini
  Copying setup.cfg_tmpl to /home/saju/pyra_env/test/starter_proj/setup.cfg
  Copying setup.py_tmpl to /home/saju/pyra_env/test/starter_proj/setup.py
Welcome to Pyramid.  Sorry for the convenience.
(pyra_env)saju@saju-desktop:~/pyra_env/test$


2) How to create alchemy project
=======================

(pyra_env)saju@saju-desktop:~/pyra_env/test$ pcreate -s alchemy alchemy_proj
Creating directory /home/saju/pyra_env/test/alchemy_proj
  Recursing into +package+
    Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/
    Copying __init__.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/__init__.py
    Copying models.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/models.py
    Recursing into scripts
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/
      Copying __init__.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/__init__.py
      Copying initializedb.py to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/scripts/initializedb.py
    Recursing into static
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/
      Copying favicon.ico to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/favicon.ico
      Copying footerbg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/footerbg.png
      Copying headerbg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/headerbg.png
      Copying ie6.css to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/ie6.css
      Copying middlebg.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/middlebg.png
      Copying pylons.css to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pylons.css
      Copying pyramid-small.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pyramid-small.png
      Copying pyramid.png to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/pyramid.png
      Copying transparent.gif to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/static/transparent.gif
    Recursing into templates
      Creating /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/templates/
      Copying mytemplate.pt_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/templates/mytemplate.pt
    Copying tests.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/tests.py
    Copying views.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/alchemy_proj/views.py
  Copying CHANGES.txt_tmpl to /home/saju/pyra_env/test/alchemy_proj/CHANGES.txt
  Copying MANIFEST.in_tmpl to /home/saju/pyra_env/test/alchemy_proj/MANIFEST.in
  Copying README.txt_tmpl to /home/saju/pyra_env/test/alchemy_proj/README.txt
  Copying development.ini_tmpl to /home/saju/pyra_env/test/alchemy_proj/development.ini
  Copying production.ini_tmpl to /home/saju/pyra_env/test/alchemy_proj/production.ini
  Copying setup.cfg_tmpl to /home/saju/pyra_env/test/alchemy_proj/setup.cfg
  Copying setup.py_tmpl to /home/saju/pyra_env/test/alchemy_proj/setup.py
Welcome to Pyramid.  Sorry for the convenience.
(pyra_env)saju@saju-desktop:~/pyra_env/test$

====================

* The setup.py file in that directory can be used to distribute your application,
or install your application for deployment or development.

* To install a newly created project for development, you should cd to the newly
created project directory and run the command "#python setup.py develop".

* The file named setup.py will be in the root of the pcreate-generated project directory.

* The command "#python setup.py develop" will install a distribution representing your
project into the interpreter’s library set so it can be found by import statements and
by other console scripts such as pserve, pshell, proutes and pviews.


python setup.py develop
==================
(pyra_env)saju@saju-desktop:~/pyra_env/test/alchemy_proj$ python setup.py develop
running develop
running egg_info
creating alchemy_proj.egg-info
writing requirements to alchemy_proj.egg-info/requires.txt
writing alchemy_proj.egg-info/PKG-INFO
writing top-level names to alchemy_proj.egg-info/top_level.txt
writing dependency_links to alchemy_proj.egg-info/dependency_links.txt
...................
........this will install all dependencies...........
...................

Running the project application
========================
* Once a project is installed for development, you can run the application it represents
using the pserve command against the generated configuration file. In our case,
this file is named development.ini.

$pserve development.ini
Starting server in PID 16601.
serving on 0.0.0.0:6543 view at http://127.0.0.1:6543

OR

$pserve development.ini --reload
Starting subprocess with file monitor
Starting server in PID 16601.
serving on http://0.0.0.0:6543


* You can change the port on which the server runs on by changing the development.ini file.
For example, you can change the port = 6543 line in the development.ini file’s [server:main]
section to port = 8080 to run the server on port 8080 instead of port 6543.


The Startup Process (IMP)
====================
http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/startup.html

=============

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/project.html

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/startup.html

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/logging.html

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/environment.html

How to explain Your First Pyramid Application

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()



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


from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

"""
* http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/firstapp.html
* The script imports the Configurator class from the pyramid.config module. An instance of the Configurator class is later used to configure your Pyramid application.
* Like many other Python web frameworks, Pyramid uses the WSGI protocol to connect an application and a web server together.

"""

def hello_world(request):
    """
    * The function accepts a single argument (request) and it returns an instance of the pyramid.response.Response class.
    * This function is known as a view callable. A view callable accepts a single argument, request.
      It is expected to return a response object.
      A view callable doesn’t need to be a function; it can be represented via another type of object, like a class or an instance.
        * A view callable is always called with a request object.
        * A view callable is required to return a response object because a response object has
      all the information necessary to formulate an actual HTTP response; this object is
          then converted to text by the WSGI server which called Pyramid and it is sent back to the requesting browser.
    """  
    print "======request.matchdict========", request.matchdict ,"\n"
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    """
       * Application Configuration
       * Methods called on the Configurator will cause registrations to be made in an
     application registry associated with the application.An application registry maps resource types to views,
     as well as housing other application-specific component registrations.
         Every Pyramid application has one (and only one) application registry.
       * In browser, type http://127.0.0.1:8080/hello/saju
         Here string "saju" will map to "name" and
         get in view via dictionary request.matchdict.
         eg:{'name': u'saju'}.
       * A call to make_wsgi_app implies that all configuration is finished (meaning all method calls to
     the configurator which set up views, and various other configuration settings have been performed).
         The make_wsgi_app method returns a WSGI application object that can be used by any WSGI server to
         present an application to a requestor. WSGI is a protocol that allows servers to talk to Python applications.    
    """  
    ###Configurator Construction###
    config = Configurator()
    ###Adding Configuration###
    ##registers a route to match any URL path that begins with /hello/ followed by a string.
    config.add_route('hello', '/hello/{name}')
    ##registers the hello_world function as a view callable and makes sure that it will be called when the hello route is matched.
    config.add_view(hello_world, route_name='hello')
    ##WSGI Application Creation###
    app = config.make_wsgi_app()
    ###WSGI Application Serving###
    server = make_server('0.0.0.0', 8080, app)
    print "===wsgiref Server Running==="
    server.serve_forever()
    print "===wsgiref Server Stopped==="

Creating Your First Pyramid Application example

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response


def hello_world(request):
    print "===request.matchdict===", request.matchdict ,"\n"
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    """In browser, type http://127.0.0.1:8080/hello/saju
       Here string "saju" will map to "name" and
       get in view via dictionary request.matchdict.
       eg:{'name': u'saju'}.
    """   
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    print "===Server Running==="
    server.serve_forever()
    print "===Server Stopped==="


OutPut
#######


In Terminal
*************
saju@saju-desktop:~/pyra_env$ python test.py
===Server Running===
===request.matchdict=== {'name': u'saju'}

In Browser
************

Hello saju!