add hostname to file entry (uncloud filescanner)

This commit is contained in:
ahmadbilalkhalid 2020-01-07 17:57:44 +05:00
parent 6086fec633
commit 48cc37c438

View file

@ -14,6 +14,7 @@ from uncloud.shared import shared
arg_parser = argparse.ArgumentParser('filescanner', add_help=False) arg_parser = argparse.ArgumentParser('filescanner', add_help=False)
arg_parser.add_argument('--hostname', required=True)
def sha512sum(file: str): def sha512sum(file: str):
@ -41,7 +42,7 @@ def sha512sum(file: str):
return None return None
def track_file(file, base_dir): def track_file(file, base_dir, host):
file_path = file.relative_to(base_dir) file_path = file.relative_to(base_dir)
file_str = str(file) file_str = str(file)
# Get Username # Get Username
@ -60,6 +61,7 @@ def track_file(file, base_dir):
'sha512sum': sha512sum(file_str), 'sha512sum': sha512sum(file_str),
'creation_date': creation_date, 'creation_date': creation_date,
'size': str(bitmath.Byte(os.path.getsize(file_str)).to_MB()), 'size': str(bitmath.Byte(os.path.getsize(file_str)).to_MB()),
'host': host
} }
logger.info('Tracking %s', file_str) logger.info('Tracking %s', file_str)
@ -67,7 +69,7 @@ def track_file(file, base_dir):
shared.etcd_client.put(entry_key, entry_value, value_in_json=True) shared.etcd_client.put(entry_key, entry_value, value_in_json=True)
def main(debug=False): def main(hostname, debug=False):
base_dir = settings['storage']['file_dir'] base_dir = settings['storage']['file_dir']
# Recursively Get All Files and Folder below BASE_DIR # Recursively Get All Files and Folder below BASE_DIR
files = glob.glob('{}/**'.format(base_dir), recursive=True) files = glob.glob('{}/**'.format(base_dir), recursive=True)
@ -77,11 +79,8 @@ def main(debug=False):
tracked_files = [ tracked_files = [
pathlib.Path(os.path.join(base_dir, f.value['owner'], f.value['filename'])) pathlib.Path(os.path.join(base_dir, f.value['owner'], f.value['filename']))
for f in shared.etcd_client.get_prefix(settings['etcd']['file_prefix'], value_in_json=True) for f in shared.etcd_client.get_prefix(settings['etcd']['file_prefix'], value_in_json=True)
if f.value['host'] == hostname
] ]
untracked_files = set(files) - set(tracked_files) untracked_files = set(files) - set(tracked_files)
for file in untracked_files: for file in untracked_files:
track_file(file, base_dir) track_file(file, base_dir, hostname)
if __name__ == '__main__':
main()