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)
Advertisements

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

A primer to Vagrant

Today I used vagrant for the very first time and I must say, I was impressed by the simplicity and power of vagrant.

So what is vagrant? Yes, Yet Another Wrapper!!  According to wikipedia, Vagrant is free and open-source software for creating and configuring virtual development environments.It can be considered a wrapper around virtualization software such as VirtualBox and configuration management software such as Chef, Salt and Puppet.

Keeping it simple, you can run headless VM inside virtualbox using this vagrant wrapper without even the need for running virtualbox GUI. so, simply just download the wrapper, put some configuration and fire your own cloud like environment to create disposable VM’s. 

Up and Running

$ vagrant init hashicorp/precise32
$ vagrant up

You can also check and alter the configuration file by just vim Vagrantfile.

By default, the very first instance is named default.

SSH into the vm : vagrant ssh
Destroy the vm: vagrant destroy
Halt the vm: vagrant halt