Code cleanup, changed version check and updated search function

This commit is contained in:
rune 2023-10-03 10:15:50 +02:00
parent 8000779fe8
commit e8d45fc477

View File

@ -29,7 +29,7 @@ database = filepath.joinpath('malias.db')
logfile = filepath.joinpath('malias.log') logfile = filepath.joinpath('malias.log')
Path(filepath).mkdir(parents=True, exist_ok=True) Path(filepath).mkdir(parents=True, exist_ok=True)
logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s') logging.basicConfig(filename=logfile,level=logging.INFO,format='%(message)s')
app_version = '0.3' app_version = '0.3.1'
def get_latest_release(): def get_latest_release():
@ -41,13 +41,12 @@ def get_latest_release():
return remoteData['tag_name'] return remoteData['tag_name']
latest_release = get_latest_release()
def release_check(): def release_check():
latest_release = get_latest_release()
if app_version != latest_release: if app_version != latest_release:
print('[b]New version available @ [i]https://iurl.no/malias[/b][/i]') print('[b]New version available @ [i]https://iurl.no/malias[/b][/i]')
else: else:
print ('') print ('You have the the latest version. Version: %s' %(app_version))
def connect_database(): def connect_database():
@ -135,7 +134,6 @@ def set_mailserver(server):
logging.info(now + ' - Info : mailcow server updated') logging.info(now + ' - Info : mailcow server updated')
print('Your mail server has been updated.') print('Your mail server has been updated.')
conn.commit() conn.commit()
release_check()
def apikey(key): def apikey(key):
@ -145,7 +143,7 @@ def apikey(key):
logging.info(now + ' - Info : API key updated') logging.info(now + ' - Info : API key updated')
print('Your API key has been updated.') print('Your API key has been updated.')
conn.commit() conn.commit()
release_check()
def get_mail_domains(info): def get_mail_domains(info):
apikey = get_api() apikey = get_api()
@ -169,7 +167,7 @@ def get_mail_domains(info):
print('%s \t\twith %s aliases' %(remoteData[i]['domain_name'],count)) print('%s \t\twith %s aliases' %(remoteData[i]['domain_name'],count))
i+=1 i+=1
print('\n\nThere is a total of %s domains with %s aliases.' %(str(i),str(total_aliases))) print('\n\nThere is a total of %s domains with %s aliases.' %(str(i),str(total_aliases)))
release_check()
else: else:
return(remoteData) return(remoteData)
@ -197,7 +195,7 @@ def create(alias,to_address):
conn.commit() conn.commit()
logging.info(now + ' - Info : alias %s created for %s on the mailcow instance %s ' %(alias,to_address,server)) logging.info(now + ' - Info : alias %s created for %s on the mailcow instance %s ' %(alias,to_address,server))
print('\n[b]Info[/b] : alias %s created for %s on the mailcow instance %s \n' %(alias,to_address,server)) print('\n[b]Info[/b] : alias %s created for %s on the mailcow instance %s \n' %(alias,to_address,server))
release_check()
def delete_alias(alias): def delete_alias(alias):
@ -228,7 +226,6 @@ def delete_alias(alias):
else: else:
print('\n[b]Error[/b] : The alias %s not found') print('\n[b]Error[/b] : The alias %s not found')
release_check()
@ -256,7 +253,6 @@ def copy_data():
else: else:
print('\n[b]Info[/b] : aliases alreday imported from the mailcow instance %s to local DB\n\n[i]Updating with any missing aliases![/i]' %(mail_server)) print('\n[b]Info[/b] : aliases alreday imported from the mailcow instance %s to local DB\n\n[i]Updating with any missing aliases![/i]' %(mail_server))
update_data() update_data()
release_check()
def update_data(): def update_data():
@ -341,7 +337,7 @@ def list_alias():
print(the_alias + '\tgoes to\t\t' + the_goto) print(the_alias + '\tgoes to\t\t' + the_goto)
i=i+1 i=i+1
print('\n\nTotal number of aliases %s on instance [b]%s[/b] and %s on [b]local DB[/b].' %(str(i),mail_server,str(l))) print('\n\nTotal number of aliases %s on instance [b]%s[/b] and %s on [b]local DB[/b].' %(str(i),mail_server,str(l)))
release_check()
def alias_id(alias): def alias_id(alias):
apikey = get_api() apikey = get_api()
@ -391,6 +387,8 @@ def check_local_db(alias_id):
def search(alias): def search(alias):
apikey = get_api() apikey = get_api()
mail_server = get_settings('mail_server') mail_server = get_settings('mail_server')
alias_server = number_of_aliases_on_server()
alias_db = number_of_aliases_in_db()
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute('SELECT data_copy FROM settings where id = 1') cursor.execute('SELECT data_copy FROM settings where id = 1')
result = cursor.fetchone()[0] result = cursor.fetchone()[0]
@ -405,8 +403,8 @@ def search(alias):
the_alias = remoteData[i][1].ljust(20,' ') the_alias = remoteData[i][1].ljust(20,' ')
print(the_alias + '\tgoes to\t\t' + remoteData[i][2]) print(the_alias + '\tgoes to\t\t' + remoteData[i][2])
i=i+1 i=i+1
print('\n\nData from local DB\n Run [i]malias -c to get any aliases on the server but [b]not[/b] in the local DB![/i]') print('\n\nData from local DB')
release_check()
else: else:
req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all') req = urllib.request.Request('https://'+mail_server+'/api/v1/get/alias/all')
req.add_header('Content-Type', 'application/json') req.add_header('Content-Type', 'application/json')
@ -422,7 +420,9 @@ def search(alias):
print(remoteData[i]['address'] + '\tgoes to\t\t' + remoteData[i]['goto']) print(remoteData[i]['address'] + '\tgoes to\t\t' + remoteData[i]['goto'])
i=i+1 i=i+1
print('\n\nData from server') print('\n\nData from server')
release_check() if alias_server != alias_db:
print('\n\nThere are %s aliases on the server and %s aliases in local DB' %(str(alias_server),str(alias_db)))
print('Run [i]malias -c[/i] to update local DB')
def get_mailcow_version(): def get_mailcow_version():
@ -434,11 +434,7 @@ def get_mailcow_version():
current = urllib.request.urlopen(req) current = urllib.request.urlopen(req)
remote = current.read().decode('utf-8') remote = current.read().decode('utf-8')
remoteData = json.loads(remote) remoteData = json.loads(remote)
#repo = Repo("https://github.com/mailcow/mailcow-dockerized")
# info = repo.git.ls_remote()
# tags = repo.tags
remote_heads = git.cmd.Git().ls_remote('https://github.com/mailcow/mailcow-dockerized', tags=True) remote_heads = git.cmd.Git().ls_remote('https://github.com/mailcow/mailcow-dockerized', tags=True)
# tags = remote_heads.rstrip('refs/tags/')
tags = remote_heads.splitlines() tags = remote_heads.splitlines()
for x in tags: for x in tags:
string = x.rsplit('/',2) string = x.rsplit('/',2)
@ -454,7 +450,7 @@ def get_mailcow_version():
def show_current_info(): def show_current_info():
API = get_api() API = get_api()
mail_server = get_settings('mail_server') mail_server = get_settings('mail_server')
latest_release = get_latest_release()
if API == 'DUMMY_KEY': if API == 'DUMMY_KEY':
API = 'Missing API Key!' API = 'Missing API Key!'
@ -516,7 +512,10 @@ parser.add_argument('-d', '--delete', help='Delete alias.\n\n',
nargs=1, metavar=('alias@domain.com'), required=False, action="append") nargs=1, metavar=('alias@domain.com'), required=False, action="append")
parser.add_argument('-i', '--info', help='Show current version and config info\n\n', parser.add_argument('-i', '--info', help='Show current config and appliacation info\n\n',
required=False, action='store_true')
parser.add_argument('-v', '--version', help='Show current version\n\n',
required=False, action='store_true') required=False, action='store_true')
parser.add_argument('-c', '--copy', help='Copy alias data from mailcow server to local DB.\n\n', parser.add_argument('-c', '--copy', help='Copy alias data from mailcow server to local DB.\n\n',
@ -543,6 +542,8 @@ elif args['delete']:
delete_alias(args['delete'][0][0]) delete_alias(args['delete'][0][0])
elif args['info']: elif args['info']:
show_current_info() show_current_info()
elif args['version']:
release_check()
elif args['copy']: elif args['copy']:
copy_data() copy_data()
elif args['list']: elif args['list']: