আর্কাইভ | Tech RSS for this section

paste deploy script

PasteDeploy is a library used by WSGI Middlewares which makes it possible to configure WSGI components together declaratively within an .ini file. It was developed by Ian Bicking.

Entry Points and Paste Deploy .ini files:

###
# app configuration
###
[app:main]
use = egg:MyProject

OR

[app:main]
paste.app_factory = configured:app_factory
name = Joy
greeting = ZeroVM

###
# wsgi server configuration
###
[server:main]
use = egg:waitress#main
host = 0.0.0.0
port = 6543

At a minimum, the configuration file defines an application instance and the server which runs it. In the First form of the configuration, The line in [app:main] above that says use = egg:MyProject is actually shorthand for a longer spelling: use = egg:MyProject#main. The #main part is omitted for brevity, as #main is a default defined by PasteDeploy. egg:MyProject#main is a string which has meaning to PasteDeploy. It points at a setuptools entry point named main defined in the MyProject project.
In the Second Form of the configuration,
– Line 1 begins a section with the app: prefix, used to define a WSGI application endpoint. The name main makes this application the default for this file.
– Line 2 tells PasteDeploy to look up the function app_factory in the module configured, and call it to get the application.
– Lines 3-4 provide configuration values to be are passed to the factory.

Take a look at the generated setup.py file for this project.

import os

from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(here, 'README.txt')) as f:
    README = f.read()
with open(os.path.join(here, 'CHANGES.txt')) as f:
    CHANGES = f.read()

requires = [
    'pyramid',
    'pyramid_chameleon',
    'pyramid_debugtoolbar',
    'waitress',
    ]

setup(name='MyProject',
      version='0.0',
      description='MyProject',
      long_description=README + '\n\n' + CHANGES,
      classifiers=[
        "Programming Language :: Python",
        "Framework :: Pyramid",
        "Topic :: Internet :: WWW/HTTP",
        "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
        ],
      author='',
      author_email='',
      url='',
      keywords='web pyramid pylons',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      install_requires=requires,
      tests_require=requires,
      test_suite="myproject,
      entry_points='''\
      [paste.app_factory]
      main = myproject:main
      ''',
      )

in the section named [paste.app_factory], there is a key named main (the entry point name) which has a value myproject:main. The key main is what our egg:MyProject#main value of the use section in our config file is pointing at, although it is actually shortened to egg:MyProject there. The value represents a dotted Python name path, which refers to a callable in our myproject package’s __init__.py module.

The egg: prefix in egg:MyProject indicates that this is an entry point URI specifier, where the “scheme” is “egg”. An “egg” is created when you run setup.py install or setup.py develop within your project.

In English, this entry point can thus be referred to as a “PasteDeploy application factory in the MyProject project which has the entry point named main where the entry point refers to a main function in the mypackage module”. Indeed, if you open up the init.py module generated within any scaffold-generated package, you’ll see a main function. This is the function called by PasteDeploy when the pserve command is invoked against our application. It accepts a global configuration object and returns an instance of our application.

[DEFAULT] Section of a PasteDeploy .ini File

You can add a [DEFAULT] section to your PasteDeploy .ini file. Such a section should consists of global parameters that are shared by all the applications, servers and middleware defined within the configuration file. The values in a [DEFAULT] section will be passed to your application’s main function as global_config (see the reference to the main function in init.py).

Advertisements

stopping swift services using python script

import sys
import shutil
import os
import subprocess, signal 

'''
    format : python stop_service {proxy|object}
    List the process and find server pid to kill
'''
service = 'swift-'+sys.argv[1]+'-server'
print service
proc_list = subprocess.Popen(['ps','-ef'],stdout = subprocess.PIPE)
out,err = proc_list.communicate()
for line in out.splitlines():
    #print line
    if (service in line):
        pid = int (line.split(None,2)[1])
	os.kill(pid,signal.SIGKILL)

print 'please run the {} service  manually'.format(service)

vim python autoindent

Create the directory ~/.vim/ftplugin; create a file named ~/.vim/ftplugin/python.vim containing:

setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=80
setlocal smarttab
setlocal expandtab

Now every files detected as Python files by Vim get the previous commands executed. Even those with filenames which do not end with .py.

Python CSV Module


import csv

ifile = open ('test.csv',"rb")
reader = csv.reader(ifile)


rownum = 0

for row in reader:
#save header row
if rownum == 0:
header = row
else:
colnum = 0
for col in row:
print '%-8s: %s' % (header[colnum],col)
colnum += 1
rownum += 1

ifile.close()

Running Jobs in Zerocloud

zexec.py

#!/usr/bin/env python
 
import requests
import json
import sys
import os
 
def get_url_and_token():
	user = os.getenv('ST_USER', None)
	auth = os.getenv('ST_AUTH', None)
	key = os.getenv('ST_KEY', None)
 
	if not (user and auth and key):
		print 'must supply ST_USER, ST_AUTH and ST_KEY env vars'
		sys.exit(1)
 
	res = requests.get(auth, headers={'x-storage-user': user,
					'x-storage-pass': key})
 
	return(res.headers.get('x-storage-url', None),
		res.headers.get('x-auth-token', None))
 
 
def zebra_execute(endpoint, token, manifest):
	return requests.post(
		endpoint,
		headers={
			'X-Auth-Token': token,
			'Content-Type': 'application/json',
			'X-ZeroVM-Execute': '1.0',
			'X-Zerovm-Deferred': 'always'},
		data=manifest)
 
 
def get_object(endpoint, token, container, object):
	resp = requests.get(
		"%s/%s/%s" % (endpoint, container, object),
		headers={'X-Auth-Token': token})
 
	if resp.status_code != 200:
		sys.stderr.write('Result code %d getting json file' % resp.result_code)
 
	return resp.content
 
 
def usage():
	print 'zexec <local file> or zexec <container> <job>'
	sys.exit(1)
 
# either run as 'zexec <local file>' or zexec <container> <job>'
json_file = ''
url, token = get_url_and_token()
 
if len(sys.argv) == 2:
	with open(sys.argv[1], 'r') as f:
		json_file = f.read()
elif len(sys.argv) == 3:
	container = sys.argv[1]
	obj = sys.argv[2]
	json_file = get_object(url, token, container, obj)
else:
	usage()
 
resp = zebra_execute(url, token, json_file)
 
print resp.content

Python Pip client

 

The pip command is a tool for installing and managing Python packages, such as those found in the Python Package Index. pip is very useful for web development as well as for sys-admins who manages cloud computing based resources created Openstack, Rackspace, AWS, Google and other cloud computing service providers. Pip installs the package from PyPI – the Python Package Index Repository. There are currently 41438 packages here.

Type the following apt-get command to install

sudo apt-get install python-pip

Pip commands:

pip install packageName
pip uninstall packageName
pip search packageName
pip help

To install something directly from the source, use

sudo pip install -e git+https://git.myproject.org/MyProject#egg=MyProject

How to install Devstack on a different directory

 

Login as user:stack

  1. sudo mkdir /var/stack
    sudo chown stack:stack /var/stack
    mkdir -p /var/stack/data/cinder/volumes
    sudo rm /etc/tgt/stack.d
    ln -s /var/stack/data/cinder/volumes /etc/tgt/stack.d
  2. Make changes to stack.sh
    111 DEST=${DEST:-/var/stack}
  3. Make changes to stackrc
    07 DEST=/var/stack