~free.ekanayaka/landscape-client/lucid-1.5.0-0ubuntu0.10.04.0

« back to all changes in this revision

Viewing changes to landscape/configuration.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-03-18 20:42:05 UTC
  • Revision ID: james.westby@ubuntu.com-20090318204205-v17ejhu5urdsrm7j
Tags: 1.0.28-0ubuntu1
New upstream release. (LP: #343954)

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
for the C{landscape-config} script.
5
5
"""
6
6
 
 
7
import base64
7
8
import time
8
9
import sys
9
10
import os
10
11
import getpass
 
12
from ConfigParser import ConfigParser, Error as ConfigParserError
 
13
from StringIO import StringIO
 
14
 
 
15
import pycurl
11
16
 
12
17
from dbus.exceptions import DBusException
13
18
 
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
18
24
 
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."""
26
32
 
 
33
class ImportOptionError(ConfigurationError):
 
34
    """Raised when there are issues with handling the --import option."""
 
35
 
27
36
 
28
37
def print_text(text, end="\n", error=False):
29
38
    if error:
36
45
 
37
46
class LandscapeSetupConfiguration(BrokerConfiguration):
38
47
 
39
 
    unsaved_options = ("no_start", "disable", "silent", "ok_no_register")
 
48
    unsaved_options = ("no_start", "disable", "silent", "ok_no_register",
 
49
                       "import_from")
 
50
 
 
51
    def __init__(self, fetch_import_url):
 
52
        super(LandscapeSetupConfiguration, self).__init__()
 
53
        self._fetch_import_url = fetch_import_url
 
54
 
 
55
    def _load_external_options(self):
 
56
        """Handle the --import parameter.
 
57
 
 
58
        Imported options behave as if they were passed in the
 
59
        command line, with precedence being given to real command
 
60
        line options.
 
61
        """
 
62
        if self.import_from:
 
63
            parser = ConfigParser()
 
64
 
 
65
            try:
 
66
                if "://" in self.import_from:
 
67
                    # If it's from a URL, download it now.
 
68
                    if self.http_proxy:
 
69
                        os.environ["http_proxy"] = self.http_proxy
 
70
                    if self.https_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." %
 
76
                                            self.import_from)
 
77
                else:
 
78
                    parser.read(self.import_from)
 
79
            except ConfigParserError, error:
 
80
                raise ImportOptionError(str(error))
 
81
 
 
82
            # But real command line options have precedence.
 
83
            options = None
 
84
            if parser.has_section(self.config_section):
 
85
                options = dict(parser.items(self.config_section))
 
86
            if not options:
 
87
                raise ImportOptionError("Nothing to import at %s." %
 
88
                                        self.import_from)
 
89
            options.update(self._command_line_options)
 
90
            self._command_line_options = options
40
91
 
41
92
    def make_parser(self):
42
93
        """
44
95
        """
45
96
        parser = super(LandscapeSetupConfiguration, self).make_parser()
46
97
 
 
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 "
317
374
    else:
318
375
        script = LandscapeSetupScript(config)
319
376
        script.run()
 
377
    
 
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)
 
384
        key_file.close()
 
385
        config.ssl_public_key = key_filename
320
386
 
321
387
    config.write()
322
388
    # Restart the client to ensure that it's using the new configuration.
422
488
    reactor.run()
423
489
 
424
490
 
 
491
def fetch_import_url(url):
 
492
    """Handle fetching of URLs passed to --url.
 
493
 
 
494
    This is done out of LandscapeSetupConfiguration since it has to deal
 
495
    with interaction with the user and downloading of files.
 
496
    """
 
497
 
 
498
    print_text("Fetching configuration from %s..." % url)
 
499
    error_message = None
 
500
    try:
 
501
        content = fetch(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))
 
510
    return content
 
511
 
 
512
 
425
513
def main(args):
426
514
    if os.getuid() != 0:
427
515
        sys.exit("landscape-config must be run as root.")
428
 
    config = LandscapeSetupConfiguration()
429
 
    config.load(args)
 
516
 
 
517
    config = LandscapeSetupConfiguration(fetch_import_url)
 
518
    try:
 
519
        config.load(args)
 
520
    except ImportOptionError, error:
 
521
        print_text(str(error), error=True)
 
522
        sys.exit(1)
430
523
 
431
524
    # Disable startup on boot and stop the client, if one is running.
432
525
    if config.disable: