Adding --debug breaks argument parsing

Bug #2040325 reported by Tired Sysadmin
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
cloud-init (Ubuntu)
Fix Released
Undecided
Calvin Mwadime Makokha

Bug Description

This is on 22.04.3, specifically 23.3.1-0ubuntu1~22.04.1

I'm planning to use "cloud-init clean" to prepare golden images. I was hoping to get some insight into what, exactly, the script is doing under the hood (reading cmd/clean.py helps but part of that is hard to follow without already knowing the internals of cloud-init) so I tried running with the '--debug' flag documented as showing "additional pre-action logging".

% cloud-init --debug clean -h
usage: /usr/bin/cloud-init clean [-h]

options:
  -h, --help show this help message and exit

Interesting, but removing --debug helps:

% cloud-init clean -h
usage: /usr/bin/cloud-init clean [-h] [-l] [--machine-id] [-r] [-s]

options:
  -h, --help show this help message and exit
  -l, --logs Remove cloud-init logs.
  --machine-id Set /etc/machine-id to [...elided for launchpad brevity...]
  -r, --reboot Reboot system after logs are cleaned so cloud-init re-runs.
  -s, --seed Remove cloud-init seed directory /var/lib/cloud/seed.

Okay, in fairness, most users won't be running --debug at the same time as --help, but I would still have expected it to print *at least* the same help output if not more debugging stuff.

So I try running it for realsies:

# cloud-init --debug clean --logs --machine-id --seed
usage: /usr/bin/cloud-init [-h] [--version] [--file FILES] [--debug] [--force]
                           {init,modules,single,query,features,analyze,devel,collect-logs,clean,status,schema} ...
/usr/bin/cloud-init: error: unrecognized arguments: --logs --machine-id --seed

# cloud-init --debug clean
Traceback (most recent call last):
  File "/usr/bin/cloud-init", line 33, in <module>
    sys.exit(load_entry_point('cloud-init==23.3.1', 'console_scripts', 'cloud-init')())
  File "/usr/lib/python3/dist-packages/cloudinit/cmd/main.py", line 1036, in main
    (name, functor) = args.action
AttributeError: 'Namespace' object has no attribute 'action'

That outcome is less than awesome.

Running without --debug worked:

# cloud-init clean --logs --machine-id --seed
# echo $?
0
#

Although in the absence of --debug output, it's necessary to go look around the filesystem to verify what's been changed vis-a-vis what options were given.

Revision history for this message
Calvin Mwadime Makokha (calvinmwadime) wrote :

Hello Tired Sysadmin,
Thank you for making cloud-init better but submitting the bug. I have reproduced the issue on a container and was able to find the problem. We had a bug when parsing the argparse options and subcommands. I will be putting up a PR on Github to solve this bug. I will update the thread with the PR link later.

Revision history for this message
Calvin Mwadime Makokha (calvinmwadime) wrote :

I have put up a PR on Github https://github.com/canonical/cloud-init/pull/4559. Please check it out.

Chad Smith (chad.smith)
Changed in cloud-init (Ubuntu):
assignee: nobody → Calvin Mwadime Makokha (calvinmwadime)
status: New → In Progress
Revision history for this message
Chad Smith (chad.smith) wrote (last edit ):

The mentioned PR addresses the traceback in handling the --debug parameter with the "cloud-init clean" command. It does not however address the feature request to support `--debug` param for the cloud-init clean subcommand to print actions that are taken by the clean operation. I think this approach to the PR is ok and meets the intent of the original bug as as --debug is not documented as a supported parameter to the specific subcommand represented by `cloud-init clean --help`.

Revision history for this message
Chad Smith (chad.smith) wrote :

If this feature is still important to you, please do file an enhancement in github so that cloud-init clean to support the --debug parameter so it can grow the ability to print out the actions it is taking to better inform users of environmental changes taken as a result of the clean action.

https://github.com/canonical/cloud-init/issues/new

Revision history for this message
Chad Smith (chad.smith) wrote :

This pull request has landed in https://github.com/canonical/cloud-init/commit/c02098ff0bf3aaf6d15b3d968af44419dddf3466 and is expected to be in the next major release of cloud-init version 24.1. (Mid/late february)

Changed in cloud-init (Ubuntu):
status: In Progress → Fix Committed
Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package cloud-init - 24.1~1g1003d438-0ubuntu1

---------------
cloud-init (24.1~1g1003d438-0ubuntu1) noble; urgency=medium

  * Upstream snapshot based on upstream/main at 1003d438.
    - Bugs fixed in this snapshot: (LP: #2040325)

 -- Alberto Contreras <email address hidden> Fri, 15 Dec 2023 11:34:16 +0100

Changed in cloud-init (Ubuntu):
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.