1 ################################################################################
2 # Copyright (c) 2020 AT&T Intellectual Property. #
4 # Licensed under the Apache License, Version 2.0 (the "License"); #
5 # you may not use this file except in compliance with the License. #
6 # You may obtain a copy of the License at #
8 # http://www.apache.org/licenses/LICENSE-2.0 #
10 # Unless required by applicable law or agreed to in writing, software #
11 # distributed under the License is distributed on an "AS IS" BASIS, #
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13 # See the License for the specific language governing permissions and #
14 # limitations under the License. #
15 ################################################################################
23 from xapp_onboarder.server import settings
25 log = logging.getLogger(__name__)
28 def get_dir_size(start_path='.'):
31 if os.path.isfile(start_path):
32 total_size += os.path.getsize(start_path)
34 if os.path.isdir(start_path):
35 for dirpath, dirnames, filenames in os.walk(start_path):
37 fp = os.path.join(dirpath, f)
38 # skip if it is symbolic link
39 if not os.path.islink(fp):
40 total_size += os.path.getsize(fp)
45 def format_artifact_dir_size():
46 """ Converts integers to common size units used in computing """
47 artifact_dir_size_string = settings.CHART_WORKSPACE_SIZE
48 size_unit = re.sub('[0-9\s\.]', '', artifact_dir_size_string)
49 size_limit = re.sub('[A-Za-z\s\.]', '', artifact_dir_size_string)
59 return float(size_limit) * float(1 << bit_shift[size_unit])
61 def trim_artifact_dir():
62 artifact_dir_size = get_dir_size(start_path=settings.CHART_WORKSPACE_PATH)
63 dir_limit = format_artifact_dir_size()
65 if artifact_dir_size > dir_limit:
66 dirs = sorted(glob.glob(settings.CHART_WORKSPACE_PATH + '/*'), key=os.path.getctime)
68 remain_size = artifact_dir_size
70 remain_size = remain_size - get_dir_size(start_path=dir)
72 if remain_size < dir_limit:
74 log.info("Trimming artifact directories: " + str(trim_dir))
76 if os.path.isfile(dir):
82 class artifacts_manager():
85 self.timer_thread = threading.Timer(60.0, trim_artifact_dir)
87 def start_trim_thread(self):
88 if not settings.MOCK_TEST_MODE:
89 log.info("Artifact directory trimming thread started.")
90 self.timer_thread.start()
93 def cancel_trim_thread(self):
94 log.info("Artifact directory trimming thread stopped.")
95 self.timer_thread.cancel()
98 self.start_trim_thread()