Skip to content

Commit 4895535

Browse files
blackboxswServer Team CI Bot
authored andcommitted
clean: cloud-init clean should not trace when run from within cloud_dir
Avoid traceback when cloud-init clean is run from within /var/lib/cloud/ deleted dirs. LP: #1795508
1 parent 94a6452 commit 4895535

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

cloudinit/cmd/clean.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
"""Define 'clean' utility and handler as part of cloud-init commandline."""
66

77
import argparse
8+
import glob
89
import os
910
import sys
1011

1112
from cloudinit.stages import Init
1213
from cloudinit.util import (
13-
ProcessExecutionError, chdir, del_dir, del_file, get_config_logfiles,
14+
ProcessExecutionError, del_dir, del_file, get_config_logfiles,
1415
is_link, subp)
1516

1617

@@ -61,18 +62,17 @@ def remove_artifacts(remove_logs, remove_seed=False):
6162

6263
if not os.path.isdir(init.paths.cloud_dir):
6364
return 0 # Artifacts dir already cleaned
64-
with chdir(init.paths.cloud_dir):
65-
for path in os.listdir('.'):
66-
if path == 'seed' and not remove_seed:
67-
continue
68-
try:
69-
if os.path.isdir(path) and not is_link(path):
70-
del_dir(path)
71-
else:
72-
del_file(path)
73-
except OSError as e:
74-
error('Could not remove {0}: {1}'.format(path, str(e)))
75-
return 1
65+
for path in glob.glob('%s/*' % init.paths.cloud_dir):
66+
if path == '%s/seed' % init.paths.cloud_dir and not remove_seed:
67+
continue
68+
try:
69+
if os.path.isdir(path) and not is_link(path):
70+
del_dir(path)
71+
else:
72+
del_file(path)
73+
except OSError as e:
74+
error('Could not remove {0}: {1}'.format(path, str(e)))
75+
return 1
7676
return 0
7777

7878

cloudinit/cmd/tests/test_clean.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def test_remove_artifacts_returns_one_on_errors(self):
136136
clean.remove_artifacts, remove_logs=False)
137137
self.assertEqual(1, retcode)
138138
self.assertEqual(
139-
'ERROR: Could not remove dir1: oops\n', m_stderr.getvalue())
139+
'ERROR: Could not remove %s/dir1: oops\n' % self.artifact_dir,
140+
m_stderr.getvalue())
140141

141142
def test_handle_clean_args_reboots(self):
142143
"""handle_clean_args_reboots when reboot arg is provided."""

0 commit comments

Comments
 (0)