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 all 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
8 changes: 4 additions & 4 deletions landscape/client/broker/exchange.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@

- C{CLIENT_ACCEPTED_TYPES}: Optionally, a list of message types that the
client accepts. The server is supposed to send the client only messages of
this type. It will be inlcuded in the payload only if the hash that the
server sends us is out-of-date. This behavior is simmetric with respect to
this type. It will be included in the payload only if the hash that the
server sends us is out-of-date. This behavior is symmetric with respect to
the C{SERVER_ACCEPTED_TYPES_DIGEST} field described above.

Server->Client Payload
Expand Down Expand Up @@ -91,7 +91,7 @@
- C{EXPECTED_EXCHANGE_TOKEN}: The token (UUID string) that the server expects
to receive back the next time the client performs an exchange. Since the
client receives a new token at each exchange, this can be used by the
server to detect cloned clients (either the orignal client or the cloned
server to detect cloned clients (either the original client or the cloned
client will eventually send an expired token). The token is sent by the
client as a special HTTP header (see L{landscape.broker.transport}).

Expand Down Expand Up @@ -163,7 +163,7 @@
next-expected-sequence in the prior connection, or 0 if there was no
previous connection.

- Get back a next-expected-sequence from the server. If that value is is not
- Get back a next-expected-sequence from the server. If that value is not
len(messages) + previous-next-expected, then resynchronize.

It does the following when acting as Receiver:
Expand Down
2 changes: 1 addition & 1 deletion landscape/client/broker/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def _message_delivered(self, results, message):
@remote
def stop_exchanger(self):
"""
Stop exchaging messages with the message server.
Stop exchanging messages with the message server.

Eventually, it is required by the plugin that no more message exchanges
are performed.
Expand Down
104 changes: 77 additions & 27 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,11 +656,15 @@ 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")
secure_id = get_secure_id(config)
if not secure_id:
set_secure_id(config, "registering")
Copy link
Contributor

Choose a reason for hiding this comment

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

I realize this is somewhat of a carryover from the existing implementation, but is it necessary to set the secure_id in the configuration file and in the registration handler? Could we get rid of it here since you set it in the registration handler now?

ServiceConfig.restart_landscape()
except ServiceConfigException as exc:
print_text(str(exc), error=True)
Expand Down Expand Up @@ -732,7 +753,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 +838,8 @@ def register(
if isinstance(result, SystemExit):
raise result

set_secure_id(config, "registering")

return result


Expand All @@ -825,6 +848,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 +871,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 +937,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 +958,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 +997,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