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 1 commit
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
84 changes: 59 additions & 25 deletions landscape/client/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class LandscapeSetupConfiguration(BrokerConfiguration):
"ok_no_register",
"import_from",
"skip_registration",
"force_registration",
)

encoding = "utf-8"
Expand Down Expand Up @@ -254,6 +255,11 @@ 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",
)
return parser


Expand Down Expand Up @@ -481,8 +487,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 @@ -643,7 +652,9 @@ def setup(config):

if not config.no_start:
try:
set_secure_id(config, "registering")
secure_id = get_secure_id(config)
if (not secure_id) or config.force_registration:
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 +743,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 +828,8 @@ def register(
if isinstance(result, SystemExit):
raise result

set_secure_id(config, "registering")

return result


Expand All @@ -825,6 +838,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 +861,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 +927,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 +948,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 +987,31 @@ def main(args, print=print):
print_text(str(e))
sys.exit("Aborting Landscape configuration")

if config.skip_registration:
return

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

if config.skip_registration:
return
should_register = False

if config.silent:
if config.silent and (not already_registered):
should_register = True
elif config.force_registration:
should_register = True
elif not config.silent:
default_answer = not already_registered
should_register = prompt_yes_no(
"\nRequest a new registration for this computer now?",
default=default_answer,
)
if should_register:
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))
71 changes: 63 additions & 8 deletions landscape/client/tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -1266,16 +1266,23 @@ def test_setup_prefers_proxies_from_config_over_environment(
setup(config)
mock_setup_script().run.assert_called_once_with()

# Reload it to enusre it was written down.
# Reload it to ensure it was written down.
config.reload()

self.assertEqual(config.http_proxy, "http://config")
self.assertEqual(config.https_proxy, "https://config")

@mock.patch("landscape.client.configuration.sys.exit")
@mock.patch("landscape.client.configuration.input", return_value="n")
@mock.patch("landscape.client.configuration.register")
@mock.patch("landscape.client.configuration.setup")
def test_main_no_registration(self, mock_setup, mock_register, mock_input):
def test_main_no_registration(
self,
mock_setup,
mock_register,
mock_input,
mock_sys_exit,
):
main(["-c", self.make_working_config()], print=noop_print)
mock_register.assert_not_called()
mock_input.assert_called_once_with(
Expand All @@ -1290,19 +1297,52 @@ def test_skip_registration(self, mock_setup, mock_register, mock_input):
Registration and input asking user to register is not called
when flag on
"""
main(["-c", self.make_working_config(), "--skip-registration"],
print=noop_print)
main(
["-c", self.make_working_config(), "--skip-registration"],
print=noop_print,
)
mock_register.assert_not_called()
mock_input.assert_not_called()

@mock.patch("landscape.client.configuration.register")
@mock.patch("landscape.client.configuration.setup")
def test_main_no_registration_silent(self, mock_setup, mock_register):
"""Skip registration works in silent mode"""
main(["-c", self.make_working_config(), "--skip-registration",
"--silent"], print=noop_print)
main(
[
"-c",
self.make_working_config(),
"--skip-registration",
"--silent",
],
print=noop_print,
)
mock_register.assert_not_called()

@mock.patch("landscape.client.configuration.sys.exit")
@mock.patch("landscape.client.configuration.input")
@mock.patch("landscape.client.configuration.register")
@mock.patch("landscape.client.configuration.setup")
def test_main_force_registration_silent(
self,
mock_setup,
mock_register,
mock_input,
mock_sys_exit,
):
"""Force registration works in silent mode"""
main(
[
"-c",
self.make_working_config(),
"--force-registration",
"--silent",
],
print=noop_print,
)
mock_register.assert_called_once()
mock_input.assert_not_called()

@mock.patch(
"landscape.client.configuration.register",
return_value="success",
Expand Down Expand Up @@ -2691,6 +2731,14 @@ def test_success_case(self):
self.assertIn("Registration request sent successfully.", self.result)
self.assertIn(sys.stdout.name, self.output)

def test_registration_skipped_case(self):
report_registration_outcome(
"registration-skipped",
print=self.record_result,
)
self.assertIn("Registration skipped.", self.result)
self.assertIn(sys.stdout.name, self.output)

def test_unknown_account_case(self):
"""
If the unknown-account error is found, an appropriate message is
Expand Down Expand Up @@ -2756,6 +2804,13 @@ def test_success_means_exit_code_0(self):
result = determine_exit_code("success")
self.assertEqual(0, result)

def test_registration_skipped_means_exit_code_0(self):
"""
When passed "success" the determine_exit_code function returns 0.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: looks like a carryover from a copy-paste; should be "registration-skipped"

Copy link
Contributor

Choose a reason for hiding this comment

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

Should be "registration-skipped"?

"""
result = determine_exit_code("registration-skipped")
self.assertEqual(0, result)

def test_a_failure_means_exit_code_2(self):
"""
When passed a failure result, the determine_exit_code function returns
Expand Down Expand Up @@ -2783,13 +2838,13 @@ def setUp(self):

def test_is_registered_false(self):
"""
If the client hasn't previouly registered, is_registered returns False.
If the client hasn't previously registered, is_registered returns False
"""
self.assertFalse(is_registered(self.config))

def test_is_registered_true(self):
"""
If the client has previouly registered, is_registered returns True.
If the client has previously registered, is_registered returns True.
"""
self.persist.set("registration.secure-id", "super-secure")
self.persist.save()
Expand Down
Loading