Merge lp:~jfontan/cloud-init/opennebula into lp:~vlastimil-holer/cloud-init/opennebula

Proposed by Javi Fontan
Status: Merged
Approved by: Vlastimil Holer
Approved revision: 664
Merged at revision: 664
Proposed branch: lp:~jfontan/cloud-init/opennebula
Merge into: lp:~vlastimil-holer/cloud-init/opennebula
Diff against target: 174 lines (+115/-4)
2 files modified
cloudinit/distros/__init__.py (+4/-0)
cloudinit/sources/DataSourceOpenNebula.py (+111/-4)
To merge this branch: bzr merge lp:~jfontan/cloud-init/opennebula
Reviewer Review Type Date Requested Status
Vlastimil Holer Approve
Review via email: mp+141061@code.launchpad.net

Commit message

Added DNS configuration. Added network configuration. Added support for variable SSH_PUBLIC_KEY. Deleted the line that filtered devices ending in a number (devices like "sr0" were excluded).

Description of the change

Add OpenNebula contextualization parameters

To post a comment you must log in.
Revision history for this message
Vlastimil Holer (vlastimil-holer) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'cloudinit/distros/__init__.py'
2--- cloudinit/distros/__init__.py 2012-11-20 06:04:31 +0000
3+++ cloudinit/distros/__init__.py 2012-12-21 11:52:22 +0000
4@@ -59,6 +59,10 @@
5 # to write this blob out in a distro format
6 raise NotImplementedError()
7
8+ def apply_resolv_conf(self, settings):
9+ net_fn = self._paths.join(False, "/etc/resolv.conf")
10+ util.write_file(net_fn, settings)
11+
12 def get_option(self, opt_name, default=None):
13 return self._cfg.get(opt_name, default)
14
15
16=== modified file 'cloudinit/sources/DataSourceOpenNebula.py'
17--- cloudinit/sources/DataSourceOpenNebula.py 2012-12-20 16:16:47 +0000
18+++ cloudinit/sources/DataSourceOpenNebula.py 2012-12-21 11:52:22 +0000
19@@ -101,6 +101,12 @@
20 self.metadata = md
21 self.userdata_raw = results.get('userdata')
22
23+ if 'network-interfaces' in results:
24+ self.distro.apply_network(results['network-interfaces'])
25+
26+ if 'dns' in results:
27+ self.distro.apply_resolv_conf(results['dns'])
28+
29 return True
30
31 def get_hostname(self, fqdn=False, resolve_ip=None):
32@@ -122,6 +128,93 @@
33 pass
34
35
36+class OpenNebulaNetwork(object):
37+ REG_ETH=re.compile('^eth')
38+ REG_DEV_MAC=re.compile('^(eth\d+).*HWaddr (..:..:..:..:..:..)')
39+
40+ def __init__(self, ifconfig, context_sh):
41+ self.ifconfig=ifconfig
42+ self.context_sh=context_sh
43+ self.ifaces=self.get_ifaces()
44+
45+ def get_ifaces(self):
46+ return [self.REG_DEV_MAC.search(f).groups() for f in self.ifconfig.split("\n") if self.REG_ETH.match(f)]
47+
48+ def mac2ip(self, mac):
49+ components=mac.split(':')[2:]
50+
51+ return [str(int(c, 16)) for c in components]
52+
53+ def get_ip(self, dev, components):
54+ var_name=dev+'_ip'
55+ if var_name in self.context_sh:
56+ return self.context_sh[var_name]
57+ else:
58+ return '.'.join(components)
59+
60+ def get_mask(self, dev, components):
61+ var_name=dev+'_mask'
62+ if var_name in self.context_sh:
63+ return self.context_sh[var_name]
64+ else:
65+ return '255.255.255.0'
66+
67+ def get_network(self, dev, components):
68+ var_name=dev+'_network'
69+ if var_name in self.context_sh:
70+ return self.context_sh[var_name]
71+ else:
72+ return '.'.join(components[:-1])+'.0'
73+
74+ def get_gateway(self, dev, components):
75+ var_name=dev+'_gateway'
76+ if var_name in self.context_sh:
77+ return self.context_sh[var_name]
78+ else:
79+ None
80+
81+ def gen_conf(self):
82+ conf=[]
83+ conf.append('auto lo')
84+ conf.append('iface lo inet loopback')
85+ conf.append('')
86+
87+ for i in self.ifaces:
88+ dev=i[0]
89+ mac=i[1]
90+ ip_components=self.mac2ip(mac)
91+
92+ conf.append('auto '+dev)
93+ conf.append('iface '+dev+' inet static')
94+ conf.append(' address '+self.get_ip(dev, ip_components))
95+ conf.append(' network '+self.get_network(dev, ip_components))
96+ conf.append(' netmask '+self.get_mask(dev, ip_components))
97+
98+ gateway=self.get_gateway(dev, ip_components)
99+ if gateway:
100+ conf.append(' gateway '+gateway)
101+
102+ conf.append('')
103+
104+ return "\n".join(conf)
105+
106+ def gen_dns(self):
107+ dnss=[]
108+
109+ if 'dns' in self.context_sh:
110+ dnss.append('nameserver '+self.context_sh['dns'])
111+
112+ keys=[d for d in self.context_sh.keys() if re.match('^eth\d+_dns$', d)]
113+
114+ for k in sorted(keys):
115+ dnss.append('nameserver '+self.context_sh[k])
116+
117+ if not dnss:
118+ return None
119+ else:
120+ return "\n".join(dnss)+"\n"
121+
122+
123 def find_candidate_devs():
124 """
125 Return a list of devices that may contain the context disk.
126@@ -136,9 +229,6 @@
127 # followed by fstype items, but with dupes removed
128 combined = (by_label + [d for d in by_fstype if d not in by_label])
129
130- # We are looking for block device (sda, not sda1), ignore partitions
131- combined = [d for d in combined if d[-1] not in "0123456789"]
132-
133 return combined
134
135
136@@ -213,8 +303,15 @@
137 raise NonContextDeviceDir("Missing context.sh")
138
139 # process single or multiple SSH keys
140+ ssh_key_var=None
141+
142 if "ssh_key" in context_sh:
143- lines = context_sh.get('ssh_key').splitlines()
144+ ssh_key_var="ssh_key"
145+ elif "ssh_public_key" in context_sh:
146+ ssh_key_var="ssh_public_key"
147+
148+ if ssh_key_var:
149+ lines = context_sh.get(ssh_key_var).splitlines()
150 results['metadata']['public-keys'] = [l for l in lines
151 if len(l) and not l.startswith("#")]
152
153@@ -223,6 +320,8 @@
154 results['metadata']['local-hostname'] = context_sh['hostname']
155 elif 'public_ip'in context_sh:
156 results['metadata']['local-hostname'] = context_sh['public_ip']
157+ elif 'eth0_ip' in context_sh:
158+ results['metadata']['local-hostname'] = context_sh['eth0_ip']
159
160 # raw user data
161 if "user_data" in context_sh:
162@@ -230,6 +329,14 @@
163 elif "userdata" in context_sh:
164 results['userdata'] = context_sh["userdata"]
165
166+ (out, err) = util.subp(['/sbin/ifconfig', '-a'])
167+ net=OpenNebulaNetwork(out, context_sh)
168+ results['network-interfaces']=net.gen_conf()
169+
170+ dns=net.gen_dns()
171+ if dns:
172+ results['dns']=dns
173+
174 return results
175
176

Subscribers

People subscribed via source and target branches

to all changes: