Skip to content

Commit

Permalink
feat: add new --register-if-needed flag for landscape-config (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
wck0 authored Aug 14, 2024
1 parent 2a7e701 commit 279baf5
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 108 deletions.
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")
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)
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:
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:
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

0 comments on commit 279baf5

Please sign in to comment.