Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/env python
- # -*- python-mode -*-
- # -*- coding: utf-8 -*-
- import glob, os
- import csv
- import sys
- import time
- start_time = time.time()
- ansibleRoot = '/home/linko/ansible'
- gitRepoRoot = ansibleRoot + '/ansible-files'
- gitRepo = gitRepoRoot + '/mysql/data'
- countLostUsers = 0
- networkList = [
- '10.17',
- '10.18',
- '10.19',
- '10.2',
- '10.20',
- '10.3',
- '10.6',
- '10.7.0',
- '10.7.64',
- ]
- ignoreUsersList = [
- "percona",
- "percona.10",
- "replication",
- "replication.10",
- "replication.10.2",
- "replication.10.3",
- "replication.10.6",
- "replication.10.7",
- ]
- groupUserDict = {}
- corpList = []
- develList = []
- rcList = []
- prodList = []
- prodUsersList = []
- text_files = glob.glob(gitRepo + "/mysql.*/**/users/*", recursive = True)
- print (f"Exclude users list: {ignoreUsersList}")
- for userFile in text_files:
- if (os.path.basename(userFile) in ignoreUsersList or ".10" not in userFile) or userFile.endswith(".10"):
- continue
- userInventory = userFile.split('/')[-3]
- if "corp" in userInventory:
- if userInventory not in corpList:
- corpList.append(userInventory)
- elif "devel" in userInventory:
- if userInventory not in develList:
- develList.append(userInventory)
- elif "rc" in userInventory:
- if userInventory not in rcList:
- rcList.append(userInventory)
- else:
- if userInventory not in prodList:
- prodList.append(userInventory)
- userFileClear = userFile.split('/')[-1].split('.')[0]
- fileGroupUser = f"{userInventory}/users/{userFileClear}"
- if fileGroupUser not in prodUsersList:
- print (f"{userInventory}/users/{userFileClear}")
- prodUsersList.append(fileGroupUser)
- print("CORP:\n {}\nPROD:\n {}\nRC:\n {}\nDEVEL:\n {}".format(corpList,prodList, rcList, develList))
- for checkUserFile in prodUsersList:
- userFileName = checkUserFile.split('/')[-1]
- userDirName = os.path.dirname(os.path.abspath(f"{gitRepo}/{checkUserFile}"))
- # print (f" Short Name: {userFileName}, Directory: {userDirName}")
- prefixed = [filename for filename in os.listdir(os.path.dirname(os.path.abspath(f"{gitRepo}/{checkUserFile}"))) if filename.startswith(f"{userFileName}")]
- # print (f" {prefixed}")
- print(f" open existing file: {userDirName}/{prefixed[0]}")
- with open(f"{userDirName}/{prefixed[0]}") as csvFile:
- reader = csv.reader(csvFile, delimiter='\t')
- for net in networkList:
- if not (os.path.exists(gitRepo + "/" + checkUserFile + "." + net) and os.path.getsize(gitRepo + "/" + checkUserFile + "." + net) > 0):
- print (f"{gitRepo}/{checkUserFile}.{net} - not found, create new one")
- with open(f"{gitRepo}/{checkUserFile}.{net}", mode = 'w') as outfile:
- writer = csv.writer(outfile, delimiter='\t')
- for key, value in reader:
- if key == "user.host":
- #print(f"user.host - {net}.%")
- writer.writerow([ "user.host", f"{net}.%"])
- else:
- writer.writerow([ key, value])
- #print(f"{key} - {value}")
- csvFile.seek(0)
- countLostUsers += 1
- print (f" Lost users: {countLostUsers}")
- print("--- Total: %s seconds ---" % (time.time() - start_time))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement