4
Copyright (c) 2020 Red Hat, Inc. <http://www.redhat.com>
5
This file is part of GlusterFS.
7
This file is licensed to you under your choice of the GNU Lesser
8
General Public License, version 3 or any later version (LGPLv3 or
9
later), or the GNU General Public License, version 2 (GPLv2), in all
10
cases as published by the Free Software Foundation.
18
from datetime import datetime
20
def find_htime_path(brick_path):
22
htime_dir = os.path.join(brick_path, '.glusterfs/changelogs/htime')
23
for file in os.listdir(htime_dir):
24
if os.path.isfile(os.path.join(htime_dir,file)) and file.startswith("HTIME"):
25
dirs.append(os.path.join(htime_dir, file))
27
raise FileNotFoundError("%s unavailable" % (os.path.join(htime_dir, file)))
30
def modify_htime_file(brick_path):
31
htime_file_path_list = find_htime_path(brick_path)
33
for htime_file_path in htime_file_path_list:
34
changelog_path = os.path.join(brick_path, '.glusterfs/changelogs')
35
temp_htime_path = os.path.join(changelog_path, 'htime/temp_htime_file')
36
with open(htime_file_path, 'r') as htime_file, open(temp_htime_path, 'w') as temp_htime_file:
37
#extract epoch times from htime file
38
paths = htime_file.read().split("\x00")
41
epoch_no = pth.split(".")[-1]
42
changelog = os.path.basename(pth)
43
#convert epoch time to year, month and day
45
date=(datetime.fromtimestamp(float(int(epoch_no))).strftime("%Y/%m/%d"))
46
#update paths in temp htime file
47
temp_htime_file.write("%s/%s/%s\x00" % (changelog_path, date, changelog))
48
#create directory in the format year/month/days
49
path = os.path.join(changelog_path, date)
51
if changelog.startswith("CHANGELOG."):
52
os.makedirs(path, mode = 0o600, exist_ok = True)
54
#copy existing changelogs to new directory structure, delete old changelog files
56
shutil.copyfile(pth, os.path.join(path, changelog))
57
except shutil.SameFileError:
62
#rename temp_htime_file with htime file
63
os.rename(htime_file_path, os.path.join('%s.bak'%htime_file_path))
64
os.rename(temp_htime_path, htime_file_path)
66
if __name__ == "__main__":
67
parser = argparse.ArgumentParser()
68
parser.add_argument('brick_path', help="This upgrade script, which is to be run on\
69
server side, takes brick path as the argument, \
70
updates paths inside htime file and alters the directory structure \
71
above the changelog files inorder to support new optimised format \
72
of the directory structure as per \
73
https://review.gluster.org/#/c/glusterfs/+/23733/")
74
args = parser.parse_args()
75
modify_htime_file(args.brick_path)