4
4
for the C{landscape-config} script.
12
from ConfigParser import ConfigParser, Error as ConfigParserError
13
from StringIO import StringIO
12
17
from dbus.exceptions import DBusException
15
20
from landscape.lib.dbus_util import (
16
21
get_bus, NoReplyError, ServiceUnknownError, SecurityError)
17
22
from landscape.lib.twisted_util import gather_results
23
from landscape.lib.fetch import fetch, HTTPCodeError
19
25
from landscape.broker.registration import InvalidCredentialsError
20
26
from landscape.broker.deployment import BrokerConfiguration
24
30
class ConfigurationError(Exception):
25
31
"""Raised when required configuration values are missing."""
33
class ImportOptionError(ConfigurationError):
34
"""Raised when there are issues with handling the --import option."""
28
37
def print_text(text, end="\n", error=False):
37
46
class LandscapeSetupConfiguration(BrokerConfiguration):
39
unsaved_options = ("no_start", "disable", "silent", "ok_no_register")
48
unsaved_options = ("no_start", "disable", "silent", "ok_no_register",
51
def __init__(self, fetch_import_url):
52
super(LandscapeSetupConfiguration, self).__init__()
53
self._fetch_import_url = fetch_import_url
55
def _load_external_options(self):
56
"""Handle the --import parameter.
58
Imported options behave as if they were passed in the
59
command line, with precedence being given to real command
63
parser = ConfigParser()
66
if "://" in self.import_from:
67
# If it's from a URL, download it now.
69
os.environ["http_proxy"] = self.http_proxy
71
os.environ["https_proxy"] = self.https_proxy
72
content = self._fetch_import_url(self.import_from)
73
parser.readfp(StringIO(content))
74
elif not os.path.isfile(self.import_from):
75
raise ImportOptionError("File %s doesn't exist." %
78
parser.read(self.import_from)
79
except ConfigParserError, error:
80
raise ImportOptionError(str(error))
82
# But real command line options have precedence.
84
if parser.has_section(self.config_section):
85
options = dict(parser.items(self.config_section))
87
raise ImportOptionError("Nothing to import at %s." %
89
options.update(self._command_line_options)
90
self._command_line_options = options
41
92
def make_parser(self):
45
96
parser = super(LandscapeSetupConfiguration, self).make_parser()
98
parser.add_option("--import", dest="import_from",
99
metavar="FILENAME_OR_URL",
100
help="Filename or URL to import configuration from. "
101
"Imported options behave as if they were passed "
102
"in the command line, with precedence being "
103
"given to real command line options.")
47
104
parser.add_option("--script-users", metavar="USERS",
48
105
help="A comma-separated list of users to allow "
49
106
"scripts to run. To allow scripts to be run "
318
375
script = LandscapeSetupScript(config)
378
if config.ssl_public_key and config.ssl_public_key.startswith("base64:"):
379
key_filename = config.get_config_filename() + ".ssl_public_key"
380
print_text("Writing SSL public key to %s..." % key_filename)
381
decoded_key = base64.decodestring(config.ssl_public_key[7:])
382
key_file = open(key_filename, "w")
383
key_file.write(decoded_key)
385
config.ssl_public_key = key_filename
322
388
# Restart the client to ensure that it's using the new configuration.
491
def fetch_import_url(url):
492
"""Handle fetching of URLs passed to --url.
494
This is done out of LandscapeSetupConfiguration since it has to deal
495
with interaction with the user and downloading of files.
498
print_text("Fetching configuration from %s..." % url)
502
except pycurl.error, error:
503
error_message = error.args[1]
504
except HTTPCodeError, error:
505
error_message = str(error)
506
if error_message is not None:
507
raise ImportOptionError(
508
"Couldn't download configuration from %s: %s" %
509
(url, error_message))
426
514
if os.getuid() != 0:
427
515
sys.exit("landscape-config must be run as root.")
428
config = LandscapeSetupConfiguration()
517
config = LandscapeSetupConfiguration(fetch_import_url)
520
except ImportOptionError, error:
521
print_text(str(error), error=True)
431
524
# Disable startup on boot and stop the client, if one is running.
432
525
if config.disable: