-
Notifications
You must be signed in to change notification settings - Fork 49
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,8 @@ class LandscapeSetupConfiguration(BrokerConfiguration): | |
"ok_no_register", | ||
"import_from", | ||
"skip_registration", | ||
"force_registration", | ||
"register_if_needed", | ||
) | ||
|
||
encoding = "utf-8" | ||
|
@@ -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 | ||
|
||
|
||
|
@@ -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: | ||
|
@@ -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) | ||
|
@@ -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), | ||
|
@@ -817,6 +838,8 @@ def register( | |
if isinstance(result, SystemExit): | ||
raise result | ||
|
||
set_secure_id(config, "registering") | ||
|
||
return result | ||
|
||
|
||
|
@@ -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": ( | ||
|
@@ -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 | ||
|
@@ -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.""" | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: could we just replace usages of There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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)) |
There was a problem hiding this comment.
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?