Advertisement
Guest User

Untitled

a guest
Aug 17th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. #Zimbra Color Folder Import
  5. #How to use: install the script in the new Zimbra server, as the set_mailbox_colors
  6. #module is executed locally. When executed, inform the username, password, and the
  7. #ip of the old Zimbra server, to get the colors via paramiko/zmbkpose.
  8.  
  9. #Packages to be installed:
  10. # * python-paramiko
  11.  
  12. #Compatibility:
  13. # * Only validated with Python 2.7
  14.  
  15. #Importing libraries
  16. from subprocess import check_output
  17. from json import loads
  18. from paramiko import SSHClient,AutoAddPolicy
  19. from logging import basicConfig,warning,info,error,INFO
  20. from argparse import ArgumentParser
  21.  
  22. #Converting zimbra's boolean to python's boolean
  23. true=True
  24. false=False
  25.  
  26. #zmmailbox and zmprov's path
  27. zmprov='/opt/zimbra/bin/zmprov'
  28. zmmailbox='/opt/zimbra/bin/zmmailbox'
  29. basicConfig(filename='/tmp/colors.log',level=INFO)
  30.  
  31. def get_accounts():
  32. accounts=check_output([zmprov,'-l','gaa'])
  33. return accounts.split()
  34.  
  35. def zmailbox_filter(zjson):
  36. colors=[]
  37. for rows in zjson['subFolders']:
  38. colors.append({'path':rows['path'],'color':rows['color']})
  39. return colors
  40.  
  41. def get_mailbox_colors(oldserver,username,passwd,logins):
  42. ssh=SSHClient()
  43. ssh.set_missing_host_key_policy(AutoAddPolicy())
  44. ssh.connect(oldserver,username=username,password=passwd)
  45. accounts = {}
  46. for rows in logins:
  47. 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):
  48. mailbox = '{0} -z -m {1} gaf --verbose'.format(zmmailbox,rows)
  49. stdin,stdout,stderr=ssh.exec_command(mailbox)
  50. try:
  51. zjson = loads(''.join(stdout.readlines()))
  52. accounts[rows] = zmailbox_filter(zjson)
  53. message='Account {0} exported with success'
  54. info(message.format(rows,stderr.readlines()))
  55. except:
  56. message='Account {0} failed in the export process with the error: {1}'
  57. warning(message.format(rows,sys.exec_info()))
  58. return accounts
  59.  
  60. def set_mailbox_colors(accounts):
  61. for key,value in accounts.iteritems():
  62. for rows in value:
  63. try:
  64. check_output([zmmailbox,'-z','-m',key,'mfc',rows['path'],rows['color']])
  65. message='Account {0} - Folder {1} imported with success!'
  66. info(message.format(key,rows['path']))
  67. except:
  68. message='Account {0} - Folder {1} failed in the import process. Motive - {2}'
  69. error(message.format(key,rows['path'].encode('utf-8'),sys.exc_info()[0]))
  70.  
  71. def main(args):
  72. info('Initializing color import process')
  73. info('Collecting list of accounts')
  74. accounts = get_accounts()
  75. info('Collecting color of every folder')
  76. folderColor = get_mailbox_colors(args.host,args.username,args.passwd,accounts)
  77. info('Importing colors in the new server')
  78. set_mailbox_colors(folderColor)
  79. info('Process concluded')
  80.  
  81. parser=ArgumentParser(description='Zimbra Color Folder Import Script')
  82. parser.add_argument('--username','-u',help='Inform the username to access the old server via SSH')
  83. parser.add_argument('--passwd','-p',help='Inform the password to access the old server via SSH')
  84. parser.add_argument('--host','-s',help='Inform the IP address of the old server')
  85.  
  86. args=parser.parse_args()
  87.  
  88. if (args.username == None or args.passwd == None or args.host == None):
  89. print(parser.print_help())
  90. else:
  91. main(args)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement