Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- #Zimbra Color Folder Import
- #How to use: install the script in the new Zimbra server, as the set_mailbox_colors
- #module is executed locally. When executed, inform the username, password, and the
- #ip of the old Zimbra server, to get the colors via paramiko/zmbkpose.
- #Packages to be installed:
- # * python-paramiko
- #Compatibility:
- # * Only validated with Python 2.7
- #Importing libraries
- from subprocess import check_output
- from json import loads
- from paramiko import SSHClient,AutoAddPolicy
- from logging import basicConfig,warning,info,error,INFO
- from argparse import ArgumentParser
- #Converting zimbra's boolean to python's boolean
- true=True
- false=False
- #zmmailbox and zmprov's path
- zmprov='/opt/zimbra/bin/zmprov'
- zmmailbox='/opt/zimbra/bin/zmmailbox'
- basicConfig(filename='/tmp/colors.log',level=INFO)
- def get_accounts():
- accounts=check_output([zmprov,'-l','gaa'])
- return accounts.split()
- def zmailbox_filter(zjson):
- colors=[]
- for rows in zjson['subFolders']:
- colors.append({'path':rows['path'],'color':rows['color']})
- return colors
- def get_mailbox_colors(oldserver,username,passwd,logins):
- ssh=SSHClient()
- ssh.set_missing_host_key_policy(AutoAddPolicy())
- ssh.connect(oldserver,username=username,password=passwd)
- accounts = {}
- for rows in logins:
- if ('galsync' not in rows) and ('ham' not in rows) and ('spam' not in rows) and ('virus' not in rows) and ('admin' not in rows):
- mailbox = '{0} -z -m {1} gaf --verbose'.format(zmmailbox,rows)
- stdin,stdout,stderr=ssh.exec_command(mailbox)
- try:
- zjson = loads(''.join(stdout.readlines()))
- accounts[rows] = zmailbox_filter(zjson)
- message='Account {0} exported with success'
- info(message.format(rows,stderr.readlines()))
- except:
- message='Account {0} failed in the export process with the error: {1}'
- warning(message.format(rows,sys.exec_info()))
- return accounts
- def set_mailbox_colors(accounts):
- for key,value in accounts.iteritems():
- for rows in value:
- try:
- check_output([zmmailbox,'-z','-m',key,'mfc',rows['path'],rows['color']])
- message='Account {0} - Folder {1} imported with success!'
- info(message.format(key,rows['path']))
- except:
- message='Account {0} - Folder {1} failed in the import process. Motive - {2}'
- error(message.format(key,rows['path'].encode('utf-8'),sys.exc_info()[0]))
- def main(args):
- info('Initializing color import process')
- info('Collecting list of accounts')
- accounts = get_accounts()
- info('Collecting color of every folder')
- folderColor = get_mailbox_colors(args.host,args.username,args.passwd,accounts)
- info('Importing colors in the new server')
- set_mailbox_colors(folderColor)
- info('Process concluded')
- parser=ArgumentParser(description='Zimbra Color Folder Import Script')
- parser.add_argument('--username','-u',help='Inform the username to access the old server via SSH')
- parser.add_argument('--passwd','-p',help='Inform the password to access the old server via SSH')
- parser.add_argument('--host','-s',help='Inform the IP address of the old server')
- args=parser.parse_args()
- if (args.username == None or args.passwd == None or args.host == None):
- print(parser.print_help())
- else:
- main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement