Hi there,
I wrote simple script to import/export vserver configs.
I assume that vserver list is sorted from smaller to bigger ID ( the
last one entry has highest ID ). I'll make a version that checks whole
file for the highest ID.
With that script it's possible to move whole vserver config between
different Cherokee config files.
It checks for duplicated nicks ( names )of vservers ( and changes name
of the new one ).
It supports multiple languages ( just a simple python dictionary, and
imports in main file )
I'm going to integrate it with cherokee-admin in near future.
I attach main script ( cherokee_im_ex.py ), english language
( cherokee_im_ex_en.py ) and polish language ( cherokee_im_ex_pl.py )
To change language just modify:
"from cherokee_im_ex_en import lang as LANG"
to (for polish):
"from cherokee_im_ex_pl import lang as LANG"
You can naturally just rename file :)
Any feedback, ideas etc is "welcome" :-)
Btw. It just works ;-)
--
Greetings,
Jędrzej Nowak
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''****************************************************************************
**
** Copyright (C) 2008 JÄdrzej Nowak <[EMAIL PROTECTED]>
**
** This file may be used under the terms of the GNU General Public License
** version 2 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************'''
import re, sys, os
from itertools import ifilter
#import language file
from cherokee_im_ex_en import lang as LANG
#possible choices
av_choices='eE1iI2'
#main path to config file
FILE='/etc/cherokee/cherokee.conf'
class base_im_ex(object):
"""base class for import/export"""
def _start(self,file):
if not file:
file=FILE
self.file=file
self.content=open(self.file,'r').readlines()
def _filter(self,id=None):
if id:
patt=re.compile('vserver!%s'%id)
else:
patt=re.compile('vserver!.+')
self.filtered=[now for now in ifilter(lambda x: patt.match(x),self.content)]
class importer(base_im_ex):
"""class for imports"""
def __init__(self,file):
self._start(file)
self.new_data=''
self._get_content()
def _get_content(self):
print LANG['GET_PROMPT']
self.new_data=sys.stdin.readlines()
if self.new_data[-1]=="":
self.new_data=new_data[:-1]
self._get_new_id()
def _get_new_id(self):
self._filter()
# the last 'vserver!\d+' is a "bigger one" ?
old_id=re.findall('vserver!(?P<id>\d+)!',self.filtered[-1])[0]
try:
self.new_id=int(old_id)+10
except Exception, e:
print "Error: %s"%e
sys.exit(0)
self._prepare_content()
def _prepare_content(self):
self._find_duplicates()
#poprawione id vservera
self.new_data=re.sub('vserver!\d+!', 'vserver!%s!'%self.new_id, ''.join(self.new_data)).split('\n')
#temp
self.new_data=[now+'\n' for now in ifilter(lambda x: x, self.new_data)]
self._join_content()
def _find_duplicates(self):
name=re.findall('vserver!\d+!nick.?=.?(?P<name>.*)',''.join(self.new_data))[0]
tmp_t=''.join(self.content)
tmp_t_n='\n'.join(self.new_data)
while re.findall('vserver!\d+!nick.?=.?%s'%name,tmp_t):
print LANG["CHANGE_NAME"],name+"1"
name+='1'
tmp_t_n=re.sub('vserver!(?P<id>\d+)!nick(?P<sp1>.?)=(?P<sp2>.?)(?P<name>.*)',"vserver!\g<id>!nick\g<sp1>=\g<sp2>%s"%'default1',tmp_t_n)
self.new_data=tmp_t_n
#print self.new_data
#sys.exit(0)
def _join_content(self):
patt=re.compile('vserver!.+')
i=0
list_of_content=self.content
while not patt.match(list_of_content[i]):
i+=1
start_pos=i
i=0
while patt.match(list_of_content[i+start_pos]):
i+=1
end_pos=i+start_pos
list_of_content[end_pos:0]=self.new_data
text_of_content=''.join(list_of_content)
print "\n--------------------------------------------------\n"
print text_of_content
print "\n--------------------------------------------------\n"
try:
file_back=open(self.file+"_back",'w')
file_back.writelines(self.content)
file_write=open(self.file,'w')
file_write.write(text_of_content+'\n')
print LANG["WRITE_SUCCESS"],self.file
except IndexError:
print LANG["WRITE_FAIL"],self.file
class exporter(base_im_ex):
"""for exports"""
def __init__(self,file):
self._start(file)
self._get_num()
def _get_num(self):
print LANG["NUM_PROMPT"]
choice=raw_input(LANG["MY_CHOICE"])
if choice=='q':
print LANG["THE_END"]
sys.exit(1)
try:
int(choice)
except ValueError:
print LANG["NOT_A_NUMBER"]
self.get_num()
self.num=choice
self._get_content()
def _get_content(self):
self._filter(self.num)
print "\n--------------------------------------------------\n"
print ''.join(self.filtered)
print "\n--------------------------------------------------\n"
if __name__=="__main__":
print LANG["MENU"]
choice=raw_input(LANG["MY_CHOICE"])
if choice=="q":
print LANG["THE_END"]
sys.exit(0)
elif choice not in av_choices:
print LANG["WRONG_CHOICE"]
sys.exit(1)
print LANG["FILE_PROMPT"]
file=raw_input(LANG["PATH_PROMPT"])
if (file and not os.path.exists(file)) or not os.path.exists(FILE):
print LANG["FILE_NOT_FOUND"]
sys.exit(1)
if av_choices.index(choice)<3:
#export
ex=exporter(file)
sys.exit(0)
else:
#import
im=importer(file)
sys.exit(0)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
lang={
'CHANGE_NAME':"Change file name to: ",
'GET_PROMPT':"Paste config of one virtual server, EOF to finish: ",
'WRITE_SUCCESS':"Write successfull, to the file: ",
'WRITE_FAIL':"Write unseccessfull, to the file: ",
'NUM_PROMPT':"Enter a number of virtual server to export, q to finish.",
'MY_CHOICE':"Choice: ",
"THE_END":"Finish",
"NOT_A_NUMBER":"That wasn't a number!",
"MENU":"Export or Import? \ne/E/1 for Export\ni/I/2 for Import\nq to finish",
"WRONG_CHOICE":"Wrong choice",
"FILE_PROMPT":"Enter a full path of Cherokee config file, default '/etc/cherokee/cherokee.conf'",
'PATH_PROMPT':"Path: ",
'FILE_NOT_FOUND':"File not found"
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
lang={
'CHANGE_NAME':"Zmiana nazwy na: ",
'GET_PROMPT':"Podaj zawartoÅÄ konfiguracji serwera virtualnego, EOF aby zakonczyc: ",
'WRITE_SUCCESS':"Zapisano pomyslnie do pliku:",
'WRITE_FAIL':"Nie udalo sie zapisac do pliku:",
'NUM_PROMPT':"Podaj numer serwera wirtualnego do exportu, q dla zakoÅczenia",
'MY_CHOICE':"Wybieram: ",
"THE_END":"Koniec",
"NOT_A_NUMBER":"To nie byl numer!",
"MENU":"Export czy Import? \ne/E/1 dla Exportu\ni/I/2 dla Importu\nq dla zakonczenia",
"WRONG_CHOICE":"Blad, wybrales niepoprawnie",
"FILE_PROMPT":"Podaj sciezke do pliku konfiguracyjnego Cherokee\n domyÅlnie '/etc/cherokee/cherokee.conf'",
'PATH_PROMPT':"Sciezka: ",
'FILE_NOT_FOUND':"Nie znaleziono takiego pliku"
}
_______________________________________________
Cherokee mailing list
[email protected]
http://lists.octality.com/listinfo/cherokee