Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: have landscape-config --silent only send a registration messa… #258

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 74 additions & 26 deletions landscape/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class LandscapeSetupConfiguration(BrokerConfiguration):
"ok_no_register",
"import_from",
"skip_registration",
"force_registration",
"register_if_needed",
)

encoding = "utf-8"
Expand Down Expand Up @@ -254,6 +256,18 @@ def make_parser(self):
action="store_true",
help="Don't send a new registration request",
)
parser.add_option(
"--force-registration",
action="store_true",
help="Force sending a new registration request",
)
parser.add_option(
"--register-if-needed",
action="store_true",
help=(
"Send a new registration request only if one has not been sent"
),
)
return parser


Expand Down Expand Up @@ -481,8 +495,11 @@ def query_landscape_edition(self):
"Landscape Domain: ",
True,
).strip("/")
self.landscape_domain = re.sub(r"^https?://", "",
self.landscape_domain)
self.landscape_domain = re.sub(
r"^https?://",
"",
self.landscape_domain,
)
self.config.ping_url = f"http://{self.landscape_domain}/ping"
self.config.url = f"https://{self.landscape_domain}/message-system"
else:
Expand Down Expand Up @@ -639,8 +656,10 @@ def setup(config):
script.run()
decode_base64_ssl_public_certificate(config)
config.write()
# Restart the client to ensure that it's using the new configuration.


def restart_client(config):
"""Restart the client to ensure that it's using the new configuration."""
if not config.no_start:
try:
set_secure_id(config, "registering")
Expand Down Expand Up @@ -732,7 +751,7 @@ def done(ignored_result, connector, reactor):


def got_connection(add_result, connector, reactor, remote):
"""Handle becomming connected to a broker."""
"""Handle becoming connected to a broker."""
handlers = {
"registration-done": partial(success, add_result),
"registration-failed": partial(failure, add_result),
Expand Down Expand Up @@ -817,6 +836,8 @@ def register(
if isinstance(result, SystemExit):
raise result

set_secure_id(config, "registering")

return result


Expand All @@ -825,6 +846,7 @@ def report_registration_outcome(what_happened, print=print):
human-readable form.
"""
messages = {
"registration-skipped": "Registration skipped.",
"success": "Registration request sent successfully.",
"unknown-account": "Invalid account name or registration key.",
"max-pending-computers": (
Expand All @@ -847,16 +869,17 @@ def report_registration_outcome(what_happened, print=print):
),
}
message = messages.get(what_happened)
use_std_out = what_happened in {"success", "registration-skipped"}
if message:
fd = sys.stdout if what_happened == "success" else sys.stderr
fd = sys.stdout if use_std_out else sys.stderr
print(message, file=fd)


def determine_exit_code(what_happened):
"""Return what the application's exit code should be depending on the
registration result.
"""
if what_happened == "success":
if what_happened in {"success", "registration-skipped"}:
return 0
else:
return 2 # An error happened
Expand Down Expand Up @@ -912,6 +935,17 @@ def set_secure_id(config, new_id):
persist.save()


def get_secure_id(config):
persist = Persist(
filename=os.path.join(
config.data_path,
f"{BrokerService.service_name}.bpickle",
),
)
identity = Identity(config, persist)
return identity.secure_id


def main(args, print=print):
"""Interact with the user and the server to set up client configuration."""

Expand All @@ -922,9 +956,17 @@ def main(args, print=print):
print_text(str(error), error=True)
sys.exit(1)

if config.skip_registration and config.force_registration:
sys.exit(
"Do not set both skip registration "
"and force registration together.",
)

already_registered = is_registered(config)

if config.is_registered:

registration_status = is_registered(config)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: could we just replace usages of registration_status with already_registered?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I picked this nit.

registration_status = already_registered

info_text = registration_info_text(config, registration_status)
print(info_text)
Expand Down Expand Up @@ -953,31 +995,37 @@ def main(args, print=print):
print_text(str(e))
sys.exit("Aborting Landscape configuration")

# Attempt to register the client.
reactor = LandscapeReactor()

if config.skip_registration:
result = "registration-skipped"
report_registration_outcome(result, print=print)
return

if config.silent:
should_register = False

if config.force_registration:
Copy link
Contributor

@srunde3 srunde3 Aug 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could rearrange this block to make it slightly simpler, if you so desire:

    if config.force_registration:
        should_register = True
    elif config.register_if_needed:
        should_register = not already_registered
    elif config.silent:
        should_register = True
    else:
        ...

should_register = True
elif config.silent and not config.register_if_needed:
should_register = True
elif config.register_if_needed:
should_register = not already_registered
else:
default_answer = not already_registered
should_register = prompt_yes_no(
"\nRequest a new registration for this computer now?",
default=default_answer,
)

if should_register or config.silent:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check is causing the client to reset the secure_id even if we pass --register-if-needed and the client is already registered; the restart_client function sets the secure_id to 'registering", and then we end up with another pending computer.

restart_client(config)
if should_register:
# Attempt to register the client.
reactor = LandscapeReactor()
result = register(
config,
reactor,
on_error=lambda _: set_secure_id(config, None),
)
report_registration_outcome(result, print=print)
sys.exit(determine_exit_code(result))
else:
default_answer = not is_registered(config)
answer = prompt_yes_no(
"\nRequest a new registration for this computer now?",
default=default_answer,
)
if answer:
result = register(
config,
reactor,
on_error=lambda _: set_secure_id(config, None),
)
report_registration_outcome(result, print=print)
sys.exit(determine_exit_code(result))
result = "registration-skipped"
report_registration_outcome(result, print=print)
sys.exit(determine_exit_code(result))
Loading
Loading