|
2 | 2 |
|
3 | 3 | import contextlib
|
4 | 4 | import os
|
| 5 | +import glob |
5 | 6 | import sys
|
6 | 7 | import shutil
|
7 | 8 | import stat
|
|
33 | 34 | TIMER_UNIT_TEMPLATE = 'timer.unit.j2'
|
34 | 35 |
|
35 | 36 | SYSTEMD_LOCATION = '/usr/lib/systemd/system'
|
| 37 | +ETC_SYSTEMD_LOCATION = '/etc/systemd/system' |
36 | 38 |
|
37 | 39 | GENERATED_SERVICES_CONF_FILE = '/etc/sonic/generated_services.conf'
|
38 | 40 |
|
@@ -92,18 +94,30 @@ def set_executable_bit(filepath):
|
92 | 94 | os.chmod(filepath, st.st_mode | stat.S_IEXEC)
|
93 | 95 |
|
94 | 96 |
|
95 |
| -def remove_if_exists(path): |
| 97 | +def remove_file(path): |
96 | 98 | """ Remove filepath if it exists """
|
97 | 99 |
|
98 |
| - if not os.path.exists(path): |
99 |
| - return |
| 100 | + try: |
| 101 | + os.remove(path) |
| 102 | + log.info(f'removed {path}') |
| 103 | + except FileNotFoundError: |
| 104 | + pass |
| 105 | + |
| 106 | + |
| 107 | +def remove_dir(path): |
| 108 | + """ Remove filepath if it exists """ |
| 109 | + |
| 110 | + try: |
| 111 | + shutil.rmtree(path) |
| 112 | + log.info(f'removed {path}') |
| 113 | + except FileNotFoundError: |
| 114 | + pass |
100 | 115 |
|
101 |
| - os.remove(path) |
102 |
| - log.info(f'removed {path}') |
103 | 116 |
|
104 | 117 | def is_list_of_strings(command):
|
105 | 118 | return isinstance(command, list) and all(isinstance(item, str) for item in command)
|
106 | 119 |
|
| 120 | + |
107 | 121 | def run_command(command: List[str]):
|
108 | 122 | """ Run arbitrary bash command.
|
109 | 123 | Args:
|
@@ -197,12 +211,22 @@ def remove(self,
|
197 | 211 | """
|
198 | 212 |
|
199 | 213 | name = package.manifest['service']['name']
|
200 |
| - remove_if_exists(os.path.join(SYSTEMD_LOCATION, f'{name}.service')) |
201 |
| - remove_if_exists(os.path.join(SYSTEMD_LOCATION, f'{name}@.service')) |
202 |
| - remove_if_exists(os.path.join(SERVICE_MGMT_SCRIPT_LOCATION, f'{name}.sh')) |
203 |
| - remove_if_exists(os.path.join(DOCKER_CTL_SCRIPT_LOCATION, f'{name}.sh')) |
204 |
| - remove_if_exists(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, f'{name}')) |
205 |
| - remove_if_exists(os.path.join(ETC_SONIC_PATH, f'{name}_reconcile')) |
| 214 | + remove_file(os.path.join(SYSTEMD_LOCATION, f'{name}.service')) |
| 215 | + remove_file(os.path.join(SYSTEMD_LOCATION, f'{name}@.service')) |
| 216 | + remove_file(os.path.join(SERVICE_MGMT_SCRIPT_LOCATION, f'{name}.sh')) |
| 217 | + remove_file(os.path.join(DOCKER_CTL_SCRIPT_LOCATION, f'{name}.sh')) |
| 218 | + remove_file(os.path.join(DEBUG_DUMP_SCRIPT_LOCATION, f'{name}')) |
| 219 | + remove_file(os.path.join(ETC_SONIC_PATH, f'{name}_reconcile')) |
| 220 | + |
| 221 | + # remove symlinks and configuration directories created by featured |
| 222 | + remove_file(os.path.join(ETC_SYSTEMD_LOCATION, f'{name}.service')) |
| 223 | + for unit_file in glob.glob(os.path.join(ETC_SYSTEMD_LOCATION, f'{name}@*.service')): |
| 224 | + remove_file(unit_file) |
| 225 | + |
| 226 | + remove_dir(os.path.join(ETC_SYSTEMD_LOCATION, f'{name}.service.d')) |
| 227 | + for unit_dir in glob.glob(os.path.join(ETC_SYSTEMD_LOCATION, f'{name}@*.service.d')): |
| 228 | + remove_dir(unit_dir) |
| 229 | + |
206 | 230 | self.update_dependent_list_file(package, remove=True)
|
207 | 231 | self.update_generated_services_conf_file(package, remove=True)
|
208 | 232 |
|
|
0 commit comments