diff options
Diffstat (limited to 'cloudinit/cmd/clean.py')
-rwxr-xr-x | cloudinit/cmd/clean.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/cloudinit/cmd/clean.py b/cloudinit/cmd/clean.py index 1a017608..65d3eece 100755 --- a/cloudinit/cmd/clean.py +++ b/cloudinit/cmd/clean.py @@ -11,8 +11,9 @@ import glob import os import sys +from cloudinit import settings from cloudinit.stages import Init -from cloudinit.subp import ProcessExecutionError, subp +from cloudinit.subp import ProcessExecutionError, runparts, subp from cloudinit.util import ( del_dir, del_file, @@ -21,6 +22,8 @@ from cloudinit.util import ( is_link, ) +ETC_MACHINE_ID = "/etc/machine-id" + def get_parser(parser=None): """Build or extend an arg parser for clean utility. @@ -48,6 +51,15 @@ def get_parser(parser=None): help="Remove cloud-init logs.", ) parser.add_argument( + "--machine-id", + action="store_true", + default=False, + help=( + "Remove /etc/machine-id for golden image creation." + " Next boot generates a new machine-id." + ), + ) + parser.add_argument( "-r", "--reboot", action="store_true", @@ -94,12 +106,21 @@ def remove_artifacts(remove_logs, remove_seed=False): except OSError as e: error("Could not remove {0}: {1}".format(path, str(e))) return 1 + try: + runparts(settings.CLEAN_RUNPARTS_DIR) + except Exception as e: + error( + f"Failure during run-parts of {settings.CLEAN_RUNPARTS_DIR}: {e}" + ) + return 1 return 0 def handle_clean_args(name, args): """Handle calls to 'cloud-init clean' as a subcommand.""" exit_code = remove_artifacts(args.remove_logs, args.remove_seed) + if args.machine_id: + del_file(ETC_MACHINE_ID) if exit_code == 0 and args.reboot: cmd = ["shutdown", "-r", "now"] try: |