411
426
def setup_init_script_and_start_client():
427
"Configure the init script to start the client on boot."
412
428
# XXX This function is misnamed; it doesn't start the client.
413
429
sysvconfig = SysVConfig()
414
430
sysvconfig.set_start_on_boot(True)
417
433
def stop_client_and_disable_init_script():
435
Stop landscape-client and change configuration to prevent starting
436
landscape-client on boot.
418
438
sysvconfig = SysVConfig()
419
439
sysvconfig.stop_landscape()
420
440
sysvconfig.set_start_on_boot(False)
443
def setup_http_proxy(config):
445
If a http_proxy and a https_proxy value are not set then copy the values,
446
if any, from the environment variables L{http_proxy} and L{https_proxy}.
448
if config.http_proxy is None and os.environ.get("http_proxy"):
449
config.http_proxy = os.environ["http_proxy"]
450
if config.https_proxy is None and os.environ.get("https_proxy"):
451
config.https_proxy = os.environ["https_proxy"]
454
def check_account_name_and_password(config):
456
Ensure that silent configurations which plan to start landscape-client are
457
either configured for OTP or have both an account_name and computer title.
459
if config.silent and not config.no_start:
460
if not (config.get("otp") or config.provisioning_otp or
461
(config.get("account_name") and config.get("computer_title"))):
462
raise ConfigurationError("An account name and computer title are "
466
def check_script_users(config):
468
If the configuration allows for script execution ensure that the configured
469
users are valid for that purpose.
471
if config.get("script_users"):
472
invalid_users = get_invalid_users(config.get("script_users"))
474
raise ConfigurationError("Unknown system users: %s" %
475
", ".join(invalid_users))
476
if not config.include_manager_plugins:
477
config.include_manager_plugins = "ScriptExecution"
480
def decode_base64_ssl_public_certificate(config):
482
Decode base64 encoded SSL certificate and push that back into place in the
485
# WARNING: ssl_public_certificate is misnamed, it's not the key of the
486
# certificate, but the actual certificate itself.
487
if config.ssl_public_key and config.ssl_public_key.startswith("base64:"):
488
decoded_cert = base64.decodestring(config.ssl_public_key[7:])
489
config.ssl_public_key = store_public_key_data(
490
config, decoded_cert)
493
def fetch_base64_ssl_public_certificate(hostname, on_info=print_text,
494
on_error=print_text):
496
Fetch base64 encoded SSL CA certificate from the discovered landscape
497
server and return that decoded info.
499
on_info("Fetching CA certificate from %s if available..." % hostname)
502
ca_url = "http://%s/get-ca-cert" % hostname
504
content = fetch(ca_url, insecure=True)
505
except HTTPCodeError, error:
506
on_error("Unable to fetch CA certificate from discovered server %s: "
507
"Server does not support client auto-registation." % hostname)
509
except FetchError, error:
510
on_error("Unable to fetch CA certificate from %s: %s"
511
% (hostname, str(error)))
515
ca_dict = json.loads(content)
517
if ca_dict["custom_ca_cert"].startswith("base64:"):
518
encoded_cert = ca_dict["custom_ca_cert"]
520
on_error("Auto-registration URL %s returns invalid CA JSON: "
521
"%s." % (ca_url, ca_dict))
523
# No custom CA certificate needed to talk with this server
524
on_info("No custom CA certificate available for %s." % hostname)
526
on_error("Unable to fetch CA certificate from discovered server "
527
"%s. Proceding without custom CA certificate."
423
532
def setup(config):
534
Perform steps to ensure that landscape-client is correctly configured
535
before we attempt to register it with a landscape server.
537
If we are not configured to be silent then interrogate the user to provide
538
necessary details for registration.
424
540
sysvconfig = SysVConfig()
425
541
if not config.no_start:
426
542
if config.silent:
435
551
sys.exit("Aborting Landscape configuration")
437
if config.http_proxy is None and os.environ.get("http_proxy"):
438
config.http_proxy = os.environ["http_proxy"]
439
if config.https_proxy is None and os.environ.get("https_proxy"):
440
config.https_proxy = os.environ["https_proxy"]
553
setup_http_proxy(config)
554
check_account_name_and_password(config)
442
555
if config.silent:
443
if not config.get("otp") and (not config.get("account_name") or not
444
config.get("computer_title")):
445
raise ConfigurationError("An account name and computer title are "
447
if config.get("script_users"):
448
invalid_users = get_invalid_users(config.get("script_users"))
450
raise ConfigurationError("Unknown system users: %s" %
451
", ".join(invalid_users))
452
if not config.include_manager_plugins:
453
config.include_manager_plugins = "ScriptExecution"
556
check_script_users(config)
455
558
script = LandscapeSetupScript(config)
458
# WARNING: ssl_public_key is misnamed, it's not the key of the certificate,
459
# but the actual certificate itself.
460
if config.ssl_public_key and config.ssl_public_key.startswith("base64:"):
461
decoded_cert = base64.decodestring(config.ssl_public_key[7:])
462
config.ssl_public_key = store_public_key_data(
463
config, decoded_cert)
560
decode_base64_ssl_public_certificate(config)
466
562
# Restart the client to ensure that it's using the new configuration.
467
563
if not config.no_start and not config.otp:
510
606
reactor = TwistedReactor()
511
607
exit_with_error = []
513
# XXX: many of these reactor.stop() calls should also specify a non-0 exit
514
# code, unless ok-no-register is passed.
609
def stop(error=None):
610
if not config.ok_no_register and error is not None:
611
exit_with_error.append(error)
517
612
connector.disconnect()
521
print_text("Invalid account name or "
616
on_message("Invalid account name or "
522
617
"registration password.", error=True)
526
print_text("System successfully registered.")
621
on_message("System successfully registered.")
529
624
def exchange_failure():
530
print_text("We were unable to contact the server. "
625
on_message("We were unable to contact the server. "
531
626
"Your internet connection may be down. "
532
627
"The landscape client will continue to try and contact "
533
628
"the server periodically.",
537
632
def handle_registration_errors(failure):
538
633
# We'll get invalid credentials through the signal.
540
635
connector.disconnect()
542
637
def catch_all(failure):
543
# We catch SecurityError here too, because on some DBUS configurations
544
# if you try to connect to a dbus name that doesn't have a listener,
545
# it'll try auto-starting the service, but then the StartServiceByName
546
# call can raise a SecurityError.
547
print_text(failure.getTraceback(), error=True)
548
print_text("Unknown error occurred.", error=True)
638
on_message(failure.getTraceback(), error=True)
639
on_message("Unknown error occurred.", error=True)
551
print_text("Please wait... ", "")
642
on_message("Please wait... ", "")
566
657
return results.addErrback(catch_all)
568
659
def got_error(failure):
569
print_text("There was an error communicating with the Landscape "
570
"client.", error=True)
571
print_text("This machine will be registered with the provided "
660
on_message("There was an error communicating with the Landscape"
661
" client.", error=True)
662
on_message("This machine will be registered with the provided "
572
663
"details when the client runs.", error=True)
573
if not config.ok_no_register:
574
exit_with_error.append(2)
577
666
connector = RemoteBrokerConnector(reactor, config)
578
667
result = connector.connect(max_retries=0, quiet=True)
584
673
if exit_with_error:
585
sys.exit(exit_with_error[0])
674
on_error(exit_with_error[0])
590
def fetch_import_url(url):
591
"""Handle fetching of URLs passed to --url.
593
This is done out of LandscapeSetupConfiguration since it has to deal
594
with interaction with the user and downloading of files.
597
print_text("Fetching configuration from %s..." % url)
601
except FetchError, error:
602
error_message = str(error)
603
if error_message is not None:
604
raise ImportOptionError(
605
"Couldn't download configuration from %s: %s" %
606
(url, error_message))
611
config = LandscapeSetupConfiguration(fetch_import_url)
680
config = LandscapeSetupConfiguration()
612
681
if args in (["-h"], ["--help"]):
613
682
# We let landscape-config --help to be run as normal user
614
683
config.load(args)