Advertisement
Linko22

Untitled

Feb 15th, 2022 (edited)
1,253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 KB | None | 0 0
  1. #!/bin/env python
  2. # -*- python-mode -*-
  3. # -*- coding: utf-8 -*-
  4.  
  5. import glob, os
  6. import csv
  7. import sys
  8. import time
  9.  
  10. start_time = time.time()
  11.  
  12. ansibleRoot = '/home/linko/ansible'
  13. gitRepoRoot = ansibleRoot + '/ansible-files'
  14. gitRepo = gitRepoRoot + '/mysql/data'
  15.  
  16. countLostUsers = 0
  17.  
  18. networkList = [
  19.   '10.17',
  20.   '10.18',
  21.   '10.19',
  22.   '10.2',
  23.   '10.20',
  24.   '10.3',
  25.   '10.6',
  26.   '10.7.0',
  27.   '10.7.64',
  28. ]
  29.  
  30. ignoreUsersList = [
  31.   "percona",
  32.   "percona.10",
  33.   "replication",
  34.   "replication.10",
  35.   "replication.10.2",
  36.   "replication.10.3",
  37.   "replication.10.6",
  38.   "replication.10.7",
  39. ]
  40.  
  41. groupUserDict = {}
  42.  
  43. corpList = []
  44. develList = []
  45. rcList = []
  46. prodList = []
  47. prodUsersList = []
  48.  
  49. text_files = glob.glob(gitRepo + "/mysql.*/**/users/*", recursive = True)
  50.  
  51. print (f"Exclude users list: {ignoreUsersList}")
  52.  
  53. for userFile in text_files:
  54.   if (os.path.basename(userFile) in ignoreUsersList or ".10" not in userFile) or userFile.endswith(".10"):
  55.     continue
  56.   userInventory = userFile.split('/')[-3]
  57.   if "corp" in userInventory:
  58.     if userInventory not in corpList:
  59.       corpList.append(userInventory)
  60.   elif "devel" in userInventory:
  61.     if userInventory not in develList:
  62.       develList.append(userInventory)
  63.   elif "rc" in userInventory:
  64.     if userInventory not in rcList:
  65.       rcList.append(userInventory)
  66.   else:
  67.     if userInventory not in prodList:
  68.       prodList.append(userInventory)
  69.     userFileClear = userFile.split('/')[-1].split('.')[0]
  70.     fileGroupUser = f"{userInventory}/users/{userFileClear}"
  71.     if fileGroupUser not in prodUsersList:
  72.       print (f"{userInventory}/users/{userFileClear}")
  73.       prodUsersList.append(fileGroupUser)
  74.  
  75. print("CORP:\n  {}\nPROD:\n  {}\nRC:\n  {}\nDEVEL:\n  {}".format(corpList,prodList, rcList, develList))
  76.  
  77. for checkUserFile in prodUsersList:
  78.   userFileName = checkUserFile.split('/')[-1]
  79.   userDirName = os.path.dirname(os.path.abspath(f"{gitRepo}/{checkUserFile}"))
  80. #  print (f"  Short Name: {userFileName}, Directory: {userDirName}")
  81.   prefixed = [filename for filename in os.listdir(os.path.dirname(os.path.abspath(f"{gitRepo}/{checkUserFile}"))) if filename.startswith(f"{userFileName}")]
  82. #  print (f"   {prefixed}")
  83.  
  84.   print(f" open existing file: {userDirName}/{prefixed[0]}")
  85.   with open(f"{userDirName}/{prefixed[0]}") as csvFile:
  86.     reader = csv.reader(csvFile, delimiter='\t')
  87.     for net in networkList:
  88.       if not (os.path.exists(gitRepo + "/" + checkUserFile + "." + net) and os.path.getsize(gitRepo + "/" + checkUserFile + "." + net) > 0):
  89.         print (f"{gitRepo}/{checkUserFile}.{net} - not found, create new one")
  90.         with open(f"{gitRepo}/{checkUserFile}.{net}", mode = 'w') as outfile:
  91.           writer = csv.writer(outfile, delimiter='\t')
  92.           for key, value in reader:
  93.             if key == "user.host":
  94.               #print(f"user.host - {net}.%")
  95.               writer.writerow([ "user.host", f"{net}.%"])
  96.             else:
  97.               writer.writerow([ key, value])
  98.               #print(f"{key} - {value}")
  99.       csvFile.seek(0)
  100.     countLostUsers += 1
  101.  
  102. print (f" Lost users: {countLostUsers}")
  103. print("--- Total: %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement