~ubuntu-branches/ubuntu/saucy/cloud-init/saucy

« back to all changes in this revision

Viewing changes to cloudinit/config/cc_landscape.py

  • Committer: Scott Moser
  • Date: 2012-07-06 21:24:18 UTC
  • mfrom: (1.1.26)
  • Revision ID: smoser@ubuntu.com-20120706212418-zwcpwxsdodi0pms3
New upstream snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vi: ts=4 expandtab
 
2
#
 
3
#    Copyright (C) 2011 Canonical Ltd.
 
4
#    Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
 
5
#
 
6
#    Author: Scott Moser <scott.moser@canonical.com>
 
7
#    Author: Juerg Haefliger <juerg.haefliger@hp.com>
 
8
#
 
9
#    This program is free software: you can redistribute it and/or modify
 
10
#    it under the terms of the GNU General Public License version 3, as
 
11
#    published by the Free Software Foundation.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
 
 
21
import os
 
22
 
 
23
from StringIO import StringIO
 
24
 
 
25
from configobj import ConfigObj
 
26
 
 
27
from cloudinit import util
 
28
 
 
29
from cloudinit.settings import PER_INSTANCE
 
30
 
 
31
frequency = PER_INSTANCE
 
32
 
 
33
LSC_CLIENT_CFG_FILE = "/etc/landscape/client.conf"
 
34
 
 
35
distros = ['ubuntu']
 
36
 
 
37
# defaults taken from stock client.conf in landscape-client 11.07.1.1-0ubuntu2
 
38
LSC_BUILTIN_CFG = {
 
39
  'client': {
 
40
    'log_level': "info",
 
41
    'url': "https://landscape.canonical.com/message-system",
 
42
    'ping_url': "http://landscape.canonical.com/ping",
 
43
    'data_path': "/var/lib/landscape/client",
 
44
  }
 
45
}
 
46
 
 
47
 
 
48
def handle(_name, cfg, cloud, log, _args):
 
49
    """
 
50
    Basically turn a top level 'landscape' entry with a 'client' dict
 
51
    and render it to ConfigObj format under '[client]' section in
 
52
    /etc/landscape/client.conf
 
53
    """
 
54
 
 
55
    ls_cloudcfg = cfg.get("landscape", {})
 
56
 
 
57
    if not isinstance(ls_cloudcfg, (dict)):
 
58
        raise RuntimeError(("'landscape' key existed in config,"
 
59
                            " but not a dictionary type,"
 
60
                            " is a %s instead"), util.obj_name(ls_cloudcfg))
 
61
 
 
62
    merge_data = [
 
63
        LSC_BUILTIN_CFG,
 
64
        cloud.paths.join(True, LSC_CLIENT_CFG_FILE),
 
65
        ls_cloudcfg,
 
66
    ]
 
67
    merged = merge_together(merge_data)
 
68
 
 
69
    lsc_client_fn = cloud.paths.join(False, LSC_CLIENT_CFG_FILE)
 
70
    lsc_dir = cloud.paths.join(False, os.path.dirname(lsc_client_fn))
 
71
    if not os.path.isdir(lsc_dir):
 
72
        util.ensure_dir(lsc_dir)
 
73
 
 
74
    contents = StringIO()
 
75
    merged.write(contents)
 
76
    contents.flush()
 
77
 
 
78
    util.write_file(lsc_client_fn, contents.getvalue())
 
79
    log.debug("Wrote landscape config file to %s", lsc_client_fn)
 
80
 
 
81
 
 
82
def merge_together(objs):
 
83
    """
 
84
    merge together ConfigObj objects or things that ConfigObj() will take in
 
85
    later entries override earlier
 
86
    """
 
87
    cfg = ConfigObj({})
 
88
    for obj in objs:
 
89
        if not obj:
 
90
            continue
 
91
        if isinstance(obj, ConfigObj):
 
92
            cfg.merge(obj)
 
93
        else:
 
94
            cfg.merge(ConfigObj(obj))
 
95
    return cfg