summaryrefslogtreecommitdiff
path: root/cloudinit/cmd
diff options
context:
space:
mode:
authorChad Smith <chad.smith@canonical.com>2019-01-31 03:52:13 +0000
committerServer Team CI Bot <josh.powers+server-team-bot@canonical.com>2019-01-31 03:52:13 +0000
commit489553547f3461bd8ff67d3099237694130eb714 (patch)
tree373e867be7edd8b9f558a5ed34b9c06ea5483c07 /cloudinit/cmd
parent94a64529dccebd8fe8c7969370b8696e46023fbd (diff)
downloadvyos-cloud-init-489553547f3461bd8ff67d3099237694130eb714.tar.gz
vyos-cloud-init-489553547f3461bd8ff67d3099237694130eb714.zip
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
Diffstat (limited to 'cloudinit/cmd')
-rw-r--r--cloudinit/cmd/clean.py26
-rw-r--r--cloudinit/cmd/tests/test_clean.py3
2 files changed, 15 insertions, 14 deletions
diff --git a/cloudinit/cmd/clean.py b/cloudinit/cmd/clean.py
index de22f7f2..28ee7b84 100644
--- a/cloudinit/cmd/clean.py
+++ b/cloudinit/cmd/clean.py
@@ -5,12 +5,13 @@
"""Define 'clean' utility and handler as part of cloud-init commandline."""
import argparse
+import glob
import os
import sys
from cloudinit.stages import Init
from cloudinit.util import (
- ProcessExecutionError, chdir, del_dir, del_file, get_config_logfiles,
+ ProcessExecutionError, del_dir, del_file, get_config_logfiles,
is_link, subp)
@@ -61,18 +62,17 @@ def remove_artifacts(remove_logs, remove_seed=False):
if not os.path.isdir(init.paths.cloud_dir):
return 0 # Artifacts dir already cleaned
- with chdir(init.paths.cloud_dir):
- for path in os.listdir('.'):
- if path == 'seed' and not remove_seed:
- continue
- try:
- if os.path.isdir(path) and not is_link(path):
- del_dir(path)
- else:
- del_file(path)
- except OSError as e:
- error('Could not remove {0}: {1}'.format(path, str(e)))
- return 1
+ for path in glob.glob('%s/*' % init.paths.cloud_dir):
+ if path == '%s/seed' % init.paths.cloud_dir and not remove_seed:
+ continue
+ try:
+ if os.path.isdir(path) and not is_link(path):
+ del_dir(path)
+ else:
+ del_file(path)
+ except OSError as e:
+ error('Could not remove {0}: {1}'.format(path, str(e)))
+ return 1
return 0
diff --git a/cloudinit/cmd/tests/test_clean.py b/cloudinit/cmd/tests/test_clean.py
index 5a3ec3bf..15c3294e 100644
--- a/cloudinit/cmd/tests/test_clean.py
+++ b/cloudinit/cmd/tests/test_clean.py
@@ -136,7 +136,8 @@ class TestClean(CiTestCase):
clean.remove_artifacts, remove_logs=False)
self.assertEqual(1, retcode)
self.assertEqual(
- 'ERROR: Could not remove dir1: oops\n', m_stderr.getvalue())
+ 'ERROR: Could not remove %s/dir1: oops\n' % self.artifact_dir,
+ m_stderr.getvalue())
def test_handle_clean_args_reboots(self):
"""handle_clean_args_reboots when reboot arg is provided."""