~milner/cloud-init/lint-cleanups

« back to all changes in this revision

Viewing changes to cloudinit/DataSource.py

  • Committer: Mike Milner
  • Date: 2012-01-18 01:23:09 UTC
  • mfrom: (502.2.5 trunk)
  • Revision ID: mike.milner@canonical.com-20120118012309-aqzzfh04frni7q0y
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
DEP_FILESYSTEM = "FILESYSTEM"
21
21
DEP_NETWORK = "NETWORK"
22
22
 
23
 
import UserDataHandler as ud
 
23
import cloudinit.UserDataHandler as ud
24
24
import cloudinit.util as util
25
25
import socket
26
26
 
 
27
 
27
28
class DataSource:
28
29
    userdata = None
29
30
    metadata = None
30
31
    userdata_raw = None
31
32
    cfgname = ""
32
 
    # system config (passed in from cloudinit, 
 
33
    # system config (passed in from cloudinit,
33
34
    # cloud-config before input from the DataSource)
34
 
    sys_cfg = { }
 
35
    sys_cfg = {}
35
36
    # datasource config, the cloud-config['datasource']['__name__']
36
 
    ds_cfg = { }  # datasource config
 
37
    ds_cfg = {}  # datasource config
37
38
 
38
 
    def __init__(self,sys_cfg=None):
 
39
    def __init__(self, sys_cfg=None):
39
40
        if not self.cfgname:
40
41
            name = str(self.__class__).split(".")[-1]
41
42
            if name.startswith("DataSource"):
45
46
            self.sys_cfg = sys_cfg
46
47
 
47
48
        self.ds_cfg = util.get_cfg_by_path(self.sys_cfg,
48
 
                          ("datasource",self.cfgname),self.ds_cfg)
 
49
                          ("datasource", self.cfgname), self.ds_cfg)
49
50
 
50
51
    def get_userdata(self):
51
52
        if self.userdata == None:
55
56
    def get_userdata_raw(self):
56
57
        return(self.userdata_raw)
57
58
 
58
 
 
59
59
    # the data sources' config_obj is a cloud-config formated
60
60
    # object that came to it from ways other than cloud-config
61
61
    # because cloud-config content would be handled elsewhere
62
62
    def get_config_obj(self):
63
 
        return({ })
 
63
        return({})
64
64
 
65
65
    def get_public_ssh_keys(self):
66
66
        keys = []
67
 
        if not self.metadata.has_key('public-keys'): return([])
 
67
        if 'public-keys' not in self.metadata:
 
68
            return([])
68
69
 
69
70
        if isinstance(self.metadata['public-keys'], str):
70
 
            return([self.metadata['public-keys'],])
71
 
            
 
71
            return([self.metadata['public-keys'], ])
 
72
 
72
73
        for _keyname, klist in self.metadata['public-keys'].items():
73
74
            # lp:506332 uec metadata service responds with
74
75
            # data that makes boto populate a string for 'klist' rather
75
76
            # than a list.
76
 
            if isinstance(klist,str):
77
 
                klist = [ klist ]
 
77
            if isinstance(klist, str):
 
78
                klist = [klist]
78
79
            for pkey in klist:
79
80
                # there is an empty string at the end of the keylist, trim it
80
81
                if pkey:
103
104
 
104
105
    def get_hostname(self, fqdn=False):
105
106
        defdomain = "localdomain"
106
 
        defhost = "localhost" 
 
107
        defhost = "localhost"
107
108
 
108
109
        domain = defdomain
109
110
        if not 'local-hostname' in self.metadata:
119
120
            fqdn = util.get_fqdn_from_hosts(hostname)
120
121
 
121
122
            if fqdn and fqdn.find(".") > 0:
122
 
                toks = fqdn.split(".")
 
123
                toks = str(fqdn).split(".")
123
124
            elif hostname:
124
 
                toks = [ hostname, defdomain ]
 
125
                toks = [hostname, defdomain]
125
126
            else:
126
 
                toks = [ defhost, defdomain ]
127
 
 
 
127
                toks = [defhost, defdomain]
128
128
 
129
129
        else:
130
130
            # if there is an ipv4 address in 'local-hostname', then
131
131
            # make up a hostname (LP: #475354) in format ip-xx.xx.xx.xx
132
132
            lhost = self.metadata['local-hostname']
133
133
            if is_ipv4(lhost):
134
 
                toks = "ip-%s" % lhost.replace(".","-")
 
134
                toks = "ip-%s" % lhost.replace(".", "-")
135
135
            else:
136
136
                toks = lhost.split(".")
137
137
 
142
142
            hostname = toks[0]
143
143
 
144
144
        if fqdn:
145
 
            return "%s.%s" % (hostname,domain)
 
145
            return "%s.%s" % (hostname, domain)
146
146
        else:
147
147
            return hostname
148
148
 
 
149
 
149
150
# return a list of classes that have the same depends as 'depends'
150
151
# iterate through cfg_list, loading "DataSourceCollections" modules
151
152
# and calling their "get_datasource_list".
163
164
    retlist = []
164
165
    for ds_coll in cfg_list:
165
166
        for pkg in pkglist:
166
 
            if pkg: pkg="%s." % pkg
 
167
            if pkg:
 
168
                pkg = "%s." % pkg
167
169
            try:
168
170
                mod = __import__("%sDataSource%s" % (pkg, ds_coll))
169
171
                if pkg:
175
177
                raise
176
178
    return(retlist)
177
179
 
 
180
 
178
181
# depends is a list of dependencies (DEP_FILESYSTEM)
179
182
# dslist is a list of 2 item lists
180
 
# dslist = [ 
 
183
# dslist = [
181
184
#   ( class, ( depends-that-this-class-needs ) )
182
185
# }
183
186
# it returns a list of 'class' that matched these deps exactly
184
187
# it is a helper function for DataSourceCollections
185
188
def list_from_depends(depends, dslist):
186
 
    retlist = [ ]
 
189
    retlist = []
187
190
    depset = set(depends)
188
191
    for elem in dslist:
189
192
        (cls, deps) = elem