RIC-769: Committing individual files rather than tar archive
[ric-plt/appmgr.git] / xapp_orchestrater / dev / xapp_onboarder / xapp_onboarder / helm_controller / artifacts_manager.py
1 ################################################################################
2 #   Copyright (c) 2020 AT&T Intellectual Property.                             #
3 #                                                                              #
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                                    #
7 #                                                                              #
8 #       http://www.apache.org/licenses/LICENSE-2.0                             #
9 #                                                                              #
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 ################################################################################
16
17 import os
18 import logging
19 import re
20 import glob
21 import threading
22 import shutil
23 from xapp_onboarder.server import settings
24
25 log = logging.getLogger(__name__)
26
27
28 def get_dir_size(start_path='.'):
29     total_size = 0
30
31     if os.path.isfile(start_path):
32         total_size += os.path.getsize(start_path)
33
34     if os.path.isdir(start_path):
35         for dirpath, dirnames, filenames in os.walk(start_path):
36             for f in filenames:
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)
41
42     return total_size
43
44
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)
50
51     bit_shift = {"B": 0,
52                  "kb": 7,
53                  "KB": 10,
54                  "mb": 17,
55                  "MB": 20,
56                  "gb": 27,
57                  "GB": 30,
58                  "TB": 40, }
59     return float(size_limit) * float(1 << bit_shift[size_unit])
60
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()
64
65     if artifact_dir_size > dir_limit:
66         dirs = sorted(glob.glob(settings.CHART_WORKSPACE_PATH + '/*'), key=os.path.getctime)
67         trim_dir = list()
68         remain_size = artifact_dir_size
69         for dir in dirs:
70             remain_size = remain_size - get_dir_size(start_path=dir)
71             trim_dir.append(dir)
72             if remain_size < dir_limit:
73                 break
74         log.info("Trimming artifact directories: " + str(trim_dir))
75         for dir in trim_dir:
76             if os.path.isfile(dir):
77                 os.remove(dir)
78             else:
79                 shutil.rmtree(dir)
80
81
82 class artifacts_manager():
83
84     def __init__(self):
85         self.timer_thread = threading.Timer(60.0, trim_artifact_dir)
86
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()
91
92
93     def cancel_trim_thread(self):
94         log.info("Artifact directory trimming thread stopped.")
95         self.timer_thread.cancel()
96
97     def start(self):
98         self.start_trim_thread()
99
100